jenkins-bot has submitted this change and it was merged.

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, 55 insertions(+), 23 deletions(-)

Approvals:
  Aaron Schulz: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/MassMessage.body.php b/MassMessage.body.php
index b4f6081..fa0a2c8 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 );
@@ -203,7 +202,8 @@
                        '<strong class="error">' .
                        $msg->inContentLanguage()->plain() .
                        '</strong>',
-                       'noparse' => false
+                       'noparse' => false,
+                       'error' => true,
                );
        }
 
diff --git a/MassMessage.hooks.php b/MassMessage.hooks.php
index 3b9e361..f5e655f 100644
--- a/MassMessage.hooks.php
+++ b/MassMessage.hooks.php
@@ -12,24 +12,20 @@
         * @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
+        * Verifies the user submitted data to check it's valid
         * @param string $page
+        * @param string $site
         * @return array
         */
-       public static function ParserFunction( $parser, $page, $site = '' ) {
-               global $wgScript, $wgAllowGlobalMessaging;
+       public static function verifyPFData( $page, $site ) {
+               global $wgServer, $wgAllowGlobalMessaging;
                $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();
@@ -51,23 +47,59 @@
                if ( !$wgAllowGlobalMessaging && $data['wiki'] != wfWikiID() ) {
                        return MassMessage::parserError( 
'massmessage-global-disallowed' );
                }
-               // 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}}
+        * Prepares the human facing output
+        * 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 );
        }
 
        /**
+        * Reads the parser function and extracts the data from it
+        * @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: merged
Gerrit-Change-Id: I1ac472a763352c43d74c8fc96ed9d02095d27548
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/MassMessage
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to