Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 7a3329bda860a28934e35a28d48c5cb9d15f61d0 0 parents
Aaron Parecki authored
Showing with 115 additions and 0 deletions.
  1. +115 −0 Glossary.php
  2. 0  README
115 Glossary.php
@@ -0,0 +1,115 @@
+<?php
+
+/*
+Installation Instructions: Put the below lines into your LocalSettings.php file
+
+define('NS_GLOSSARY', 100);
+$wgExtraNamespaces[NS_GLOSSARY] = "Glossary";
+$wgExtraNamespaces[NS_GLOSSARY + 1] = "Glossary_talk";
+$wgContentNamespaces[] = NS_GLOSSARY;
+$wgGlossaryNamespace = NS_GLOSSARY;
+
+require_once('extensions/Glossary.php');
+*/
+
+$wgExtensionFunctions[] = "myGlossary";
+$wgExtensionCredits['parserhook'][] = array(
+ 'name' => 'Glossary',
+ 'author' => 'Aaron Parecki',
+ 'description' => 'Adds <nowiki><glossary></nowiki> tag for creating a list of all pages in a specific namespace, and creates links to the glossary terms for all occurrences of glossary terms',
+ 'url' => 'http://aaronparecki.com/MediaWiki'
+);
+
+function myGlossary() {
+ global $wgParser, $wgHooks;
+
+ $wgParser->setHook("glossary", array('myGlossary', 'showList'));
+ $wgHooks['ParserAfterStrip'][] = 'myGlossaryParser';
+
+ return true;
+}
+
+function myGlossaryParser(&$parser, &$text)
+{
+ myGlossary::parser($parser, $text);
+ return true;
+}
+
+class myGlossary
+{
+ /**
+ * Find any terms in the Glossary namespace and make them into links to their pages.
+ */
+ public static function parser(&$parser, &$text)
+ {
+ // Split the text into alternating sections by square brackets. All square-bracket links will
+ // be in the odd numbered elements, so only do the replacements on the even numbered elements.
+ $sections = preg_split('/(\[.+?\]?\])/i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
+
+ $terms = self::_gatherTerms();
+
+ $newText = '';
+ foreach($sections as $i=>$s)
+ {
+ $newSectionText = $s;
+ if($i % 2 == 0)
+ {
+ foreach($terms as $term)
+ $newSectionText = preg_replace('/(\b' . preg_quote($term['title']->getText()) . 's?\b)/i', '[[' . $term['title'] . '|$1]]', $newSectionText);
+ }
+ $newText .= $newSectionText;
+ }
+ $text = $newText;
+
+ return true;
+ }
+
+ // The callback function for converting the input text to HTML output
+ public static function showList($input, $args)
+ {
+ global $wgGlossaryNamespace, $wgOut;
+
+ if(!isset($wgGlossaryNamespace))
+ $wgGlossaryNamespace = 100;
+
+ $parser = new Parser;
+ $pOptions = new ParserOptions;
+
+ $terms = self::_gatherTerms();
+ $html = '';
+ foreach($terms as $term)
+ {
+ $html .= '===[[' . $term['title'] . '|' . $term['title']->getText() . ']]' . "===\n";
+ $html .= $term['article']->getContent() . "\n\n";
+ }
+
+ return $wgOut->parse($html);
+ }
+
+ private static function _gatherTerms()
+ {
+ global $wgExtraNamespaces, $wgGlossaryNamespace;
+
+ $dbr = wfGetDB( DB_SLAVE );
+ $aTables = array('page');
+ $aFields = array('page_namespace', 'page_title');
+ $aWhere = array('page_namespace'=>$wgGlossaryNamespace);
+ $aJoin = array();
+ $aOptions = array();
+
+ $res = $dbr->select($aTables, $aFields, $aWhere, __METHOD__);
+
+ $results = array();
+ if($dbr->numRows($res) > 0)
+ {
+ while($row = $dbr->fetchObject($res))
+ {
+ $title = Title::newFromRow($row);
+ $article = new Article($title);
+ $results[] = array('title'=>$title, 'article'=>$article);
+ }
+ }
+ return $results;
+ }
+}
+?>
0  README
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.