| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Api; |
| 23 |
|
| 24 |
use ChangeTags; |
| 25 |
use UnexpectedValueException; |
| 26 |
use Wikimedia\ParamValidator\ParamValidator; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ApiManageTags extends ApiBase { |
| 33 |
|
| 34 |
public function execute() { |
| 35 |
$params = $this->extractRequestParams(); |
| 36 |
$authority = $this->getAuthority(); |
| 37 |
|
| 38 |
|
| 39 |
if ( $params['operation'] !== 'delete' |
| 40 |
&& !$authority->isAllowed( 'managechangetags' ) |
| 41 |
) { |
| 42 |
$this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' ); |
| 43 |
} elseif ( !$authority->isAllowed( 'deletechangetags' ) ) { |
| 44 |
$this->dieWithError( 'tags-delete-no-permission', 'permissiondenied' ); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
if ( $params['tags'] ) { |
| 49 |
$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $authority ); |
| 50 |
if ( !$ableToTag->isOK() ) { |
| 51 |
$this->dieStatus( $ableToTag ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
$result = $this->getResult(); |
| 56 |
$tag = $params['tag']; |
| 57 |
$reason = $params['reason']; |
| 58 |
$ignoreWarnings = $params['ignorewarnings']; |
| 59 |
$tags = $params['tags'] ?: []; |
| 60 |
switch ( $params['operation'] ) { |
| 61 |
case 'create': |
| 62 |
$status = ChangeTags::createTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
| 63 |
break; |
| 64 |
case 'delete': |
| 65 |
$status = ChangeTags::deleteTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
| 66 |
break; |
| 67 |
case 'activate': |
| 68 |
$status = ChangeTags::activateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
| 69 |
break; |
| 70 |
case 'deactivate': |
| 71 |
$status = ChangeTags::deactivateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
| 72 |
break; |
| 73 |
default: |
| 74 |
|
| 75 |
throw new UnexpectedValueException( 'invalid operation' ); |
| 76 |
} |
| 77 |
if ( !$status->isOK() ) { |
| 78 |
$this->dieStatus( $status ); |
| 79 |
} |
| 80 |
|
| 81 |
$ret = [ |
| 82 |
'operation' => $params['operation'], |
| 83 |
'tag' => $params['tag'], |
| 84 |
]; |
| 85 |
if ( !$status->isGood() ) { |
| 86 |
$ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' ); |
| 87 |
} |
| 88 |
$ret['success'] = $status->value !== null; |
| 89 |
if ( $ret['success'] ) { |
| 90 |
$ret['logid'] = $status->value; |
| 91 |
} |
| 92 |
|
| 93 |
$result->addValue( null, $this->getModuleName(), $ret ); |
| 94 |
} |
| 95 |
|
| 96 |
public function mustBePosted() { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
public function isWriteMode() { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
public function getAllowedParams() { |
| 105 |
return [ |
| 106 |
'operation' => [ |
| 107 |
ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ], |
| 108 |
ParamValidator::PARAM_REQUIRED => true, |
| 109 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 110 |
], |
| 111 |
'tag' => [ |
| 112 |
ParamValidator::PARAM_TYPE => 'string', |
| 113 |
ParamValidator::PARAM_REQUIRED => true, |
| 114 |
], |
| 115 |
'reason' => [ |
| 116 |
ParamValidator::PARAM_TYPE => 'string', |
| 117 |
ParamValidator::PARAM_DEFAULT => '', |
| 118 |
], |
| 119 |
'ignorewarnings' => [ |
| 120 |
ParamValidator::PARAM_TYPE => 'boolean', |
| 121 |
ParamValidator::PARAM_DEFAULT => false, |
| 122 |
], |
| 123 |
'tags' => [ |
| 124 |
ParamValidator::PARAM_TYPE => 'tags', |
| 125 |
ParamValidator::PARAM_ISMULTI => true, |
| 126 |
], |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
public function needsToken() { |
| 131 |
return 'csrf'; |
| 132 |
} |
| 133 |
|
| 134 |
protected function getExamplesMessages() { |
| 135 |
return [ |
| 136 |
'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC' |
| 137 |
=> 'apihelp-managetags-example-create', |
| 138 |
'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC' |
| 139 |
=> 'apihelp-managetags-example-delete', |
| 140 |
'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC' |
| 141 |
=> 'apihelp-managetags-example-activate', |
| 142 |
'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC' |
| 143 |
=> 'apihelp-managetags-example-deactivate', |
| 144 |
]; |
| 145 |
} |
| 146 |
|
| 147 |
public function getHelpUrls() { |
| 148 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management'; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
class_alias( ApiManageTags::class, 'ApiManageTags' ); |