Extension:SectionLinkToTop

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
SectionLinkToTop

Release status:Extension status beta

ImplementationTemplate:Extension#type Tag
DescriptionTemplate:Extension#description Allows you to add a link next to the edit section link to return to the top of a page.
Author(s)Template:Extension#username Coban Holmes (Cobantalk)
Latest versionTemplate:Extension#version 1.0 (2009-09-17)
MediaWikiTemplate:Extension#mediawiki Tested on 1.15.1
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license GPL
Download The source is available on this page
Hooks usedTemplate:Extension#hook
DoEditSectionLinkManual:Hooks/DoEditSectionLink
MagicWordwgVariableIDsManual:Hooks/MagicWordwgVariableIDs
MagicWordMagicWordsManual:Hooks/MagicWordMagicWords
LanguageGetMagicManual:Hooks/LanguageGetMagic
ParserAfterStripManual:Hooks/ParserAfterStrip
ParserAfterTidyManual:Hooks/ParserAfterTidy

Translate the SectionLinkToTop extension if it is available at translatewiki.net

Check usage and version matrix.

Description[edit]

This extension creates a new magic word __SECTIONLINKTOTOP__ which will add a link to the top of the page next to the edit section link.

Installation[edit]

  1. Copy the code below and paste it into a new file called "SectionLinkToTop/SectionLinkToTop.php" in your extensions directory
  2. Append the following line to the end of your LocalSettings.php file:
    require_once( "$IP/extensions/SectionLinkToTop/SectionLinkToTop.php" );
    

Source code[edit]

<?php
 
/**
 * Copyright (C) 2009 Coban Holmes <coban.holmes@gmail.com>
 * http://www.mediawiki.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * This extension implements a new MagicWord __SECTIONLINKTOTOP__.
 * If an article contains this MagicWord, a link to the top of the page will be added next to the edit link.
 * 
 * How to use:
 * * include this extension in LocalSettings.php: 
 *   require_once($IP.'/extensions/SectionLinkToTop/SectionLinkToTop.php');
 * * Add "__SECTIONLINKTOTOP__" to any article of your choice.
 * 
 * @author Coban Holmes
 * @version 1.0
 */
 
 
 
if (!defined('MEDIAWIKI')) {
        die("This requires the MediaWiki enviroment.");
}
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'SectionLinkToTop',
        'version' => '1.0',
        'author' => 'Coban Holmes',
        'url' => 'http://www.mediawiki.org/wiki/Extension:SectionLinkToTop',
        'description' => 'Add MagicWord "&#95;&#95;SECTIONLINKTOTOP&#95;&#95;".',
);

$sectionLinkToTop = new SectionLinkToTop();

$wgHooks['MagicWordMagicWords'][] = array($sectionLinkToTop, 'addMagicWord');
$wgHooks['MagicWordwgVariableIDs'][] = array($sectionLinkToTop, 'addMagicWordId');
$wgHooks['LanguageGetMagic'][] = array($sectionLinkToTop, 'languageGetMagic');
$wgHooks['DoEditSectionLink'][] = array($sectionLinkToTop, 'doEditSectionLink');
$wgHooks['ParserAfterStrip'][] = array($sectionLinkToTop, 'checkForMagicWord');
$wgHooks['ParserAfterTidy'][] = array($sectionLinkToTop, 'parserAfterTidy');

class SectionLinkToTop
{
	//$linkToTopEnabled will be set to true when the magic word is found on a page
	private $linkToTopEnabled = false;
	//$isHooked will be set to true if we are able to add the link in the DoEditSectionLink hook
	private $isHooked = false;
	//The text for the link
	private $linkToTopText = '<span class="editsection sectionlinktotop">[<a href="#top" title="Return to the top of the page">top</a>]</span>';
	
	//constructor - empty
	function SectionLinkToTop() {}
 
	//register the new magic word with the system
	function addMagicWord(&$magicWords) {
		$magicWords[] = 'MAG_SECTIONLINKTOTOP';
		return true;
	}

	//add the id
	function addMagicWordId(&$magicWords) {
		$magicWords[] = 'MAG_SECTIONLINKTOTOP';
		return true;
	}
	
	//Find the magic word on the page, remove it and enable the extension
	function checkForMagicWord(&$parser, &$text, &$strip_state) {
		if (MagicWord::get('MAG_SECTIONLINKTOTOP')->matchAndRemove( $text )){
			$this->linkToTopEnabled = true;
		}
		return true;
	}
	
	function languageGetMagic(&$magicWords, $langCode) {
		$magicWords['MAG_SECTIONLINKTOTOP'] = array( 0, '__SECTIONLINKTOTOP__' );
		return true;
	}
	
	//This hook only gets run if the user has edit rights
	function doEditSectionLink($skin, $title, $section, $tooltip, $result ) {
        if ($this->linkToTopEnabled){
			$result = $result.$this->linkToTopText;
			$this->isHooked = true;
        }
        return true;
	}
	
	//We will have to add the link here if we couldn't in doEditSectionLink
	function parserAfterTidy( &$parser, &$text ) {
		if (!$this->isHooked && $this->linkToTopEnabled){
			$pattern = '#(<h[2-6]>)( <span.*</h[2-6]>)#i';
			$replace = '\1'.$this->linkToTopText.'\2';
			$text = preg_replace($pattern, $replace, $text);
		}
		return true;
	}
}