* (which in turn the browser understands, and can display).
*
* <pre>
- * There are five main entry points into the Parser class:
+ * There are six main entry points into the Parser class:
* parse()
* produces HTML output
* preSaveTransform().
* Cleans a signature before saving it to preferences
* extractSections()
* Extracts sections from an article for section editing
+ * getTransclusionText()
+ * Extracts the text of a template with only <includeonly>, etc., parsed
*
* Globals used:
* objects: $wgLang, $wgContLang
const OT_WIKI = 2;
const OT_PREPROCESS = 3;
const OT_MSG = 3;
+ const OT_INCLUDES = 4;
// Marker Suffix needs to be accessible staticly.
const MARKER_SUFFIX = "-QINU\x7f";
*/
# Persistent:
var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables,
- $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex, $mPreprocessor,
- $mExtLinkBracketedRegex, $mUrlProtocols, $mDefaultStripList, $mVarCache, $mConf,
- $mFunctionTagHooks;
+ $mSubstWords, $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex,
+ $mPreprocessor, $mExtLinkBracketedRegex, $mUrlProtocols, $mDefaultStripList,
+ $mVarCache, $mConf, $mFunctionTagHooks;
# Cleared with clearState():
$this->mFunctionHooks = array();
$this->mFunctionTagHooks = array();
$this->mFunctionSynonyms = array( 0 => array(), 1 => array() );
- $this->mDefaultStripList = $this->mStripList = array( 'nowiki', 'gallery', 'a' );
+ $this->mDefaultStripList = $this->mStripList = array();
$this->mUrlProtocols = wfUrlProtocols();
$this->mExtLinkBracketedRegex = '/\[(\b(' . wfUrlProtocols() . ')'.
'[^][<>"\\x00-\\x20\\x7F]+) *([^\]\\x0a\\x0d]*?)\]/S';
wfProfileIn( __METHOD__ );
- $this->setHook( 'pre', array( $this, 'renderPreTag' ) );
CoreParserFunctions::register( $this );
+ CoreTagHooks::register( $this );
$this->initialiseVariables();
wfRunHooks( 'ParserFirstCallInit', array( &$this ) );
* Set the context title
*/
function setTitle( $t ) {
- if ( !$t || $t instanceof FakeTitle ) {
- $t = Title::newFromText( 'NO TITLE' );
- }
+ if ( !$t || $t instanceof FakeTitle ) {
+ $t = Title::newFromText( 'NO TITLE' );
+ }
+
if ( strval( $t->getFragment() ) !== '' ) {
# Strip the fragment to avoid various odd effects
$this->mTitle = clone $t;
* to internalParse() which does all the real work.
*/
- global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang;
+ global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang, $wgDisableLangConversion;
$fname = __METHOD__.'-' . wfGetCaller();
wfProfileIn( __METHOD__ );
wfProfileIn( $fname );
}
$this->mOptions = $options;
- $this->setTitle( $title );
+ $this->setTitle( $title ); // Page title has to be set for the pre-processor
+
$oldRevisionId = $this->mRevisionId;
$oldRevisionTimestamp = $this->mRevisionTimestamp;
if( $revid !== null ) {
# No more strip!
wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
$text = $this->internalParse( $text );
+
$text = $this->mStripState->unstripGeneral( $text );
# Clean up special characters, only run once, next-to-last before doBlockLevels
$this->replaceLinkHolders( $text );
- # the position of the parserConvert() call should not be changed. it
- # assumes that the links are all replaced and the only thing left
- # is the <nowiki> mark.
- # Side-effects: this calls $this->mOutput->setTitleText()
- $text = $wgContLang->parserConvert( $text, $this );
+ // The position of the convert() call should not be changed. it
+ // assumes that the links are all replaced and the only thing left
+ // is the <nowiki> mark.
+ if ( !( $wgDisableLangConversion
+ || isset( $this->mDoubleUnderscores['nocontentconvert'] )
+ || $this->mTitle->isTalkPage()
+ || $this->mTitle->isConversionTable() ) ) {
+ $text = $wgContLang->convert( $text );
+ }
+
+ // A title may have been set in a conversion rule.
+ // Note that if a user tries to set a title in a conversion
+ // rule but content conversion was not done, then the parser
+ // won't pick it up. This is probably expected behavior.
+ if ( $wgContLang->getConvRuleTitle() ) {
+ $this->mOutput->setTitleText( $wgContLang->getConvRuleTitle() );
+ }
$text = $this->mStripState->unstripNoWiki( $text );
wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
-//!JF Move to its own function
-
- $uniq_prefix = $this->mUniqPrefix;
- $matches = array();
- $elements = array_keys( $this->mTransparentTagHooks );
- $text = self::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
-
- foreach( $matches as $marker => $data ) {
- list( $element, $content, $params, $tag ) = $data;
- $tagName = strtolower( $element );
- if( isset( $this->mTransparentTagHooks[$tagName] ) ) {
- $output = call_user_func_array( $this->mTransparentTagHooks[$tagName],
- array( $content, $params, $this ) );
- } else {
- $output = $tag;
+ if ( $this->mTransparentTagHooks ) {
+ //!JF Move to its own function
+ $uniq_prefix = $this->mUniqPrefix;
+ $matches = array();
+ $elements = array_keys( $this->mTransparentTagHooks );
+ $text = self::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
+
+ foreach( $matches as $marker => $data ) {
+ list( $element, $content, $params, $tag ) = $data;
+ $tagName = strtolower( $element );
+ if( isset( $this->mTransparentTagHooks[$tagName] ) ) {
+ $output = call_user_func_array( $this->mTransparentTagHooks[$tagName],
+ array( $content, $params, $this ) );
+ } else {
+ $output = $tag;
+ }
+ $this->mStripState->general->setPair( $marker, $output );
}
- $this->mStripState->general->setPair( $marker, $output );
}
+
+ # This was originally inserted for transparent tag hooks (now deprecated)
+ # but some extensions (notably <poem>) rely on the extra unstripGeneral()
+ # after unstripNoWiki() so they can modify the contents of <nowiki> tags.
$text = $this->mStripState->unstripGeneral( $text );
$text = Sanitizer::normalizeCharReferences( $text );
$text .= "\n<!-- \n$limitReport-->\n";
}
$this->mOutput->setText( $text );
+
$this->mRevisionId = $oldRevisionId;
$this->mRevisionTimestamp = $oldRevisionTimestamp;
wfProfileOut( $fname );
return $text;
}
+ /**
+ * Get the wikitext of a page as though it was transcluded.
+ *
+ * Specifically <includeonly> etc. are parsed, redirects are followed, comments
+ * are removed, but templates arguments and parser functions are untouched.
+ *
+ * This is not called by the parser itself, see braceSubstitution for its transclusion.
+ */
+ public function getTransclusionText( $title, $options ) {
+ // Must initialize first
+ $this->clearState();
+ $this->setOutputType( self::OT_INCLUDES );
+ $this->mOptions = $options;
+ $this->setTitle( new FakeTitle );
+
+ list( $text, $title ) = $this->getTemplateDom( $title );
+ $flags = PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES;
+ return $this->getPreprocessor()->newFrame()->expand( $text, $flags );
+ }
+
/**
* Get a random string
*
$taglist = implode( '|', $elements );
$start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?" . ">)|<(!--)/i";
- while ( '' != $text ) {
+ while ( $text != '' ) {
$p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
$stripped .= $p[0];
if( count( $p ) < 5 ) {
* Get a list of strippable XML-like elements
*/
function getStripList() {
- global $wgRawHtml;
- $elements = $this->mStripList;
- if( $wgRawHtml ) {
- $elements[] = 'html';
- }
- if( $this->mOptions->getUseTeX() ) {
- $elements[] = 'math';
- }
- return $elements;
+ return $this->mStripList;
}
/**
$this->mStripState->general->setPair( $rnd, $text );
return $rnd;
}
-
+
/**
* Interface with html tidy
* @deprecated Use MWTidy::tidy()
*/
public static function tidy( $text ) {
wfDeprecated( __METHOD__ );
- return MWTidy::tidy( $text );
+ return MWTidy::tidy( $text );
}
/**
$attributes = Sanitizer::fixTagAttributes ( $attributes , 'table' );
$outLine = str_repeat( '<dl><dd>' , $indent_level ) . "<table{$attributes}>";
- array_push ( $td_history , false );
- array_push ( $last_tag_history , '' );
- array_push ( $tr_history , false );
- array_push ( $tr_attributes , '' );
- array_push ( $has_opened_tr , false );
+ array_push( $td_history , false );
+ array_push( $last_tag_history , '' );
+ array_push( $tr_history , false );
+ array_push( $tr_attributes , '' );
+ array_push( $has_opened_tr , false );
} else if ( count ( $td_history ) == 0 ) {
// Don't do any of the following
$out .= $outLine."\n";
// Whats after the tag is now only attributes
$attributes = $this->mStripState->unstripBoth( $line );
- $attributes = Sanitizer::fixTagAttributes ( $attributes , 'tr' );
- array_pop ( $tr_attributes );
- array_push ( $tr_attributes , $attributes );
+ $attributes = Sanitizer::fixTagAttributes( $attributes, 'tr' );
+ array_pop( $tr_attributes );
+ array_push( $tr_attributes, $attributes );
$line = '';
$last_tag = array_pop ( $last_tag_history );
*/
function internalParse( $text, $isMain = true, $frame=false ) {
wfProfileIn( __METHOD__ );
-
+
$origText = $text;
# Hook to suspend the parser in this state
if ($frame) {
// use frame depth to infer how include/noinclude tags should be handled
// depth=0 means this is the top-level document; otherwise it's an included document
- if( !$frame->depth )
+ if( !$frame->depth )
$flag = 0;
else
$flag = Parser::PTD_FOR_INCLUSION;
$text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
$text = $this->doDoubleUnderscore( $text );
+
$text = $this->doHeadings( $text );
if( $this->mOptions->getUseDynamicDates() ) {
$df = DateFormatter::getInstance();
/**
* Replace special strings like "ISBN xxx" and "RFC xxx" with
* magic external links.
- *
+ *
* DML
* @private
*/
$urlChar = self::EXT_LINK_URL_CLASS;
$text = preg_replace_callback(
'!(?: # Start cases
- (<a.*?</a>) | # m[1]: Skip link text
+ (<a.*?</a>) | # m[1]: Skip link text
(<.*?>) | # m[2]: Skip stuff inside HTML elements' . "
(\\b(?:$prots)$urlChar+) | # m[3]: Free external links" . '
(?:RFC|PMID)\s+([0-9]+) | # m[4]: RFC or PMID, capture number
$text = $this->maybeMakeExternalImage( $url );
if ( $text === false ) {
# Not an image, make a link
- $text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free',
+ $text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free',
$this->getExternalLinkAttribs( $url ) );
# Register it in the output object...
# Replace unnecessary URL escape codes with their equivalent characters
wfProfileIn( __METHOD__.'-setup' );
static $tc = FALSE, $e1, $e1_img;
# the % is needed to support urlencoded titles as well
- if ( !$tc ) {
+ if ( !$tc ) {
$tc = Title::legalChars() . '#%';
# Match a link having the form [[namespace:link|alternate]]trail
- $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
+ $e1 = "/^([{$tc}]*)(\\|.*?)?]](.*)\$/sD";
# Match cases where there is no "]]", which might still be images
$e1_img = "/^([{$tc}]+)\\|(.*)\$/sD";
}
wfProfileIn( __METHOD__."-e1" );
if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
- $text = $m[2];
+
+ if( $m[2] === '' ) {
+ $text = '';
+ } elseif( $m[2] === '|' ) {
+ $text = $this->getPipeTrickText( $m[1] );
+ } else {
+ $text = substr( $m[2], 1 );
+ }
+
# If we get a ] at the beginning of $m[3] that means we have a link that's something like:
# [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
# the real problem is with the $e1 regex
$text .= ']'; # so that replaceExternalLinks($text) works later
$m[3] = substr( $m[3], 1 );
}
+
+ # Handle pipe-trick for [[|<blah>]]
+ $lnk = $m[1] === '' ? $this->getPipeTrickLink( $text ) : $m[1];
# fix up urlencoded title texts
- if( strpos( $m[1], '%' ) !== false ) {
+ if( strpos( $lnk, '%' ) !== false ) {
# Should anchors '#' also be rejected?
- $m[1] = str_replace( array('<', '>'), array('<', '>'), urldecode($m[1]) );
+ $lnk = str_replace( array('<', '>'), array('<', '>'), urldecode($lnk) );
}
+
$trail = $m[3];
} elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
$might_be_img = true;
$text = $m[2];
- if ( strpos( $m[1], '%' ) !== false ) {
- $m[1] = urldecode($m[1]);
- }
+ $lnk = strpos( $m[1], '%' ) === false ? $m[1] : urldecode( $m[1] );
$trail = "";
} else { # Invalid form; output directly
$s .= $prefix . '[[' . $line ;
# Don't allow internal links to pages containing
# PROTO: where PROTO is a valid URL protocol; these
# should be external links.
- if ( preg_match( '/^\b(?:' . wfUrlProtocols() . ')/', $m[1] ) ) {
+ if ( preg_match( '/^\b(?:' . wfUrlProtocols() . ')/', $lnk ) ) {
$s .= $prefix . '[[' . $line ;
wfProfileOut( __METHOD__."-misc" );
continue;
# Make subpage if necessary
if ( $useSubpages ) {
- $link = $this->maybeDoSubpageLink( $m[1], $text );
+ $link = $this->maybeDoSubpageLink( $lnk, $text );
} else {
- $link = $m[1];
+ $link = $lnk;
}
- $noforce = (substr( $m[1], 0, 1 ) !== ':');
+ $noforce = (substr( $lnk, 0, 1 ) !== ':');
if (!$noforce) {
# Strip off leading ':'
$link = substr( $link, 1 );
wfProfileOut( __METHOD__."-might_be_img" );
}
- $wasblank = ( '' == $text );
+ $wasblank = ( $text == '' );
if ( $wasblank ) $text = $link;
# Link not escaped by : , create the various objects
}
# cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
$s .= $prefix . $this->armorLinks( $this->makeImage( $nt, $text, $holders ) ) . $trail;
+ } else {
+ $s .= $prefix . $trail;
}
$this->mOutput->addImage( $nt->getDBkey() );
wfProfileOut( __METHOD__."-image" );
return Linker::normalizeSubpageLink( $this->mTitle, $target, $text );
}
+ /**
+ * From the [[title|]] return link-text as though the used typed [[title|link-text]]
+ * @param string $link from [[$link|]]
+ * @return string $text for [[$link|$text]]
+ */
+ function getPipeTrickText( $link ) {
+ return Linker::getPipeTrickText( $link );
+ }
+
+ /**
+ * From the [[|link-text]] return the title as though the user typed [[title|link-text]]
+ * @param string $text from [[|$text]]
+ * @param Title $title to resolve the link against
+ * @return string $link for [[$link|$text]]
+ */
+ function getPipeTrickLink( $text ) {
+ return Linker::getPipeTrickLink( $text, $this->mTitle );
+ }
+
/**#@+
* Used by doBlockLevels()
* @private
*/
/* private */ function closeParagraph() {
$result = '';
- if ( '' != $this->mLastSection ) {
+ if ( $this->mLastSection != '' ) {
$result = '</' . $this->mLastSection . ">\n";
}
$this->mInPre = false;
}
} elseif( $prefixLength || $lastPrefixLength ) {
// We need to open or close prefixes, or both.
-
+
# Either open or close a level...
$commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
$paragraphStack = false;
$output .= $this->closeList( $lastPrefix[$lastPrefixLength-1] );
--$lastPrefixLength;
}
-
+
// Continue the current prefix if appropriate.
if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
$output .= $this->nextItem( $prefix[$commonPrefixLength-1] );
}
-
+
// Open prefixes where appropriate.
while ( $prefixLength > $commonPrefixLength ) {
$char = substr( $prefix, $commonPrefixLength, 1 );
}
$lastPrefix = $prefix2;
}
-
+
// If we have no prefixes, go to paragraph mode.
if( 0 == $prefixLength ) {
wfProfileIn( __METHOD__."-paragraph" );
$t = substr( $t, 1 );
} else {
// paragraph
- if ( '' == trim($t) ) {
+ if ( trim($t) == '' ) {
if ( $paragraphStack ) {
$output .= $paragraphStack.'<br />';
$paragraphStack = false;
$output .= $this->closeList( $prefix2[$prefixLength-1] );
--$prefixLength;
}
- if ( '' != $this->mLastSection ) {
+ if ( $this->mLastSection != '' ) {
$output .= '</' . $this->mLastSection . '>';
$this->mLastSection = '';
}
# Use the time zone
global $wgLocaltimezone;
if ( isset( $wgLocaltimezone ) ) {
- $oldtz = getenv( 'TZ' );
- putenv( 'TZ='.$wgLocaltimezone );
+ $oldtz = date_default_timezone_get();
+ date_default_timezone_set( $wgLocaltimezone );
}
- wfSuppressWarnings(); // E_STRICT system time bitching
$localTimestamp = date( 'YmdHis', $ts );
$localMonth = date( 'm', $ts );
$localMonth1 = date( 'n', $ts );
$localYear = date( 'Y', $ts );
$localHour = date( 'H', $ts );
if ( isset( $wgLocaltimezone ) ) {
- putenv( 'TZ='.$oldtz );
+ date_default_timezone_set( $oldtz );
}
- wfRestoreWarnings();
switch ( $index ) {
case 'currentmonth':
$subjPage = $this->mTitle->getSubjectPage();
$value = $subjPage->getPrefixedUrl();
break;
+ case 'pipetrick':
+ $text = $this->mTitle->getText();
+ $value = $this->getPipeTrickText( $text );
+ break;
+ case 'pipetricke':
+ $text = $this->mTitle->getText();
+ $value = wfUrlEncode( str_replace( ' ', '_', $this->getPipeTrickText( $text ) ) );
+ break;
case 'revisionid':
// Let the edit saving system know we should parse the page
// *after* a revision ID has been assigned.
return $wgServerName;
case 'scriptpath':
return $wgScriptPath;
+ case 'stylepath':
+ return $wgStylePath;
case 'directionmark':
return $wgContLang->getDirMark();
case 'contentlanguage':
return null;
}
- if ( $index )
+ if ( $index )
$this->mVarCache[$index] = $value;
return $value;
}
/**
- * initialise the magic variables (like CURRENTMONTHNAME)
+ * initialise the magic variables (like CURRENTMONTHNAME) and substitution modifiers
*
* @private
*/
function initialiseVariables() {
wfProfileIn( __METHOD__ );
$variableIDs = MagicWord::getVariableIDs();
+ $substIDs = MagicWord::getSubstIDs();
$this->mVariables = new MagicWordArray( $variableIDs );
+ $this->mSubstWords = new MagicWordArray( $substIDs );
wfProfileOut( __METHOD__ );
}
* self::OT_HTML: all templates and extension tags
*
* @param string $tex The text to transform
- * @param PPFrame $frame Object describing the arguments passed to the template.
+ * @param PPFrame $frame Object describing the arguments passed to the template.
* Arguments may also be provided as an associative array, as was the usual case before MW1.12.
* Providing arguments this way may be useful for extensions wishing to perform variable replacement explicitly.
* @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
*/
function limitationWarn( $limitationType, $current=null, $max=null) {
//does no harm if $current and $max are present but are unnecessary for the message
- $warning = wfMsgExt( "$limitationType-warning", array( 'parsemag', 'escape' ), $current, $max );
+ $warning = wfMsgExt( "$limitationType-warning", array( 'parsemag', 'escape' ), $current, $max );
$this->mOutput->addWarning( $warning );
$this->addTrackingCategory( "$limitationType-category" );
}
# 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
+
+ $substMatch = $this->mSubstWords->matchStartAndRemove( $part1 );
+
+ # Possibilities for substMatch: "subst", "safesubst" or FALSE
+ # Decide whether to expand template or keep wikitext as-is.
+ if ( $this->ot['wiki'] )
+ {
+ if ( $substMatch === false ) {
+ $literal = true; # literal when in PST with no prefix
+ } else {
+ $literal = false; # expand when in PST with subst: or safesubst:
+ }
+ } else {
+ if ( $substMatch == 'subst' ) {
+ $literal = true; # literal when not in PST with plain subst:
+ } else {
+ $literal = false; # expand when not in PST with safesubst: or no prefix
+ }
+ }
+ if ( $literal ) {
$text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
$isLocalObj = true;
$found = true;
$found = true;
$noparse = true;
$preprocessFlags = 0;
-
+
if ( is_array( $result ) ) {
if ( isset( $result[0] ) ) {
$text = $result[0];
$name = $frame->expand( $params['name'] );
$attrText = !isset( $params['attr'] ) ? null : $frame->expand( $params['attr'] );
$content = !isset( $params['inner'] ) ? null : $frame->expand( $params['inner'] );
-
$marker = "{$this->mUniqPrefix}-$name-" . sprintf('%08X', $this->mMarkerIndex++) . self::MARKER_SUFFIX;
$isFunctionTag = isset( $this->mFunctionTagHooks[strtolower($name)] ) &&
( $this->ot['html'] || $this->ot['pre'] );
+ if ( $isFunctionTag ) {
+ $markerType = 'none';
+ } else {
+ $markerType = 'general';
+ }
if ( $this->ot['html'] || $isFunctionTag ) {
$name = strtolower( $name );
$attributes = Sanitizer::decodeTagAttributes( $attrText );
if ( isset( $params['attributes'] ) ) {
$attributes = $attributes + $params['attributes'];
}
- switch ( $name ) {
- case 'html':
- if( $wgRawHtml ) {
- $output = $content;
- break;
- } else {
- throw new MWException( '<html> extension tag encountered unexpectedly' );
- }
- case 'nowiki':
- $content = strtr($content, array('-{' => '-{', '}-' => '}-'));
- $output = Xml::escapeTagsOnly( $content );
- break;
- case 'gallery':
- $output = $this->renderImageGallery( $content, $attributes );
- break;
- case 'a':
- $output = $this->renderHyperlink( $content, $attributes, $frame );
- break;
- case 'math':
- if ( $this->mOptions->getUseTeX() ) {
- $output = $wgContLang->armourMath(
- MathRenderer::renderMath( $content, $attributes ) );
- break;
- }
- /* else let a tag hook handle it (bug 21222) */
- default:
- if( isset( $this->mTagHooks[$name] ) ) {
- # Workaround for PHP bug 35229 and similar
- if ( !is_callable( $this->mTagHooks[$name] ) ) {
- throw new MWException( "Tag hook for $name is not callable\n" );
- }
- $output = call_user_func_array( $this->mTagHooks[$name],
- array( $content, $attributes, $this, $frame ) );
- } elseif( isset( $this->mFunctionTagHooks[$name] ) ) {
- list( $callback, $flags ) = $this->mFunctionTagHooks[$name];
- if( !is_callable( $callback ) )
- throw new MWException( "Tag hook for $name is not callable\n" );
-
- $output = call_user_func_array( $callback,
- array( &$this, $frame, $content, $attributes ) );
- } else {
- $output = '<span class="error">Invalid tag extension name: ' .
- htmlspecialchars( $name ) . '</span>';
- }
+
+ if( isset( $this->mTagHooks[$name] ) ) {
+ # Workaround for PHP bug 35229 and similar
+ if ( !is_callable( $this->mTagHooks[$name] ) ) {
+ throw new MWException( "Tag hook for $name is not callable\n" );
+ }
+ $output = call_user_func_array( $this->mTagHooks[$name],
+ array( $content, $attributes, $this, $frame ) );
+ } elseif( isset( $this->mFunctionTagHooks[$name] ) ) {
+ list( $callback, $flags ) = $this->mFunctionTagHooks[$name];
+ if( !is_callable( $callback ) )
+ throw new MWException( "Tag hook for $name is not callable\n" );
+
+ $output = call_user_func_array( $callback,
+ array( &$this, $frame, $content, $attributes ) );
+ } else {
+ $output = '<span class="error">Invalid tag extension name: ' .
+ htmlspecialchars( $name ) . '</span>';
+ }
+
+ if ( is_array( $output ) ) {
+ // Extract flags to local scope (to override $markerType)
+ $flags = $output;
+ $output = $flags[0];
+ unset( $flags[0] );
+ extract( $flags );
}
} else {
if ( is_null( $attrText ) ) {
}
}
- if( $isFunctionTag ) {
+ if( $markerType === 'none' ) {
return $output;
- } elseif ( $name === 'html' || $name === 'nowiki' ) {
+ } elseif ( $markerType === 'nowiki' ) {
$this->mStripState->nowiki->setPair( $marker, $output );
- } else {
+ } elseif ( $markerType === 'general' ) {
$this->mStripState->general->setPair( $marker, $output );
+ } else {
+ throw new MWException( __METHOD__.': invalid marker type' );
}
return $marker;
}
*/
function doDoubleUnderscore( $text ) {
wfProfileIn( __METHOD__ );
+
// The position of __TOC__ needs to be recorded
$mw = MagicWord::get( 'toc' );
if( $mw->match( $text ) ) {
$this->mOutput->setIndexPolicy( 'index' );
$this->addTrackingCategory( 'index-category' );
}
+
wfProfileOut( __METHOD__ );
return $text;
- }
-
+ }
+
/**
* Add a tracking category, getting the title from a system message,
* or print a debug message if the title is invalid.
*/
protected function addTrackingCategory( $msg ){
$cat = wfMsgForContent( $msg );
-
+
# Allow tracking categories to be disabled by setting them to "-"
if( $cat === '-' ) return false;
-
+
$containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
if ( $containerCategory ) {
$this->mOutput->addCategory( $containerCategory->getDBkey(), $this->getDefaultSort() );
* @private
*/
function formatHeadings( $text, $origText, $isMain=true ) {
- global $wgMaxTocLevel, $wgContLang, $wgEnforceHtmlIds;
+ global $wgMaxTocLevel, $wgContLang, $wgHtml5, $wgExperimentalHtmlIds;
$doNumberHeadings = $this->mOptions->getNumberHeadings();
$showEditLink = $this->mOptions->getEditSection();
# Save headline for section edit hint before it's escaped
$headlineHint = $safeHeadline;
- if ( $wgEnforceHtmlIds ) {
- $legacyHeadline = false;
- $safeHeadline = Sanitizer::escapeId( $safeHeadline,
- 'noninitial' );
- } else {
+ if ( $wgHtml5 && $wgExperimentalHtmlIds ) {
# For reverse compatibility, provide an id that's
# HTML4-compatible, like we used to.
#
# to type in section names like "abc_.D7.93.D7.90.D7.A4"
# manually, so let's not bother worrying about it.
$legacyHeadline = Sanitizer::escapeId( $safeHeadline,
- 'noninitial' );
- $safeHeadline = Sanitizer::escapeId( $safeHeadline, 'xml' );
+ array( 'noninitial', 'legacy' ) );
+ $safeHeadline = Sanitizer::escapeId( $safeHeadline );
if ( $legacyHeadline == $safeHeadline ) {
# No reason to have both (in fact, we can't)
$legacyHeadline = false;
- } elseif ( $legacyHeadline != Sanitizer::escapeId(
- $legacyHeadline, 'xml' ) ) {
- # The legacy id is invalid XML. We used to allow this, but
- # there's no reason to do so anymore. Backward
- # compatibility will fail slightly in this case, but it's
- # no big deal.
- $legacyHeadline = false;
}
+ } else {
+ $legacyHeadline = false;
+ $safeHeadline = Sanitizer::escapeId( $safeHeadline,
+ 'noninitial' );
}
# HTML names must be case-insensitively unique (bug 10721). FIXME:
# Don't number the heading if it is the only one (looks silly)
if( $doNumberHeadings && count( $matches[3] ) > 1) {
# the two are different if the line contains a link
- $headline=$numbering . ' ' . $headline;
+ $headline = $numbering . ' ' . $headline;
}
# Create the anchor for linking from the TOC to the section
$toc .= $sk->tocLine($anchor, $tocline,
$numbering, $toclevel, ($isTemplate ? false : $sectionIndex));
}
-
+
# Add the section to the section tree
# Find the DOM node for this header
while ( $node && !$isTemplate ) {
if ( $bits['i'] == $sectionIndex )
break;
}
- $byteOffset += mb_strlen( $this->mStripState->unstripBoth(
+ $byteOffset += mb_strlen( $this->mStripState->unstripBoth(
$frame->expand( $node, PPFrame::RECOVER_ORIG ) ) );
$node = $node->getNextSibling();
}
- $tocraw[] = array(
+ $tocraw[] = array(
'toclevel' => $toclevel,
'level' => $level,
'line' => $tocline,
'byteoffset' => ( $isTemplate ? null : $byteOffset ),
'anchor' => $anchor,
);
-
+
# give headline the correct <h#> tag
if( $showEditLink && $sectionIndex !== false ) {
if( $isTemplate ) {
$toc = $sk->tocList( $toc );
$this->mOutput->setTOCHTML( $toc );
}
-
+
if ( $isMain ) {
$this->mOutput->setSections( $tocraw );
}
$blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
$i = 0;
-
+
foreach( $blocks as $block ) {
if( $showEditLink && $headlineCount > 0 && $i == 0 && $block !== "\n" ) {
# This is the [edit] link that appears for the top block of text when
return $full;
}
}
-
+
/**
* Merge $tree2 into $tree1 by replacing the section with index
* $section in $tree1 and its descendants with the sections in $tree2.
$numbering = array( 0 );
$titletext = $title->getPrefixedDBkey();
foreach ( $tree1 as $s ) {
- if ( $targetLevel !== false ) {
+ if ( $targetLevel !== false ) {
if ( $s['level'] <= $targetLevel )
// We've skipped enough
$targetLevel = false;
$s['fromtitle'] != $titletext ) {
self::incrementNumbering( $numbering,
$s['toclevel'], $lastLevel );
-
+
// Rewrite index, byteoffset and number
if ( $s['fromtitle'] == $titletext ) {
$s['index'] = $nextIndex++;
$s2['level'] += $s['level'] - 1;
$s2['index'] = $nextIndex++;
$s2['byteoffset'] += $s['byteoffset'];
-
+
self::incrementNumbering( $numbering,
$s2['toclevel'], $lastLevel );
$s2['number'] = implode( '.', array_map(
}
return $newTree;
}
-
+
/**
* Increment a section number. Helper function for mergeSectionTrees()
* @param $number array Array representing a section number
} else
$number[$level - 1]++;
}
-
+
/**
* Transform wiki markup when saving a page by doing \r\n -> \n
* conversion, substitting signatures, {{subst:}} templates, etc.
* (see also bug 12815)
*/
$ts = $this->mOptions->getTimestamp();
- $tz = wfMsgForContent( 'timezone-utc' );
if ( isset( $wgLocaltimezone ) ) {
- $unixts = wfTimestamp( TS_UNIX, $ts );
- $oldtz = getenv( 'TZ' );
- putenv( 'TZ='.$wgLocaltimezone );
- $ts = date( 'YmdHis', $unixts );
- $tz = date( 'T', $unixts ); # might vary on DST changeover!
+ $tz = $wgLocaltimezone;
+ } else {
+ $tz = date_default_timezone_get();
+ }
- /* Allow translation of timezones trough wiki. date() can return
- * whatever crap the system uses, localised or not, so we cannot
- * ship premade translations.
- */
- $key = 'timezone-' . strtolower( trim( $tz ) );
- $value = wfMsgForContent( $key );
- if ( !wfEmptyMsg( $key, $value ) ) $tz = $value;
+ $unixts = wfTimestamp( TS_UNIX, $ts );
+ $oldtz = date_default_timezone_get();
+ date_default_timezone_set( $tz );
+ $ts = date( 'YmdHis', $unixts );
+ $tzMsg = date( 'T', $unixts ); # might vary on DST changeover!
- putenv( 'TZ='.$oldtz );
- }
+ /* Allow translation of timezones trough wiki. date() can return
+ * whatever crap the system uses, localised or not, so we cannot
+ * ship premade translations.
+ */
+ $key = 'timezone-' . strtolower( trim( $tzMsg ) );
+ $value = wfMsgForContent( $key );
+ if ( !wfEmptyMsg( $key, $value ) ) $tzMsg = $value;
- $d = $wgContLang->timeanddate( $ts, false, false ) . " ($tz)";
+ date_default_timezone_set( $oldtz );
+
+ $d = $wgContLang->timeanddate( $ts, false, false ) . " ($tzMsg)";
# Variable replacement
# Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
'~~~' => $sigText
) );
- # Context links: [[|name]] and [[name (context)|]]
- #
+ # Links of the form [[|<blah>]] or [[<blah>|]] perform pipe tricks
+ # Note this only allows the # in the position it works.
global $wgLegalTitleChars;
- $tc = "[$wgLegalTitleChars]";
- $nc = '[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can use non-ascii!
-
- $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\))\\|]]/"; # [[ns:page (context)|]]
- $p4 = "/\[\[(:?$nc+:|:|)($tc+?)(($tc+))\\|]]/"; # [[ns:page(context)|]]
- $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\)|)(, $tc+|)\\|]]/"; # [[ns:page (context), context|]]
- $p2 = "/\[\[\\|($tc+)]]/"; # [[|page]]
-
- # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
- $text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text );
- $text = preg_replace( $p4, '[[\\1\\2\\3|\\2]]', $text );
- $text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text );
-
- $t = $this->mTitle->getText();
- $m = array();
- if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) {
- $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
- } elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && '' != "$m[1]$m[2]" ) {
- $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
- } else {
- # if there's no context, don't bother duplicating the title
- $text = preg_replace( $p2, '[[\\1]]', $text );
- }
+ $pipeTrickRe = "/\[\[(?:(\\|)([$wgLegalTitleChars]+)|([#$wgLegalTitleChars]+)\\|)\]\]/";
+ $text = preg_replace_callback( $pipeTrickRe, array( $this, 'pstPipeTrickCallback' ), $text );
# Trim trailing whitespace
$text = rtrim( $text );
return $text;
}
+ /**
+ * Called from pstPass2 to perform the pipe trick on links.
+ * Original was either [[|text]] or [[link|]]
+ *
+ * @param Array ("|" or "", text, link) $m
+ */
+ function pstPipeTrickCallback( $m )
+ {
+ if( $m[1] ) { # [[|<blah>]]
+ $text = $m[2];
+ $link = $this->getPipeTrickLink( $text );
+ } else { # [[<blah>|]]
+ $link = $m[3];
+ $text = $this->getPipeTrickText( $link );
+ }
+
+ return $link === $text ? "[[$link]]" : "[[$link|$text]]";
+ }
+
/**
* Fetch the user's signature text, if any, and normalize to
* validated, ready-to-insert wikitext.
global $wgMaxSigChars;
$username = $user->getName();
-
+
// If not given, retrieve from the user object.
if ( $nickname === false )
$nickname = $user->getOption( 'nickname' );
-
+
if ( is_null( $fancySig ) )
$fancySig = $user->getBoolOption( 'fancysig' );
-
+
$nickname = $nickname == null ? $username : $nickname;
if( mb_strlen( $nickname ) > $wgMaxSigChars ) {
return $oldVal;
}
+ /* An old work-around for bug 2257 - deprecated 2010-02-13 */
function setTransparentTagHook( $tag, $callback ) {
+ wfDeprecated( __METHOD__ );
$tag = strtolower( $tag );
$oldVal = isset( $this->mTransparentTagHooks[$tag] ) ? $this->mTransparentTagHooks[$tag] : null;
$this->mTransparentTagHooks[$tag] = $callback;
* @param integer $flags a combination of the following flags:
* SFH_NO_HASH No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
*
- * SFH_OBJECT_ARGS Pass the template arguments as PPNode objects instead of text. This
+ * SFH_OBJECT_ARGS Pass the template arguments as PPNode objects instead of text. This
* allows for conditional expansion of the parse tree, allowing you to eliminate dead
- * branches and thus speed up parsing. It is also possible to analyse the parse tree of
+ * branches and thus speed up parsing. It is also possible to analyse the parse tree of
* the arguments, and to control the way they are expanded.
*
* The $frame parameter is a PPFrame. This can be used to produce expanded text from the
* arguments, for instance:
* $text = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
*
- * For technical reasons, $args[0] is pre-expanded and will be a string. This may change in
+ * For technical reasons, $args[0] is pre-expanded and will be a string. This may change in
* future versions. Please call $frame->expand() on it anyway so that your code keeps
* working if/when this is changed.
*
* If you want whitespace to be trimmed from $args, you need to do it yourself, post-
* expansion.
*
- * Please read the documentation in includes/parser/Preprocessor.php for more information
+ * Please read the documentation in includes/parser/Preprocessor.php for more information
* about the methods available in PPFrame and PPNode.
*
* @return The old callback function for this name, if any
return $this->mLinkHolders->replaceText( $text );
}
- /**
- * Tag hook handler for 'pre'.
- */
- function renderPreTag( $text, $attribs ) {
- // Backwards-compatibility hack
- $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
-
- $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
- return Xml::openElement( 'pre', $attribs ) .
- Xml::escapeTagsOnly( $content ) .
- '</pre>';
- }
-
- /**
- * Tag hook handler for 'a'. Renders a HTML <a> tag, allowing most attributes, filtering href against
- * allowed protocols and spam blacklist.
- **/
- function renderHyperlink( $text, $params, $frame = false ) {
- foreach ( $params as $name => $value ) {
- $params[ $name ] = $this->replaceVariables( $value, $frame );
- }
-
- $whitelist = Sanitizer::attributeWhitelist( 'a' );
- $params = Sanitizer::validateAttributes( $params, $whitelist );
-
- $content = $this->recursiveTagParse( trim( $text ), $frame );
-
- if ( isset( $params[ 'href' ] ) ) {
- $href = $params[ 'href' ];
- $this->mOutput->addExternalLink( $href );
- unset( $params[ 'href' ] );
- } else {
- # Non-link <a> tag
- return Xml::openElement( 'a', $params ) . $content . Xml::closeElement( 'a' );
- }
-
- $sk = $this->mOptions->getSkin();
- $html = $sk->makeExternalLink( $href, $content, false, '', $params );
-
- return $html;
- }
-
/**
* Renders an image gallery from a text with one line per image.
* text labels may be given by using |-style alternative text. E.g.
if ( count( $matches ) == 0 ) {
continue;
}
-
+
if ( strpos( $matches[0], '%' ) !== false )
$matches[1] = urldecode( $matches[1] );
$tp = Title::newFromText( $matches[1]/*, NS_FILE*/ );
# * upright reduce width for upright images, rounded to full __0 px
# * border draw a 1px border around the image
# * alt Text for HTML alt attribute (defaults to empty)
- # * link Set the target of the image link. Can be external, interwiki, or local
+ # * link Set the target of the image link. Can be external, interwiki, or local
# vertical-align values (no % or length right now):
# * baseline
# * sub
return $ret;
}
-
+
protected function stripAltText( $caption, $holders ) {
# Strip bad stuff out of the title (tooltip). We can't just use
# replaceLinkHoldersText() here, because if this function is called
# remove the tags
$tooltip = $this->mStripState->unstripBoth( $tooltip );
$tooltip = Sanitizer::stripAllTags( $tooltip );
-
+
return $tooltip;
}
$links['interwiki'][] = $this->mLinkHolders->interwiki[$key];
$pos = $start_pos + strlen( "<!--IWLINK $key-->" );
}
-
+
$data['linkholder'] = $links;
return $data;
function unserialiseHalfParsedText( $data, $intPrefix = null /* Unique identifying prefix */ ) {
if (!$intPrefix)
$intPrefix = $this->getRandomString();
-
+
// First, extract the strip state.
$stripState = $data['stripstate'];
$this->mStripState->general->merge( $stripState->general );