Nikerabbit has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/91583


Change subject: Add $wgTranslatePageTranslationUseParserHook
......................................................................

Add $wgTranslatePageTranslationUseParserHook

Also includes small caching purging fix

Change-Id: Iaeca61faf42cd99c4df8c533a144f701534300e2
---
M Translate.php
M TranslateHooks.php
M tag/PageTranslationHooks.php
M tag/TPSection.php
M tag/TranslatablePage.php
A tests/phpunit/TPSectionTest.php
6 files changed, 102 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate 
refs/changes/83/91583/1

diff --git a/Translate.php b/Translate.php
index dc31c09..e2c36e1 100644
--- a/Translate.php
+++ b/Translate.php
@@ -388,6 +388,13 @@
  */
 $wgTranslatePageTranslationULS = false;
 
+/**
+ * Temporary configuration variable to switch between new and old way of
+ * processing translatable pages internally.
+ * @see https://bugzilla.wikimedia.org/48891
+ */
+$wgTranslatePageTranslationUseParserHook = false;
+
 # </source>
 # === Message group configuration ===
 # <source lang=php>
diff --git a/TranslateHooks.php b/TranslateHooks.php
index 851d381..7d360d4 100644
--- a/TranslateHooks.php
+++ b/TranslateHooks.php
@@ -183,6 +183,23 @@
                        array( 'TranslateHooks', 'translationDialogMagicWord' )
                );
 
+               global $wgTranslatePageTranslationUseParserHook;
+               if ( !$wgTranslatePageTranslationUseParserHook ) {
+                       return true;
+               }
+
+               $parser->setHook( 'translate', function ( $input, $params, 
$parser, $frame ) {
+                       // Replace variable syntax with actual value
+                       $re = '~<tvar\|([^>]+)>(.*?)</>~u';
+                       $output = preg_replace( $re, '\2', $input );
+
+                       // Remove markers to avoid whitespace buildup.
+                       $output = TPSection::removeMarkers( $output );
+
+                       $output = $parser->recursiveTagParse( $output, $frame );
+                       return $output;
+               } );
+
                return true;
        }
 
diff --git a/tag/PageTranslationHooks.php b/tag/PageTranslationHooks.php
index 0125f68..028bcc2 100644
--- a/tag/PageTranslationHooks.php
+++ b/tag/PageTranslationHooks.php
@@ -25,9 +25,13 @@
         * @return bool
         */
        public static function renderTagPage( $parser, &$text, $state ) {
+               global $wgTranslatePageTranslationUseParserHook;
+
                $title = $parser->getTitle();
 
-               if ( strpos( $text, '<translate>' ) !== false ) {
+               if ( !$wgTranslatePageTranslationUseParserHook
+                       && strpos( $text, '<translate>' ) !== false
+               ) {
                        try {
                                $parse = TranslatablePage::newFromText( 
$parser->getTitle(), $text )->getParse();
                                $text = $parse->getTranslationPageText( null );
@@ -51,7 +55,8 @@
                        $parser->getOutput()->setDisplayTitle( $name );
                }
 
-               // Disable edit section links
+               // Disable edit section links.
+               // This doesn't work with parser hooks!
                $parser->getOptions()->setEditSection( false );
 
                return true;
@@ -456,6 +461,7 @@
                // Add the ready tag
                $page = TranslatablePage::newFromTitle( $wikiPage->getTitle() );
                $page->addReadyTag( $revision->getId() );
+               TranslatablePage::isSourcePage( $wikiPage->getTitle(), 'clear' 
);
 
                return true;
        }
diff --git a/tag/TPSection.php b/tag/TPSection.php
index 5e564db..7f05658 100644
--- a/tag/TPSection.php
+++ b/tag/TPSection.php
@@ -88,4 +88,19 @@
 
                return $vars;
        }
+
+       /**
+        * Removes section markers and whitespace surrounding them from the 
text.
+        * @param string $content
+        * @return string
+        */
+       public static function removeMarkers( $content ) {
+               // Currently handle only these two standard places.
+               // Is this too strict?
+               $rer1 = '~^<!--T:(.*?)-->\n~m'; // Normal sections
+               $rer2 = '~\s*<!--T:(.*?)-->$~m'; // Sections with title
+               $content = preg_replace( $rer1, '', $content );
+               $content = preg_replace( $rer2, '', $content );
+               return $content;
+       }
 }
diff --git a/tag/TranslatablePage.php b/tag/TranslatablePage.php
index c612c7f..f97ff15 100644
--- a/tag/TranslatablePage.php
+++ b/tag/TranslatablePage.php
@@ -410,12 +410,7 @@
                                list( /*full*/, $id ) = $match;
                                $section->id = $id;
 
-                               // Currently handle only these two standard 
places.
-                               // Is this too strict?
-                               $rer1 = '~^<!--T:(.*?)-->\n~'; // Normal 
sections
-                               $rer2 = '~\s*<!--T:(.*?)-->$~m'; // Sections 
with title
-                               $content = preg_replace( $rer1, '', $content );
-                               $content = preg_replace( $rer2, '', $content );
+                               $content = TPSection::removeMarkers( $content );
 
                                if ( preg_match( $re, $content ) === 1 ) {
                                        throw new TPException( array( 
'pt-shake-position', $content ) );
@@ -795,18 +790,19 @@
 
        /**
         * @param Title $title
+        * @param bool $clear Whether to clear cache
         * @return bool
         */
-       public static function isSourcePage( Title $title ) {
+       public static function isSourcePage( Title $title, $clear = false ) {
                static $cache = null;
 
                $cacheObj = wfGetCache( CACHE_ANYTHING );
                $cacheKey = wfMemcKey( 'pagetranslation', 'sourcepages' );
 
-               if ( $cache === null ) {
+               if ( !$clear && $cache === null ) {
                        $cache = $cacheObj->get( $cacheKey );
                }
-               if ( !is_array( $cache ) ) {
+               if ( $clear || !is_array( $cache ) ) {
                        $cache = self::getTranslatablePages();
                        $cacheObj->set( $cacheKey, $cache, 60 * 5 );
                }
diff --git a/tests/phpunit/TPSectionTest.php b/tests/phpunit/TPSectionTest.php
new file mode 100644
index 0000000..0658f20
--- /dev/null
+++ b/tests/phpunit/TPSectionTest.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Unit tests for class TPSection
+ *
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ * @file
+ */
+
+/**
+ * Unit tests for class TPSection
+ * @ingroup PageTranslation
+ */
+class TPSectionTest extends MediaWikiTestCase {
+
+       /**
+        * @dataProvider removeMarkersProvider
+        * @covers TPSection::removeMarkers
+        */
+       public function testRemoveMarkers( $input, $expected ) {
+               $output = TPSection::removeMarkers( $input );
+               $this->assertSame( $expected, $output );
+       }
+
+       public function removeMarkersProvider() {
+               $testCases = array();
+
+               $testCases[] = array(
+                       "\n== A == <!--T:42-->\n",
+                       "\n== A ==\n",
+               );
+
+               $testCases[] = array(
+                       "\n== A ==  <!--T:38-->\n\n<!--T:41-->\n* B",
+                       "\n== A ==\n\n* B",
+               );
+
+               $testCases[] = array(
+                       "\n<!--T:666-->\nC\n",
+                       "\nC\n",
+               );
+
+               $testCases[] = array(
+                       "<!--T:1-->\nD",
+                       "D",
+               );
+
+               return $testCases;
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/91583
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaeca61faf42cd99c4df8c533a144f701534300e2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to