Cenarium has uploaded a new change for review.

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

Change subject: Pre-save transform inside extension tags
......................................................................

Pre-save transform inside extension tags

This adds an option for extension tags to be run through the pre-
save transform. Tags with this option enabled are not stripped by
the preprocessor during pre-save transform and thus their content
is transformed. This requires spliting the preprocessor cache.

This patch is based on one provided by bluehairedlawyer on bugzilla,
later uploaded on gerrit as I1cb6750dd5ba9db9fbcccf74f8987e80d011210d
and updated by Dan Collins. It incorporates improvements suggested by
reviewers there.

Bug: T4700
Change-Id: Ic75084b873f0936a84ee80c1d134c54a14d79406
---
M includes/parser/CoreTagHooks.php
M includes/parser/Parser.php
M tests/parser/parserTests.txt
3 files changed, 88 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/16/272916/1

diff --git a/includes/parser/CoreTagHooks.php b/includes/parser/CoreTagHooks.php
index d4c4f6d..c72630d 100644
--- a/includes/parser/CoreTagHooks.php
+++ b/includes/parser/CoreTagHooks.php
@@ -34,7 +34,7 @@
                global $wgRawHtml;
                $parser->setHook( 'pre', [ __CLASS__, 'pre' ] );
                $parser->setHook( 'nowiki', [ __CLASS__, 'nowiki' ] );
-               $parser->setHook( 'gallery', [ __CLASS__, 'gallery' ] );
+               $parser->setHook( 'gallery', [ __CLASS__, 'gallery' ] , [ 
'preSaveTrans' => true ] );
                $parser->setHook( 'indicator', [ __CLASS__, 'indicator' ] );
                if ( $wgRawHtml ) {
                        $parser->setHook( 'html', [ __CLASS__, 'html' ] );
diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php
index 477d1f7..13b82ac 100644
--- a/includes/parser/Parser.php
+++ b/includes/parser/Parser.php
@@ -111,6 +111,7 @@
 
        # Flags for preprocessToDom
        const PTD_FOR_INCLUSION = 1;
+       const PTD_FOR_PSTETAGS = 2;
 
        # Allowed values for $this->mOutputType
        # Parameter to startExternalParse().
@@ -152,6 +153,7 @@
        public $mImageParamsMagicArray = [];
        public $mMarkerIndex = 0;
        public $mFirstCall = true;
+       private $preSaveTransformableExtensionTags = [];
 
        # Initialised by initialiseVariables()
 
@@ -990,7 +992,16 @@
         * @return array
         */
        public function getStripList() {
-               return $this->mStripList;
+               $res = $this->mStripList;
+               if ( $this->ot['wiki'] && count( 
$this->preSaveTransformableExtensionTags ) ) {
+                       // If PST-able extension tags are defined, don't strip 
them
+                       foreach ( $this->mStripList as $key => $tag ) {
+                               if ( isset( 
$this->preSaveTransformableExtensionTags[$tag] ) ) {
+                                       unset( $res[$key] );
+                               }
+                       }
+               }
+               return $res;
        }
 
        /**
@@ -3346,7 +3357,14 @@
                        $frame = $this->getPreprocessor()->newCustomFrame( 
$frame );
                }
 
-               $dom = $this->preprocessToDom( $text );
+               if ( $this->ot['wiki'] && count( 
$this->preSaveTransformableExtensionTags ) ) {
+                       // when pre-save transformable tags are defined, add 
flag to avoid breaking
+                       // the preprocessor cache (since $this->getStripList() 
is altered)
+                       $dom = $this->preprocessToDom( $text, 
self::PTD_FOR_PSTETAGS );
+               } else {
+                       $dom = $this->preprocessToDom( $text );
+               }
+
                $flags = $argsOnly ? PPFrame::NO_TEMPLATES : 0;
                $text = $frame->expand( $dom, $flags );
 
@@ -5112,18 +5130,26 @@
         *
         * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
         * @param callable $callback The callback function (and object) to use 
for the tag
+        * @param array $options Options to use, such as 'preSaveTrans' to 
perform pre-save
+        * transformations on the entire content inside the tag
         * @throws MWException
         * @return callable|null The old value of the mTagHooks array 
associated with the hook
         */
-       public function setHook( $tag, $callback ) {
+       public function setHook( $tag, $callback, $options = [] ) {
                $tag = strtolower( $tag );
                if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
                        throw new MWException( "Invalid character {$m[0]} in 
setHook('$tag', ...) call" );
                }
                $oldVal = isset( $this->mTagHooks[$tag] ) ? 
$this->mTagHooks[$tag] : null;
                $this->mTagHooks[$tag] = $callback;
+
                if ( !in_array( $tag, $this->mStripList ) ) {
                        $this->mStripList[] = $tag;
+                       if ( isset( $options['preSaveTrans'] ) && 
$options['preSaveTrans'] === true ) {
+                               if ( !isset( 
$this->preSaveTransformableExtensionTags[$tag] ) ) {
+                                       
$this->preSaveTransformableExtensionTags[$tag] = '';
+                               }
+                       }
                }
 
                return $oldVal;
@@ -5164,6 +5190,7 @@
                $this->mTagHooks = [];
                $this->mFunctionTagHooks = [];
                $this->mStripList = $this->mDefaultStripList;
+               $this->preSaveTransformableExtensionTags = [];
        }
 
        /**
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index 9ad7bd5..9b9ce89 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -26761,3 +26761,60 @@
 <li>b</li>
 </ul>
 !! end
+
+Pipe trick in gallery
+!! options
+pst
+!! input
+<gallery>
+image1.png |
+image2.gif|||||
+
+image3|
+image4    |300px| centre
+ image5.svg| http://///////
+[[x (y)|]]]]
+* image6
+</gallery>
+!! result
+<gallery>
+image1.png |
+image2.gif|||||
+
+image3|
+image4    |300px| centre
+ image5.svg| http://///////
+[[x (y)|x]]]]
+* image6
+</gallery>
+!! end
+
+!! test
+Pipe trick in gallery (with options)
+!! options
+pst
+!! input
+<gallery widths='70px' heights='40px' perrow='2' caption='Foo [[Main Page 
(yes)|]]' >
+File:Nonexistant.jpg|caption
+File:Nonexistant.jpg
+image:foobar.jpg|some '''caption''' [[Main Page (not)|]]
+image:foobar.jpg
+image:foobar.jpg|Blabla|alt=This is a foo-bar.|blabla.
+</gallery>
+!! result
+<gallery widths='70px' heights='40px' perrow='2' caption='Foo [[Main Page 
(yes)|Main Page]]' >
+File:Nonexistant.jpg|caption
+File:Nonexistant.jpg
+image:foobar.jpg|some '''caption''' [[Main Page (not)|Main Page]]
+image:foobar.jpg
+image:foobar.jpg|Blabla|alt=This is a foo-bar.|blabla.
+</gallery>
+!! end
+
+TODO:
+more images
+more tables
+math
+character entities
+and much more
+Try for 100% code coverage

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic75084b873f0936a84ee80c1d134c54a14d79406
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Cenarium <cenarium.sy...@gmail.com>
Gerrit-Reviewer: Dan Collins <dcolli...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to