Extension:CustomTOCLength
| This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc. Note: No localisation updates are provided for this extension by translatewiki.net. |
|
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 | ||
|
|||
|
|||
| Check usage and version matrix; stats | |||
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 |
Usage[edit]
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.
Download instructions[edit]
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.
Installation[edit]
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
Configuration parameters[edit]
$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.
Code[edit]
<?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; }
See also[edit]
Todo[edit]
- 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.