Legoktm has uploaded a new change for review.

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


Change subject: Re-implement parser function
......................................................................

Re-implement parser function

This gets rid of the MASSMESSAGE_PARSE hack, and makes
the parser function code more flexible.

Change-Id: I1ac472a763352c43d74c8fc96ed9d02095d27548
---
M MassMessage.body.php
M MassMessage.hooks.php
2 files changed, 52 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MassMessage 
refs/changes/57/89357/1

diff --git a/MassMessage.body.php b/MassMessage.body.php
index e2a2636..7a62c01 100644
--- a/MassMessage.body.php
+++ b/MassMessage.body.php
@@ -170,11 +170,10 @@
                }
 
                // Prep the parser
-               if ( !defined( 'MASSMESSAGE_PARSE' ) ) { // Unit tests call 
this function multiple times
-                       define( 'MASSMESSAGE_PARSE', true );
-               }
                $parserOptions = $page->makeParserOptions( $context );
                $parser = new Parser();
+               $parser->firstCallInit(); // So our intial parser function is 
added
+               $parser->setFunctionHook( 'target', 
'MassMessageHooks::storeDataParserFunction' ); // Now overwrite it
 
                // Parse
                $output = $parser->parse( $text, $spamlist, $parserOptions );
@@ -199,7 +198,8 @@
                        '<strong class="error">' .
                        wfMessage( $msg )->params( $param )->plain() .
                        '</strong>',
-                       'noparse' => false
+                       'noparse' => false,
+                       'error' => true,
                );
        }
 }
diff --git a/MassMessage.hooks.php b/MassMessage.hooks.php
index e4f4d82..65b3581 100644
--- a/MassMessage.hooks.php
+++ b/MassMessage.hooks.php
@@ -12,24 +12,19 @@
         * @return bool
         */
        public static function onParserFirstCallInit( Parser &$parser ) {
-               $parser->setFunctionHook( 'target', 
'MassMessageHooks::ParserFunction' );
+               $parser->setFunctionHook( 'target', 
'MassMessageHooks::outputParserFunction' );
                return true;
        }
 
        /**
-        * Parser function for {{#target:User talk:Example|en.wikipedia.org}}
-        * Hostname is optional for local delivery
-        * @param Parser $parser
-        * @param string $site
         * @param string $page
+        * @param string $site
         * @return array
         */
-       public static function ParserFunction( $parser, $page, $site = '' ) {
-               global $wgScript;
+       public static function verifyPFData( $page, $site ) {
+               global $wgServer;
                $data = array( 'site' => $site, 'title' => $page );
                if ( trim( $site ) === '' ) {
-                       // Assume it's a local delivery
-                       global $wgServer;
                        $site = MassMessage::getBaseUrl( $wgServer );
                        $data['site'] = $site;
                        $data['wiki'] = wfWikiID();
@@ -45,23 +40,57 @@
                if ( !isset( $data['wiki'] ) && MassMessage::getDBName( 
$data['site'] ) === null ) {
                        return MassMessage::parserError( 
'massmessage-parse-badurl', $site );
                }
-               // Use a message so wikis can customize the output
-               $msg = wfMessage( 'massmessage-target' )->params( $site, 
$wgScript, $page )->plain();
-               $output = $parser->getOutput();
+               return $data;
+       }
 
-               // Store the data in case we're parsing it manually
-               if ( defined( 'MASSMESSAGE_PARSE' ) ) {
-                       if ( !$output->getProperty( 'massmessage-targets' ) ) {
-                               $output->setProperty( 'massmessage-targets', 
serialize( array( $data ) ) );
-                       } else {
-                               $output->setProperty( 'massmessage-targets' , 
serialize( array_merge( unserialize( $output->getProperty( 
'massmessage-targets' ) ),  array( $data ) ) ) );
-                       }
+       /**
+        * Main parser function for {{#target:User 
talk:Example|en.wikipedia.org}}
+        * Hostname is optional for local delivery
+        * @param Parser $parser
+        * @param string $site
+        * @param string $page
+        * @return array
+        */
+       public static function outputParserFunction( $parser, $page, $site = '' 
) {
+               global $wgScript;
+
+               $data = self::verifyPFData( $page, $site );
+               if ( isset( $data['error'] ) ) {
+                       return $data;
                }
 
+               $site = $data['site'];
+               $page = $data['title'];
+
+               // Use a message so wikis can customize the output
+               $msg = wfMessage( 'massmessage-target' )->params( $site, 
$wgScript, $page )->plain();
+
                return array( $msg, 'noparse' => false );
        }
 
        /**
+        * @param Parser $parser
+        * @param string $page
+        * @param string $site
+        * @return string
+        */
+       public static function storeDataParserFunction( $parser, $page, $site = 
'' ) {
+               $data = self::verifyPFData( $page, $site );
+               if ( isset( $data['error'] ) ) {
+                       return ''; // Output doesn't matter
+               }
+               $output = $parser->getOutput();
+               $current = $output->getProperty( 'massmessage-targets' );
+               if ( !$current ) {
+                       $output->setProperty( 'massmessage-targets', serialize( 
array( $data ) ) );
+               } else {
+                       $output->setProperty( 'massmessage-targets' , serialize(
+                               array_merge( unserialize( $current ),  array( 
$data ) ) ) );
+               }
+               return '';
+       }
+
+       /**
         * Add our username to the list of reserved ones
         * @param $reservedUsernames array
         * @return bool

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1ac472a763352c43d74c8fc96ed9d02095d27548
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MassMessage
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

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

Reply via email to