Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.72% covered (warning)
78.72%
37 / 47
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiTitleBlacklist
78.72% covered (warning)
78.72%
37 / 47
50.00% covered (danger)
50.00%
2 / 4
9.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
84.00% covered (warning)
84.00%
21 / 25
0.00% covered (danger)
0.00%
0 / 1
6.15
 getAllowedParams
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
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
24namespace MediaWiki\Extension\TitleBlacklist\Api;
25
26use ApiBase;
27use MediaWiki\Extension\TitleBlacklist\TitleBlacklist;
28use MediaWiki\Extension\TitleBlacklist\TitleBlacklistEntry;
29use MediaWiki\Title\Title;
30use Wikimedia\ParamValidator\ParamValidator;
31
32/**
33 * Module to check a title against the blacklist
34 *
35 * @ingroup API
36 * @ingroup Extensions
37 */
38class 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            $res->addValue( 'titleblacklist', 'reason', $this->msg( $message, $result )->page( $title )->text() );
76            $res->addValue( 'titleblacklist', 'message', $message );
77            $res->addValue( 'titleblacklist', 'line', htmlspecialchars( $blacklisted->getRaw() ) );
78        } else {
79            // not blacklisted
80            $this->getResult()->addValue( 'titleblacklist', 'result', 'ok' );
81        }
82    }
83
84    public function getAllowedParams() {
85        return [
86            'title' => [
87                ParamValidator::PARAM_REQUIRED => true,
88            ],
89            'action' => [
90                ParamValidator::PARAM_DEFAULT => 'edit',
91                ParamValidator::PARAM_ISMULTI => false,
92                ParamValidator::PARAM_TYPE => [
93                    // createtalk and createpage are useless as they're treated exactly like create
94                    'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account'
95                ],
96            ],
97            'nooverride' => [
98                ParamValidator::PARAM_DEFAULT => false,
99            ]
100        ];
101    }
102
103    /**
104     * @see ApiBase::getExamplesMessages()
105     * @return string[]
106     */
107    protected function getExamplesMessages() {
108        return [
109            'action=titleblacklist&tbtitle=Foo'
110                => 'apihelp-titleblacklist-example-1',
111            'action=titleblacklist&tbtitle=Bar&tbaction=edit'
112                => 'apihelp-titleblacklist-example-2',
113        ];
114    }
115}