Add logic for "tags" in ApiBase
authorvictorbarbu <victorbarbu08@gmail.com>
Wed, 20 Jan 2016 09:51:01 +0000 (11:51 +0200)
committervictorbarbu <victorbarbu08@gmail.com>
Thu, 4 Feb 2016 20:30:50 +0000 (22:30 +0200)
Bug: T97720
Change-Id: I275c516b4744e243333ec0818d2be1e5508e54a3

includes/api/ApiBase.php
includes/api/ApiDelete.php
includes/api/ApiEditPage.php
includes/api/ApiHelp.php
includes/api/ApiParamInfo.php
includes/api/ApiRollback.php
includes/api/ApiTag.php

index 13d13a6..c60a870 100644 (file)
@@ -74,6 +74,8 @@ abstract class ApiBase extends ContextSource {
         * - string: Any non-empty string, not expected to be very long or contain newlines.
         *   <input type="text"> would be an appropriate HTML form field.
         * - submodule: The name of a submodule of this module, see PARAM_SUBMODULE_MAP.
+        * - tags: A string naming an existing, explicitly-defined tag. Should usually be
+        *   used with PARAM_ISMULTI.
         * - text: Any non-empty string, expected to be very long or contain newlines.
         *   <textarea> would be an appropriate HTML form field.
         * - timestamp: A timestamp in any format recognized by MWTimestamp, or the
@@ -1063,6 +1065,16 @@ abstract class ApiBase extends ContextSource {
                                                break;
                                        case 'upload': // nothing to do
                                                break;
+                                       case 'tags':
+                                               // If change tagging was requested, check that the tags are valid.
+                                               if ( !is_array( $value ) && !$multi ) {
+                                                       $value = array( $value );
+                                               }
+                                               $tagsStatus = ChangeTags::canAddTagsAccompanyingChange( $value );
+                                               if ( !$tagsStatus->isGood() ) {
+                                                       $this->dieStatus( $tagsStatus );
+                                               }
+                                               break;
                                        default:
                                                ApiBase::dieDebug( __METHOD__, "Param $encParamName's type is unknown - $type" );
                                }
index edcee86..b0bf5dd 100644 (file)
@@ -188,7 +188,7 @@ class ApiDelete extends ApiBase {
                        ),
                        'reason' => null,
                        'tags' => array(
-                               ApiBase::PARAM_TYPE => ChangeTags::listExplicitlyDefinedTags(),
+                               ApiBase::PARAM_TYPE => 'tags',
                                ApiBase::PARAM_ISMULTI => true,
                        ),
                        'watch' => array(
index 59264e8..4a83129 100644 (file)
@@ -363,10 +363,11 @@ class ApiEditPage extends ApiBase {
 
                // Apply change tags
                if ( count( $params['tags'] ) ) {
-                       if ( $user->isAllowed( 'applychangetags' ) ) {
+                       $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
+                       if ( $tagStatus->isOk() ) {
                                $requestArray['wpChangeTags'] = implode( ',', $params['tags'] );
                        } else {
-                               $this->dieUsage( 'You don\'t have permission to set change tags.', 'taggingnotallowed' );
+                               $this->dieStatus( $tagStatus );
                        }
                }
 
@@ -579,7 +580,7 @@ class ApiEditPage extends ApiBase {
                        ),
                        'summary' => null,
                        'tags' => array(
-                               ApiBase::PARAM_TYPE => ChangeTags::listExplicitlyDefinedTags(),
+                               ApiBase::PARAM_TYPE => 'tags',
                                ApiBase::PARAM_ISMULTI => true,
                        ),
                        'minor' => false,
index bbea20b..c4bf7d8 100644 (file)
@@ -533,6 +533,17 @@ class ApiHelp extends ApiBase {
                                                                        $type = null;
                                                                        break;
 
+                                                               case 'tags':
+                                                                       $tags = ChangeTags::listExplicitlyDefinedTags();
+                                                                       $count = count( $tags );
+                                                                       $info[] = $context->msg( 'api-help-param-list' )
+                                                                               ->params( $multi ? 2 : 1 )
+                                                                               ->params( $context->getLanguage()->commaList( $tags ) )
+                                                                               ->parse();
+                                                                       $hintPipeSeparated = false;
+                                                                       $type = null;
+                                                                       break;
+
                                                                case 'limit':
                                                                        if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
                                                                                $info[] = $context->msg( 'api-help-param-limit2' )
index 18ca0ab..a8e5629 100644 (file)
@@ -338,6 +338,8 @@ class ApiParamInfo extends ApiBase {
                                        if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
                                                $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
                                        }
+                               } elseif ( $settings[ApiBase::PARAM_TYPE] === 'tags' ) {
+                                       $item['type'] = ChangeTags::listExplicitlyDefinedTags();
                                } else {
                                        $item['type'] = $settings[ApiBase::PARAM_TYPE];
                                }
index 0fa2e31..055c7fe 100644 (file)
@@ -122,7 +122,7 @@ class ApiRollback extends ApiBase {
                                ApiBase::PARAM_TYPE => 'integer'
                        ),
                        'tags' => array(
-                               ApiBase::PARAM_TYPE => ChangeTags::listExplicitlyDefinedTags(),
+                               ApiBase::PARAM_TYPE => 'tags',
                                ApiBase::PARAM_ISMULTI => true,
                        ),
                        'user' => array(
index 4bf799e..c1a6810 100644 (file)
  */
 class ApiTag extends ApiBase {
 
-       protected function getAvailableTags() {
-               return ChangeTags::listExplicitlyDefinedTags();
-       }
-
        public function execute() {
                $params = $this->extractRequestParams();
                $user = $this->getUser();
@@ -150,7 +146,7 @@ class ApiTag extends ApiBase {
                                ApiBase::PARAM_ISMULTI => true,
                        ),
                        'add' => array(
-                               ApiBase::PARAM_TYPE => $this->getAvailableTags(),
+                               ApiBase::PARAM_TYPE => 'tags',
                                ApiBase::PARAM_ISMULTI => true,
                        ),
                        'remove' => array(