From OrganicDesign Wiki
<?php
/**
* TocList extension - A parser-function which renders a bullet list of the heading structure
*
* @package MediaWiki
* @subpackage Extensions
* @author [http://www.mediawiki.org/wiki/User:Nad User:Nad]
* @copyright © 2008 [http://www.mediawiki.org/wiki/User:Nad User:Nad]
* @licence GNU General Public Licence 2.0 or later
*/
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('TOCLIST_VERSION','1.0.0, 2008-07-05');
$egTocListMagic = "toclist";
$wgExtensionFunctions[] = 'efSetupTocList';
$wgHooks['LanguageGetMagic'][] = 'efTocListLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'TocList',
'author' => '[http://www.mediawiki.org/wiki/User:Nad User:Nad]',
'description' => 'A parser-function which renders a bullet list of the heading structure',
'url' => 'http://www.organicdesign.co.nz/Extension:TocList.php',
'version' => TOCLIST_VERSION
);
/**
* Expand the toclist-magic
*/
function efMagicToclist(&$parser) {
global $wgTitle;
$article = new Article($wgTitle);
$text = $parser->doHeadings($article->getContent());
preg_match_all("|<h([1-6]+)>\\s*(.+?)\\s*</h[1-6]+ *>|i", $text, $toc, PREG_PATTERN_ORDER);
$text = '';
for ($i = 0; $i < count($toc[0]); $i++) $text .= str_repeat('*', $toc[1][$i]).$toc[2][$i]."\n";
return $text;
}
/**
* Called from $wgExtensionFunctions array when initialising extensions
*/
function efSetupTocList() {
global $wgParser, $egTocListMagic;
$wgParser->setFunctionHook($egTocListMagic, 'efMagicToclist');
}
/**
* Needed in MediaWiki >1.8.0 for magic word hooks to work properly
*/
function efTocListLanguageGetMagic(&$magicWords, $langCode = 0) {
global $egTocListMagic;
$magicWords[$egTocListMagic] = array($langCode, $egTocListMagic);
return true;
}