Addshore has submitted this change and it was merged.

Change subject: Check id entered in SpecialSetSiteLink is an ItemId
......................................................................


Check id entered in SpecialSetSiteLink is an ItemId

(also make capitalisation of "id" more consistent in these special pages)

Bug: 56933
Change-Id: I56c35d06039a34473e09225ffc551da5978a169e
(cherry picked from commit 95a51924c897276c73260c2a76a7eccffc8c520b)
---
M lib/WikibaseLib.classes.php
A lib/includes/MessageException.php
A lib/includes/UserInputException.php
M repo/Wikibase.i18n.php
M repo/includes/specials/SpecialModifyEntity.php
M repo/includes/specials/SpecialSetSiteLink.php
6 files changed, 144 insertions(+), 19 deletions(-)

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



diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php
index fb5168e..22e5e41 100644
--- a/lib/WikibaseLib.classes.php
+++ b/lib/WikibaseLib.classes.php
@@ -82,6 +82,10 @@
                'ReportingExceptionHandler' => 'includes/ExceptionHandler.php',
                'RethrowingExceptionHandler' => 'includes/ExceptionHandler.php',
 
+               // exceptions, should really really be in core
+               'MessageException' => 'includes/MessageException.php',
+               'UserInputException' => 'includes/UserInputException.php',
+
                // includes/changes
                'Wikibase\Change' => 'includes/changes/Change.php',
                'Wikibase\ChangeRow' => 'includes/changes/ChangeRow.php',
diff --git a/lib/includes/MessageException.php 
b/lib/includes/MessageException.php
new file mode 100644
index 0000000..9bc322f
--- /dev/null
+++ b/lib/includes/MessageException.php
@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * Should be moved to core!
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < aude.w...@gmail.com >
+ */
+class MessageException extends Exception {
+
+       /**
+        * @var string
+        */
+       protected $key;
+
+       /**
+        * var array
+        */
+       protected $params;
+
+       /**
+        * @param string $key
+        * @param array $params
+        * @param string $message
+        * @param Exception $previous
+        */
+       public function __construct( $key, array $params, $message, Exception 
$previous = null ) {
+               parent::__construct( $message, 0, $previous );
+
+               $this->key = $key;
+               $this->params = $params;
+       }
+
+       /**
+        * @return string
+        */
+       public function getKey() {
+               return $this->key;
+       }
+
+       /**
+        * @return array
+        */
+       public function getParams() {
+               return $this->params;
+       }
+
+}
diff --git a/lib/includes/UserInputException.php 
b/lib/includes/UserInputException.php
new file mode 100644
index 0000000..81e2af6
--- /dev/null
+++ b/lib/includes/UserInputException.php
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * Used in special pages and elsewhere to handle user input errors,
+ * allow them to bubble up to presentation layer and contain message
+ * that can be displayed to the user in their language.
+ *
+ * Ideally should be moved to core!
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < aude.w...@gmail.com >
+ */
+class UserInputException extends MessageException {
+
+}
diff --git a/repo/Wikibase.i18n.php b/repo/Wikibase.i18n.php
index f71886f..7819b76 100644
--- a/repo/Wikibase.i18n.php
+++ b/repo/Wikibase.i18n.php
@@ -153,25 +153,25 @@
        'wikibase-newentity-submit' => 'Create',
        'special-setlabel' => 'Set a label',
        'wikibase-setlabel-introfull' => 'You are setting the label in $2 for 
[[$1]].',
-       'wikibase-setlabel-intro' => 'This form allows you to set the label of 
an entity. You need to provide the ID of the entity (e.g. Q23), a language code 
(e.g. "en") and the label to set to.',
+       'wikibase-setlabel-intro' => 'This form allows you to set the label of 
an entity. You need to provide the id of the entity (e.g. Q23), a language code 
(e.g. "en") and the label to set to.',
        'wikibase-setlabel-label' => 'Label:',
        'wikibase-setlabel-submit' => 'Set the label',
        'wikibase-setlabel-warning-remove' => 'Do you really want to remove the 
label of [[$1]]?',
        'special-setdescription' => 'Set a description',
        'wikibase-setdescription-introfull' => 'You are setting the description 
in $2 for [[$1]].',
-       'wikibase-setdescription-intro' => 'This form allows you to set the 
description of an entity. You need to provide the ID of the entity (e.g. Q23), 
a language code (e.g. "en") and the description to set to.',
+       'wikibase-setdescription-intro' => 'This form allows you to set the 
description of an entity. You need to provide the id of the entity (e.g. Q23), 
a language code (e.g. "en") and the description to set to.',
        'wikibase-setdescription-label' => 'Description:',
        'wikibase-setdescription-submit' => 'Set the description',
        'wikibase-setdescription-warning-remove' => 'Do you really want to 
remove the description of [[$1]]?',
        'special-setaliases' => 'Set aliases',
        'wikibase-setaliases-introfull' => 'You are setting the aliases in $2 
for [[$1]]. Several aliases are separated by a pipe (<code>|</code>) 
character.',
-       'wikibase-setaliases-intro' => 'This form allows you to set the aliases 
of an entity. You need to provide the ID of the entity (e.g. Q23), a language  
code (e.g. "en") and the aliases to set to. Several aliases are separated by a 
pipe (<code>|</code>) character.',
+       'wikibase-setaliases-intro' => 'This form allows you to set the aliases 
of an entity. You need to provide the id of the entity (e.g. Q23), a language  
code (e.g. "en") and the aliases to set to. Several aliases are separated by a 
pipe (<code>|</code>) character.',
        'wikibase-setaliases-label' => 'Aliases:',
        'wikibase-setaliases-submit' => 'Set the aliases',
        'wikibase-setaliases-warning-remove' => 'Do you really want to remove 
all aliases of [[$1]]?',
        'special-setsitelink' => 'Set a site link',
        'wikibase-setsitelink-introfull' => 'You are setting the site link of 
$2 for [[$1]].',
-       'wikibase-setsitelink-intro' => 'This form allows you to set the site 
link of an entity. You need to provide the ID of the entity (e.g. Q23), a site 
id (e.g. "enwiki") and the site link to set to.',
+       'wikibase-setsitelink-intro' => 'This form allows you to set the site 
link of an entity. You need to provide the id of the entity (e.g. Q23), a site 
id (e.g. "enwiki") and the site link to set to.',
        'wikibase-setsitelink-site' => 'Site id:',
        'wikibase-setsitelink-label' => 'Site link:',
        'wikibase-setsitelink-submit' => 'Set the site link',
@@ -179,10 +179,11 @@
        'wikibase-setsitelink-invalid-site' => 'The site id "$1" is unknown. 
Please use an existing site id, such as "enwiki".',
        'wikibase-setsitelink-add-failed' => 'The site link could not be 
saved.',
        'wikibase-setsitelink-remove-failed' => 'The site link could not be 
removed.',
+       'wikibase-setsitelink-not-itemid' => '"$1" is not a valid item id. Site 
links can only be set for items.',
        'wikibase-setentity-id' => 'ID:',
        'wikibase-setentity-language' => 'Language:',
        'wikibase-setentity-invalid-langcode' => 'The language identifier "$1" 
is unknown. Please use a language identifier known to the system, such as 
"en".',
-       'wikibase-setentity-invalid-id' => 'The ID "$1" is unknown to the 
system. Please use a valid entity ID.',
+       'wikibase-setentity-invalid-id' => 'The id "$1" is unknown to the 
system. Please use a valid entity id.',
        'special-dispatchstats' => 'Change dispatch statistics',
        'wikibase-dispatchstats-intro' => 'This page provides statistics about 
dispatching changes to the clients.
 A client wiki is notified of a change on {{SITENAME}} once the change has been 
dispatched on that wiki, and the job queue on that wiki has processed that 
notification.
@@ -863,6 +864,10 @@
 * $1 - the invalid id',
        'wikibase-setsitelink-add-failed' => 'Error message when the site link 
could not be saved.',
        'wikibase-setsitelink-remove-failed' => 'Error message when the site 
link could not be removed.',
+       'wikibase-setsitelink-not-itemid' => 'Error message when an entity id, 
other than item id, is entered. The message advises users that the id is 
invalid and only items have site links.
+
+Parameters:
+* $1 - the invalid id',
        'wikibase-setentity-id' => 'Label for the input field to select the ID 
of the entity.
 {{Identical|ID}}',
        'wikibase-setentity-language' => 'Label for the input field to select 
the language the label should be set in.
diff --git a/repo/includes/specials/SpecialModifyEntity.php 
b/repo/includes/specials/SpecialModifyEntity.php
index c80edb7..505582b 100644
--- a/repo/includes/specials/SpecialModifyEntity.php
+++ b/repo/includes/specials/SpecialModifyEntity.php
@@ -3,7 +3,8 @@
 namespace Wikibase\Repo\Specials;
 
 use Html;
-use UserBlockedError;
+use UserInputException;
+use Wikibase\DataModel\Entity\EntityIdParsingException;
 use Wikibase\EditEntity;
 use Wikibase\EntityContentFactory;
 use Wikibase\EntityId;
@@ -71,7 +72,12 @@
                $this->setHeaders();
                $this->outputHeader();
 
-               $this->prepareArguments( $subPage );
+               try {
+                       $this->prepareArguments( $subPage );
+               } catch ( UserInputException $ex ) {
+                       $error = $this->msg( $ex->getKey(), $ex->getParams() 
)->parse();
+                       $this->showErrorHTML( $error );
+               }
 
                $summary = $this->modifyEntity();
 
@@ -112,23 +118,47 @@
                $parts = ( $subPage === '' ) ? array() : explode( '/', 
$subPage, 2 );
 
                // Get id
-               $rawId = $this->getRequest()->getVal( 'id', isset( $parts[0] ) 
? $parts[0] : '' );
-               $id = EntityId::newFromPrefixedId( $rawId );
+               $rawId = $this->getRequest()->getVal( 'id', isset( $parts[0] ) 
? $parts[0] : null );
+               $id = $rawId ? $this->parseEntityId( $rawId ) : null;
 
-               if ( $id === null ) {
-                       $this->entityContent = null;
+               if ( $id !== null ) {
+                       $entityContent = $this->loadEntityContent( $id );
+
+                       if ( $entityContent === null ) {
+                               throw new SpecialPageException(
+                                       'wikibase-setentity-invalid-id',
+                                       array( $rawId ),
+                                       'Entity id is unknown'
+                               );
+                       } else {
+                               $this->entityContent = $entityContent;
+                       }
                }
-               else {
-                       $this->entityContent = 
WikibaseRepo::getDefaultInstance()->getEntityContentFactory()->getFromId( $id );
+       }
+
+       /**
+        * @param string $rawId
+        *
+        * @return EntityId
+        */
+       protected function parseEntityId( $rawId ) {
+               $idParser = 
WikibaseRepo::getDefaultInstance()->getEntityIdParser();
+
+               try {
+                       $id = $idParser->parse( $rawId );
+               } catch ( EntityIdParsingException $ex ) {
+                       $this->showErrorHtml( $this->msg( 
'wikibase-setentity-invalid-id', $rawId )->parse() );
+                       return null;
                }
 
-               if ( $rawId === '' ) {
-                       $rawId = null;
-               }
+               return $id;
+       }
 
-               if ( $this->entityContent === null && $rawId !== null ) {
-                       $this->showErrorHTML( $this->msg( 
'wikibase-setentity-invalid-id', $rawId )->parse() );
-               }
+       /**
+        * @param EntityId $id
+        */
+       protected function loadEntityContent( EntityId $id ) {
+               return 
WikibaseRepo::getDefaultInstance()->getEntityContentFactory()->getFromId( $id );
        }
 
        /**
diff --git a/repo/includes/specials/SpecialSetSiteLink.php 
b/repo/includes/specials/SpecialSetSiteLink.php
index 0a09bb5..8038cdf 100644
--- a/repo/includes/specials/SpecialSetSiteLink.php
+++ b/repo/includes/specials/SpecialSetSiteLink.php
@@ -3,11 +3,14 @@
 namespace Wikibase\Repo\Specials;
 
 use Html;
+use InvalidArgumentException;
 use Sites;
 use Status;
+use UserInputException;
 use Wikibase\EntityContent;
 use Wikibase\ChangeOp\ChangeOpSiteLink;
 use Wikibase\ChangeOp\ChangeOpException;
+use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\Summary;
 
 /**
@@ -76,6 +79,22 @@
        }
 
        /**
+        * @see SpecialModifyEntity::parseEntityId()
+        */
+       protected function parseEntityId( $rawId ) {
+               try {
+                       return new ItemId( $rawId );
+               } catch ( InvalidArgumentException $ex ) {
+                       throw new UserInputException(
+                               'wikibase-setsitelink-not-itemid',
+                               array( $rawId ),
+                               $ex->getMessage(),
+                               $ex
+                       );
+               }
+       }
+
+       /**
         * @see SpecialModifyEntity::modifyEntity()
         *
         * @since 0.4

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I56c35d06039a34473e09225ffc551da5978a169e
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
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