r66833 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66832‎ | r66833 | r66834 >
Date:16:23, 24 May 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Handle <nowiki> as good as possible
Modified paths:
  • /trunk/extensions/Translate/tag/PageTranslationHooks.php (modified) (history)
  • /trunk/extensions/Translate/tag/TPParse.php (modified) (history)
  • /trunk/extensions/Translate/tag/TranslatablePage.php (modified) (history)
  • /trunk/extensions/Translate/tests/pagetranslation/FailDoubleOpen.ptfile (added) (history)
  • /trunk/extensions/Translate/tests/pagetranslation/Nowiki.ptfile (added) (history)
  • /trunk/extensions/Translate/tests/pagetranslation/Nowiki.pttarget (added) (history)
  • /trunk/extensions/Translate/tests/pagetranslation/NowikiInside.ptfile (added) (history)
  • /trunk/extensions/Translate/tests/pagetranslation/NowikiInside.pttarget (added) (history)
  • /trunk/extensions/Translate/tests/pagetranslation/NowikiOnly.ptfile (added) (history)
  • /trunk/extensions/Translate/tests/pagetranslation/NowikiOnly.pttest (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/tests/pagetranslation/Nowiki.ptfile
@@ -0,0 +1,3 @@
 2+I just want to speak about <nowiki><translate></nowiki>.
 3+
 4+<translate>Yes!</translate>
Index: trunk/extensions/Translate/tests/pagetranslation/NowikiOnly.ptfile
@@ -0,0 +1 @@
 2+<nowiki><translate></nowiki>
\ No newline at end of file
Index: trunk/extensions/Translate/tests/pagetranslation/NowikiOnly.pttest
@@ -0,0 +1,7 @@
 2+<?php
 3+
 4+if ( !defined( 'MEDIAWIKI' ) ) die();
 5+
 6+if ( $parse->countSections() !== 0 ) {
 7+ $this->output( "Testfile $filename failed misc tests... number of sections should be zero" );
 8+}
\ No newline at end of file
Index: trunk/extensions/Translate/tests/pagetranslation/NowikiInside.pttarget
@@ -0,0 +1,3 @@
 2+I just want to speak about <nowiki><translate></nowiki>.
 3+
 4+Yes!
Index: trunk/extensions/Translate/tests/pagetranslation/Nowiki.pttarget
@@ -0,0 +1,3 @@
 2+I just want to speak about <nowiki><translate></nowiki>.
 3+
 4+Yes!
Index: trunk/extensions/Translate/tests/pagetranslation/FailDoubleOpen.ptfile
@@ -0,0 +1,3 @@
 2+<translate>
 3+<translate>
 4+</translate>
\ No newline at end of file
Index: trunk/extensions/Translate/tests/pagetranslation/NowikiInside.ptfile
@@ -0,0 +1,4 @@
 2+<translate>
 3+I just want to speak about <nowiki><translate></nowiki>.
 4+
 5+Yes!</translate>
Index: trunk/extensions/Translate/tag/TranslatablePage.php
@@ -139,6 +139,9 @@
140140
141141 $text = $this->getText();
142142
 143+ $nowiki = array();
 144+ $text = self::armourNowiki( $nowiki, $text );
 145+
143146 $sections = array();
144147 $tagPlaceHolders = array();
145148
@@ -149,7 +152,7 @@
150153 if ( $ok === 0 ) break; // No matches
151154
152155 // Do-placehold for the whole stuff
153 - $ph = $this->getUniq();
 156+ $ph = self::getUniq();
154157 $start = $matches[0][0][1];
155158 $len = strlen( $matches[0][0][0] );
156159 $end = $start + $len;
@@ -168,6 +171,8 @@
169172 throw new TPException( array( 'pt-parse-nested', $sectiontext ) );
170173 }
171174
 175+ $sectiontext = self::unArmourNowiki( $nowiki, $sectiontext );
 176+
172177 $ret = $this->sectionise( $sections, $sectiontext );
173178
174179 $tagPlaceHolders[$ph] =
@@ -188,6 +193,8 @@
189194 $text = str_replace( $ph, $value, $text );
190195 }
191196
 197+ $text = self::unArmourNowiki( $nowiki, $text );
 198+
192199 $parse = new TPParse( $this->getTitle() );
193200 $parse->template = $text;
194201 $parse->sections = $sections;
@@ -200,10 +207,27 @@
201208
202209 // Inner functionality //
203210
 211+ public static function armourNowiki( &$holders, $text ) {
 212+ $re = '~(<nowiki>)(.*?)(</nowiki>)~';
 213+ while ( preg_match( $re, $text, $matches ) ) {
 214+ $ph = self::getUniq();
 215+ $text = str_replace( $matches[0], $ph, $text );
 216+ $holders[$ph] = $matches[0];
 217+ }
 218+ return $text;
 219+ }
 220+
 221+ public static function unArmourNowiki( $holders, $text ) {
 222+ foreach ( $holders as $ph => $value ) {
 223+ $text = str_replace( $ph, $value, $text );
 224+ }
 225+ return $text;
 226+ }
 227+
204228 /**
205229 * Returns a random string that can be used as placeholder.
206230 */
207 - protected function getUniq() {
 231+ protected static function getUniq() {
208232 static $i = 0;
209233 return "\x7fUNIQ" . dechex( mt_rand( 0, 0x7fffffff ) ) . dechex( mt_rand( 0, 0x7fffffff ) ) . '|' . $i++;
210234 }
@@ -229,7 +253,7 @@
230254 if ( trim( $_ ) === '' ) {
231255 $template .= $_;
232256 } else {
233 - $ph = $this->getUniq();
 257+ $ph = self::getUniq();
234258 $sections[$ph] = $this->shakeSection( $_ );
235259 $template .= $ph;
236260 }
Index: trunk/extensions/Translate/tag/TPParse.php
@@ -141,9 +141,13 @@
142142 $text = str_replace( $ph, $sectiontext, $text );
143143 }
144144
 145+ $nph = array();
 146+ $text = TranslatablePage::armourNowiki( $nph, $text );
 147+
145148 // Remove translation markup
146149 $cb = array( __CLASS__, 'replaceTagCb' );
147150 $text = preg_replace_callback( '~(<translate>\n?)(.*?)(\n?</translate>)~s', $cb, $text );
 151+ $text = TranslatablePage::unArmourNowiki( $nph, $text );
148152
149153 return $text;
150154 }
Index: trunk/extensions/Translate/tag/PageTranslationHooks.php
@@ -7,12 +7,15 @@
88
99 public static function renderTagPage( $parser, &$text, $state ) {
1010 $title = $parser->getTitle();
11 - if ( strpos( $text, '</translate>' ) !== false ) {
 11+ if ( strpos( $text, '<translate>' ) !== false ) {
 12+ $nowiki = array();
 13+ $text = TranslatablePage::armourNowiki( $nowiki, $text );
1214 $cb = array( __CLASS__, 'replaceTagCb' );
1315 # Remove the tags nicely, trying to not leave excess whitespace lying around
14 - $text = preg_replace_callback( '~(\n?<translate>\s*?)(.*?)(\s*?</translate>)~s', $cb, $text );
 16+ $text = preg_replace_callback( '~(<translate>)\s*(.*?)(</translate>)~s', $cb, $text );
1517 # Replace variable markers
1618 $text = preg_replace_callback( '~(<tvar[^<>]+>)(.*)(</>)~s', $cb, $text );
 19+ $text = TranslatablePage::unArmourNowiki( $nowiki, $text );
1720 }
1821
1922 // For translation pages, parse plural, grammar etc with correct language
@@ -279,7 +282,9 @@
280283
281284 // Add the ready tag
282285 $page = TranslatablePage::newFromTitle( $article->getTitle() );
283 - $page->addReadyTag( $revision->getId() );
 286+ if ( $page->getParse()->countSections() > 0 ) {
 287+ $page->addReadyTag( $revision->getId() );
 288+ }
284289
285290 return true;
286291 }

Status & tagging log