Wctaiwan has uploaded a new change for review.

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

Change subject: Add ability to import pages when creating lists
......................................................................

Add ability to import pages when creating lists

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
7 files changed, 93 insertions(+), 12 deletions(-)


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

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..763fe23 100644
--- a/includes/MassMessage.php
+++ b/includes/MassMessage.php
@@ -151,16 +151,15 @@
         * @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 );
diff --git a/includes/SpecialCreateMassMessageList.php 
b/includes/SpecialCreateMassMessageList.php
index 0d7b48e..d502a7b 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,25 @@
                        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 || !$source->exists() && 
!$source->inNamespace( NS_CATEGORY ) ) {
+                               return Status::newFatal( 
'massmessage-create-invalidsource' );
+                       }
 
-                       // TODO: Implement importing from existing lists
-                       $targets = array();
+                       if ( $source->inNamespace( NS_CATEGORY ) ) {
+                               $targets = self::getCategoryTargets( $source );
+                       } elseif ( $source->hasContentModel( 
'MassMessageListContent' ) ) {
+                               $targets = Revision::newFromTitle( $source 
)->getContent()->getTargets();
+                       } elseif ( $source->hasContentModel( 
CONTENT_MODEL_WIKITEXT ) ) {
+                               $targets = $this->getParserFunctionTargets( 
$source );
+                       } else {
+                               return Status::newFatal( 
'massmessage-create-invalidsource' );
+                       }
 
+                       if ( $targets === null ) {
+                               return Status::newFatal( 
'massmessage-create-invalidsource' );
+                       }
                } else {
                        $targets = array();
                }
@@ -82,7 +104,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 +130,34 @@
                }
                return $options;
        }
+
+       protected static function getCategoryTargets( Title $source ) {
+               $members = Category::newFromTitle( $source )->getMembers();
+               $targets = array();
+
+               /** @var Title member */
+               foreach ( $members as $member ) {
+                       $targets[] = array( 'title' => 
$member->getPrefixedText() );
+               }
+               return $targets;
+       }
+
+       protected function getParserFunctionTargets( Title $source ) {
+               $pages = MassMessage::getParserFunctionTargets( $source, 
$this->getContext() );
+
+               // If $source has no parser function targets, assume the user 
made a mistake.
+               if ( empty( $pages ) ) {
+                       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..c304772
--- /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 );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic0d3d212bc51d8b5732d5371e41a2a9ad5749254
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MassMessage
Gerrit-Branch: contenthandler
Gerrit-Owner: Wctaiwan <wctai...@gmail.com>

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

Reply via email to