Extension:HideEmptySections

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
Hide Empty Sections
Release status: stable
Implementation Hook
Description Hide sections that do not have any text in it.
Author(s) vivekrstalk
Latest version 1.0
MediaWiki 1.14
License No license specified
Download Code
Hooks used
ParserAfterStrip
Translate the HideEmptySections extension if it is available at translatewiki.net
Check usage and version matrix.

This extension hides any section that does not have any text associated with it. It also removes any extra line spaces that may be present in the source of the page.

The 'hiding' takes place at view time only and the actual source of the page is not affected.

It is very useful if you have a definite structure that all the users of the wiki will fill out. Keeping the section headers in place helps others to add more information while not compromising the template structure.

For example:

== Overview ==
Blah Blah Blah


== Heading One ==

<!-- Write info here -->

=== Sub head one ===

=== Sub head two ===

== Heading Two ==
gah gah gah

=== Sub head three ===

=== Sub head four ===

== Heading Three ==

=== Sub head five ===
Some random info here...

=== Sub head six ===

== Heading Four ==
Enough info

will be rendered as:

== Overview ==
Blah Blah Blah

== Heading Two ==
gah gah gah

== Heading Three ==

=== Sub head five ===
Some random info here...

== Heading Four ==
Enough info

Download instructions[edit]

Please copy and paste the code found below and place it in $IP/extensions/HideEmptySections.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/HideEmptySections.php" );

Code[edit]

<?php
 
# Hide Empty Sections MediaWiki Extension
# Created by Vivek R. Shivaprabhu (vivekrs.rsv@gmail.com)
 
if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
 
global $wgHooks;
global $wgExtensionCredits;
 
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'Hide Empty Sections',
	'type' => 'hook',
	'author' => 'Vivek R. Shivaprabhu (vivekrs.rsv@gmail.com)',
	'version' => '1.0',
	'update' => '03-05-2009',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Hide_Empty_Sections',
	'description' => 'Hide sections that do not have any text in it.',
);
 
$wgHooks['ParserAfterStrip'][] = 'fnHideEmptySections';

function fnHideEmptySections( &$parser, &$text, &$strip_state ) {
	global $action; // Access the global "action" variable
	// Only do the replacement if the action is not edit or history
	if(
		$action !== 'edit'
		&& $action !== 'history'
		&& $action !== 'delete'
		&& $action !== 'watch'
		&& strpos( $parser->mTitle->mPrefixedText, 'Special:' ) === false
		&& $parser->mTitle->mNamespace !== 8
	)
	{
		$comment_pattern = '/<!--(.|\n)+?-->/';
		$text = preg_replace ($comment_pattern, '', $text);

		$pattern[] = '/([^=])(====[^=]+?====\s*)+(={2,4}[^=]|$)/';
		$pattern[] = '/([^=])(===[^=]+?===\s*)+(={2,3}[^=]|$)/';
		$pattern[] = '/([^=])(==[^=]+?==\s*)+(==[^=]|$)/';
		$replace = '\1\3';
		$text = trim ( preg_replace ($pattern, $replace, ' ' . $text) );
	}

	while ( preg_match ('/\n\s*\n\s*\n/', $text ) )
	{
		$text = preg_replace ( '/\n\s*\n\s*\n/', "\n\n", $text );
	}

	return true;
}