Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiManageTags.php
blob3c080939c0ca24d1ee942685aa44b001bede261e
1 <?php
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
19 * @file
22 /**
23 * @ingroup API
24 * @since 1.25
26 class ApiManageTags extends ApiBase {
28 public function execute() {
29 $params = $this->extractRequestParams();
30 $user = $this->getUser();
32 // make sure the user is allowed
33 if ( $params['operation'] !== 'delete'
34 && !$this->getUser()->isAllowed( 'managechangetags' )
35 ) {
36 $this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' );
37 } elseif ( !$this->getUser()->isAllowed( 'deletechangetags' ) ) {
38 $this->dieWithError( 'tags-delete-no-permission', 'permissiondenied' );
41 // Check if user can add the log entry tags which were requested
42 if ( $params['tags'] ) {
43 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
44 if ( !$ableToTag->isOK() ) {
45 $this->dieStatus( $ableToTag );
49 $result = $this->getResult();
50 $funcName = "{$params['operation']}TagWithChecks";
51 $status = ChangeTags::$funcName(
52 $params['tag'],
53 $params['reason'],
54 $user,
55 $params['ignorewarnings'],
56 $params['tags'] ?: []
59 if ( !$status->isOK() ) {
60 $this->dieStatus( $status );
63 $ret = [
64 'operation' => $params['operation'],
65 'tag' => $params['tag'],
67 if ( !$status->isGood() ) {
68 $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
70 $ret['success'] = $status->value !== null;
71 if ( $ret['success'] ) {
72 $ret['logid'] = $status->value;
75 $result->addValue( null, $this->getModuleName(), $ret );
78 public function mustBePosted() {
79 return true;
82 public function isWriteMode() {
83 return true;
86 public function getAllowedParams() {
87 return [
88 'operation' => [
89 ApiBase::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
90 ApiBase::PARAM_REQUIRED => true,
92 'tag' => [
93 ApiBase::PARAM_TYPE => 'string',
94 ApiBase::PARAM_REQUIRED => true,
96 'reason' => [
97 ApiBase::PARAM_TYPE => 'string',
99 'ignorewarnings' => [
100 ApiBase::PARAM_TYPE => 'boolean',
101 ApiBase::PARAM_DFLT => false,
103 'tags' => [
104 ApiBase::PARAM_TYPE => 'tags',
105 ApiBase::PARAM_ISMULTI => true,
110 public function needsToken() {
111 return 'csrf';
114 protected function getExamplesMessages() {
115 return [
116 'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
117 => 'apihelp-managetags-example-create',
118 'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC'
119 => 'apihelp-managetags-example-delete',
120 'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
121 => 'apihelp-managetags-example-activate',
122 'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC'
123 => 'apihelp-managetags-example-deactivate',
127 public function getHelpUrls() {
128 return 'https://www.mediawiki.org/wiki/API:Tag_management';