Wctaiwan has uploaded a new change for review.

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

Change subject: Allow valid lists to contain invalid sites
......................................................................

Allow valid lists to contain invalid sites

Change MassMessageTargets::getMassMessageListContentTargets to filter
invalid targets and show a warning when viewing lists with invalid
targets.

This prevents configuration changes from suddenly making pages and
revisions inaccessible.

Change-Id: If45a0ef56c2f92fd8a7debceea0e39c4dd532ba8
---
M i18n/en.json
M i18n/qqq.json
M includes/MassMessageTargets.php
M includes/content/MassMessageListContent.php
M includes/content/MassMessageListContentHandler.php
5 files changed, 50 insertions(+), 24 deletions(-)


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

diff --git a/i18n/en.json b/i18n/en.json
index 9e2f0ff..4d57abe 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -50,6 +50,7 @@
        "massmessage-content-remove": "remove",
        "massmessage-content-removeerror": "Removing the page failed with error 
code $1.",
        "massmessage-content-empty": "There are no pages in this list.",
+       "massmessage-content-invalidtargets": "This list contains invalid 
targets.",
        "massmessage-content-emptylist": "(none)",
        "massmessage-content-addheading": "Add pages",
        "massmessage-content-addtitle": "Title:",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index aaecf42..7d7a505 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -51,6 +51,7 @@
        "massmessage-content-remove": "Label for the link to remove a page from 
a delivery list",
        "massmessage-content-removeerror": "Error message shown when removing a 
page using the link next to each item failed",
        "massmessage-content-empty": "Message shown when viewing a delivery 
list if it is completely empty",
+       "massmessage-content-invalidtargets": "Message shown when viewing a 
delivery list that contains invalid targets",
        "massmessage-content-emptylist": "Message that replaces a section of a 
delivery list (e.g. pages on a particular wiki) when all pages in the section 
are removed",
        "massmessage-content-addheading": "Heading for the form for adding 
pages when viewing delivery lists",
        "massmessage-content-addtitle": "Label for the title field in the form 
for adding pages",
diff --git a/includes/MassMessageTargets.php b/includes/MassMessageTargets.php
index a6bf3f7..d867e75 100644
--- a/includes/MassMessageTargets.php
+++ b/includes/MassMessageTargets.php
@@ -92,18 +92,31 @@
         * @return array
         */
        public static function getMassMessageListContentTargets ( Title 
$spamlist ) {
-               global $wgCanonicalServer;
+               global $wgAllowGlobalMessaging, $wgCanonicalServer;
 
                $targets = Revision::newFromTitle( $spamlist 
)->getContent()->getTargets();
-               foreach ( $targets as &$target ) {
+               $validTargets = array();
+               foreach ( $targets as $target ) {
                        if ( array_key_exists( 'site', $target ) ) {
-                               $target['wiki'] = MassMessage::getDBName( 
$target['site'] );
+                               if ( $wgAllowGlobalMessaging ) {
+                                       $wiki = MassMessage::getDBName( 
$target['site'] );
+                                       if ( $wiki !== null ) {
+                                               $validTargets[] = array(
+                                                       'title' => 
$target['title'],
+                                                       'site' => 
$target['site'],
+                                                       'wiki' => $wiki
+                                               );
+                                       }
+                               }
                        } else {
-                               $target['wiki'] = wfWikiID();
-                               $target['site'] = MassMessage::getBaseUrl( 
$wgCanonicalServer );
+                               $validTargets[] = array(
+                                       'title' => $target['title'],
+                                       'site' => MassMessage::getBaseUrl( 
$wgCanonicalServer ),
+                                       'wiki' => wfWikiID()
+                               );
                        }
                }
-               return $targets;
+               return $validTargets;
        }
 
        /**
diff --git a/includes/content/MassMessageListContent.php 
b/includes/content/MassMessageListContent.php
index 1252e93..0af4a4b 100644
--- a/includes/content/MassMessageListContent.php
+++ b/includes/content/MassMessageListContent.php
@@ -30,12 +30,9 @@
 
        /**
         * Decode and validate the contents.
-        *
         * @return bool Whether the contents are valid
         */
        public function isValid() {
-               global $wgAllowGlobalMessaging;
-
                $this->decode(); // Populate $this->description and 
$this->targets.
                if ( !is_string( $this->description ) || !is_array( 
$this->targets ) ) {
                        return false;
@@ -47,24 +44,36 @@
                        ) {
                                return false;
                        }
-                       if ( array_key_exists( 'site', $target ) ) {
-                               $wiki = MassMessage::getDBName( $target['site'] 
);
-                               if ( $wiki === null || !$wgAllowGlobalMessaging 
&& $wiki !== wfWikiId() ) {
-                                       return false;
-                               }
-                       }
                }
                return true;
        }
 
        /**
+        * Whether the content object contains invalid targets
+        * @return bool
+        */
+       public function hasInvalidTargets() {
+               global $wgAllowGlobalMessaging;
+
+               $targets = $this->getTargets();
+               foreach ( $targets as $target ) {
+                       if ( array_key_exists( 'site', $target ) ) {
+                               if ( !$wgAllowGlobalMessaging
+                                       || MassMessage::getDBName( 
$target['site'] ) === null
+                               ) {
+                                       return true;
+                               }
+                       }
+               }
+               return false;
+       }
+
+       /**
         * Returns a list content object with pre-save transformations applied.
         * The implementation normalizes the JSON data.
-        *
         * @param Title $title
         * @param User $user
         * @param ParserOptions $popts
-        *
         * @return MassMessageListContent
         */
        public function preSaveTransform( Title $title, User $user, 
ParserOptions $popts ) {
@@ -97,10 +106,6 @@
                global $wgCanonicalServer;
 
                $targets = $this->getTargets();
-               if ( $targets === null ) {
-                       return null;
-               }
-
                $targetStrings = array();
                foreach ( $targets as $target ) {
                        if ( array_key_exists( 'site', $target ) ) {
@@ -149,7 +154,14 @@
 
                // Generate output HTML, if needed.
                if ( $generateHtml ) {
-                       $output->setText( $output->getText() . 
$this->getTargetsHtml() . self::getAddForm() );
+                       if ( $this->hasInvalidTargets() ) {
+                               $warning = Html::element( 'p', array( 'class' 
=> 'error' ),
+                                       wfMessage( 
'massmessage-content-invalidtargets' )->text() );
+                       } else {
+                               $warning = '';
+                       }
+                       $output->setText( $warning . $output->getText() . 
$this->getTargetsHtml()
+                               . self::getAddForm() );
                } else {
                        $output->setText( '' );
                }
diff --git a/includes/content/MassMessageListContentHandler.php 
b/includes/content/MassMessageListContentHandler.php
index 07c57a4..33384ef 100644
--- a/includes/content/MassMessageListContentHandler.php
+++ b/includes/content/MassMessageListContentHandler.php
@@ -138,8 +138,7 @@
                }
 
                if ( $site !== null && $site !== MassMessage::getBaseUrl( 
$wgCanonicalServer ) ) {
-                       $wiki = MassMessage::getDBName( $site );
-                       if ( $wiki === null || !$wgAllowGlobalMessaging && 
$wiki !== wfWikiID() ) {
+                       if ( !$wgAllowGlobalMessaging || 
MassMessage::getDBName( $site ) === null ) {
                                if ( array_key_exists( 'errors', $result ) ) {
                                        $result['errors'][] = 'invalidsite';
                                } else {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If45a0ef56c2f92fd8a7debceea0e39c4dd532ba8
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