'Terminology',
'description' => 'Provides tooltips from the [[Terminology]] defined for all instances of the given term',
'version' => '20100729',
'author' => 'BarkerJr modified 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 != "") {
$changed = false;
$doc = new DOMDocument();
@ $doc->loadHTML('' . $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->documentElement, $term, $definition)) {
$changed = true;
}
}
}
if ($changed) {
$text = $doc->saveHTML();
}
}
}
return true;
}
function terminologyParseThisNode($doc, $node, $term, $definition) {
$changed = false;
if ($node->nodeType == XML_TEXT_NODE) {
$texts = preg_split('/\b('.preg_quote($term).'s?)\b/u', $node->textContent, -1, PREG_SPLIT_DELIM_CAPTURE);
if (count($texts) > 1) {
$container = $doc->createElement('span');
for ($x = 0; $x < count($texts); $x++) {
if ($x % 2) {
$span = $doc->createElement('span', $texts[$x]);
if (!is_file ('extensions/tooltip/wz_tooltip.js')) {
$span->setAttribute('title', $term . ": " . $definition);
$span->setAttribute('class', 'terminologydef');
} else {
$bad = array ("\"", "'");
$good = array ("\\\"", "\'");
$span->setAttribute('onmouseover', "Tip('".str_replace ($bad, $good, $definition)."', STICKY, true, DURATION, -1000, WIDTH, -600)");
$span->setAttribute('class', 'terminologydef');
$span->setAttribute('onmouseout', "UnTip()");
}
$span->setAttribute('style', 'cursor:help');
$container->appendChild($span);
} else {
$container->appendChild($doc->createTextNode($texts[$x]));
}
}
$node->parentNode->replaceChild($container, $node);
$changed = true;
}
} elseif ($node->hasChildNodes()) {
// We have to do this because foreach gets confused by changing data
$nodes = $node->childNodes;
$previousLength = $nodes->length;
for ($x = 0; $x < $nodes->length; $x++) {
if ($nodes->length <> $previousLength) {
$x += $nodes->length - $previousLength;
}
$previousLength = $nodes->length;
$child = $nodes->item($x);
if (terminologyParseThisNode($doc, $child, $term, $definition)) {
$changed = true;
}
}
}
return $changed;
}