Extension:PlaceNewSection

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
PlaceNewSection
Release status: beta
Implementation Extended syntax
Description Controls placement of new sections
Author(s) (Nxtalk)
MediaWiki Tested on 1.14 and 1.15
License GPL
Download see below
Hooks used
LanguageGetMagic
PlaceNewSection
Translate the PlaceNewSection extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

This extension introduces two magic words, __ADDNEWSECTIONBELOW__ and __ADDNEWSECTIONABOVE__ that control placement of new talk page sections.

Usage[edit]

If __ADDNEWSECTIONBELOW__ is placed on a page, new sections will be added directly below it instead of after the end. Newer sections will be at the top, and older sections at the bottom.

If __ADDNEWSECTIONABOVE__ is used, new sections will be added directly above it, and the order will be reversed (new sections at the bottom, like the MediaWiki default), but anything below this magic word will stay at the bottom.

Download instructions[edit]

MediaWiki 1.19[edit]

MediaWiki 1.19 does not need to be patched; it already has the PlaceNewSection hook.

Older versions[edit]

This extension requires a modification to Article.php in MediaWiki <= 1.17 and WikiPage.php in MediaWiki 1.18 (these files are in the includes directory).

Find the replaceSection function and this code:

			if( $section == 'new' ) {
				# Inserting a new section
				$subject = $summary ? wfMsgForContent('newsectionheaderdefaultlevel',$summary) . "\n\n" : '';
				$text = strlen( trim( $oldtext ) ) > 0
						? "{$oldtext}\n\n{$subject}{$text}"
						: "{$subject}{$text}";

then change it to:

			if( $section == 'new' ) {
				# Inserting a new section
				$subject = $summary ? wfMsgForContent('newsectionheaderdefaultlevel',$summary) . "\n\n" : '';
				if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
					$text = strlen( trim( $oldtext ) ) > 0
							? "{$oldtext}\n\n{$subject}{$text}"
							: "{$subject}{$text}";
				}

After that cut and paste the code found below and place it in $IP/extensions/PlaceNewSection/PlaceNewSection.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/PlaceNewSection/PlaceNewSection.php");

Code[edit]

<?php
 
$wgExtensionCredits['parserhook'][] = array(
    'name'           => 'PlaceNewSection',
    'description'    => 'Adds the <nowiki>__ADDNEWSECTIONBELOW__</nowiki> and <nowiki>__ADDNEWSECTIONABOVE__</nowiki> magic words to control new section placement',
    'author'         => '[http://www.mediawiki.org/wiki/User:Nx Nx]',
    'url'            => 'http://www.mediawiki.org/wiki/Extension:PlaceNewSection',
);
 
$wgHooks['PlaceNewSection'][] = 'efPlaceNewSection';
$wgHooks['LanguageGetMagic'][] = 'efAddNewSectionMagic';
 
function efAddNewSectionMagic( &$magicWords, $langCode ) {
	$magicWords['addnewsectionbelow'] = array(0, '__ADDNEWSECTIONBELOW__');
	$magicWords['addnewsectionabove'] = array(0, '__ADDNEWSECTIONABOVE__');
	MagicWord::$mDoubleUnderscoreIDs[] = 'addnewsectionbelow';
	MagicWord::$mDoubleUnderscoreIDs[] = 'addnewsectionabove';
	return true;
}
 
function efPlaceNewSectionInitRegex( $mw ) {
	$case = $mw->isCaseSensitive ? '' : 'iu';
	return "/({$mw->getBaseRegex()})/{$case}";
}
 
function efPlaceNewSection( $article, $oldtext, $subject, &$text ) {
	$mw = MagicWord::get( 'addnewsectionbelow' );
	if( $mw->match( $oldtext ) ) {
		$regexp = efPlaceNewSectionInitRegex( $mw );
		$text = preg_replace( $regexp, '$1' . StringUtils::escapeRegexReplacement( "\n\n{$subject}{$text}" ) , $oldtext, 1 );
		return false;
	}
	$mw = MagicWord::get( 'addnewsectionabove' );
	if( $mw->match( $oldtext ) ) {
		$regexp = efPlaceNewSectionInitRegex( $mw );
		$text = preg_replace( $regexp, StringUtils::escapeRegexReplacement( "{$subject}{$text}\n\n" ) . '$1' , $oldtext, 1 );
		return false;
	}
	return true;
}