Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.00% |
39 / 50 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
ApiTitleBlacklist | |
78.00% |
39 / 50 |
|
50.00% |
2 / 4 |
11.06 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
82.14% |
23 / 28 |
|
0.00% |
0 / 1 |
7.28 | |||
getAllowedParams | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * TitleBlacklist extension API |
4 | * |
5 | * Copyright © 2011 Wikimedia Foundation and Ian Baker <ian@wikimedia.org> |
6 | * Based on code by Victor Vasiliev, Bryan Tong Minh, Roan Kattouw, and Alex Z. |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; if not, write to the Free Software Foundation, Inc., |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | * http://www.gnu.org/copyleft/gpl.html |
22 | */ |
23 | |
24 | namespace MediaWiki\Extension\TitleBlacklist\Api; |
25 | |
26 | use MediaWiki\Api\ApiBase; |
27 | use MediaWiki\Api\ApiMain; |
28 | use MediaWiki\Extension\TitleBlacklist\TitleBlacklist; |
29 | use MediaWiki\Extension\TitleBlacklist\TitleBlacklistEntry; |
30 | use MediaWiki\Title\Title; |
31 | use Wikimedia\ParamValidator\ParamValidator; |
32 | |
33 | /** |
34 | * Module to check a title against the blacklist |
35 | * |
36 | * @ingroup API |
37 | * @ingroup Extensions |
38 | */ |
39 | class ApiTitleBlacklist extends ApiBase { |
40 | |
41 | public function __construct( ApiMain $mainModule, string $moduleName ) { |
42 | parent::__construct( $mainModule, $moduleName, 'tb' ); |
43 | } |
44 | |
45 | public function execute() { |
46 | $params = $this->extractRequestParams(); |
47 | $action = $params['action']; |
48 | $override = !$params['nooverride']; |
49 | |
50 | // createtalk and createpage are useless as they're treated exactly like create |
51 | if ( $action === 'createpage' || $action === 'createtalk' ) { |
52 | $action = 'create'; |
53 | } |
54 | |
55 | $title = Title::newFromText( $params['title'] ); |
56 | if ( !$title ) { |
57 | $this->dieWithError( |
58 | [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] |
59 | ); |
60 | } |
61 | |
62 | $blacklisted = TitleBlacklist::singleton()->userCannot( |
63 | $title, $this->getUser(), $action, $override |
64 | ); |
65 | if ( $blacklisted instanceof TitleBlacklistEntry ) { |
66 | // this title is blacklisted. |
67 | $result = [ |
68 | htmlspecialchars( $blacklisted->getRaw() ), |
69 | htmlspecialchars( $params['title'] ), |
70 | ]; |
71 | |
72 | $res = $this->getResult(); |
73 | $res->addValue( 'titleblacklist', 'result', 'blacklisted' ); |
74 | // there aren't any messages for create(talk|page), using edit for those instead |
75 | $message = $blacklisted->getErrorMessage( $action !== 'create' ? $action : 'edit' ); |
76 | $messageObj = $this->msg( $message, $result )->page( $title ); |
77 | if ( $this->getErrorFormatter()->getFormat() === 'html' ) { |
78 | $res->addValue( 'titleblacklist', 'reason', $messageObj->parse() ); |
79 | } else { |
80 | $res->addValue( 'titleblacklist', 'reason', $messageObj->text() ); |
81 | } |
82 | $res->addValue( 'titleblacklist', 'message', $message ); |
83 | $res->addValue( 'titleblacklist', 'line', htmlspecialchars( $blacklisted->getRaw() ) ); |
84 | } else { |
85 | // not blacklisted |
86 | $this->getResult()->addValue( 'titleblacklist', 'result', 'ok' ); |
87 | } |
88 | } |
89 | |
90 | public function getAllowedParams() { |
91 | return [ |
92 | 'title' => [ |
93 | ParamValidator::PARAM_REQUIRED => true, |
94 | ], |
95 | 'action' => [ |
96 | ParamValidator::PARAM_DEFAULT => 'edit', |
97 | ParamValidator::PARAM_ISMULTI => false, |
98 | ParamValidator::PARAM_TYPE => [ |
99 | // createtalk and createpage are useless as they're treated exactly like create |
100 | 'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account' |
101 | ], |
102 | ], |
103 | 'nooverride' => [ |
104 | ParamValidator::PARAM_DEFAULT => false, |
105 | ] |
106 | ]; |
107 | } |
108 | |
109 | /** |
110 | * @see ApiBase::getExamplesMessages() |
111 | * @return string[] |
112 | */ |
113 | protected function getExamplesMessages() { |
114 | return [ |
115 | 'action=titleblacklist&tbtitle=Foo' |
116 | => 'apihelp-titleblacklist-example-1', |
117 | 'action=titleblacklist&tbtitle=Bar&tbaction=edit' |
118 | => 'apihelp-titleblacklist-example-2', |
119 | ]; |
120 | } |
121 | } |