Team:Cornell/Templates/LinktoTop

From 2011.igem.org

Revision as of 20:43, 18 September 2011 by Cpaduano (Talk | contribs)

* 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 "__SECTIONLINKTOTOP__".', ); $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 = '[top]'; //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 = '#()( )#i'; $replace = '\1'.$this->linkToTopText.'\2'; $text = preg_replace($pattern, $replace, $text); } return true; } } ?>