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

Change subject: Combine target fetching; add importing on creation
......................................................................


Combine target fetching; add importing on creation

Contains the following minor changes:
* Simplify MassMessage::getCategoryTargets()
* Bugfix wrt. error message on API errors

Change-Id: Ic0d3d212bc51d8b5732d5371e41a2a9ad5749254
---
M MassMessage.php
M i18n/en.json
M i18n/qqq.json
M includes/MassMessage.php
M includes/SpecialCreateMassMessageList.php
M includes/SpecialEditMassMessageList.php
A modules/ext.MassMessage.create.js
M tests/MassMessageTest.php
8 files changed, 107 insertions(+), 24 deletions(-)

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



diff --git a/MassMessage.php b/MassMessage.php
index 1033937..6228d6e 100644
--- a/MassMessage.php
+++ b/MassMessage.php
@@ -127,6 +127,13 @@
        'localBasePath' => $dir . '/modules',
        'remoteExtPath' => 'MassMessage/modules',
 );
+$wgResourceModules['ext.MassMessage.create'] = array(
+       'scripts' => array(
+               'ext.MassMessage.create.js',
+       ),
+       'localBasePath' => $dir . '/modules',
+       'remoteExtPath' => 'MassMessage/modules',
+);
 
 // Logging
 $wgLogTypes[] = 'massmessage';
diff --git a/i18n/en.json b/i18n/en.json
index 2d3a73a..c181306 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -59,6 +59,7 @@
        "massmessage-create-invalidtitle": "The specified title is invalid.",
        "massmessage-create-exists": "A page already exists at the specified 
title.",
        "massmessage-create-nopermission": "You do not have permission to 
create a list at this title.",
+       "massmessage-create-invalidsource": "The specified source is not a 
valid delivery list or category.",
        "massmessage-create-tojsonerror": "The list could not be encoded for 
storage.",
        "massmessage-create-apierror": "Creating the list through the API 
failed with errror code $1.",
        "editmassmessagelist-legend": "Edit mass message delivery list",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 47b349a..4f4bedb 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -61,6 +61,7 @@
        "massmessage-create-exists": "Error message shown on 
[[Special:CreateMassMessageList]] when a page with the title already exists",
        "massmessage-create-nopermission": "Error message shown on 
[[Special:CreateMassMessageList]] when the user cannot create a page with the 
title",
        "massmessage-create-tojsonerror": "Error message shown on 
[[Special:CreateMassMessageList]] when the input could not be successfully 
encoded",
+       "massmessage-create-invalidsource": "Error message shown on 
[[Special:CreateMassMessageList]] when the source from which to import targets 
is invalid",
        "massmessage-create-apierror": "Error message shown on 
[[Special:CreateMassMessageList]] when the API request to create the page 
failed",
        "editmassmessagelist-legend": "Form legend for 
[[Special:EditMassMessageList]]",
        "massmessage-edit-title": "Label for an inputbox on 
[[Special:EditMassMessageList]]",
diff --git a/includes/MassMessage.php b/includes/MassMessage.php
index 8bcbbe6..e29c3ac 100644
--- a/includes/MassMessage.php
+++ b/includes/MassMessage.php
@@ -146,24 +146,45 @@
        }
 
        /**
+        * Get an array of targets given a title.
+        * @param Title $spamlist
+        * @param IContextSource $context
+        * @return array|null
+        */
+        public static function getTargets( Title $spamlist, $context ) {
+               if ( !$spamlist->exists() && !$spamlist->inNamespace( 
NS_CATEGORY ) ) {
+                       return null;
+               }
+
+               if ( $spamlist->inNamespace( NS_CATEGORY ) ) {
+                       return self::getCategoryTargets( $spamlist );
+               } elseif ( $spamlist->hasContentModel( 'MassMessageListContent' 
) ) {
+                       return self::getMassMessageListContentTargets( 
$spamlist );
+               } elseif ( $spamlist->hasContentModel( CONTENT_MODEL_WIKITEXT ) 
) {
+                       return self::getParserFunctionTargets( $spamlist, 
$context );
+               } else {
+                       return null;
+               }
+        }
+
+       /**
         * Get an array of targets from a category
         * @param  Title $spamlist
         * @return array
         */
        public static function getCategoryTargets( Title $spamlist ) {
-               $cat = Category::newFromTitle( $spamlist );
-               $members = $cat->getMembers();
+               $members = Category::newFromTitle( $spamlist )->getMembers();
                $targets = array();
 
                /** @var Title $member */
                foreach ( $members as $member ) {
-                       $target = array();
-                       $target['title'] = $member->getPrefixedText();
-                       $target['wiki'] = wfWikiID();
-                       $targets[] = $target;
+                       $targets[] = array(
+                               'title' => $member->getPrefixedText(),
+                               'wiki' => wfWikiID(),
+                       );
                }
 
-               return self::normalizeTargets( $targets );
+               return $targets;
        }
 
        /**
@@ -182,7 +203,7 @@
                                $target['wiki'] = wfWikiID();
                        }
                }
-               return self::normalizeTargets( $targets );
+               return $targets;
        }
 
        /**
@@ -206,7 +227,7 @@
                $data = unserialize( $output->getProperty( 
'massmessage-targets' ) );
 
                if ( $data ) {
-                       return self::normalizeTargets( $data );
+                       return $data;
                } else {
                        return array(); // No parser functions on page
                }
@@ -368,13 +389,7 @@
                $spamlist = self::getSpamlist( $data['spamlist'] );
 
                // Get the array of pages to deliver to.
-               if ( $spamlist->inNamespace( NS_CATEGORY ) ) {
-                       $pages = self::getCategoryTargets( $spamlist );
-               } elseif ( $spamlist->hasContentModel( 'MassMessageListContent' 
) ) {
-                       $pages = self::getMassMessageListContentTargets( 
$spamlist );
-               } else {
-                       $pages = self::getParserFunctionTargets( $spamlist, 
$context );
-               }
+               $pages = self::normalizeTargets( self::getTargets( $spamlist, 
$context ) );
 
                // Log it.
                self::logToWiki( $spamlist, $context->getUser(), 
$data['subject'] );
diff --git a/includes/SpecialCreateMassMessageList.php 
b/includes/SpecialCreateMassMessageList.php
index 0d7b48e..ce1f9ec 100644
--- a/includes/SpecialCreateMassMessageList.php
+++ b/includes/SpecialCreateMassMessageList.php
@@ -7,6 +7,15 @@
        }
 
        /**
+        * Add ResourceLoader module and call parent implementation.
+        * @param string $par
+        */
+       public function execute( $par ) {
+               $this->getOutput()->addModules( 'ext.MassMessage.create' );
+               parent::execute( $par );
+       }
+
+       /**
         * @return array
         */
        protected function getFormFields() {
@@ -28,7 +37,6 @@
                        ),
                        'source' => array(
                                'type' => 'text',
-                               'disabled' => true,
                                'label-message' => 'massmessage-create-source',
                        ),
                );
@@ -48,11 +56,16 @@
                        return Status::newFatal( 
'massmessage-create-nopermission' );
                }
 
-               if ( $data['content'] === 'import' ) {
+               if ( $data['content'] === 'import' ) { // Importing from an 
existing list
+                       $source = Title::newFromText( $data['source'] );
+                       if ( !$source ) {
+                               return Status::newFatal( 
'massmessage-create-invalidsource' );
+                       }
 
-                       // TODO: Implement importing from existing lists
-                       $targets = array();
-
+                       $targets = $this->getTargets( $source );
+                       if ( $targets === null ) {
+                               return Status::newFatal( 
'massmessage-create-invalidsource' );
+                       }
                } else {
                        $targets = array();
                }
@@ -82,7 +95,7 @@
                        $api->execute();
                } catch ( UsageException $e ) {
                        return Status::newFatal( $this->msg( 
'massmessage-create-apierror',
-                               $e->getCodeString ) );
+                               $e->getCodeString() ) );
                }
 
                $this->getOutput()->redirect( $title->getFullUrl() );
@@ -108,4 +121,26 @@
                }
                return $options;
        }
+
+       /**
+        * Get targets from an existing delivery list or category.
+        * @param Title $source
+        * @return array|null
+        */
+       protected function getTargets( Title $source ) {
+               $pages = MassMessage::getTargets( $source, $this->getContext() 
);
+               if ( $pages === null ) {
+                       return null;
+               }
+
+               $targets = array();
+               foreach ( $pages as $page ) {
+                       $target = array( 'title' => $page['title'] );
+                       if ( $page['wiki'] !== wfWikiID() ) {
+                               $target['site'] = $page['site'];
+                       }
+                       $targets[] = $target;
+               }
+               return $targets;
+       }
 }
diff --git a/includes/SpecialEditMassMessageList.php 
b/includes/SpecialEditMassMessageList.php
index 5807167..877ef8f 100644
--- a/includes/SpecialEditMassMessageList.php
+++ b/includes/SpecialEditMassMessageList.php
@@ -135,7 +135,8 @@
                        $api = new ApiMain( $request, true );
                        $api->execute();
                } catch ( UsageException $e ) {
-                       return Status::newFatal( $this->msg( 
'massmessage-edit-apierror', $e->getCodeString ) );
+                       return Status::newFatal( $this->msg( 
'massmessage-edit-apierror',
+                               $e->getCodeString() ) );
                }
 
                $this->getOutput()->redirect( $this->title->getFullUrl() );
diff --git a/modules/ext.MassMessage.create.js 
b/modules/ext.MassMessage.create.js
new file mode 100644
index 0000000..d396cf5
--- /dev/null
+++ b/modules/ext.MassMessage.create.js
@@ -0,0 +1,20 @@
+( function ( mw, $ ) {
+       $( function () {
+
+               // Set the correct field state on load.
+               if ( $( '#mw-input-wpcontent-import' ).is( ':checked' ) ) {
+                       $( '#mw-input-wpsource' ).prop( 'disabled', false );
+               } else {
+                       $( '#mw-input-wpsource' ).prop( 'disabled', true );
+               }
+
+               $( '#mw-input-wpcontent-new' ).click( function () {
+                       $( '#mw-input-wpsource' ).prop( 'disabled', true );
+               } );
+
+               $( '#mw-input-wpcontent-import' ).click( function () {
+                       $( '#mw-input-wpsource' ).prop( 'disabled', false );
+               } );
+
+       } );
+} )( mediaWiki, jQuery );
diff --git a/tests/MassMessageTest.php b/tests/MassMessageTest.php
index 9bc0bfe..910df11 100644
--- a/tests/MassMessageTest.php
+++ b/tests/MassMessageTest.php
@@ -109,7 +109,10 @@
        public function testGetParserFunctionTargets( $text, $check ) {
                $title = Title::newFromText( 'Input list ');
                self::updatePage( $title, $text );
-               $data = MassMessage::getParserFunctionTargets( $title, 
RequestContext::getMain() );
+               $data = MassMessage::normalizeTargets(
+                       MassMessage::getParserFunctionTargets( $title, 
RequestContext::getMain() )
+               );
+
                if ( empty( $check ) ) {
                        // Check that the spamlist is empty
                        $this->assertTrue( empty( $data ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic0d3d212bc51d8b5732d5371e41a2a9ad5749254
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/MassMessage
Gerrit-Branch: contenthandler
Gerrit-Owner: Wctaiwan <wctai...@gmail.com>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Prtksxna <psax...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to