'Terminology', 'description' => 'Provides tooltips from the [[Terminology]] defined for all instances of the given term', 'version' => '20100729', 'author' => 'BarkerJr modified (and ported to PHP4) by Benjamin Kahn (xkahn@zoned.net)' ); $wgExtensionFunctions[] = 'terminologySetup'; function terminologySetup() { global $wgOut, $wgScriptPath; $wgOut->addHTML(""); if (is_file ('extensions/tooltip/wz_tooltip.js')) { $wgOut->addHTML(""); } } $wgHooks['ParserBeforeTidy'][] = 'terminologyParser'; function terminologyParser(&$parser, &$text) { global $wgRequest; $action = $wgRequest->getVal( 'action', 'view' ); if ($action=="edit" || $action=="ajax" || isset($_POST['wpPreview'])) return false; $rev = Revision::newFromTitle(Title::makeTitle(null, 'Terminology')); if ($rev) { $content = $rev->getText(); if ($content != "" && $text != "") { $changed = false; @ $doc = xmldoc ($text); $c = explode("\n", $content); reset($c); while (list($key, $entry) = each($c)) { $terms = explode(':', $entry, 2); if (@$terms[0][0] == ';') { // It's possible that the definition is on the next line if (count($terms) == 1) { list($k1, $e1) = each($c); if ($e1[0] == ':') { $term = trim(substr($terms[0], 1)); $definition = trim(substr($e1, 1)); } else { continue; } } elseif (count($terms) == 2) { $term = trim(substr($terms[0], 1)); $definition = trim($terms[1]); } else { continue; } if (terminologyParseThisNode($doc, $doc->root(), $term, $definition)) { $changed = true; } } } if ($changed) { $text = $doc->dump_mem(true); } } } return true; } function terminologyParseThisNode($doc, $node, $term, $definition) { $changed = false; if ($node->node_type() == XML_TEXT_NODE) { $texts = preg_split('/\b('.preg_quote($term).'s?)\b/u', $node->get_content(), -1, PREG_SPLIT_DELIM_CAPTURE); if (count($texts) > 1) { $container = $doc->create_element('span'); for ($x = 0; $x < count($texts); $x++) { if ($x % 2) { $span = $doc->create_element('span'); $span->set_content($texts[$x]); if (!is_file ('extensions/tooltip/wz_tooltip.js')) { $span->set_attribute('title', $term . ": " . $definition); $span->set_attribute('class', 'terminologydef'); } else { $bad = array ("\"", "'"); $good = array ("\\\"", "\'"); $span->set_attribute('onmouseover', "Tip('".str_replace ($bad, $good, $definition)."', STICKY, true, DURATION, -1000, WIDTH, -600)"); $span->set_attribute('onmouseout', "UnTip()"); $span->set_attribute('class', 'terminologydef'); } $span->set_attribute('style', 'cursor:help'); $container->append_child($span); } else { $container->append_child($doc->create_text_node($texts[$x])); } } $parent = $node->parent_node(); $parent->replace_child($container, $node); $changed = true; } } elseif ($node->has_child_nodes()) { // We have to do this because foreach gets confused by changing data $nodes = $node->child_nodes(); $previousLength = count($nodes); for ($x = 0; $x < count($nodes); $x++) { if (count($nodes) <> $previousLength) { $x += count($nodes) - $previousLength; } $previousLength = count($nodes); $child = $nodes[$x]; if (terminologyParseThisNode($doc, $child, $term, $definition)) { $changed = true; } } } return $changed; }