jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/332114 )

Change subject: Move title validation for Special:CreateCollaborationHub to 
that field
......................................................................


Move title validation for Special:CreateCollaborationHub to that field

This will make the error be output directly on the field in question.
Additionally, add validation checks for wrong namespace.

Change-Id: I1e35786a982905bb8a43292368dab7e42c61c100
---
M i18n/en.json
M i18n/qqq.json
M includes/SpecialCreateCollaborationHub.php
3 files changed, 50 insertions(+), 9 deletions(-)

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



diff --git a/i18n/en.json b/i18n/en.json
index 311394b..dba6474 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -120,5 +120,6 @@
        "collaborationkit-green3": "Khaki",
        "collaborationkit-black": "Black",
        "collaborationkit-column-active": "Active members",
-       "collaborationkit-column-inactive": "Inactive members"
+       "collaborationkit-column-inactive": "Inactive members",
+       "collaborationkit-createhub-wrongnamespace": "This page name cannot be 
used as a Collaboration Hub. Collaboration Hubs can only be created in the 
{{PLURAL:$1|$2 namespace|following namespaces: $2}}."
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 4a9d86c..10011f9 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -120,5 +120,6 @@
        "collaborationkit-green3": "Color label",
        "collaborationkit-black": "Color label",
        "collaborationkit-column-active": "Column header for the list of active 
members of a collaboration hub project",
-       "collaborationkit-column-inactive": "Column header for the list of 
members of a project who are no longer active on wiki."
+       "collaborationkit-column-inactive": "Column header for the list of 
members of a project who are no longer active on wiki.",
+       "collaborationkit-createhub-wrongnamespace": "Error message from 
Special:CreateCollaborationHub if the title specified is not allowed to be used 
with the CollaborationHubContent content model. Normally this means that the 
title is in the wrong namespace. $1 - Number of namespaces that 
CollaborationHub is allowed on. $2 - Comma separated list of namespace names 
that are allowed to be used."
 }
diff --git a/includes/SpecialCreateCollaborationHub.php 
b/includes/SpecialCreateCollaborationHub.php
index fd101ef..612acd1 100644
--- a/includes/SpecialCreateCollaborationHub.php
+++ b/includes/SpecialCreateCollaborationHub.php
@@ -37,6 +37,8 @@
                                'type' => 'text',
                                'cssclass' => 'mw-ck-title-input',
                                'label-message' => 
'collaborationkit-createhub-title',
+                               'validation-callback' => [ $this, 
'titleValidate' ],
+                               'required' => true
                        ],
                        // Display name can be different from page title
                        'display_name' => [
@@ -97,6 +99,39 @@
        }
 
        /**
+        * Callback to validate given title
+        *
+        * @param $value string The title value to test
+        * @return bool|string|Message True on success, or Message for error
+        */
+       public function titleValidate( $value ) {
+               $title = Title::newFromText( $value );
+               if ( !$title ) {
+                       return $this->msg( 
'collaborationkit-createhub-invalidtitle' );
+               }
+
+               // TODO: Add an option to import it to itself as target if the
+               // page already exists, archiving the existing page to a 
subpage (T136475)
+               if ( $title->exists() ) {
+                       return $this->msg( 'collaborationkit-createhub-exists' 
);
+               }
+
+               $handler = new CollaborationHubContentHandler();
+               if ( !$handler->canBeUsedOn( $title ) ) {
+                       // Most likely a namespace issue.
+                       $lang = $this->getLanguage();
+                       $allowedNSConfig = $this->getConfig()->get( 
'CollaborationListAllowedNamespaces' );
+                       $allowedNS = array_keys( array_filter( $allowedNSConfig 
) );
+                       $textNS = array_map( [ $lang, 'getFormattedNsText' ], 
$allowedNS );
+                       return $this->msg( 
'collaborationkit-createhub-wrongnamespace' )
+                               ->numParams( count( $textNS ) )
+                               ->params( $lang->commaList( $textNS ) );
+               }
+
+               return true;
+       }
+
+       /**
         * Build and return the aossociative array for the content source field.
         * @param $mapping array
         * @return array
@@ -116,14 +151,18 @@
        public function onSubmit( array $data ) {
                $title = Title::newFromText( $data['title'] );
                if ( !$title ) {
+                       // Should already be checked, but make extra sure.
                        return Status::newFatal( 
'collaborationkit-createhub-invalidtitle' );
-               } elseif ( $title->exists() ) {
-                       // TODO: Add an option to import it to itself as target 
if the page already exists, archiving the existing page to a subpage (T136475)
-                       return Status::newFatal( 
'collaborationkit-createhub-exists' );
-               } elseif (
-                       !$title->userCan( 'edit' ) ||
-                       !$title->userCan( 'create' ) ||
-                       !$title->userCan( 'editcontentmodel' )
+               }
+
+               $user = $this->getUser();
+               // TODO: Consider changing to getUserPermissionsErrors for
+               // better error message. Possibly as a first step in constructor
+               // as the non-title specific error.
+               if (
+                       !$title->userCan( 'edit', $user ) ||
+                       !$title->userCan( 'create', $user ) ||
+                       !$title->userCan( 'editcontentmodel', $user )
                ) {
                        return Status::newFatal( 
'collaborationkit-createhub-nopermission' );
                }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I1e35786a982905bb8a43292368dab7e42c61c100
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/CollaborationKit
Gerrit-Branch: master
Gerrit-Owner: Brian Wolff <bawolff...@gmail.com>
Gerrit-Reviewer: Brian Wolff <bawolff...@gmail.com>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
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