Extension:CustomTOCLength

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
CustomTOCLength

Release status: beta

Implementation Parser extension
Description Adjust number of headings needed for TOC to show
Author(s) Säsongsmat.nu (RotseeTalk)
Last version 0.1 (2011-08-19)
MediaWiki >1.16
PHP 5
License BSD
Download No link
Parameters

$wgTOCLimit

Hooks used
InternalParseBeforeLinks

Check usage (experimental)

This extension allows for adjusting the number of headings in an article required for the table of contents (TOC) to show up. The default value on MediaWiki is 4, install this extension to be able to raise that value if you want TOC only for longer articles (or set it ridiculously high if you want no TOC's at all). Written for Säsongsmat.nu and tested there with MW 1.16 and 1.17.

Contents

[edit] Usage

Include the extension and set the variable $wgTOCLimit to any number in LocalSettings.php. Any number larger than 4 will change the number of headings required for TOC to show. It is currently not possible to lower the number (at least 4 headings is always needed for TOC to show up), see todo list below.

If you want to always hide the TOC you can use a very high number, or try Extension:NoTOC that does only this instead.

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/CustomTOCLength/CustomTOCLength.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/CustomTOCLength/CustomTOCLength.php");
$wgTOCLimit = 6; //Set the number of headings that should be needed here

[edit] Configuration parameters

$wgTOCLimit sets the number of headings needed for the TOC to show up. Anything larger that 4 will change MediaWikis behaviour accordingly. Set to a very high number if you never want TOC to show. That will make the TOC disappear completely, not just hide it with CSS.

[edit] Code

<?php
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'CustomTOCLength',
        'author' =>'[http://xn--ssongsmat-v2a.nu Säsongsmat.nu], Leo Wallentin',
        'url' => 'http://www.mediawiki.org/wiki/Extension:CustomTOCLength', 
        'description' => 'Allows for adjusting the number of headings for which to show the table of contents upwards.',
        'version' => '0.1',
        'path' => __FILE__,
);
 
 
$wgHooks['InternalParseBeforeLinks'][] = 'efCTOCL';
 
function efCTOCL(&$parser, &$text) {
 
        //Set this variable in LocalSettings.php to activate plugin
        global $wgTOCLimit;
 
        if ( isset($wgTOCLimit) && $wgTOCLimit ) {
 
                //Find number of headings
                $matches = array();
                $numMatches = preg_match_all( '/^\={2,5}(.*?)\={2,5}$/m', $text, $matches );
 
                $parser->mShowToc = $numMatches >= $wgTOCLimit;
 
        }
 
        return true;
}

[edit] See also

[edit] Todo

  • Make $wgTOCLimit < 4 work by altering Parser::mForceTocPosition. If $parser->mForceTocPosition is already set, or $wgTOCLimit <= 4, do nothing, otherwise set mForceTocPosition to true and insert __TOC__ after first paragraph in $text.
  • Whenever MediaWiki comes with a proper TOC hook, use that instead.
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox