Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / api / ApiManageTags.php
1 <?php
2
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.
8 *
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.
13 *
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
18 *
19 * @file
20 */
21
22 /**
23 * @ingroup API
24 * @since 1.25
25 */
26 class ApiManageTags extends ApiBase {
27
28 public function execute() {
29 $params = $this->extractRequestParams();
30 $user = $this->getUser();
31
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' );
39 }
40
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 );
46 }
47 }
48
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'] ?: []
57 );
58
59 if ( !$status->isOK() ) {
60 $this->dieStatus( $status );
61 }
62
63 $ret = [
64 'operation' => $params['operation'],
65 'tag' => $params['tag'],
66 ];
67 if ( !$status->isGood() ) {
68 $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
69 }
70 $ret['success'] = $status->value !== null;
71 if ( $ret['success'] ) {
72 $ret['logid'] = $status->value;
73 }
74
75 $result->addValue( null, $this->getModuleName(), $ret );
76 }
77
78 public function mustBePosted() {
79 return true;
80 }
81
82 public function isWriteMode() {
83 return true;
84 }
85
86 public function getAllowedParams() {
87 return [
88 'operation' => [
89 ApiBase::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
90 ApiBase::PARAM_REQUIRED => true,
91 ],
92 'tag' => [
93 ApiBase::PARAM_TYPE => 'string',
94 ApiBase::PARAM_REQUIRED => true,
95 ],
96 'reason' => [
97 ApiBase::PARAM_TYPE => 'string',
98 ],
99 'ignorewarnings' => [
100 ApiBase::PARAM_TYPE => 'boolean',
101 ApiBase::PARAM_DFLT => false,
102 ],
103 'tags' => [
104 ApiBase::PARAM_TYPE => 'tags',
105 ApiBase::PARAM_ISMULTI => true,
106 ],
107 ];
108 }
109
110 public function needsToken() {
111 return 'csrf';
112 }
113
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',
124 ];
125 }
126
127 public function getHelpUrls() {
128 return 'https://www.mediawiki.org/wiki/API:Tag_management';
129 }
130 }