Debian Codegraph @

extensions/TitleBlacklist/includes/Api/ApiTitleBlacklist.php

raw symbols SCIP
1 <?php
2 /**
3 * TitleBlacklist extension API
4 *
5 * Copyright © 2011 Wikimedia Foundation and Ian Baker <[email protected]>
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\Extension\TitleBlacklist\TitleBlacklist;
28 use MediaWiki\Extension\TitleBlacklist\TitleBlacklistEntry;
29 use MediaWiki\Title\Title;
30 use Wikimedia\ParamValidator\ParamValidator;
31
32 /**
33 * Module to check a title against the blacklist
34 *
35 * @ingroup API
36 * @ingroup Extensions
37 */
38 class ApiTitleBlacklist extends ApiBase {
39
40 public function __construct( $mainModule, $moduleName ) {
41 parent::__construct( $mainModule, $moduleName, 'tb' );
42 }
43
44 public function execute() {
45 $params = $this->extractRequestParams();
46 $action = $params['action'];
47 $override = !$params['nooverride'];
48
49 // createtalk and createpage are useless as they're treated exactly like create
50 if ( $action === 'createpage' || $action === 'createtalk' ) {
51 $action = 'create';
52 }
53
54 $title = Title::newFromText( $params['title'] );
55 if ( !$title ) {
56 $this->dieWithError(
57 [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ]
58 );
59 }
60
61 $blacklisted = TitleBlacklist::singleton()->userCannot(
62 $title, $this->getUser(), $action, $override
63 );
64 if ( $blacklisted instanceof TitleBlacklistEntry ) {
65 // this title is blacklisted.
66 $result = [
67 htmlspecialchars( $blacklisted->getRaw() ),
68 htmlspecialchars( $params['title'] ),
69 ];
70
71 $res = $this->getResult();
72 $res->addValue( 'titleblacklist', 'result', 'blacklisted' );
73 // there aren't any messages for create(talk|page), using edit for those instead
74 $message = $blacklisted->getErrorMessage( $action !== 'create' ? $action : 'edit' );
75 $messageObj = $this->msg( $message, $result )->page( $title );
76 if ( $this->getErrorFormatter()->getFormat() === 'html' ) {
77 $res->addValue( 'titleblacklist', 'reason', $messageObj->parse() );
78 } else {
79 $res->addValue( 'titleblacklist', 'reason', $messageObj->text() );
80 }
81 $res->addValue( 'titleblacklist', 'message', $message );
82 $res->addValue( 'titleblacklist', 'line', htmlspecialchars( $blacklisted->getRaw() ) );
83 } else {
84 // not blacklisted
85 $this->getResult()->addValue( 'titleblacklist', 'result', 'ok' );
86 }
87 }
88
89 public function getAllowedParams() {
90 return [
91 'title' => [
92 ParamValidator::PARAM_REQUIRED => true,
93 ],
94 'action' => [
95 ParamValidator::PARAM_DEFAULT => 'edit',
96 ParamValidator::PARAM_ISMULTI => false,
97 ParamValidator::PARAM_TYPE => [
98 // createtalk and createpage are useless as they're treated exactly like create
99 'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account'
100 ],
101 ],
102 'nooverride' => [
103 ParamValidator::PARAM_DEFAULT => false,
104 ]
105 ];
106 }
107
108 /**
109 * @see ApiBase::getExamplesMessages()
110 * @return string[]
111 */
112 protected function getExamplesMessages() {
113 return [
114 'action=titleblacklist&tbtitle=Foo'
115 => 'apihelp-titleblacklist-example-1',
116 'action=titleblacklist&tbtitle=Bar&tbaction=edit'
117 => 'apihelp-titleblacklist-example-2',
118 ];
119 }
120 }