r62505 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62504‎ | r62505 | r62506 >
Date:09:34, 15 February 2010
Author:conrad
Status:resolved (Comments)
Tags:
Comment:
clean r61713 (and r61710) per code review
Modified paths:
  • /trunk/phase3/includes/MagicWord.php (modified) (history)
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/parser/Parser.php
@@ -94,7 +94,7 @@
9595 */
9696 # Persistent:
9797 var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables,
98 - $mSubsts, $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex,
 98+ $mSubstWords, $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex,
9999 $mPreprocessor, $mExtLinkBracketedRegex, $mUrlProtocols, $mDefaultStripList,
100100 $mVarCache, $mConf, $mFunctionTagHooks;
101101
@@ -2690,7 +2690,7 @@
26912691 $substIDs = MagicWord::getSubstIDs();
26922692
26932693 $this->mVariables = new MagicWordArray( $variableIDs );
2694 - $this->mSubsts = new MagicWordArray( $substIDs );
 2694+ $this->mSubstWords = new MagicWordArray( $substIDs );
26952695 wfProfileOut( __METHOD__ );
26962696 }
26972697
@@ -2862,14 +2862,25 @@
28632863 wfProfileIn( __METHOD__.'-modifiers' );
28642864 if ( !$found ) {
28652865
2866 - $substMatch = $this->mSubsts->matchStartAndRemove( $part1 );
 2866+ $substMatch = $this->mSubstWords->matchStartAndRemove( $part1 );
28672867
28682868 # Possibilities for substMatch: "subst", "safesubst" or FALSE
2869 - # Whether to include depends also on whether we are in the pre-save-transform
2870 - #
2871 - # safesubst || (subst && PST) || (false && !PST) => transclude (skip the if)
2872 - # (false && PST) || (subst && !PST) => return input (handled by if)
2873 - if ( $substMatch != 'safesubst' && ($substMatch == 'subst' xor $this->ot['wiki']) ) {
 2869+ # Decide whether to expand template or keep wikitext as-is.
 2870+ if ( $this->ot['wiki'] )
 2871+ {
 2872+ if ( $substMatch === false ) {
 2873+ $literal = true; # literal when in PST with no prefix
 2874+ } else {
 2875+ $literal = false; # expand when in PST with subst: or safesubst:
 2876+ }
 2877+ } else {
 2878+ if ( $substMatch == 'subst' ) {
 2879+ $literal = true; # literal when not in PST with plain subst:
 2880+ } else {
 2881+ $literal = false; # expand when not in PST with safesubst: or no prefix
 2882+ }
 2883+ }
 2884+ if ( $literal ) {
28742885 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
28752886 $isLocalObj = true;
28762887 $found = true;
Index: trunk/phase3/includes/MagicWord.php
@@ -572,7 +572,7 @@
573573 }
574574
575575 /**
576 - * Get an unanchored regex
 576+ * Get an unanchored regex that does not match parameters
577577 */
578578 function getRegex() {
579579 if ( is_null( $this->regex ) ) {
@@ -589,29 +589,29 @@
590590 }
591591
592592 /**
593 - * Get a regex for matching variables
 593+ * Get a regex for matching variables with parameters
594594 */
595595 function getVariableRegex() {
596596 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
597597 }
598598
599599 /**
600 - * Get a regex for matching a prefix. Does not match parameters.
 600+ * Get a regex anchored to the start of the string that does not match parameters
601601 */
602602 function getRegexStart() {
603603 $base = $this->getBaseRegex();
604604 $newRegex = array( '', '' );
605605 if ( $base[0] !== '' ) {
606 - $newRegex[0] = str_replace( "\\$1", "", "/^(?:{$base[0]})/iuS" );
 606+ $newRegex[0] = "/^(?:{$base[0]})/iuS";
607607 }
608608 if ( $base[1] !== '' ) {
609 - $newRegex[1] = str_replace( "\\$1", "", "/^(?:{$base[1]})/S" );
 609+ $newRegex[1] = "/^(?:{$base[1]})/S";
610610 }
611611 return $newRegex;
612612 }
613613
614614 /**
615 - * Get an anchored regex for matching variables
 615+ * Get an anchored regex for matching variables with parameters
616616 */
617617 function getVariableStartToEndRegex() {
618618 $base = $this->getBaseRegex();
@@ -710,22 +710,24 @@
711711 }
712712
713713 /**
714 - * Returns the magic word id removed from the start, or false
715 - * does not match parameters.
 714+ * Return the ID of the magic word at the start of $text, and remove
 715+ * the prefix from $text.
 716+ * Return false if no match found and $text is not modified.
 717+ * Does not match parameters.
716718 */
717719 public function matchStartAndRemove( &$text ) {
718 - $found = FALSE;
719720 $regexes = $this->getRegexStart();
720721 foreach ( $regexes as $regex ) {
721722 if ( $regex === '' ) {
722723 continue;
723724 }
724 - preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
725 - foreach ( $matches as $m ) {
726 - list( $found, $param ) = $this->parseMatch( $m );
 725+ preg_match( $regex, $text, $match );
 726+ if ( $match ) {
 727+ list( $found, $param ) = $this->parseMatch( $match );
 728+ $text = substr( $text, strlen( $found ) + 1 );
 729+ return $found;
727730 }
728 - $text = preg_replace( $regex, '', $text );
729731 }
730 - return $found;
 732+ return false;
731733 }
732734 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r62814Fixes for r62505:...tstarling07:02, 22 February 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r61710bug 22297 - "syntax for substitution that doesn't break transclusion"...conrad11:58, 30 January 2010
r61713Fix for r61710. Changing subst: to subst:$1 would cause huge problems with lo...conrad12:46, 30 January 2010

Comments

#Comment by Tim Starling (talk | contribs)   06:49, 22 February 2010
if ( $this->ot['wiki'] )
{

Incorrect brace style, see Manual:Coding conventions

preg_match( $regex, $text, $match );
if ( $match ) {

Very unusual way to detect whether preg_match() succeeded or not. You should have used:

if ( preg_match( $regex, $text, $match ) ) {
$text = substr( $text, strlen( $found ) + 1 );

This is completely wrong and broken. $found here is the internal magic word ID, it's only related to the matched string due to convention, and even then, only in English. We already have translated versions of safesubst in Hebrew and Ukrainian. You should have used:

$text = substr( $text, strlen( $match[0] ) );

I'm doing it.

Status & tagging log