closeParagraph();
if ( $preOpenMatch and !$preCloseMatch ) {
$this->mInPre = true;
}
if ( $closematch ) {
$inBlockElem = false;
} else {
$inBlockElem = true;
}
} else if ( !$inBlockElem && !$this->mInPre ) {
if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
// pre
if ($this->mLastSection != 'pre') {
$paragraphStack = false;
$output .= $this->closeParagraph().'';
$this->mLastSection = 'pre';
}
$t = substr( $t, 1 );
} else {
// paragraph
if ( '' == trim($t) ) {
if ( $paragraphStack ) {
$output .= $paragraphStack.'
';
$paragraphStack = false;
$this->mLastSection = 'p';
} else {
if ($this->mLastSection != 'p' ) {
$output .= $this->closeParagraph();
$this->mLastSection = '';
$paragraphStack = '';
} else {
$paragraphStack = '
';
}
}
} else {
if ( $paragraphStack ) {
$output .= $paragraphStack;
$paragraphStack = false;
$this->mLastSection = 'p';
} else if ($this->mLastSection != 'p') {
$output .= $this->closeParagraph().'
';
$this->mLastSection = 'p';
}
}
}
}
wfProfileOut( "$fname-paragraph" );
}
// somewhere above we forget to get out of pre block (bug 785)
if($preCloseMatch && $this->mInPre) {
$this->mInPre = false;
}
if ($paragraphStack === false) {
$output .= $t."\n";
}
}
while ( $prefixLength ) {
$output .= $this->closeList( $pref2{$prefixLength-1} );
--$prefixLength;
}
if ( '' != $this->mLastSection ) {
$output .= '' . $this->mLastSection . '>';
$this->mLastSection = '';
}
wfProfileOut( $fname );
return $output;
}
/**
* Split up a string on ':', ignoring any occurences inside tags
* to prevent illegal overlapping.
* @param string $str the string to split
* @param string &$before set to everything before the ':'
* @param string &$after set to everything after the ':'
* return string the position of the ':', or false if none found
*/
function findColonNoLinks($str, &$before, &$after) {
$fname = 'Parser::findColonNoLinks';
wfProfileIn( $fname );
$pos = strpos( $str, ':' );
if( $pos === false ) {
// Nothing to find!
wfProfileOut( $fname );
return false;
}
$lt = strpos( $str, '<' );
if( $lt === false || $lt > $pos ) {
// Easy; no tag nesting to worry about
$before = substr( $str, 0, $pos );
$after = substr( $str, $pos+1 );
wfProfileOut( $fname );
return $pos;
}
// Ugly state machine to walk through avoiding tags.
$state = MW_COLON_STATE_TEXT;
$stack = 0;
$len = strlen( $str );
for( $i = 0; $i < $len; $i++ ) {
$c = $str{$i};
switch( $state ) {
// (Using the number is a performance hack for common cases)
case 0: // MW_COLON_STATE_TEXT:
switch( $c ) {
case "<":
// Could be either a tag or an tag
$state = MW_COLON_STATE_TAGSTART;
break;
case ":":
if( $stack == 0 ) {
// We found it!
$before = substr( $str, 0, $i );
$after = substr( $str, $i + 1 );
wfProfileOut( $fname );
return $i;
}
// Embedded in a tag; don't break it.
break;
default:
// Skip ahead looking for something interesting
$colon = strpos( $str, ':', $i );
if( $colon === false ) {
// Nothing else interesting
wfProfileOut( $fname );
return false;
}
$lt = strpos( $str, '<', $i );
if( $stack === 0 ) {
if( $lt === false || $colon < $lt ) {
// We found it!
$before = substr( $str, 0, $colon );
$after = substr( $str, $colon + 1 );
wfProfileOut( $fname );
return $i;
}
}
if( $lt === false ) {
// Nothing else interesting to find; abort!
// We're nested, but there's no close tags left. Abort!
break 2;
}
// Skip ahead to next tag start
$i = $lt;
$state = MW_COLON_STATE_TAGSTART;
}
break;
case 1: // MW_COLON_STATE_TAG:
// In a
switch( $c ) {
case ">":
$stack++;
$state = MW_COLON_STATE_TEXT;
break;
case "/":
// Slash may be followed by >?
$state = MW_COLON_STATE_TAGSLASH;
break;
default:
// ignore
}
break;
case 2: // MW_COLON_STATE_TAGSTART:
switch( $c ) {
case "/":
$state = MW_COLON_STATE_CLOSETAG;
break;
case "!":
$state = MW_COLON_STATE_COMMENT;
break;
case ">":
// Illegal early close? This shouldn't happen D:
$state = MW_COLON_STATE_TEXT;
break;
default:
$state = MW_COLON_STATE_TAG;
}
break;
case 3: // MW_COLON_STATE_CLOSETAG:
// In a
if( $c == ">" ) {
$stack--;
if( $stack < 0 ) {
wfDebug( "Invalid input in $fname; too many close tags\n" );
wfProfileOut( $fname );
return false;
}
$state = MW_COLON_STATE_TEXT;
}
break;
case MW_COLON_STATE_TAGSLASH:
if( $c == ">" ) {
// Yes, a self-closed tag
$state = MW_COLON_STATE_TEXT;
} else {
// Probably we're jumping the gun, and this is an attribute
$state = MW_COLON_STATE_TAG;
}
break;
case 5: // MW_COLON_STATE_COMMENT:
if( $c == "-" ) {
$state = MW_COLON_STATE_COMMENTDASH;
}
break;
case MW_COLON_STATE_COMMENTDASH:
if( $c == "-" ) {
$state = MW_COLON_STATE_COMMENTDASHDASH;
} else {
$state = MW_COLON_STATE_COMMENT;
}
break;
case MW_COLON_STATE_COMMENTDASHDASH:
if( $c == ">" ) {
$state = MW_COLON_STATE_TEXT;
} else {
$state = MW_COLON_STATE_COMMENT;
}
break;
default:
throw new MWException( "State machine error in $fname" );
}
}
if( $stack > 0 ) {
wfDebug( "Invalid input in $fname; not enough close tags (stack $stack, state $state)\n" );
return false;
}
wfProfileOut( $fname );
return false;
}
/**
* Return value of a magic variable (like PAGENAME)
*
* @private
*/
function getVariableValue( $index ) {
global $wgContLang, $wgSitename, $wgServer, $wgServerName, $wgScriptPath;
/**
* Some of these require message or data lookups and can be
* expensive to check many times.
*/
static $varCache = array();
if ( wfRunHooks( 'ParserGetVariableValueVarCache', array( &$this, &$varCache ) ) )
if ( isset( $varCache[$index] ) )
return $varCache[$index];
$ts = time();
wfRunHooks( 'ParserGetVariableValueTs', array( &$this, &$ts ) );
switch ( $index ) {
case MAG_CURRENTMONTH:
return $varCache[$index] = $wgContLang->formatNum( date( 'm', $ts ) );
case MAG_CURRENTMONTHNAME:
return $varCache[$index] = $wgContLang->getMonthName( date( 'n', $ts ) );
case MAG_CURRENTMONTHNAMEGEN:
return $varCache[$index] = $wgContLang->getMonthNameGen( date( 'n', $ts ) );
case MAG_CURRENTMONTHABBREV:
return $varCache[$index] = $wgContLang->getMonthAbbreviation( date( 'n', $ts ) );
case MAG_CURRENTDAY:
return $varCache[$index] = $wgContLang->formatNum( date( 'j', $ts ) );
case MAG_CURRENTDAY2:
return $varCache[$index] = $wgContLang->formatNum( date( 'd', $ts ) );
case MAG_PAGENAME:
return $this->mTitle->getText();
case MAG_PAGENAMEE:
return $this->mTitle->getPartialURL();
case MAG_FULLPAGENAME:
return $this->mTitle->getPrefixedText();
case MAG_FULLPAGENAMEE:
return $this->mTitle->getPrefixedURL();
case MAG_SUBPAGENAME:
return $this->mTitle->getSubpageText();
case MAG_SUBPAGENAMEE:
return $this->mTitle->getSubpageUrlForm();
case MAG_BASEPAGENAME:
return $this->mTitle->getBaseText();
case MAG_BASEPAGENAMEE:
return wfUrlEncode( str_replace( ' ', '_', $this->mTitle->getBaseText() ) );
case MAG_TALKPAGENAME:
if( $this->mTitle->canTalk() ) {
$talkPage = $this->mTitle->getTalkPage();
return $talkPage->getPrefixedText();
} else {
return '';
}
case MAG_TALKPAGENAMEE:
if( $this->mTitle->canTalk() ) {
$talkPage = $this->mTitle->getTalkPage();
return $talkPage->getPrefixedUrl();
} else {
return '';
}
case MAG_SUBJECTPAGENAME:
$subjPage = $this->mTitle->getSubjectPage();
return $subjPage->getPrefixedText();
case MAG_SUBJECTPAGENAMEE:
$subjPage = $this->mTitle->getSubjectPage();
return $subjPage->getPrefixedUrl();
case MAG_REVISIONID:
return $this->mRevisionId;
case MAG_NAMESPACE:
return str_replace('_',' ',$wgContLang->getNsText( $this->mTitle->getNamespace() ) );
case MAG_NAMESPACEE:
return wfUrlencode( $wgContLang->getNsText( $this->mTitle->getNamespace() ) );
case MAG_TALKSPACE:
return $this->mTitle->canTalk() ? str_replace('_',' ',$this->mTitle->getTalkNsText()) : '';
case MAG_TALKSPACEE:
return $this->mTitle->canTalk() ? wfUrlencode( $this->mTitle->getTalkNsText() ) : '';
case MAG_SUBJECTSPACE:
return $this->mTitle->getSubjectNsText();
case MAG_SUBJECTSPACEE:
return( wfUrlencode( $this->mTitle->getSubjectNsText() ) );
case MAG_CURRENTDAYNAME:
return $varCache[$index] = $wgContLang->getWeekdayName( date( 'w', $ts ) + 1 );
case MAG_CURRENTYEAR:
return $varCache[$index] = $wgContLang->formatNum( date( 'Y', $ts ), true );
case MAG_CURRENTTIME:
return $varCache[$index] = $wgContLang->time( wfTimestamp( TS_MW, $ts ), false, false );
case MAG_CURRENTWEEK:
// @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
// int to remove the padding
return $varCache[$index] = $wgContLang->formatNum( (int)date( 'W', $ts ) );
case MAG_CURRENTDOW:
return $varCache[$index] = $wgContLang->formatNum( date( 'w', $ts ) );
case MAG_NUMBEROFARTICLES:
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
case MAG_NUMBEROFFILES:
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfFiles() );
case MAG_NUMBEROFUSERS:
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfUsers() );
case MAG_NUMBEROFPAGES:
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfPages() );
case MAG_NUMBEROFADMINS:
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfAdmins() );
case MAG_CURRENTTIMESTAMP:
return $varCache[$index] = wfTimestampNow();
case MAG_CURRENTVERSION:
global $wgVersion;
return $wgVersion;
case MAG_SITENAME:
return $wgSitename;
case MAG_SERVER:
return $wgServer;
case MAG_SERVERNAME:
return $wgServerName;
case MAG_SCRIPTPATH:
return $wgScriptPath;
case MAG_DIRECTIONMARK:
return $wgContLang->getDirMark();
case MAG_CONTENTLANGUAGE:
global $wgContLanguageCode;
return $wgContLanguageCode;
default:
$ret = null;
if ( wfRunHooks( 'ParserGetVariableValueSwitch', array( &$this, &$varCache, &$index, &$ret ) ) )
return $ret;
else
return null;
}
}
/**
* initialise the magic variables (like CURRENTMONTHNAME)
*
* @private
*/
function initialiseVariables() {
$fname = 'Parser::initialiseVariables';
wfProfileIn( $fname );
global $wgVariableIDs;
$this->mVariables = array();
foreach ( $wgVariableIDs as $id ) {
$mw =& MagicWord::get( $id );
$mw->addToArray( $this->mVariables, $id );
}
wfProfileOut( $fname );
}
/**
* parse any parentheses in format ((title|part|part))
* and call callbacks to get a replacement text for any found piece
*
* @param string $text The text to parse
* @param array $callbacks rules in form:
* '{' => array( # opening parentheses
* 'end' => '}', # closing parentheses
* 'cb' => array(2 => callback, # replacement callback to call if {{..}} is found
* 4 => callback # replacement callback to call if {{{{..}}}} is found
* )
* )
* @private
*/
function replace_callback ($text, $callbacks) {
wfProfileIn( __METHOD__ . '-self' );
$openingBraceStack = array(); # this array will hold a stack of parentheses which are not closed yet
$lastOpeningBrace = -1; # last not closed parentheses
for ($i = 0; $i < strlen($text); $i++) {
# check for any opening brace
$rule = null;
$nextPos = -1;
foreach ($callbacks as $key => $value) {
$pos = strpos ($text, $key, $i);
if (false !== $pos && (-1 == $nextPos || $pos < $nextPos)) {
$rule = $value;
$nextPos = $pos;
}
}
if ($lastOpeningBrace >= 0) {
$pos = strpos ($text, $openingBraceStack[$lastOpeningBrace]['braceEnd'], $i);
if (false !== $pos && (-1 == $nextPos || $pos < $nextPos)){
$rule = null;
$nextPos = $pos;
}
$pos = strpos ($text, '|', $i);
if (false !== $pos && (-1 == $nextPos || $pos < $nextPos)){
$rule = null;
$nextPos = $pos;
}
}
if ($nextPos == -1)
break;
$i = $nextPos;
# found openning brace, lets add it to parentheses stack
if (null != $rule) {
$piece = array('brace' => $text[$i],
'braceEnd' => $rule['end'],
'count' => 1,
'title' => '',
'parts' => null);
# count openning brace characters
while ($i+1 < strlen($text) && $text[$i+1] == $piece['brace']) {
$piece['count']++;
$i++;
}
$piece['startAt'] = $i+1;
$piece['partStart'] = $i+1;
# we need to add to stack only if openning brace count is enough for any given rule
foreach ($rule['cb'] as $cnt => $fn) {
if ($piece['count'] >= $cnt) {
$lastOpeningBrace ++;
$openingBraceStack[$lastOpeningBrace] = $piece;
break;
}
}
continue;
}
else if ($lastOpeningBrace >= 0) {
# first check if it is a closing brace
if ($openingBraceStack[$lastOpeningBrace]['braceEnd'] == $text[$i]) {
# lets check if it is enough characters for closing brace
$count = 1;
while ($i+$count < strlen($text) && $text[$i+$count] == $text[$i])
$count++;
# if there are more closing parentheses than opening ones, we parse less
if ($openingBraceStack[$lastOpeningBrace]['count'] < $count)
$count = $openingBraceStack[$lastOpeningBrace]['count'];
# check for maximum matching characters (if there are 5 closing characters, we will probably need only 3 - depending on the rules)
$matchingCount = 0;
$matchingCallback = null;
foreach ($callbacks[$openingBraceStack[$lastOpeningBrace]['brace']]['cb'] as $cnt => $fn) {
if ($count >= $cnt && $matchingCount < $cnt) {
$matchingCount = $cnt;
$matchingCallback = $fn;
}
}
if ($matchingCount == 0) {
$i += $count - 1;
continue;
}
# lets set a title or last part (if '|' was found)
if (null === $openingBraceStack[$lastOpeningBrace]['parts'])
$openingBraceStack[$lastOpeningBrace]['title'] = substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'], $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
else
$openingBraceStack[$lastOpeningBrace]['parts'][] = substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'], $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
$pieceStart = $openingBraceStack[$lastOpeningBrace]['startAt'] - $matchingCount;
$pieceEnd = $i + $matchingCount;
if( is_callable( $matchingCallback ) ) {
$cbArgs = array (
'text' => substr($text, $pieceStart, $pieceEnd - $pieceStart),
'title' => trim($openingBraceStack[$lastOpeningBrace]['title']),
'parts' => $openingBraceStack[$lastOpeningBrace]['parts'],
'lineStart' => (($pieceStart > 0) && ($text[$pieceStart-1] == "\n")),
);
# finally we can call a user callback and replace piece of text
wfProfileOut( __METHOD__ . '-self' );
$replaceWith = call_user_func( $matchingCallback, $cbArgs );
wfProfileIn( __METHOD__ . '-self' );
$text = substr($text, 0, $pieceStart) . $replaceWith . substr($text, $pieceEnd);
$i = $pieceStart + strlen($replaceWith) - 1;
}
else {
# null value for callback means that parentheses should be parsed, but not replaced
$i += $matchingCount - 1;
}
# reset last openning parentheses, but keep it in case there are unused characters
$piece = array('brace' => $openingBraceStack[$lastOpeningBrace]['brace'],
'braceEnd' => $openingBraceStack[$lastOpeningBrace]['braceEnd'],
'count' => $openingBraceStack[$lastOpeningBrace]['count'],
'title' => '',
'parts' => null,
'startAt' => $openingBraceStack[$lastOpeningBrace]['startAt']);
$openingBraceStack[$lastOpeningBrace--] = null;
if ($matchingCount < $piece['count']) {
$piece['count'] -= $matchingCount;
$piece['startAt'] -= $matchingCount;
$piece['partStart'] = $piece['startAt'];
# do we still qualify for any callback with remaining count?
foreach ($callbacks[$piece['brace']]['cb'] as $cnt => $fn) {
if ($piece['count'] >= $cnt) {
$lastOpeningBrace ++;
$openingBraceStack[$lastOpeningBrace] = $piece;
break;
}
}
}
continue;
}
# lets set a title if it is a first separator, or next part otherwise
if ($text[$i] == '|') {
if (null === $openingBraceStack[$lastOpeningBrace]['parts']) {
$openingBraceStack[$lastOpeningBrace]['title'] = substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'], $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
$openingBraceStack[$lastOpeningBrace]['parts'] = array();
}
else
$openingBraceStack[$lastOpeningBrace]['parts'][] = substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'], $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
$openingBraceStack[$lastOpeningBrace]['partStart'] = $i + 1;
}
}
}
wfProfileOut( __METHOD__ . '-self' );
return $text;
}
/**
* Replace magic variables, templates, and template arguments
* with the appropriate text. Templates are substituted recursively,
* taking care to avoid infinite loops.
*
* Note that the substitution depends on value of $mOutputType:
* OT_WIKI: only {{subst:}} templates
* OT_MSG: only magic variables
* OT_HTML: all templates and magic variables
*
* @param string $tex The text to transform
* @param array $args Key-value pairs representing template parameters to substitute
* @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
* @private
*/
function replaceVariables( $text, $args = array(), $argsOnly = false ) {
# Prevent too big inclusions
if( strlen( $text ) > MAX_INCLUDE_SIZE ) {
return $text;
}
$fname = 'Parser::replaceVariables';
wfProfileIn( $fname );
# This function is called recursively. To keep track of arguments we need a stack:
array_push( $this->mArgStack, $args );
$braceCallbacks = array();
if ( !$argsOnly ) {
$braceCallbacks[2] = array( &$this, 'braceSubstitution' );
}
if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
$braceCallbacks[3] = array( &$this, 'argSubstitution' );
}
$callbacks = array();
$callbacks['{'] = array('end' => '}', 'cb' => $braceCallbacks);
$callbacks['['] = array('end' => ']', 'cb' => array(2=>null));
$text = $this->replace_callback ($text, $callbacks);
array_pop( $this->mArgStack );
wfProfileOut( $fname );
return $text;
}
/**
* Replace magic variables
* @private
*/
function variableSubstitution( $matches ) {
$fname = 'Parser::variableSubstitution';
$varname = $matches[1];
wfProfileIn( $fname );
$skip = false;
if ( $this->mOutputType == OT_WIKI ) {
# Do only magic variables prefixed by SUBST
$mwSubst =& MagicWord::get( MAG_SUBST );
if (!$mwSubst->matchStartAndRemove( $varname ))
$skip = true;
# Note that if we don't substitute the variable below,
# we don't remove the {{subst:}} magic word, in case
# it is a template rather than a magic variable.
}
if ( !$skip && array_key_exists( $varname, $this->mVariables ) ) {
$id = $this->mVariables[$varname];
$text = $this->getVariableValue( $id );
$this->mOutput->mContainsOldMagic = true;
} else {
$text = $matches[0];
}
wfProfileOut( $fname );
return $text;
}
# Split template arguments
function getTemplateArgs( $argsString ) {
if ( $argsString === '' ) {
return array();
}
$args = explode( '|', substr( $argsString, 1 ) );
# If any of the arguments contains a '[[' but no ']]', it needs to be
# merged with the next arg because the '|' character between belongs
# to the link syntax and not the template parameter syntax.
$argc = count($args);
for ( $i = 0; $i < $argc-1; $i++ ) {
if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
$args[$i] .= '|'.$args[$i+1];
array_splice($args, $i+1, 1);
$i--;
$argc--;
}
}
return $args;
}
/**
* Return the text of a template, after recursively
* replacing any variables or templates within the template.
*
* @param array $piece The parts of the template
* $piece['text']: matched text
* $piece['title']: the title, i.e. the part before the |
* $piece['parts']: the parameter array
* @return string the text of the template
* @private
*/
function braceSubstitution( $piece ) {
global $wgContLang, $wgLang, $wgAllowDisplayTitle, $action;
$fname = 'Parser::braceSubstitution';
wfProfileIn( $fname );
# Flags
$found = false; # $text has been filled
$nowiki = false; # wiki markup in $text should be escaped
$noparse = false; # Unsafe HTML tags should not be stripped, etc.
$noargs = false; # Don't replace triple-brace arguments in $text
$replaceHeadings = false; # Make the edit section links go to the template not the article
$isHTML = false; # $text is HTML, armour it against wikitext transformation
$forceRawInterwiki = false; # Force interwiki transclusion to be done in raw mode not rendered
# Title object, where $text came from
$title = NULL;
$linestart = '';
# $part1 is the bit before the first |, and must contain only title characters
# $args is a list of arguments, starting from index 0, not including $part1
$part1 = $piece['title'];
# If the third subpattern matched anything, it will start with |
if (null == $piece['parts']) {
$replaceWith = $this->variableSubstitution (array ($piece['text'], $piece['title']));
if ($replaceWith != $piece['text']) {
$text = $replaceWith;
$found = true;
$noparse = true;
$noargs = true;
}
}
$args = (null == $piece['parts']) ? array() : $piece['parts'];
$argc = count( $args );
# SUBST
if ( !$found ) {
$mwSubst =& MagicWord::get( MAG_SUBST );
if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType == OT_WIKI) ) {
# One of two possibilities is true:
# 1) Found SUBST but not in the PST phase
# 2) Didn't find SUBST and in the PST phase
# In either case, return without further processing
$text = $piece['text'];
$found = true;
$noparse = true;
$noargs = true;
}
}
# MSG, MSGNW, INT and RAW
if ( !$found ) {
# Check for MSGNW:
$mwMsgnw =& MagicWord::get( MAG_MSGNW );
if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
$nowiki = true;
} else {
# Remove obsolete MSG:
$mwMsg =& MagicWord::get( MAG_MSG );
$mwMsg->matchStartAndRemove( $part1 );
}
# Check for RAW:
$mwRaw =& MagicWord::get( MAG_RAW );
if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
$forceRawInterwiki = true;
}
# Check if it is an internal message
$mwInt =& MagicWord::get( MAG_INT );
if ( $mwInt->matchStartAndRemove( $part1 ) ) {
if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
$text = $linestart . wfMsgReal( $part1, $args, true );
$found = true;
}
}
}
# Parser functions
if ( !$found ) {
wfProfileIn( __METHOD__ . '-pfunc' );
$colonPos = strpos( $part1, ':' );
if ( $colonPos !== false ) {
# Case sensitive functions
$function = substr( $part1, 0, $colonPos );
if ( isset( $this->mFunctionSynonyms[1][$function] ) ) {
$function = $this->mFunctionSynonyms[1][$function];
} else {
# Case insensitive functions
$function = strtolower( $function );
if ( isset( $this->mFunctionSynonyms[0][$function] ) ) {
$function = $this->mFunctionSynonyms[0][$function];
} else {
$function = false;
}
}
if ( $function ) {
$funcArgs = array_map( 'trim', $args );
$funcArgs = array_merge( array( &$this, trim( substr( $part1, $colonPos + 1 ) ) ), $funcArgs );
$result = call_user_func_array( $this->mFunctionHooks[$function], $funcArgs );
$found = true;
// The text is usually already parsed, doesn't need triple-brace tags expanded, etc.
//$noargs = true;
//$noparse = true;
if ( is_array( $result ) ) {
if ( isset( $result[0] ) ) {
$text = $linestart . $result[0];
unset( $result[0] );
}
// Extract flags into the local scope
// This allows callers to set flags such as nowiki, noparse, found, etc.
extract( $result );
} else {
$text = $linestart . $result;
}
}
}
wfProfileOut( __METHOD__ . '-pfunc' );
}
# Template table test
# Did we encounter this template already? If yes, it is in the cache
# and we need to check for loops.
if ( !$found && isset( $this->mTemplates[$piece['title']] ) ) {
$found = true;
# Infinite loop test
if ( isset( $this->mTemplatePath[$part1] ) ) {
$noparse = true;
$noargs = true;
$found = true;
$text = $linestart .
'{{' . $part1 . '}}' .
'';
wfDebug( "$fname: template loop broken at '$part1'\n" );
} else {
# set $text to cached message.
$text = $linestart . $this->mTemplates[$piece['title']];
}
}
# Load from database
$lastPathLevel = $this->mTemplatePath;
if ( !$found ) {
wfProfileIn( __METHOD__ . '-loadtpl' );
$ns = NS_TEMPLATE;
# declaring $subpage directly in the function call
# does not work correctly with references and breaks
# {{/subpage}}-style inclusions
$subpage = '';
$part1 = $this->maybeDoSubpageLink( $part1, $subpage );
if ($subpage !== '') {
$ns = $this->mTitle->getNamespace();
}
$title = Title::newFromText( $part1, $ns );
if ( !is_null( $title ) ) {
$checkVariantLink = sizeof($wgContLang->getVariants())>1;
# Check for language variants if the template is not found
if($checkVariantLink && $title->getArticleID() == 0){
$wgContLang->findVariantLink($part1, $title);
}
if ( !$title->isExternal() ) {
# Check for excessive inclusion
$dbk = $title->getPrefixedDBkey();
if ( $this->incrementIncludeCount( $dbk ) ) {
if ( $title->getNamespace() == NS_SPECIAL && $this->mOptions->getAllowSpecialInclusion() && $this->mOutputType != OT_WIKI ) {
$text = SpecialPage::capturePath( $title );
if ( is_string( $text ) ) {
$found = true;
$noparse = true;
$noargs = true;
$isHTML = true;
$this->disableCache();
}
} else {
$articleContent = $this->fetchTemplate( $title );
if ( $articleContent !== false ) {
$found = true;
$text = $articleContent;
$replaceHeadings = true;
}
}
}
# If the title is valid but undisplayable, make a link to it
if ( $this->mOutputType == OT_HTML && !$found ) {
$text = '[['.$title->getPrefixedText().']]';
$found = true;
}
} elseif ( $title->isTrans() ) {
// Interwiki transclusion
if ( $this->mOutputType == OT_HTML && !$forceRawInterwiki ) {
$text = $this->interwikiTransclude( $title, 'render' );
$isHTML = true;
$noparse = true;
} else {
$text = $this->interwikiTransclude( $title, 'raw' );
$replaceHeadings = true;
}
$found = true;
}
# Template cache array insertion
# Use the original $piece['title'] not the mangled $part1, so that
# modifiers such as RAW: produce separate cache entries
if( $found ) {
if( $isHTML ) {
// A special page; don't store it in the template cache.
} else {
$this->mTemplates[$piece['title']] = $text;
}
$text = $linestart . $text;
}
}
wfProfileOut( __METHOD__ . '-loadtpl' );
}
# Recursive parsing, escaping and link table handling
# Only for HTML output
if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
$text = wfEscapeWikiText( $text );
} elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found ) {
if ( $noargs ) {
$assocArgs = array();
} else {
# Clean up argument array
$assocArgs = array();
$index = 1;
foreach( $args as $arg ) {
$eqpos = strpos( $arg, '=' );
if ( $eqpos === false ) {
$assocArgs[$index++] = $arg;
} else {
$name = trim( substr( $arg, 0, $eqpos ) );
$value = trim( substr( $arg, $eqpos+1 ) );
if ( $value === false ) {
$value = '';
}
if ( $name !== false ) {
$assocArgs[$name] = $value;
}
}
}
# Add a new element to the templace recursion path
$this->mTemplatePath[$part1] = 1;
}
if ( !$noparse ) {
# If there are any tags, only include them
if ( in_string( '', $text ) && in_string( '', $text ) ) {
preg_match_all( '/(.*?)\n?<\/onlyinclude>/s', $text, $m );
$text = '';
foreach ($m[1] as $piece)
$text .= $piece;
}
# Remove sections and tags
$text = preg_replace( '/.*?<\/noinclude>/s', '', $text );
$text = strtr( $text, array( '' => '' , '' => '' ) );
if( $this->mOutputType == OT_HTML ) {
# Strip , , etc.
$text = $this->strip( $text, $this->mStripState );
$text = Sanitizer::removeHTMLtags( $text, array( &$this, 'replaceVariables' ), $assocArgs );
}
$text = $this->replaceVariables( $text, $assocArgs );
# If the template begins with a table or block-level
# element, it should be treated as beginning a new line.
if (!$piece['lineStart'] && preg_match('/^({\\||:|;|#|\*)/', $text)) {
$text = "\n" . $text;
}
} elseif ( !$noargs ) {
# $noparse and !$noargs
# Just replace the arguments, not any double-brace items
# This is used for rendered interwiki transclusion
$text = $this->replaceVariables( $text, $assocArgs, true );
}
}
# Prune lower levels off the recursion check path
$this->mTemplatePath = $lastPathLevel;
if ( !$found ) {
wfProfileOut( $fname );
return $piece['text'];
} else {
wfProfileIn( __METHOD__ . '-placeholders' );
if ( $isHTML ) {
# Replace raw HTML by a placeholder
# Add a blank line preceding, to prevent it from mucking up
# immediately preceding headings
$text = "\n\n" . $this->insertStripItem( $text, $this->mStripState );
} else {
# replace ==section headers==
# XXX this needs to go away once we have a better parser.
if ( $this->mOutputType != OT_WIKI && $replaceHeadings ) {
if( !is_null( $title ) )
$encodedname = base64_encode($title->getPrefixedDBkey());
else
$encodedname = base64_encode("");
$m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
PREG_SPLIT_DELIM_CAPTURE);
$text = '';
$nsec = 0;
for( $i = 0; $i < count($m); $i += 2 ) {
$text .= $m[$i];
if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
$hl = $m[$i + 1];
if( strstr($hl, "" . $m2[3];
$nsec++;
}
}
}
wfProfileOut( __METHOD__ . '-placeholders' );
}
# Prune lower levels off the recursion check path
$this->mTemplatePath = $lastPathLevel;
if ( !$found ) {
wfProfileOut( $fname );
return $piece['text'];
} else {
wfProfileOut( $fname );
return $text;
}
}
/**
* Fetch the unparsed text of a template and register a reference to it.
*/
function fetchTemplate( $title ) {
$text = false;
// Loop to fetch the article, with up to 1 redirect
for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
$rev = Revision::newFromTitle( $title );
$this->mOutput->addTemplate( $title, $title->getArticleID() );
if ( !$rev ) {
break;
}
$text = $rev->getText();
if ( $text === false ) {
break;
}
// Redirect?
$title = Title::newFromRedirect( $text );
}
return $text;
}
/**
* Transclude an interwiki link.
*/
function interwikiTransclude( $title, $action ) {
global $wgEnableScaryTranscluding, $wgCanonicalNamespaceNames;
if (!$wgEnableScaryTranscluding)
return wfMsg('scarytranscludedisabled');
// The namespace will actually only be 0 or 10, depending on whether there was a leading :
// But we'll handle it generally anyway
if ( $title->getNamespace() ) {
// Use the canonical namespace, which should work anywhere
$articleName = $wgCanonicalNamespaceNames[$title->getNamespace()] . ':' . $title->getDBkey();
} else {
$articleName = $title->getDBkey();
}
$url = str_replace('$1', urlencode($articleName), Title::getInterwikiLink($title->getInterwiki()));
$url .= "?action=$action";
if (strlen($url) > 255)
return wfMsg('scarytranscludetoolong');
return $this->fetchScaryTemplateMaybeFromCache($url);
}
function fetchScaryTemplateMaybeFromCache($url) {
global $wgTranscludeCacheExpiry;
$dbr =& wfGetDB(DB_SLAVE);
$obj = $dbr->selectRow('transcache', array('tc_time', 'tc_contents'),
array('tc_url' => $url));
if ($obj) {
$time = $obj->tc_time;
$text = $obj->tc_contents;
if ($time && time() < $time + $wgTranscludeCacheExpiry ) {
return $text;
}
}
$text = Http::get($url);
if (!$text)
return wfMsg('scarytranscludefailed', $url);
$dbw =& wfGetDB(DB_MASTER);
$dbw->replace('transcache', array('tc_url'), array(
'tc_url' => $url,
'tc_time' => time(),
'tc_contents' => $text));
return $text;
}
/**
* Triple brace replacement -- used for template arguments
* @private
*/
function argSubstitution( $matches ) {
$arg = trim( $matches['title'] );
$text = $matches['text'];
$inputArgs = end( $this->mArgStack );
if ( array_key_exists( $arg, $inputArgs ) ) {
$text = $inputArgs[$arg];
} else if ($this->mOutputType == OT_HTML && null != $matches['parts'] && count($matches['parts']) > 0) {
$text = $matches['parts'][0];
}
return $text;
}
/**
* Returns true if the function is allowed to include this entity
* @private
*/
function incrementIncludeCount( $dbk ) {
if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
$this->mIncludeCount[$dbk] = 0;
}
if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
return true;
} else {
return false;
}
}
/**
* Detect __NOGALLERY__ magic word and set a placeholder
*/
function stripNoGallery( &$text ) {
# if the string __NOGALLERY__ (not case-sensitive) occurs in the HTML,
# do not add TOC
$mw = MagicWord::get( MAG_NOGALLERY );
$this->mOutput->mNoGallery = $mw->matchAndRemove( $text ) ;
}
/**
* Detect __TOC__ magic word and set a placeholder
*/
function stripToc( $text ) {
# if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
# do not add TOC
$mw = MagicWord::get( MAG_NOTOC );
if( $mw->matchAndRemove( $text ) ) {
$this->mShowToc = false;
}
$mw = MagicWord::get( MAG_TOC );
if( $mw->match( $text ) ) {
$this->mShowToc = true;
$this->mForceTocPosition = true;
// Set a placeholder. At the end we'll fill it in with the TOC.
$text = $mw->replace( '', $text, 1 );
// Only keep the first one.
$text = $mw->replace( '', $text );
}
return $text;
}
/**
* This function accomplishes several tasks:
* 1) Auto-number headings if that option is enabled
* 2) Add an [edit] link to sections for logged in users who have enabled the option
* 3) Add a Table of contents on the top for users who have enabled the option
* 4) Auto-anchor headings
*
* It loops through all headlines, collects the necessary data, then splits up the
* string and re-inserts the newly formatted headlines.
*
* @param string $text
* @param boolean $isMain
* @private
*/
function formatHeadings( $text, $isMain=true ) {
global $wgMaxTocLevel, $wgContLang;
$doNumberHeadings = $this->mOptions->getNumberHeadings();
if( !$this->mTitle->userCanEdit() ) {
$showEditLink = 0;
} else {
$showEditLink = $this->mOptions->getEditSection();
}
# Inhibit editsection links if requested in the page
$esw =& MagicWord::get( MAG_NOEDITSECTION );
if( $esw->matchAndRemove( $text ) ) {
$showEditLink = 0;
}
# Get all headlines for numbering them and adding funky stuff like [edit]
# links - this is for later, but we need the number of headlines right now
$numMatches = preg_match_all( '/)(.*?)<\/H[1-6] *>/i', $text, $matches );
# if there are fewer than 4 headlines in the article, do not show TOC
# unless it's been explicitly enabled.
$enoughToc = $this->mShowToc &&
(($numMatches >= 4) || $this->mForceTocPosition);
# Allow user to stipulate that a page should have a "new section"
# link added via __NEWSECTIONLINK__
$mw =& MagicWord::get( MAG_NEWSECTIONLINK );
if( $mw->matchAndRemove( $text ) )
$this->mOutput->setNewSection( true );
# if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
# override above conditions and always show TOC above first header
$mw =& MagicWord::get( MAG_FORCETOC );
if ($mw->matchAndRemove( $text ) ) {
$this->mShowToc = true;
$enoughToc = true;
}
# Never ever show TOC if no headers
if( $numMatches < 1 ) {
$enoughToc = false;
}
# We need this to perform operations on the HTML
$sk =& $this->mOptions->getSkin();
# headline counter
$headlineCount = 0;
$sectionCount = 0; # headlineCount excluding template sections
# Ugh .. the TOC should have neat indentation levels which can be
# passed to the skin functions. These are determined here
$toc = '';
$full = '';
$head = array();
$sublevelCount = array();
$levelCount = array();
$toclevel = 0;
$level = 0;
$prevlevel = 0;
$toclevel = 0;
$prevtoclevel = 0;
foreach( $matches[3] as $headline ) {
$istemplate = 0;
$templatetitle = '';
$templatesection = 0;
$numbering = '';
if (preg_match("//", $headline, $mat)) {
$istemplate = 1;
$templatetitle = base64_decode($mat[1]);
$templatesection = 1 + (int)base64_decode($mat[2]);
$headline = preg_replace("//", "", $headline);
}
if( $toclevel ) {
$prevlevel = $level;
$prevtoclevel = $toclevel;
}
$level = $matches[1][$headlineCount];
if( $doNumberHeadings || $enoughToc ) {
if ( $level > $prevlevel ) {
# Increase TOC level
$toclevel++;
$sublevelCount[$toclevel] = 0;
if( $toclevel<$wgMaxTocLevel ) {
$toc .= $sk->tocIndent();
}
}
elseif ( $level < $prevlevel && $toclevel > 1 ) {
# Decrease TOC level, find level to jump to
if ( $toclevel == 2 && $level <= $levelCount[1] ) {
# Can only go down to level 1
$toclevel = 1;
} else {
for ($i = $toclevel; $i > 0; $i--) {
if ( $levelCount[$i] == $level ) {
# Found last matching level
$toclevel = $i;
break;
}
elseif ( $levelCount[$i] < $level ) {
# Found first matching level below current level
$toclevel = $i + 1;
break;
}
}
}
if( $toclevel<$wgMaxTocLevel ) {
$toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
}
}
else {
# No change in level, end TOC line
if( $toclevel<$wgMaxTocLevel ) {
$toc .= $sk->tocLineEnd();
}
}
$levelCount[$toclevel] = $level;
# count number of headlines for each level
@$sublevelCount[$toclevel]++;
$dot = 0;
for( $i = 1; $i <= $toclevel; $i++ ) {
if( !empty( $sublevelCount[$i] ) ) {
if( $dot ) {
$numbering .= '.';
}
$numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
$dot = 1;
}
}
}
# The canonized header is a version of the header text safe to use for links
# Avoid insertion of weird stuff like
';
}
/**
* Renders an image gallery from a text with one line per image.
* text labels may be given by using |-style alternative text. E.g.
* Image:one.jpg|The number "1"
* Image:tree.jpg|A tree
* given as text will return the HTML of a gallery with two images,
* labeled 'The number "1"' and
* 'A tree'.
*/
function renderImageGallery( $text, $params ) {
$ig = new ImageGallery();
$ig->setShowBytes( false );
$ig->setShowFilename( false );
$ig->setParsing();
$ig->useSkin( $this->mOptions->getSkin() );
if( isset( $params['caption'] ) )
$ig->setCaption( $params['caption'] );
$lines = explode( "\n", $text );
foreach ( $lines as $line ) {
# match lines like these:
# Image:someimage.jpg|This is some image
preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
# Skip empty lines
if ( count( $matches ) == 0 ) {
continue;
}
$nt =& Title::newFromText( $matches[1] );
if( is_null( $nt ) ) {
# Bogus title. Ignore these so we don't bomb out later.
continue;
}
if ( isset( $matches[3] ) ) {
$label = $matches[3];
} else {
$label = '';
}
$pout = $this->parse( $label,
$this->mTitle,
$this->mOptions,
false, // Strip whitespace...?
false // Don't clear state!
);
$html = $pout->getText();
$ig->add( new Image( $nt ), $html );
# Only add real images (bug #5586)
if ( $nt->getNamespace() == NS_IMAGE ) {
$this->mOutput->addImage( $nt->getDBkey() );
}
}
return $ig->toHTML();
}
/**
* Parse image options text and use it to make an image
*/
function makeImage( &$nt, $options ) {
global $wgUseImageResize;
$align = '';
# Check if the options text is of the form "options|alt text"
# Options are:
# * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
# * left no resizing, just left align. label is used for alt= only
# * right same, but right aligned
# * none same, but not aligned
# * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
# * center center the image
# * framed Keep original image size, no magnify-button.
$part = explode( '|', $options);
$mwThumb =& MagicWord::get( MAG_IMG_THUMBNAIL );
$mwManualThumb =& MagicWord::get( MAG_IMG_MANUALTHUMB );
$mwLeft =& MagicWord::get( MAG_IMG_LEFT );
$mwRight =& MagicWord::get( MAG_IMG_RIGHT );
$mwNone =& MagicWord::get( MAG_IMG_NONE );
$mwWidth =& MagicWord::get( MAG_IMG_WIDTH );
$mwCenter =& MagicWord::get( MAG_IMG_CENTER );
$mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
$caption = '';
$width = $height = $framed = $thumb = false;
$manual_thumb = '' ;
foreach( $part as $key => $val ) {
if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
$thumb=true;
} elseif ( ! is_null( $match = $mwManualThumb->matchVariableStartToEnd($val) ) ) {
# use manually specified thumbnail
$thumb=true;
$manual_thumb = $match;
} elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
# remember to set an alignment, don't render immediately
$align = 'right';
} elseif ( ! is_null( $mwLeft->matchVariableStartToEnd($val) ) ) {
# remember to set an alignment, don't render immediately
$align = 'left';
} elseif ( ! is_null( $mwCenter->matchVariableStartToEnd($val) ) ) {
# remember to set an alignment, don't render immediately
$align = 'center';
} elseif ( ! is_null( $mwNone->matchVariableStartToEnd($val) ) ) {
# remember to set an alignment, don't render immediately
$align = 'none';
} elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
wfDebug( "MAG_IMG_WIDTH match: $match\n" );
# $match is the image width in pixels
if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
$width = intval( $m[1] );
$height = intval( $m[2] );
} else {
$width = intval($match);
}
} elseif ( ! is_null( $mwFramed->matchVariableStartToEnd($val) ) ) {
$framed=true;
} else {
$caption = $val;
}
}
# Strip bad stuff out of the alt text
$alt = $this->replaceLinkHoldersText( $caption );
# make sure there are no placeholders in thumbnail attributes
# that are later expanded to html- so expand them now and
# remove the tags
$alt = $this->unstrip($alt, $this->mStripState);
$alt = Sanitizer::stripAllTags( $alt );
# Linker does the rest
$sk =& $this->mOptions->getSkin();
return $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $width, $height, $framed, $thumb, $manual_thumb );
}
/**
* Set a flag in the output object indicating that the content is dynamic and
* shouldn't be cached.
*/
function disableCache() {
wfDebug( "Parser output marked as uncacheable.\n" );
$this->mOutput->mCacheTime = -1;
}
/**#@+
* Callback from the Sanitizer for expanding items found in HTML attribute
* values, so they can be safely tested and escaped.
* @param string $text
* @param array $args
* @return string
* @private
*/
function attributeStripCallback( &$text, $args ) {
$text = $this->replaceVariables( $text, $args );
$text = $this->unstripForHTML( $text );
return $text;
}
function unstripForHTML( $text ) {
$text = $this->unstrip( $text, $this->mStripState );
$text = $this->unstripNoWiki( $text, $this->mStripState );
return $text;
}
/**#@-*/
/**#@+
* Accessor/mutator
*/
function Title( $x = NULL ) { return wfSetVar( $this->mTitle, $x ); }
function Options( $x = NULL ) { return wfSetVar( $this->mOptions, $x ); }
function OutputType( $x = NULL ) { return wfSetVar( $this->mOutputType, $x ); }
/**#@-*/
/**#@+
* Accessor
*/
function getTags() { return array_keys( $this->mTagHooks ); }
/**#@-*/
/**
* Break wikitext input into sections, and either pull or replace
* some particular section's text.
*
* External callers should use the getSection and replaceSection methods.
*
* @param $text Page wikitext
* @param $section Numbered section. 0 pulls the text before the first
* heading; other numbers will pull the given section
* along with its lower-level subsections.
* @param $mode One of "get" or "replace"
* @param $newtext Replacement text for section data.
* @return string for "get", the extracted section text.
* for "replace", the whole page with the section replaced.
*/
private function extractSections( $text, $section, $mode, $newtext='' ) {
# strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
# comments to be stripped as well)
$striparray = array();
$oldOutputType = $this->mOutputType;
$oldOptions = $this->mOptions;
$this->mOptions = new ParserOptions();
$this->mOutputType = OT_WIKI;
$striptext = $this->strip( $text, $striparray, true );
$this->mOutputType = $oldOutputType;
$this->mOptions = $oldOptions;
# now that we can be sure that no pseudo-sections are in the source,
# split it up by section
$uniq = preg_quote( $this->uniqPrefix(), '/' );
$comment = "(?:$uniq-!--.*?QINU)";
$secs = preg_split(
/*
"/
^(
(?:$comment|<\/?noinclude>)* # Initial comments will be stripped
(?:
(=+) # Should this be limited to 6?
.+? # Section title...
\\2 # Ending = count must match start
|
^
.*?
<\/h\\3\s*>
)
(?:$comment|<\/?noinclude>|\s+)* # Trailing whitespace ok
)$
/mix",
*/
"/
(
^
(?:$comment|<\/?noinclude>)* # Initial comments will be stripped
(=+) # Should this be limited to 6?
.+? # Section title...
\\2 # Ending = count must match start
(?:$comment|<\/?noinclude>|[ \\t]+)* # Trailing whitespace ok
$
|
.*?
<\/h\\3\s*>
)
/mix",
$striptext, -1,
PREG_SPLIT_DELIM_CAPTURE);
if( $mode == "get" ) {
if( $section == 0 ) {
// "Section 0" returns the content before any other section.
$rv = $secs[0];
} else {
$rv = "";
}
} elseif( $mode == "replace" ) {
if( $section == 0 ) {
$rv = $newtext . "\n\n";
$remainder = true;
} else {
$rv = $secs[0];
$remainder = false;
}
}
$count = 0;
$sectionLevel = 0;
for( $index = 1; $index < count( $secs ); ) {
$headerLine = $secs[$index++];
if( $secs[$index] ) {
// A wiki header
$headerLevel = strlen( $secs[$index++] );
} else {
// An HTML header
$index++;
$headerLevel = intval( $secs[$index++] );
}
$content = $secs[$index++];
$count++;
if( $mode == "get" ) {
if( $count == $section ) {
$rv = $headerLine . $content;
$sectionLevel = $headerLevel;
} elseif( $count > $section ) {
if( $sectionLevel && $headerLevel > $sectionLevel ) {
$rv .= $headerLine . $content;
} else {
// Broke out to a higher-level section
break;
}
}
} elseif( $mode == "replace" ) {
if( $count < $section ) {
$rv .= $headerLine . $content;
} elseif( $count == $section ) {
$rv .= $newtext . "\n\n";
$sectionLevel = $headerLevel;
} elseif( $count > $section ) {
if( $headerLevel <= $sectionLevel ) {
// Passed the section's sub-parts.
$remainder = true;
}
if( $remainder ) {
$rv .= $headerLine . $content;
}
}
}
}
# reinsert stripped tags
$rv = $this->unstrip( $rv, $striparray );
$rv = $this->unstripNoWiki( $rv, $striparray );
$rv = trim( $rv );
return $rv;
}
/**
* This function returns the text of a section, specified by a number ($section).
* A section is text under a heading like == Heading == or \Heading\
, or
* the first section before any such heading (section 0).
*
* If a section contains subsections, these are also returned.
*
* @param $text String: text to look in
* @param $section Integer: section number
* @return string text of the requested section
*/
function getSection( $text, $section ) {
return $this->extractSections( $text, $section, "get" );
}
function replaceSection( $oldtext, $section, $text ) {
return $this->extractSections( $oldtext, $section, "replace", $text );
}
}
/**
* @todo document
* @package MediaWiki
*/
class ParserOutput
{
var $mText, # The output text
$mLanguageLinks, # List of the full text of language links, in the order they appear
$mCategories, # Map of category names to sort keys
$mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
$mCacheTime, # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
$mVersion, # Compatibility check
$mTitleText, # title text of the chosen language variant
$mLinks, # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
$mTemplates, # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
$mImages, # DB keys of the images used, in the array key only
$mExternalLinks, # External link URLs, in the key only
$mHTMLtitle, # Display HTML title
$mSubtitle, # Additional subtitle
$mNewSection, # Show a new section link?
$mNoGallery; # No gallery on category page? (__NOGALLERY__)
function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
$containsOldMagic = false, $titletext = '' )
{
$this->mText = $text;
$this->mLanguageLinks = $languageLinks;
$this->mCategories = $categoryLinks;
$this->mContainsOldMagic = $containsOldMagic;
$this->mCacheTime = '';
$this->mVersion = MW_PARSER_VERSION;
$this->mTitleText = $titletext;
$this->mLinks = array();
$this->mTemplates = array();
$this->mImages = array();
$this->mExternalLinks = array();
$this->mHTMLtitle = "" ;
$this->mSubtitle = "" ;
$this->mNewSection = false;
$this->mNoGallery = false;
}
function getText() { return $this->mText; }
function &getLanguageLinks() { return $this->mLanguageLinks; }
function getCategoryLinks() { return array_keys( $this->mCategories ); }
function &getCategories() { return $this->mCategories; }
function getCacheTime() { return $this->mCacheTime; }
function getTitleText() { return $this->mTitleText; }
function &getLinks() { return $this->mLinks; }
function &getTemplates() { return $this->mTemplates; }
function &getImages() { return $this->mImages; }
function &getExternalLinks() { return $this->mExternalLinks; }
function getNoGallery() { return $this->mNoGallery; }
function containsOldMagic() { return $this->mContainsOldMagic; }
function setText( $text ) { return wfSetVar( $this->mText, $text ); }
function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
function setTitleText( $t ) { return wfSetVar ($this->mTitleText, $t); }
function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
function addImage( $name ) { $this->mImages[$name] = 1; }
function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
function addExternalLink( $url ) { $this->mExternalLinks[$url] = 1; }
function setNewSection( $value ) {
$this->mNewSection = (bool)$value;
}
function getNewSection() {
return (bool)$this->mNewSection;
}
function addLink( $title, $id ) {
$ns = $title->getNamespace();
$dbk = $title->getDBkey();
if ( !isset( $this->mLinks[$ns] ) ) {
$this->mLinks[$ns] = array();
}
$this->mLinks[$ns][$dbk] = $id;
}
function addTemplate( $title, $id ) {
$ns = $title->getNamespace();
$dbk = $title->getDBkey();
if ( !isset( $this->mTemplates[$ns] ) ) {
$this->mTemplates[$ns] = array();
}
$this->mTemplates[$ns][$dbk] = $id;
}
/**
* Return true if this cached output object predates the global or
* per-article cache invalidation timestamps, or if it comes from
* an incompatible older version.
*
* @param string $touched the affected article's last touched timestamp
* @return bool
* @public
*/
function expired( $touched ) {
global $wgCacheEpoch;
return $this->getCacheTime() == -1 || // parser says it's uncacheable
$this->getCacheTime() < $touched ||
$this->getCacheTime() <= $wgCacheEpoch ||
!isset( $this->mVersion ) ||
version_compare( $this->mVersion, MW_PARSER_VERSION, "lt" );
}
}
/**
* Set options of the Parser
* @todo document
* @package MediaWiki
*/
class ParserOptions
{
# All variables are private
var $mUseTeX; # Use texvc to expand