Open main menu

Extension:CustomTOCLength

MediaWiki extensions manual
OOjs UI icon advanced.svg
CustomTOCLength
Release status: beta
Implementation Parser extension
Description Allows to adjust the number of headings needed for TOC to show
Author(s) Leo Wallentin (Säsongsmat.nu) (Rotseetalk)
Latest version 0.1.1 (2013-07-26)
MediaWiki 1.16+
PHP 5
Database changes No
License BSD 3-clause "Modified" License
Download See the code section
  • $wgTOCLimit
Translate the CustomTOCLength extension if it is available at translatewiki.net
Check usage and version matrix.

The CustomTOCLength 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). It is written for Säsongsmat.nu and tested there with MW 1.16 and 1.17.

UsageEdit

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 the to-do 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.

DownloadEdit

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 .

InstallationEdit

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

ConfigurationEdit

$wgTOCLimit sets the number of headings needed for the TOC to show up. Anything larger that 4 will change MediaWiki's 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.

CodeEdit

<?php

if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

$wgExtensionCredits['parserhook'][] = array(
	'name' => 'CustomTOCLength',
	'author' => array( '[http://xn--ssongsmat-v2a.nu Säsongsmat.nu]', 'Leo Wallentin' ),
	'url' => 'https://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.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 alsoEdit

To-doEdit

  • 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.

Extremely ugly workaround:

1. Backup files

cp ~/includes/parser/Parser.php ~/includes/parser/Parser.php.bak
sudo vim ~/includes/parser/Parser.php

2. Replace the '4' in below line:

( ( $numMatches >= 4 ) || $this->mForceTocPosition );

With the number that you want, for instance 2:

( ( $numMatches >= 2 ) || $this->mForceTocPosition );
  • Whenever MediaWiki comes with a proper TOC hook, use that instead.