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 ) );
# Use the time zone
global $wgLocaltimezone;
if ( isset( $wgLocaltimezone ) ) {
$oldtz = getenv( 'TZ' );
putenv( 'TZ='.$wgLocaltimezone );
}
$localTimestamp = date( 'YmdHis', $ts );
$localMonth = date( 'm', $ts );
$localMonthName = date( 'n', $ts );
$localDay = date( 'j', $ts );
$localDay2 = date( 'd', $ts );
$localDayOfWeek = date( 'w', $ts );
$localWeek = date( 'W', $ts );
$localYear = date( 'Y', $ts );
$localHour = date( 'H', $ts );
if ( isset( $wgLocaltimezone ) ) {
putenv( 'TZ='.$oldtz );
}
switch ( $index ) {
case 'currentmonth':
return $varCache[$index] = $wgContLang->formatNum( date( 'm', $ts ) );
case 'currentmonthname':
return $varCache[$index] = $wgContLang->getMonthName( date( 'n', $ts ) );
case 'currentmonthnamegen':
return $varCache[$index] = $wgContLang->getMonthNameGen( date( 'n', $ts ) );
case 'currentmonthabbrev':
return $varCache[$index] = $wgContLang->getMonthAbbreviation( date( 'n', $ts ) );
case 'currentday':
return $varCache[$index] = $wgContLang->formatNum( date( 'j', $ts ) );
case 'currentday2':
return $varCache[$index] = $wgContLang->formatNum( date( 'd', $ts ) );
case 'localmonth':
return $varCache[$index] = $wgContLang->formatNum( $localMonth );
case 'localmonthname':
return $varCache[$index] = $wgContLang->getMonthName( $localMonthName );
case 'localmonthnamegen':
return $varCache[$index] = $wgContLang->getMonthNameGen( $localMonthName );
case 'localmonthabbrev':
return $varCache[$index] = $wgContLang->getMonthAbbreviation( $localMonthName );
case 'localday':
return $varCache[$index] = $wgContLang->formatNum( $localDay );
case 'localday2':
return $varCache[$index] = $wgContLang->formatNum( $localDay2 );
case 'pagename':
return $this->mTitle->getText();
case 'pagenamee':
return $this->mTitle->getPartialURL();
case 'fullpagename':
return $this->mTitle->getPrefixedText();
case 'fullpagenamee':
return $this->mTitle->getPrefixedURL();
case 'subpagename':
return $this->mTitle->getSubpageText();
case 'subpagenamee':
return $this->mTitle->getSubpageUrlForm();
case 'basepagename':
return $this->mTitle->getBaseText();
case 'basepagenamee':
return wfUrlEncode( str_replace( ' ', '_', $this->mTitle->getBaseText() ) );
case 'talkpagename':
if( $this->mTitle->canTalk() ) {
$talkPage = $this->mTitle->getTalkPage();
return $talkPage->getPrefixedText();
} else {
return '';
}
case 'talkpagenamee':
if( $this->mTitle->canTalk() ) {
$talkPage = $this->mTitle->getTalkPage();
return $talkPage->getPrefixedUrl();
} else {
return '';
}
case 'subjectpagename':
$subjPage = $this->mTitle->getSubjectPage();
return $subjPage->getPrefixedText();
case 'subjectpagenamee':
$subjPage = $this->mTitle->getSubjectPage();
return $subjPage->getPrefixedUrl();
case 'revisionid':
return $this->mRevisionId;
case 'namespace':
return str_replace('_',' ',$wgContLang->getNsText( $this->mTitle->getNamespace() ) );
case 'namespacee':
return wfUrlencode( $wgContLang->getNsText( $this->mTitle->getNamespace() ) );
case 'talkspace':
return $this->mTitle->canTalk() ? str_replace('_',' ',$this->mTitle->getTalkNsText()) : '';
case 'talkspacee':
return $this->mTitle->canTalk() ? wfUrlencode( $this->mTitle->getTalkNsText() ) : '';
case 'subjectspace':
return $this->mTitle->getSubjectNsText();
case 'subjectspacee':
return( wfUrlencode( $this->mTitle->getSubjectNsText() ) );
case 'currentdayname':
return $varCache[$index] = $wgContLang->getWeekdayName( date( 'w', $ts ) + 1 );
case 'currentyear':
return $varCache[$index] = $wgContLang->formatNum( date( 'Y', $ts ), true );
case 'currenttime':
return $varCache[$index] = $wgContLang->time( wfTimestamp( TS_MW, $ts ), false, false );
case 'currenthour':
return $varCache[$index] = $wgContLang->formatNum( date( 'H', $ts ), true );
case '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 'currentdow':
return $varCache[$index] = $wgContLang->formatNum( date( 'w', $ts ) );
case 'localdayname':
return $varCache[$index] = $wgContLang->getWeekdayName( $localDayOfWeek + 1 );
case 'localyear':
return $varCache[$index] = $wgContLang->formatNum( $localYear, true );
case 'localtime':
return $varCache[$index] = $wgContLang->time( $localTimestamp, false, false );
case 'localhour':
return $varCache[$index] = $wgContLang->formatNum( $localHour, true );
case 'localweek':
// @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
// int to remove the padding
return $varCache[$index] = $wgContLang->formatNum( (int)$localWeek );
case 'localdow':
return $varCache[$index] = $wgContLang->formatNum( $localDayOfWeek );
case 'numberofarticles':
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
case 'numberoffiles':
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfFiles() );
case 'numberofusers':
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfUsers() );
case 'numberofpages':
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfPages() );
case 'numberofadmins':
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfAdmins() );
case 'currenttimestamp':
return $varCache[$index] = wfTimestampNow();
case 'localtimestamp':
return $varCache[$index] = $localTimestamp;
case 'currentversion':
global $wgVersion;
return $wgVersion;
case 'sitename':
return $wgSitename;
case 'server':
return $wgServer;
case 'servername':
return $wgServerName;
case 'scriptpath':
return $wgScriptPath;
case 'directionmark':
return $wgContLang->getDirMark();
case '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 );
$variableIDs = MagicWord::getVariableIDs();
$this->mVariables = array();
foreach ( $variableIDs 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
* 3 => callback # replacement callback to call if {{{..}}} is found
* )
* )
* 'min' => 2, # Minimum parenthesis count in cb
* 'max' => 3, # Maximum parenthesis count in cb
* @private
*/
function replace_callback ($text, $callbacks) {
wfProfileIn( __METHOD__ );
$openingBraceStack = array(); # this array will hold a stack of parentheses which are not closed yet
$lastOpeningBrace = -1; # last not closed parentheses
$validOpeningBraces = implode( '', array_keys( $callbacks ) );
$i = 0;
while ( $i < strlen( $text ) ) {
# Find next opening brace, closing brace or pipe
if ( $lastOpeningBrace == -1 ) {
$currentClosing = '';
$search = $validOpeningBraces;
} else {
$currentClosing = $openingBraceStack[$lastOpeningBrace]['braceEnd'];
$search = $validOpeningBraces . '|' . $currentClosing;
}
$rule = null;
$i += strcspn( $text, $search, $i );
if ( $i < strlen( $text ) ) {
if ( $text[$i] == '|' ) {
$found = 'pipe';
} elseif ( $text[$i] == $currentClosing ) {
$found = 'close';
} else {
$found = 'open';
$rule = $callbacks[$text[$i]];
}
} else {
# All done
break;
}
if ( $found == 'open' ) {
# found opening brace, let's add it to parentheses stack
$piece = array('brace' => $text[$i],
'braceEnd' => $rule['end'],
'title' => '',
'parts' => null);
# count opening brace characters
$piece['count'] = strspn( $text, $piece['brace'], $i );
$piece['startAt'] = $piece['partStart'] = $i + $piece['count'];
$i += $piece['count'];
# we need to add to stack only if opening brace count is enough for one of the rules
if ( $piece['count'] >= $rule['min'] ) {
$lastOpeningBrace ++;
$openingBraceStack[$lastOpeningBrace] = $piece;
}
} elseif ( $found == 'close' ) {
# lets check if it is enough characters for closing brace
$maxCount = $openingBraceStack[$lastOpeningBrace]['count'];
$count = strspn( $text, $text[$i], $i, $maxCount );
# 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;
$cbType = $callbacks[$openingBraceStack[$lastOpeningBrace]['brace']];
if ( $count > $cbType['max'] ) {
# The specified maximum exists in the callback array, unless the caller
# has made an error
$matchingCount = $cbType['max'];
} else {
# Count is less than the maximum
# Skip any gaps in the callback array to find the true largest match
# Need to use array_key_exists not isset because the callback can be null
$matchingCount = $count;
while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $cbType['cb'] ) ) {
--$matchingCount;
}
}
if ($matchingCount <= 0) {
$i += $count;
continue;
}
$matchingCallback = $cbType['cb'][$matchingCount];
# let's 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
$replaceWith = call_user_func( $matchingCallback, $cbArgs );
$text = substr($text, 0, $pieceStart) . $replaceWith . substr($text, $pieceEnd);
$i = $pieceStart + strlen($replaceWith);
} else {
# null value for callback means that parentheses should be parsed, but not replaced
$i += $matchingCount;
}
# reset last opening 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?
$currentCbList = $callbacks[$piece['brace']]['cb'];
while ( $piece['count'] ) {
if ( array_key_exists( $piece['count'], $currentCbList ) ) {
$lastOpeningBrace++;
$openingBraceStack[$lastOpeningBrace] = $piece;
break;
}
--$piece['count'];
}
}
} elseif ( $found == 'pipe' ) {
# lets set a title if it is a first separator, or next part otherwise
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;
}
}
wfProfileOut( __METHOD__ );
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 ) > $this->mOptions->getMaxIncludeSize() ) {
return $text;
}
$fname = __METHOD__ /*. '-L' . count( $this->mArgStack )*/;
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_MSG ) {
$braceCallbacks[3] = array( &$this, 'argSubstitution' );
}
if ( $braceCallbacks ) {
$callbacks = array(
'{' => array(
'end' => '}',
'cb' => $braceCallbacks,
'min' => $argsOnly ? 3 : 2,
'max' => isset( $braceCallbacks[3] ) ? 3 : 2,
),
'[' => array(
'end' => ']',
'cb' => array(2=>null),
'min' => 2,
'max' => 2,
)
);
$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( '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 = __METHOD__ /*. '-L' . count( $this->mArgStack )*/;
wfProfileIn( $fname );
wfProfileIn( __METHOD__.'-setup' );
# 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
$titleText = $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 );
wfProfileOut( __METHOD__.'-setup' );
# SUBST
wfProfileIn( __METHOD__.'-modifiers' );
if ( !$found ) {
$mwSubst =& MagicWord::get( 'subst' );
if ( $mwSubst->matchStartAndRemove( $part1 ) xor $this->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( 'msgnw' );
if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
$nowiki = true;
} else {
# Remove obsolete MSG:
$mwMsg =& MagicWord::get( 'msg' );
$mwMsg->matchStartAndRemove( $part1 );
}
# Check for RAW:
$mwRaw =& MagicWord::get( 'raw' );
if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
$forceRawInterwiki = true;
}
# Check if it is an internal message
$mwInt =& MagicWord::get( 'int' );
if ( $mwInt->matchStartAndRemove( $part1 ) ) {
$text = $linestart . wfMsgReal( $part1, $args, true );
$found = true;
}
}
wfProfileOut( __METHOD__.'-modifiers' );
# 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( __METHOD__.": 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 ) ) {
$titleText = $title->getPrefixedText();
$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() ) {
if ( $title->getNamespace() == NS_SPECIAL && $this->mOptions->getAllowSpecialInclusion() && $this->ot['html'] ) {
$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 ( !$found && ( $this->ot['html'] || $this->ot['pre'] ) ) {
$text = "[[$titleText]]";
$found = true;
}
} elseif ( $title->isTrans() ) {
// Interwiki transclusion
if ( $this->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' );
}
if ( $found && !$this->incrementIncludeSize( 'pre-expand', strlen( $text ) ) ) {
# Error, oversize inclusion
$text = $linestart .
"[[$titleText]]";
$noparse = true;
$noargs = true;
}
# Recursive parsing, escaping and link table handling
# Only for HTML output
if ( $nowiki && $found && ( $this->ot['html'] || $this->ot['pre'] ) ) {
$text = wfEscapeWikiText( $text );
} elseif ( !$this->ot['msg'] && $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->ot['html'] || $this->ot['pre'] ) {
# Strip , , etc.
$text = $this->strip( $text, $this->mStripState );
if ( $this->ot['html'] ) {
$text = Sanitizer::removeHTMLtags( $text, array( &$this, 'replaceVariables' ), $assocArgs );
} elseif ( $this->ot['pre'] && $this->mOptions->getRemoveComments() ) {
$text = Sanitizer::removeHTMLcomments( $text );
}
}
$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 && !$this->incrementIncludeSize( 'post-expand', strlen( $text ) ) ) {
# Error, oversize inclusion
$text = $linestart .
"[[$titleText]]";
$noparse = true;
$noargs = true;
}
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->ot['wiki'] && !$this->ot['pre'] && $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 || $this->mOutputType == OT_PREPROCESS ) &&
null != $matches['parts'] && count($matches['parts']) > 0) {
$text = $matches['parts'][0];
}
if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) {
$text = $matches['text'] .
'';
}
return $text;
}
/**
* Increment an include size counter
*
* @param string $type The type of expansion
* @param integer $size The size of the text
* @return boolean False if this inclusion would take it over the maximum, true otherwise
*/
function incrementIncludeSize( $type, $size ) {
if ( $this->mIncludeSizes[$type] + $size > $this->mOptions->getMaxIncludeSize() ) {
return false;
} else {
$this->mIncludeSizes[$type] += $size;
return true;
}
}
/**
* 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( '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( 'notoc' );
if( $mw->matchAndRemove( $text ) ) {
$this->mShowToc = false;
}
$mw = MagicWord::get( '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( '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( '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( '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;
}
$tp = Title::newFromText( $matches[1] );
$nt =& $tp;
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( 'img_thumbnail' );
$mwManualThumb =& MagicWord::get( 'img_manualthumb' );
$mwLeft =& MagicWord::get( 'img_left' );
$mwRight =& MagicWord::get( 'img_right' );
$mwNone =& MagicWord::get( 'img_none' );
$mwWidth =& MagicWord::get( 'img_width' );
$mwCenter =& MagicWord::get( 'img_center' );
$mwFramed =& MagicWord::get( '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( "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->setOutputType( OT_WIKI );
$striptext = $this->strip( $text, $striparray, true );
$this->setOutputType( $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 getSubtitle() { return $this->mSubtitle; }
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 setSubtitle( $st ) { return wfSetVar( $this->mSubtitle, $st ); }
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 = null ) {
$ns = $title->getNamespace();
$dbk = $title->getDBkey();
if ( !isset( $this->mLinks[$ns] ) ) {
$this->mLinks[$ns] = array();
}
if ( is_null( $id ) ) {
$id = $title->getArticleID();
}
$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