r26020 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26019‎ | r26020 | r26021 >
Date:19:24, 22 September 2007
Author:magnusmanske
Status:old
Tags:
Comment:
Extension to transclude template as separate page, e.g. for ISBN and ISSN.
Modified paths:
  • /trunk/extensions/TemplateLink (added) (history)
  • /trunk/extensions/TemplateLink/README.txt (added) (history)
  • /trunk/extensions/TemplateLink/TemplateLink.body.php (added) (history)
  • /trunk/extensions/TemplateLink/TemplateLink.i18n.php (added) (history)
  • /trunk/extensions/TemplateLink/TemplateLink.setup.php (added) (history)

Diff [purge]

Index: trunk/extensions/TemplateLink/README.txt
@@ -0,0 +1,11 @@
 2+This extension allows for a template to be transcluded as a new page, using Special:TemplateLink.
 3+The template parameter separator "|" has to be replaced with "::" as to not confuse the parser.
 4+
 5+Example:
 6+[[Special:TemplateLink/test::param1=value1::param2=value2]]
 7+
 8+links to a special page that will display "Test" (variation configurable in i18n) as title and
 9+
 10+{{test|param1=value1|param2=value2}}
 11+
 12+as content.
Property changes on: trunk/extensions/TemplateLink/README.txt
___________________________________________________________________
Added: svn:eol-style
113 + native
Index: trunk/extensions/TemplateLink/TemplateLink.body.php
@@ -0,0 +1,55 @@
 2+<?php
 3+class TemplateLink extends SpecialPage
 4+{
 5+ function TemplateLink(){
 6+ SpecialPage::SpecialPage("TemplateLink");
 7+ self::loadMessages();
 8+ }
 9+
 10+ function execute( $par ){
 11+ global $wgOut;
 12+
 13+ $this->setHeaders();
 14+
 15+ # Check if parameter is empty
 16+ $par = trim( $par );
 17+ if( $par == '' ){
 18+ $wgOut->addWikiText( wfMsg('templatelink_empty') );
 19+ return;
 20+ }
 21+
 22+ # Expand template
 23+ $arr = explode( '::' , $par );
 24+ $wikitext = '{{' . implode( '|' , $arr ). '}}';
 25+
 26+ # Output
 27+# $wgOut->addWikiText( $wikitext ); # This works, but is not recommended on mediawiki.org...
 28+ $wgOut->addHTML( $this->sandboxParse( $wikitext ) ); # ...so we'll use this one.
 29+
 30+
 31+ # Setting page tatle based on used template
 32+ $wgOut->setPageTitle( wfMsg( 'templatelink_newtitle' , ucfirst( $arr[0] ) ) );
 33+ }
 34+
 35+ function sandboxParse($wikiText){
 36+ global $wgTitle, $wgUser;
 37+ $myParser = new Parser();
 38+ $myParserOptions = new ParserOptions();
 39+ $myParserOptions->initialiseFromUser($wgUser);
 40+ $result = $myParser->parse($wikiText, $wgTitle, $myParserOptions);
 41+ return $result->getText();
 42+ }
 43+
 44+ function loadMessages(){
 45+ static $messagesLoaded = false;
 46+ global $wgMessageCache;
 47+ if( $messagesLoaded )return;
 48+ $messagesLoaded = true;
 49+
 50+ require( dirname( __FILE__ ). '/TemplateLink.i18n.php' );
 51+ foreach( $allMessages as $lang => $langMessages ){
 52+ $wgMessageCache->addMessages( $langMessages, $lang );
 53+ }
 54+ return true;
 55+ }
 56+}
Index: trunk/extensions/TemplateLink/TemplateLink.i18n.php
@@ -0,0 +1,13 @@
 2+<?php
 3+$allMessages = array(
 4+ 'en' => array(
 5+ 'templatelink' => 'Template Link',
 6+ 'templatelink_empty' => 'You have to supply a parameter.',
 7+ 'templatelink_newtitle' => '$1 (transcluded)',
 8+ ),
 9+ 'de' => array(
 10+ 'templatelink' => 'Vorlagen-Link',
 11+ 'templatelink_empty' => 'Ein Parameter muss angegeben werden.',
 12+ 'templatelink_newtitle' => '$1 (ersetzt)',
 13+ )
 14+);
Index: trunk/extensions/TemplateLink/TemplateLink.setup.php
@@ -0,0 +1,26 @@
 2+<?php
 3+# Alert the user that this is not a valid entry point to MediaWiki if they try to access the skin file directly.
 4+if (!defined('MEDIAWIKI')) {
 5+ echo <<<EOT
 6+To install my extension, put the following line in LocalSettings.php:
 7+require_once( "$IP/extensions/TemplateLink/TemplateLink.setup.php" );
 8+EOT;
 9+ exit( 1 );
 10+}
 11+
 12+$wgAutoloadClasses['TemplateLink'] = dirname(__FILE__) . '/TemplateLink.body.php'; # Tell MediaWiki to load the extension body.
 13+$wgSpecialPages['TemplateLink'] = 'TemplateLink'; # Let MediaWiki know about your new special page.
 14+$wgHooks['LoadAllMessages'][] = 'TemplateLink::loadMessages'; # Load the internationalization messages for your special page.
 15+$wgHooks['LanguageGetSpecialPageAliases'][] = 'TemplateLinkLocalizedPageName'; # Add any aliases for the special page.
 16+
 17+function TemplateLinkLocalizedPageName(&$specialPageArray, $code) {
 18+ # The localized title of the special page is among the messages of the extension:
 19+ TemplateLink::loadMessages();
 20+ $text = wfMsg('TemplateLink');
 21+
 22+ # Convert from title in text form to DBKey and put it into the alias array:
 23+ $title = Title::newFromText($text);
 24+ $specialPageArray['TemplateLink'][] = $title->getDBKey();
 25+
 26+ return true;
 27+}

Status & tagging log