/** * Do various kinds of initialisation on the first call of the parser */ function firstCallInit() { parent::__construct(); if (!$this->mFirstCall) { return; } $this->mFirstCall = false; wfProfileIn(__METHOD__); $this->setHook('pre', array($this, 'renderPreTag')); CoreParserFunctions::register($this); CoreLinkFunctions::register($this); $this->initialiseVariables(); wfRunHooks('ParserFirstCallInit', array(&$this)); wfProfileOut(__METHOD__); }
/** * Return value of a magic variable (like PAGENAME) * * @private * * @param $index integer * @param bool|\PPFrame $frame * * @throws MWException * @return string */ function getVariableValue($index, $frame = false) { global $wgContLang, $wgSitename, $wgServer; global $wgArticlePath, $wgScriptPath, $wgStylePath; if (is_null($this->mTitle)) { // 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 // operation (but the unit-tests do funky stuff) throw new MWException(__METHOD__ . ' Should only be ' . ' called while parsing (no title set)'); } /** * Some of these require message or data lookups and can be * expensive to check many times. */ if (wfRunHooks('ParserGetVariableValueVarCache', array(&$this, &$this->mVarCache))) { if (isset($this->mVarCache[$index])) { return $this->mVarCache[$index]; } } $ts = wfTimestamp(TS_UNIX, $this->mOptions->getTimestamp()); wfRunHooks('ParserGetVariableValueTs', array(&$this, &$ts)); $pageLang = $this->getFunctionLang(); 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 'revisionsize': # 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__ . ": {{REVISIONSIZE}} used, setting vary-revision...\n"); $value = $this->getRevisionSize(); 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 = str_replace('_', ' ', $this->mTitle->getSubjectNsText()); break; case 'subjectspacee': $value = wfUrlencode($this->mTitle->getSubjectNsText()); break; case 'currentdayname': $value = $pageLang->getWeekdayName((int) 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((int) 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; case 'cascadingsources': $value = CoreParserFunctions::cascadingsources($this); break; default: $ret = null; wfRunHooks('ParserGetVariableValueSwitch', array(&$this, &$this->mVarCache, &$index, &$ret, &$frame)); return $ret; } if ($index) { $this->mVarCache[$index] = $value; } return $value; }
/** * Do various kinds of initialisation on the first call of the parser */ function firstCallInit() { if (!$this->mFirstCall) { return; } $this->mFirstCall = false; wfProfileIn(__METHOD__); CoreParserFunctions::register($this); CoreTagHooks::register($this); $this->initialiseVariables(); wfRunHooks('ParserFirstCallInit', array(&$this)); wfProfileOut(__METHOD__); }
public function anchorEncode($s) { return array(CoreParserFunctions::anchorencode($this->getParser(), $s)); }
/** * Create a new TitleBlacklistEntry from a line of text * * @param $line String containing a line of blacklist text * @return TitleBlacklistEntry */ public static function newFromString($line, $source) { $raw = $line; // Keep line for raw data $options = array(); // Strip comments $line = preg_replace("/^\\s*([^#]*)\\s*((.*)?)\$/", "\\1", $line); $line = trim($line); // Parse the rest of message preg_match('/^(.*?)(\\s*<([^<>]*)>)?$/', $line, $pockets); @(list($full, $regex, $null, $opts_str) = $pockets); $regex = trim($regex); $regex = str_replace('_', ' ', $regex); // We'll be matching against text form $opts_str = trim($opts_str); // Parse opts $opts = preg_split('/\\s*\\|\\s*/', $opts_str); foreach ($opts as $opt) { $opt2 = strtolower($opt); if ($opt2 == 'autoconfirmed') { $options['autoconfirmed'] = true; } if ($opt2 == 'moveonly') { $options['moveonly'] = true; } if ($opt2 == 'newaccountonly') { $options['newaccountonly'] = true; } if ($opt2 == 'noedit') { $options['noedit'] = true; } if ($opt2 == 'casesensitive') { $options['casesensitive'] = true; } if ($opt2 == 'reupload') { $options['reupload'] = true; } if (preg_match('/errmsg\\s*=\\s*(.+)/i', $opt, $matches)) { $options['errmsg'] = $matches[1]; } if ($opt2 == 'antispoof') { $options['antispoof'] = true; } } // Process magic words preg_match_all('/{{\\s*([a-z]+)\\s*:\\s*(.+?)\\s*}}/', $regex, $magicwords, PREG_SET_ORDER); foreach ($magicwords as $mword) { global $wgParser; // Functions we're calling don't need, nevertheless let's use it switch (strtolower($mword[1])) { case 'ns': $cpf_result = CoreParserFunctions::ns($wgParser, $mword[2]); if (is_string($cpf_result)) { $regex = str_replace($mword[0], $cpf_result, $regex); // All result will have the same value, so we can just use str_seplace() } break; case 'int': $cpf_result = wfMessage($mword[2])->inContentLanguage()->text(); if (is_string($cpf_result)) { $regex = str_replace($mword[0], $cpf_result, $regex); } } } // Return result if ($regex) { return new TitleBlacklistEntry($regex, $options, $raw, $source); } else { return null; } }
/** * Returns the value of a variable like '{{FULLPAGENAME}}' in the context of the given PPFrame objects * $frame->$title instead of the Parser objects subject. Returns null in case the requested variable * does not support '{{THIS:}}'. * * @param string $mwId magic word ID of the variable * @param Parser $parser * @param PPFrame $frame * @param array $args * * @return string|null */ static function getThisVariableValue( $mwId, Parser &$parser, $frame, $args = array() ) { $ret = null; $title = $frame->title; if( $title === null ) { return null; } // check whether info is available, e.g. 'THIS:FULLPAGENAME' requires 'FULLPAGENAME' switch( $mwId ) { case 'namespace': // 'namespace' function name was renamed as PHP 5.3 came along if( is_callable( 'CoreParserFunctions::mwnamespace' ) ) { $ret = CoreParserFunctions::mwnamespace( $parser, $title->getPrefixedText() ); break; } // else: no different from the other variables // no-break, default function call case 'fullpagename': case 'fullpagenamee': case 'pagename': case 'pagenamee': case 'basepagename': case 'basepagenamee': case 'subpagename': case 'subpagenamee': case 'subjectpagename': case 'subjectpagenamee': case 'talkpagename': case 'talkpagenamee': case 'namespacee': // special treat for 'namespace', on top case 'subjectspace': case 'subjectspacee': case 'talkspace': case 'talkspacee': // core parser function information requested $ret = CoreParserFunctions::$mwId( $parser, $title->getPrefixedText() ); break; default: // give other extensions a chance to hook up with this and return their own values: wfRunHooks( 'GetThisVariableValueSwitch', array( &$parser, $title, &$mwId, &$ret, $frame, $args ) ); } return $ret; }