Umherirrender has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/76498


Change subject: Added CoreVariables class and Parser::setVariableHook
......................................................................

Added CoreVariables class and Parser::setVariableHook

This makes it easier for extensions to add a variable, because there
have not to listen to all extension variable calls on the hook, they can
instead register a callback for the own variable.

Change-Id: I58ed30c5886209aac510b6adbff39d4e0e587b1e
---
M includes/AutoLoader.php
A includes/parser/CoreVariables.php
M includes/parser/Parser.php
3 files changed, 461 insertions(+), 292 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/98/76498/1

diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php
index bb8c272..510ad37 100644
--- a/includes/AutoLoader.php
+++ b/includes/AutoLoader.php
@@ -781,6 +781,7 @@
        'CacheTime' => 'includes/parser/CacheTime.php',
        'CoreParserFunctions' => 'includes/parser/CoreParserFunctions.php',
        'CoreTagHooks' => 'includes/parser/CoreTagHooks.php',
+       'CoreVariables' => 'includes/parser/CoreVariables.php',
        'DateFormatter' => 'includes/parser/DateFormatter.php',
        'LinkHolderArray' => 'includes/parser/LinkHolderArray.php',
        'MWTidy' => 'includes/parser/Tidy.php',
diff --git a/includes/parser/CoreVariables.php 
b/includes/parser/CoreVariables.php
new file mode 100644
index 0000000..3a8eb52
--- /dev/null
+++ b/includes/parser/CoreVariables.php
@@ -0,0 +1,422 @@
+<?php
+/**
+ * Variables provided by MediaWiki core
+ *
+ * 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
+ *
+ * @file
+ * @ingroup Parser
+ * @since 1.22
+ */
+
+/**
+ * Various core variables, registered in Parser::firstCallInit()
+ * @ingroup Parser
+ * @since 1.22
+ */
+class CoreVariables {
+       /**
+        * @param $parser Parser
+        * @return void
+        */
+       static function register( $parser ) {
+               # Syntax for arguments (see Parser::setVariableHook):
+               #  "name for lookup in localized magic words array",
+               #  function callback
+
+               $parser->setVariableHook( 'numberofpages', array( __CLASS__, 
'numberofpages' ) );
+               $parser->setVariableHook( 'numberofusers', array( __CLASS__, 
'numberofusers' ) );
+               $parser->setVariableHook( 'numberofactiveusers', array( 
__CLASS__, 'numberofactiveusers' ) );
+               $parser->setVariableHook( 'numberofarticles', array( __CLASS__, 
'numberofarticles' ) );
+               $parser->setVariableHook( 'numberoffiles', array( __CLASS__, 
'numberoffiles' ) );
+               $parser->setVariableHook( 'numberofadmins', array( __CLASS__, 
'numberofadmins' ) );
+               $parser->setVariableHook( 'numberofedits', array( __CLASS__, 
'numberofedits' ) );
+               $parser->setVariableHook( 'numberofviews', array( __CLASS__, 
'numberofviews' ) );
+               $parser->setVariableHook( 'namespace', array( __CLASS__, 
'mwnamespace' ) );
+               $parser->setVariableHook( 'namespacee', array( __CLASS__, 
'namespacee' ) );
+               $parser->setVariableHook( 'namespacenumber', array( __CLASS__, 
'namespacenumber' ) );
+               $parser->setVariableHook( 'talkspace', array( __CLASS__, 
'talkspace' ) );
+               $parser->setVariableHook( 'talkspacee', array( __CLASS__, 
'talkspacee' ) );
+               $parser->setVariableHook( 'subjectspace', array( __CLASS__, 
'subjectspace' ) );
+               $parser->setVariableHook( 'subjectspacee', array( __CLASS__, 
'subjectspacee' ) );
+               $parser->setVariableHook( 'pagename', array( __CLASS__, 
'pagename' ) );
+               $parser->setVariableHook( 'pagenamee', array( __CLASS__, 
'pagenamee' ) );
+               $parser->setVariableHook( 'fullpagename', array( __CLASS__, 
'fullpagename' ) );
+               $parser->setVariableHook( 'fullpagenamee', array( __CLASS__, 
'fullpagenamee' ) );
+               $parser->setVariableHook( 'subpagename', array( __CLASS__, 
'subpagename' ) );
+               $parser->setVariableHook( 'subpagenamee', array( __CLASS__, 
'subpagenamee' ) );
+               $parser->setVariableHook( 'rootpagename', array( __CLASS__, 
'rootpagename' ) );
+               $parser->setVariableHook( 'rootpagenamee', array( __CLASS__, 
'rootpagenamee' ) );
+               $parser->setVariableHook( 'basepagename', array( __CLASS__, 
'basepagename' ) );
+               $parser->setVariableHook( 'basepagenamee', array( __CLASS__, 
'basepagenamee' ) );
+               $parser->setVariableHook( 'talkpagename', array( __CLASS__, 
'talkpagename' ) );
+               $parser->setVariableHook( 'talkpagenamee', array( __CLASS__, 
'talkpagenamee' ) );
+               $parser->setVariableHook( 'subjectpagename', array( __CLASS__, 
'subjectpagename' ) );
+               $parser->setVariableHook( 'subjectpagenamee', array( __CLASS__, 
'subjectpagenamee' ) );
+               $parser->setVariableHook( 'currentversion', array( __CLASS__, 
'currentversion' ) );
+               $parser->setVariableHook( 'articlepath', array( __CLASS__, 
'articlepath' ) );
+               $parser->setVariableHook( 'sitename', array( __CLASS__, 
'sitename' ) );
+               $parser->setVariableHook( 'server', array( __CLASS__, 'server' 
) );
+               $parser->setVariableHook( 'servername', array( __CLASS__, 
'servername' ) );
+               $parser->setVariableHook( 'scriptpath', array( __CLASS__, 
'scriptpath' ) );
+               $parser->setVariableHook( 'stylepath', array( __CLASS__, 
'stylepath' ) );
+               $parser->setVariableHook( 'directionmark', array( __CLASS__, 
'directionmark' ) );
+               $parser->setVariableHook( 'contentlanguage', array( __CLASS__, 
'contentlanguage' ) );
+               $parser->setVariableHook( 'pageid', array( __CLASS__, 'pageid' 
) );
+               $parser->setVariableHook( 'revisionid', array( __CLASS__, 
'revisionid' ) );
+               $parser->setVariableHook( 'revisionday', array( __CLASS__, 
'revisionday' ) );
+               $parser->setVariableHook( 'revisionday2', array( __CLASS__, 
'revisionday2' ) );
+               $parser->setVariableHook( 'revisionmonth', array( __CLASS__, 
'revisionmonth' ) );
+               $parser->setVariableHook( 'revisionmonth1', array( __CLASS__, 
'revisionmonth1' ) );
+               $parser->setVariableHook( 'revisionyear', array( __CLASS__, 
'revisionyear' ) );
+               $parser->setVariableHook( 'revisiontimestamp', array( 
__CLASS__, 'revisiontimestamp' ) );
+               $parser->setVariableHook( 'revisionuser', array( __CLASS__, 
'revisionuser' ) );
+               $parser->setVariableHook( 'currentmonth', array( __CLASS__, 
'currentmonth' ) );
+               $parser->setVariableHook( 'currentmonth1', array( __CLASS__, 
'currentmonth1' ) );
+               $parser->setVariableHook( 'currentmonthname', array( __CLASS__, 
'currentmonthname' ) );
+               $parser->setVariableHook( 'currentmonthnamegen', array( 
__CLASS__, 'currentmonthnamegen' ) );
+               $parser->setVariableHook( 'currentmonthabbrev', array( 
__CLASS__, 'currentmonthabbrev' ) );
+               $parser->setVariableHook( 'currentday', array( __CLASS__, 
'currentday' ) );
+               $parser->setVariableHook( 'currentday2', array( __CLASS__, 
'currentday2' ) );
+               $parser->setVariableHook( 'localmonth', array( __CLASS__, 
'localmonth' ) );
+               $parser->setVariableHook( 'localmonth1', array( __CLASS__, 
'localmonth1' ) );
+               $parser->setVariableHook( 'localmonthname', array( __CLASS__, 
'localmonthname' ) );
+               $parser->setVariableHook( 'localmonthnamegen', array( 
__CLASS__, 'localmonthnamegen' ) );
+               $parser->setVariableHook( 'localmonthabbrev', array( __CLASS__, 
'localmonthabbrev' ) );
+               $parser->setVariableHook( 'localday', array( __CLASS__, 
'localday' ) );
+               $parser->setVariableHook( 'localday2', array( __CLASS__, 
'localday2' ) );
+               $parser->setVariableHook( 'currentdayname', array( __CLASS__, 
'currentdayname' ) );
+               $parser->setVariableHook( 'currentyear', array( __CLASS__, 
'currentyear' ) );
+               $parser->setVariableHook( 'currenttime', array( __CLASS__, 
'currenttime' ) );
+               $parser->setVariableHook( 'currenthour', array( __CLASS__, 
'currenthour' ) );
+               $parser->setVariableHook( 'currentweek', array( __CLASS__, 
'currentweek' ) );
+               $parser->setVariableHook( 'currentdow', array( __CLASS__, 
'currentdow' ) );
+               $parser->setVariableHook( 'localdayname', array( __CLASS__, 
'localdayname' ) );
+               $parser->setVariableHook( 'localyear', array( __CLASS__, 
'localyear' ) );
+               $parser->setVariableHook( 'localtime', array( __CLASS__, 
'localtime' ) );
+               $parser->setVariableHook( 'localhour', array( __CLASS__, 
'localhour' ) );
+               $parser->setVariableHook( 'localweek', array( __CLASS__, 
'localweek' ) );
+               $parser->setVariableHook( 'localdow', array( __CLASS__, 
'localdow' ) );
+               $parser->setVariableHook( 'currenttimestamp', array( __CLASS__, 
'currenttimestamp' ) );
+               $parser->setVariableHook( 'localtimestamp', array( __CLASS__, 
'localtimestamp' ) );
+       }
+
+       static function numberofpages( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
SiteStats::pages() );
+       }
+       static function numberofusers( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
SiteStats::users() );
+       }
+       static function numberofactiveusers( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
SiteStats::activeUsers() );
+       }
+       static function numberofarticles( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
SiteStats::articles() );
+       }
+       static function numberoffiles( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
SiteStats::images() );
+       }
+       static function numberofadmins( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
SiteStats::numberingroup( 'sysop' ) );
+       }
+       static function numberofedits( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
SiteStats::edits() );
+       }
+       static function numberofviews( $parser ) {
+               global $wgDisableCounters;
+               return !$wgDisableCounters ? 
$parser->getFunctionLang()->formatNum( SiteStats::views() ) : '';
+       }
+
+       /**
+        * return the namespace name of the title set on the parser
+        * Note: function name changed to "mwnamespace" rather than "namespace"
+        * to not break PHP 5.3
+        * @return mixed|string
+        */
+       static function mwnamespace( $parser ) {
+               global $wgContLang;
+               return str_replace( '_', ' ', $wgContLang->getNsText( 
$parser->getTitle()->getNamespace() ) );
+       }
+       static function namespacee( $parser ) {
+               global $wgContLang;
+               return wfUrlencode( $wgContLang->getNsText( 
$parser->getTitle()->getNamespace() ) );
+       }
+       static function namespacenumber( $parser ) {
+               return $parser->getTitle()->getNamespace();
+       }
+       static function talkspace( $parser ) {
+               return $parser->getTitle()->canTalk() ? str_replace( '_', ' ', 
$parser->getTitle()->getTalkNsText() ) : '';
+       }
+       static function talkspacee( $parser ) {
+               return $parser->getTitle()->canTalk() ? wfUrlencode( 
$parser->getTitle()->getTalkNsText() ) : '';
+       }
+       static function subjectspace( $parser ) {
+               return $parser->getTitle()->getSubjectNsText();
+       }
+       static function subjectspacee( $parser ) {
+               return wfUrlencode( $parser->getTitle()->getSubjectNsText() );
+       }
+
+       /**
+        * Functions to get the pagename of the parser
+        * @return String
+        */
+       static function pagename( $parser ) {
+               return wfEscapeWikiText( $parser->getTitle()->getText() );
+       }
+       static function pagenamee( $parser ) {
+               return wfEscapeWikiText( $parser->getTitle()->getPartialURL() );
+       }
+       static function fullpagename( $parser ) {
+               return wfEscapeWikiText( $parser->getTitle()->getPrefixedText() 
);
+       }
+       static function fullpagenamee( $parser ) {
+               return wfEscapeWikiText( $parser->getTitle()->getPrefixedURL() 
);
+       }
+       static function subpagename( $parser ) {
+               return wfEscapeWikiText( $parser->getTitle()->getSubpageText() 
);
+       }
+       static function subpagenamee( $parser ) {
+               return wfEscapeWikiText( 
$parser->getTitle()->getSubpageUrlForm() );
+       }
+       static function rootpagename( $parser ) {
+               return wfEscapeWikiText( $parser->getTitle()->getRootText() );
+       }
+       static function rootpagenamee( $parser ) {
+               return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', 
$parser->getTitle()->getRootText() ) ) );
+       }
+       static function basepagename( $parser ) {
+               return wfEscapeWikiText( $parser->getTitle()->getBaseText() );
+       }
+       static function basepagenamee( $parser ) {
+               return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', 
$parser->getTitle()->getBaseText() ) ) );
+       }
+       static function talkpagename( $parser ) {
+               return $parser->getTitle()->canTalk() ? wfEscapeWikiText( 
$parser->getTitle()->getTalkPage()->getPrefixedText() ) : '';
+       }
+       static function talkpagenamee( $parser ) {
+               return $parser->getTitle()->canTalk() ? wfEscapeWikiText( 
$parser->getTitle()->getTalkPage()->getPrefixedURL() ) : '';
+       }
+       static function subjectpagename( $parser ) {
+               return wfEscapeWikiText( 
$parser->getTitle()->getSubjectPage()->getPrefixedText() );
+       }
+       static function subjectpagenamee( $parser ) {
+               return wfEscapeWikiText( 
$parser->getTitle()->getSubjectPage()->getPrefixedURL() );
+       }
+
+       static function pageid( $parser ) { // requested in bug 23427
+               $pageid = $parser->getTitle()->getArticleID();
+               if ( $pageid == 0 ) {
+                       # 0 means the page doesn't exist in the database,
+                       # which means the user is previewing a new page.
+                       # The vary-revision flag must be set, because the magic 
word
+                       # will have a different value once the page is saved.
+                       $parser->getOutput()->setFlag( 'vary-revision' );
+                       wfDebug( __METHOD__ . ": {{PAGEID}} used in a new page, 
setting vary-revision...\n" );
+               }
+               return $pageid ? $pageid : null;
+       }
+       static function revisionid( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting 
vary-revision...\n" );
+               return $parser->getRevisionId();
+       }
+       static function revisionday( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned. This is for null 
edits.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONDAY}} used, setting 
vary-revision...\n" );
+               return intval( substr( $parser->getRevisionTimestamp(), 6, 2 ) 
);
+       }
+       static function revisionday2( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned. This is for null 
edits.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONDAY2}} used, setting 
vary-revision...\n" );
+               return substr( $parser->getRevisionTimestamp(), 6, 2 );
+       }
+       static function revisionmonth( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned. This is for null 
edits.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONMONTH}} used, setting 
vary-revision...\n" );
+               return substr( $parser->getRevisionTimestamp(), 4, 2 );
+       }
+       static function revisionmonth1( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned. This is for null 
edits.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONMONTH1}} used, setting 
vary-revision...\n" );
+               return intval( substr( $parser->getRevisionTimestamp(), 4, 2 ) 
);
+       }
+       static function revisionyear( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned. This is for null 
edits.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONYEAR}} used, setting 
vary-revision...\n" );
+               return substr( $parser->getRevisionTimestamp(), 0, 4 );
+       }
+       static function revisiontimestamp( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned. This is for null 
edits.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONTIMESTAMP}} used, setting 
vary-revision...\n" );
+               return $parser->getRevisionTimestamp();
+       }
+       static function revisionuser( $parser ) {
+               # Let the edit saving system know we should parse the page
+               # *after* a revision ID has been assigned. This is for null 
edits.
+               $parser->getOutput()->setFlag( 'vary-revision' );
+               wfDebug( __METHOD__ . ": {{REVISIONUSER}} used, setting 
vary-revision...\n" );
+               return $parser->getRevisionUser();
+       }
+
+       static function currentversion( $parser ) {
+               return SpecialVersion::getVersion();
+       }
+       static function articlepath( $parser ) {
+               global $wgArticlePath;
+               return $wgArticlePath;
+       }
+       static function sitename( $parser ) {
+               global $wgSitename;
+               return $wgSitename;
+       }
+       static function server( $parser ) {
+               global $wgServer;
+               return $wgServer;
+       }
+       static function servername( $parser ) {
+               global $wgServer;
+               $serverParts = wfParseUrl( $wgServer );
+               return $serverParts && isset( $serverParts['host'] ) ? 
$serverParts['host'] : $wgServer;
+       }
+       static function scriptpath( $parser ) {
+               global $wgScriptPath;
+               return $wgScriptPath;
+       }
+       static function stylepath( $parser ) {
+               global $wgStylePath;
+               return $wgStylePath;
+       }
+       static function directionmark( $parser ) {
+               return $parser->getFunctionLang()->getDirMark();
+       }
+       static function contentlanguage( $parser ) {
+               global $wgLanguageCode;
+               return $wgLanguageCode;
+       }
+
+       /**
+        * Run the ParserGetVariableValueTs hook to allow extension 
modification of the timestamp
+        */
+       private static function getVariableValueTs( $parser ) {
+               $ts = wfTimestamp( TS_UNIX, 
$parser->getOptions()->getTimestamp() );
+               wfRunHooks( 'ParserGetVariableValueTs', array( &$parser, &$ts ) 
);
+               return $ts;
+       }
+
+       static function currentmonth( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'm' ) 
);
+       }
+       static function currentmonth1( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'n' ) 
);
+       }
+       static function currentmonthname( $parser ) {
+               return $parser->getFunctionLang()->getMonthName( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'n' ) 
);
+       }
+       static function currentmonthnamegen( $parser ) {
+               return $parser->getFunctionLang()->getMonthNameGen( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'n' ) 
);
+       }
+       static function currentmonthabbrev( $parser ) {
+               return $parser->getFunctionLang()->getMonthAbbreviation( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'n' ) 
);
+       }
+       static function currentday( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'j' ) 
);
+       }
+       static function currentday2( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'd' ) 
);
+       }
+       static function localmonth( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'm' ) );
+       }
+       static function localmonth1( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'n' ) );
+       }
+       static function localmonthname( $parser ) {
+               return $parser->getFunctionLang()->getMonthName( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'n' ) );
+       }
+       static function localmonthnamegen( $parser ) {
+               return $parser->getFunctionLang()->getMonthNameGen( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'n' ) );
+       }
+       static function localmonthabbrev( $parser ) {
+               return $parser->getFunctionLang()->getMonthAbbreviation( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'n' ) );
+       }
+       static function localday( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'j' ) );
+       }
+       static function localday2( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'd' ) );
+       }
+       static function currentdayname( $parser ) {
+               return $parser->getFunctionLang()->getWeekdayName( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'w' ) 
+ 1 );
+       }
+       static function currentyear( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'Y' ), 
true );
+       }
+       static function currenttime( $parser ) {
+               return $parser->getFunctionLang()->time( wfTimestamp( TS_MW, 
self::getVariableValueTs( $parser ) ), false, false );
+       }
+       static function currenthour( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'H' ), 
true );
+       }
+       static function currentweek( $parser ) {
+               # @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
+               # int to remove the padding
+               return $parser->getFunctionLang()->formatNum( 
(int)MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 
'W' ) );
+       }
+       static function currentdow( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getInstance( self::getVariableValueTs( $parser ) )->format( 'w' ) 
);
+       }
+       static function localdayname( $parser ) {
+               return $parser->getFunctionLang()->getWeekdayName( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'w' ) + 1 );
+       }
+       static function localyear( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'Y' ), true );
+       }
+       static function localtime( $parser ) {
+               return $parser->getFunctionLang()->time( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'YmdHis' ), false, false );
+       }
+       static function localhour( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'H' ), true );
+       }
+       static function localweek( $parser ) {
+               # @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
+               # int to remove the padding
+               return $parser->getFunctionLang()->formatNum( 
(int)MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) 
)->format( 'W' ) );
+       }
+       static function localdow( $parser ) {
+               return $parser->getFunctionLang()->formatNum( 
MWTimestamp::getLocalInstance( self::getVariableValueTs( $parser ) )->format( 
'w' ) );
+       }
+       static function currenttimestamp( $parser ) {
+               return wfTimestamp( TS_MW, self::getVariableValueTs( $parser ) 
);
+       }
+       static function localtimestamp( $parser ) {
+               return MWTimestamp::getLocalInstance( self::getVariableValueTs( 
$parser ) )->format( 'YmdHis' );
+       }
+}
diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php
index 813aaca..a8ba04b 100644
--- a/includes/parser/Parser.php
+++ b/includes/parser/Parser.php
@@ -121,6 +121,7 @@
        var $mFunctionHooks = array();
        var $mFunctionSynonyms = array( 0 => array(), 1 => array() );
        var $mFunctionTagHooks = array();
+       var $mVariableHooks = array();
        var $mStripList = array();
        var $mDefaultStripList = array();
        var $mVarCache = array();
@@ -265,6 +266,7 @@
 
                CoreParserFunctions::register( $this );
                CoreTagHooks::register( $this );
+               CoreVariables::register( $this );
                $this->initialiseVariables();
 
                wfRunHooks( 'ParserFirstCallInit', array( &$this ) );
@@ -2656,10 +2658,7 @@
         * @return string
         */
        function getVariableValue( $index, $frame = false ) {
-               global $wgContLang, $wgSitename, $wgServer;
-               global $wgArticlePath, $wgScriptPath, $wgStylePath;
-
-               if ( is_null( $this->mTitle ) ) {
+               if ( is_null( $this->getTitle() ) ) {
                        // If no title set, bad things are going to happen
                        // later. Title should always be set since this
                        // should only be called in the middle of a parse
@@ -2678,296 +2677,24 @@
                        }
                }
 
-               $ts = wfTimestamp( TS_UNIX, $this->mOptions->getTimestamp() );
-               wfRunHooks( 'ParserGetVariableValueTs', array( &$this, &$ts ) );
+               if ( isset( $this->mVariableHooks[$index] ) ) {
+                       list( $callback ) = $this->mVariableHooks[$index];
 
-               $pageLang = $this->getFunctionLang();
+                       # Workaround for PHP bug 35229 and similar
+                       if ( !is_callable( $callback ) ) {
+                               throw new MWException( "Variable hook for 
$index is not callable\n" );
+                       }
 
-               switch ( $index ) {
-                       case 'currentmonth':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getInstance( $ts )->format( 'm' ) );
-                               break;
-                       case 'currentmonth1':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'currentmonthname':
-                               $value = $pageLang->getMonthName( 
MWTimestamp::getInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'currentmonthnamegen':
-                               $value = $pageLang->getMonthNameGen( 
MWTimestamp::getInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'currentmonthabbrev':
-                               $value = $pageLang->getMonthAbbreviation( 
MWTimestamp::getInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'currentday':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getInstance( $ts )->format( 'j' ) );
-                               break;
-                       case 'currentday2':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getInstance( $ts )->format( 'd' ) );
-                               break;
-                       case 'localmonth':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getLocalInstance( $ts )->format( 'm' ) );
-                               break;
-                       case 'localmonth1':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'localmonthname':
-                               $value = $pageLang->getMonthName( 
MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'localmonthnamegen':
-                               $value = $pageLang->getMonthNameGen( 
MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'localmonthabbrev':
-                               $value = $pageLang->getMonthAbbreviation( 
MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
-                               break;
-                       case 'localday':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getLocalInstance( $ts )->format( 'j' ) );
-                               break;
-                       case 'localday2':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getLocalInstance( $ts )->format( 'd' ) );
-                               break;
-                       case 'pagename':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getText() );
-                               break;
-                       case 'pagenamee':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getPartialURL() );
-                               break;
-                       case 'fullpagename':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getPrefixedText() );
-                               break;
-                       case 'fullpagenamee':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getPrefixedURL() );
-                               break;
-                       case 'subpagename':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getSubpageText() );
-                               break;
-                       case 'subpagenamee':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getSubpageUrlForm() );
-                               break;
-                       case 'rootpagename':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getRootText() );
-                               break;
-                       case 'rootpagenamee':
-                               $value = wfEscapeWikiText( wfUrlEncode( 
str_replace( ' ', '_', $this->mTitle->getRootText() ) ) );
-                               break;
-                       case 'basepagename':
-                               $value = wfEscapeWikiText( 
$this->mTitle->getBaseText() );
-                               break;
-                       case 'basepagenamee':
-                               $value = wfEscapeWikiText( wfUrlEncode( 
str_replace( ' ', '_', $this->mTitle->getBaseText() ) ) );
-                               break;
-                       case 'talkpagename':
-                               if ( $this->mTitle->canTalk() ) {
-                                       $talkPage = 
$this->mTitle->getTalkPage();
-                                       $value = wfEscapeWikiText( 
$talkPage->getPrefixedText() );
-                               } else {
-                                       $value = '';
-                               }
-                               break;
-                       case 'talkpagenamee':
-                               if ( $this->mTitle->canTalk() ) {
-                                       $talkPage = 
$this->mTitle->getTalkPage();
-                                       $value = wfEscapeWikiText( 
$talkPage->getPrefixedURL() );
-                               } else {
-                                       $value = '';
-                               }
-                               break;
-                       case 'subjectpagename':
-                               $subjPage = $this->mTitle->getSubjectPage();
-                               $value = wfEscapeWikiText( 
$subjPage->getPrefixedText() );
-                               break;
-                       case 'subjectpagenamee':
-                               $subjPage = $this->mTitle->getSubjectPage();
-                               $value = wfEscapeWikiText( 
$subjPage->getPrefixedURL() );
-                               break;
-                       case 'pageid': // requested in bug 23427
-                               $pageid = $this->getTitle()->getArticleID();
-                               if ( $pageid == 0 ) {
-                                       # 0 means the page doesn't exist in the 
database,
-                                       # which means the user is previewing a 
new page.
-                                       # The vary-revision flag must be set, 
because the magic word
-                                       # will have a different value once the 
page is saved.
-                                       $this->mOutput->setFlag( 
'vary-revision' );
-                                       wfDebug( __METHOD__ . ": {{PAGEID}} 
used in a new page, setting vary-revision...\n" );
-                               }
-                               $value = $pageid ? $pageid : null;
-                               break;
-                       case 'revisionid':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONID}} used, 
setting vary-revision...\n" );
-                               $value = $this->mRevisionId;
-                               break;
-                       case 'revisionday':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned. This 
is for null edits.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONDAY}} used, 
setting vary-revision...\n" );
-                               $value = intval( substr( 
$this->getRevisionTimestamp(), 6, 2 ) );
-                               break;
-                       case 'revisionday2':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned. This 
is for null edits.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONDAY2}} used, 
setting vary-revision...\n" );
-                               $value = substr( $this->getRevisionTimestamp(), 
6, 2 );
-                               break;
-                       case 'revisionmonth':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned. This 
is for null edits.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONMONTH}} 
used, setting vary-revision...\n" );
-                               $value = substr( $this->getRevisionTimestamp(), 
4, 2 );
-                               break;
-                       case 'revisionmonth1':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned. This 
is for null edits.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONMONTH1}} 
used, setting vary-revision...\n" );
-                               $value = intval( substr( 
$this->getRevisionTimestamp(), 4, 2 ) );
-                               break;
-                       case 'revisionyear':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned. This 
is for null edits.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONYEAR}} used, 
setting vary-revision...\n" );
-                               $value = substr( $this->getRevisionTimestamp(), 
0, 4 );
-                               break;
-                       case 'revisiontimestamp':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned. This 
is for null edits.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONTIMESTAMP}} 
used, setting vary-revision...\n" );
-                               $value = $this->getRevisionTimestamp();
-                               break;
-                       case 'revisionuser':
-                               # Let the edit saving system know we should 
parse the page
-                               # *after* a revision ID has been assigned. This 
is for null edits.
-                               $this->mOutput->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": {{REVISIONUSER}} used, 
setting vary-revision...\n" );
-                               $value = $this->getRevisionUser();
-                               break;
-                       case 'namespace':
-                               $value = str_replace( '_', ' ', 
$wgContLang->getNsText( $this->mTitle->getNamespace() ) );
-                               break;
-                       case 'namespacee':
-                               $value = wfUrlencode( $wgContLang->getNsText( 
$this->mTitle->getNamespace() ) );
-                               break;
-                       case 'namespacenumber':
-                               $value = $this->mTitle->getNamespace();
-                               break;
-                       case 'talkspace':
-                               $value = $this->mTitle->canTalk() ? 
str_replace( '_', ' ', $this->mTitle->getTalkNsText() ) : '';
-                               break;
-                       case 'talkspacee':
-                               $value = $this->mTitle->canTalk() ? 
wfUrlencode( $this->mTitle->getTalkNsText() ) : '';
-                               break;
-                       case 'subjectspace':
-                               $value = $this->mTitle->getSubjectNsText();
-                               break;
-                       case 'subjectspacee':
-                               $value = ( wfUrlencode( 
$this->mTitle->getSubjectNsText() ) );
-                               break;
-                       case 'currentdayname':
-                               $value = $pageLang->getWeekdayName( 
MWTimestamp::getInstance( $ts )->format( 'w' ) + 1 );
-                               break;
-                       case 'currentyear':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getInstance( $ts )->format( 'Y' ), true );
-                               break;
-                       case 'currenttime':
-                               $value = $pageLang->time( wfTimestamp( TS_MW, 
$ts ), false, false );
-                               break;
-                       case 'currenthour':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getInstance( $ts )->format( 'H' ), true );
-                               break;
-                       case 'currentweek':
-                               # @bug 4594 PHP5 has it zero padded, PHP4 does 
not, cast to
-                               # int to remove the padding
-                               $value = $pageLang->formatNum( 
(int)MWTimestamp::getInstance( $ts )->format( 'W' ) );
-                               break;
-                       case 'currentdow':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getInstance( $ts )->format( 'w' ) );
-                               break;
-                       case 'localdayname':
-                               $value = $pageLang->getWeekdayName( 
MWTimestamp::getLocalInstance( $ts )->format( 'w' ) + 1 );
-                               break;
-                       case 'localyear':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getLocalInstance( $ts )->format( 'Y' ), true );
-                               break;
-                       case 'localtime':
-                               $value = $pageLang->time( 
MWTimestamp::getLocalInstance( $ts )->format( 'YmdHis' ), false, false );
-                               break;
-                       case 'localhour':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getLocalInstance( $ts )->format( 'H' ), true );
-                               break;
-                       case 'localweek':
-                               # @bug 4594 PHP5 has it zero padded, PHP4 does 
not, cast to
-                               # int to remove the padding
-                               $value = $pageLang->formatNum( 
(int)MWTimestamp::getLocalInstance( $ts )->format( 'W' ) );
-                               break;
-                       case 'localdow':
-                               $value = $pageLang->formatNum( 
MWTimestamp::getLocalInstance( $ts )->format( 'w' ) );
-                               break;
-                       case 'numberofarticles':
-                               $value = $pageLang->formatNum( 
SiteStats::articles() );
-                               break;
-                       case 'numberoffiles':
-                               $value = $pageLang->formatNum( 
SiteStats::images() );
-                               break;
-                       case 'numberofusers':
-                               $value = $pageLang->formatNum( 
SiteStats::users() );
-                               break;
-                       case 'numberofactiveusers':
-                               $value = $pageLang->formatNum( 
SiteStats::activeUsers() );
-                               break;
-                       case 'numberofpages':
-                               $value = $pageLang->formatNum( 
SiteStats::pages() );
-                               break;
-                       case 'numberofadmins':
-                               $value = $pageLang->formatNum( 
SiteStats::numberingroup( 'sysop' ) );
-                               break;
-                       case 'numberofedits':
-                               $value = $pageLang->formatNum( 
SiteStats::edits() );
-                               break;
-                       case 'numberofviews':
-                               global $wgDisableCounters;
-                               $value = !$wgDisableCounters ? 
$pageLang->formatNum( SiteStats::views() ) : '';
-                               break;
-                       case 'currenttimestamp':
-                               $value = wfTimestamp( TS_MW, $ts );
-                               break;
-                       case 'localtimestamp':
-                               $value = MWTimestamp::getLocalInstance( $ts 
)->format( 'YmdHis' );
-                               break;
-                       case 'currentversion':
-                               $value = SpecialVersion::getVersion();
-                               break;
-                       case 'articlepath':
-                               return $wgArticlePath;
-                       case 'sitename':
-                               return $wgSitename;
-                       case 'server':
-                               return $wgServer;
-                       case 'servername':
-                               $serverParts = wfParseUrl( $wgServer );
-                               return $serverParts && isset( 
$serverParts['host'] ) ? $serverParts['host'] : $wgServer;
-                       case 'scriptpath':
-                               return $wgScriptPath;
-                       case 'stylepath':
-                               return $wgStylePath;
-                       case 'directionmark':
-                               return $pageLang->getDirMark();
-                       case 'contentlanguage':
-                               global $wgLanguageCode;
-                               return $wgLanguageCode;
-                       default:
-                               $ret = null;
-                               if ( wfRunHooks( 
'ParserGetVariableValueSwitch', array( &$this, &$this->mVarCache, &$index, 
&$ret, &$frame ) ) ) {
-                                       return $ret;
-                               } else {
-                                       return null;
-                               }
+                       $arg = array( $this );
+                       $value = call_user_func_array( $callback, $arg );
+               } else {
+                       // backward compatibility
+                       $ret = null;
+                       if ( wfRunHooks( 'ParserGetVariableValueSwitch', array( 
&$this, &$this->mVarCache, &$index, &$ret, &$frame ) ) ) {
+                               return $ret;
+                       } else {
+                               return null;
+                       }
                }
 
                if ( $index ) {
@@ -4963,6 +4690,25 @@
        }
 
        /**
+        * Create a variable, e.g. "{{PAGENAME}}".
+        * @param $variable
+        * @param $callback
+        * @throws MWException
+        * @return null
+        */
+       function setVariableHook( $variable, $callback ) {
+               $variable = strtolower( $variable );
+               if ( preg_match( '/[<>\r\n]/', $variable, $m ) ) {
+                       throw new MWException( "Invalid character {$m[0]} in 
setVariableHook('$variable', ...) call" );
+               }
+               $old = isset( $this->mVariableHooks[$variable] ) ?
+                       $this->mVariableHooks[$variable] : null;
+               $this->mVariableHooks[$variable] = array( $callback );
+
+               return $old;
+       }
+
+       /**
         * @todo FIXME: Update documentation. makeLinkObj() is deprecated.
         * Replace "<!--LINK-->" link placeholders with actual links, in the 
buffer
         * Placeholders created in Skin::makeLinkObj()

-- 
To view, visit https://gerrit.wikimedia.org/r/76498
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I58ed30c5886209aac510b6adbff39d4e0e587b1e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to