2011-07-21 22:08:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TitleBlacklist extension API
|
|
|
|
*
|
|
|
|
* Copyright © 2011 Wikimedia Foundation and Ian Baker <ian@wikimedia.org>
|
|
|
|
* Based on code by Victor Vasiliev, Bryan Tong Minh, Roan Kattouw, and Alex Z.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
2022-04-08 13:20:08 +00:00
|
|
|
namespace MediaWiki\Extension\TitleBlacklist\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use MediaWiki\Extension\TitleBlacklist\TitleBlacklist;
|
|
|
|
use MediaWiki\Extension\TitleBlacklist\TitleBlacklistEntry;
|
2023-08-19 04:20:04 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-04-03 23:30:13 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
2011-07-21 22:08:44 +00:00
|
|
|
/**
|
2023-07-04 20:34:57 +00:00
|
|
|
* Module to check a title against the blacklist
|
2011-07-21 22:08:44 +00:00
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2023-07-04 20:34:57 +00:00
|
|
|
class ApiTitleBlacklist extends ApiBase {
|
2011-07-21 22:08:44 +00:00
|
|
|
|
2023-07-04 20:34:57 +00:00
|
|
|
public function __construct( $mainModule, $moduleName ) {
|
|
|
|
parent::__construct( $mainModule, $moduleName, 'tb' );
|
2011-07-21 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$action = $params['action'];
|
2016-11-15 13:53:25 +00:00
|
|
|
$override = !$params['nooverride'];
|
2011-07-21 22:08:44 +00:00
|
|
|
|
2012-10-17 21:52:05 +00:00
|
|
|
// createtalk and createpage are useless as they're treated exactly like create
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $action === 'createpage' || $action === 'createtalk' ) {
|
2011-07-21 22:08:44 +00:00
|
|
|
$action = 'create';
|
|
|
|
}
|
|
|
|
|
2011-08-22 20:56:19 +00:00
|
|
|
$title = Title::newFromText( $params['title'] );
|
|
|
|
if ( !$title ) {
|
2018-03-09 12:08:06 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ]
|
|
|
|
);
|
2011-08-22 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 15:43:58 +00:00
|
|
|
$blacklisted = TitleBlacklist::singleton()->userCannot(
|
|
|
|
$title, $this->getUser(), $action, $override
|
|
|
|
);
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $blacklisted instanceof TitleBlacklistEntry ) {
|
2011-07-21 22:08:44 +00:00
|
|
|
// this title is blacklisted.
|
2017-06-06 16:18:36 +00:00
|
|
|
$result = [
|
2011-07-21 22:08:44 +00:00
|
|
|
htmlspecialchars( $blacklisted->getRaw() ),
|
|
|
|
htmlspecialchars( $params['title'] ),
|
2017-06-06 16:18:36 +00:00
|
|
|
];
|
2011-08-22 20:27:57 +00:00
|
|
|
|
2011-08-22 20:29:29 +00:00
|
|
|
$res = $this->getResult();
|
|
|
|
$res->addValue( 'titleblacklist', 'result', 'blacklisted' );
|
2012-10-17 21:52:05 +00:00
|
|
|
// there aren't any messages for create(talk|page), using edit for those instead
|
|
|
|
$message = $blacklisted->getErrorMessage( $action !== 'create' ? $action : 'edit' );
|
2023-07-02 08:29:12 +00:00
|
|
|
$res->addValue( 'titleblacklist', 'reason', $this->msg( $message, $result )->page( $title )->text() );
|
2011-08-22 20:29:29 +00:00
|
|
|
$res->addValue( 'titleblacklist', 'message', $message );
|
|
|
|
$res->addValue( 'titleblacklist', 'line', htmlspecialchars( $blacklisted->getRaw() ) );
|
2011-07-21 22:08:44 +00:00
|
|
|
} else {
|
|
|
|
// not blacklisted
|
|
|
|
$this->getResult()->addValue( 'titleblacklist', 'result', 'ok' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2017-06-06 16:18:36 +00:00
|
|
|
return [
|
|
|
|
'title' => [
|
2022-04-03 23:30:13 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
2017-06-06 16:18:36 +00:00
|
|
|
],
|
|
|
|
'action' => [
|
2022-04-03 23:30:13 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => 'edit',
|
|
|
|
ParamValidator::PARAM_ISMULTI => false,
|
|
|
|
ParamValidator::PARAM_TYPE => [
|
2012-10-17 21:52:05 +00:00
|
|
|
// createtalk and createpage are useless as they're treated exactly like create
|
|
|
|
'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account'
|
2017-06-06 16:18:36 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'nooverride' => [
|
2022-04-03 23:30:13 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => false,
|
2017-06-06 16:18:36 +00:00
|
|
|
]
|
|
|
|
];
|
2011-07-21 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:17:19 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2019-06-05 16:27:36 +00:00
|
|
|
* @return string[]
|
2014-10-29 19:17:19 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2017-06-06 16:18:36 +00:00
|
|
|
return [
|
2014-10-29 19:17:19 +00:00
|
|
|
'action=titleblacklist&tbtitle=Foo'
|
|
|
|
=> 'apihelp-titleblacklist-example-1',
|
|
|
|
'action=titleblacklist&tbtitle=Bar&tbaction=edit'
|
|
|
|
=> 'apihelp-titleblacklist-example-2',
|
2017-06-06 16:18:36 +00:00
|
|
|
];
|
2011-07-21 22:08:44 +00:00
|
|
|
}
|
|
|
|
}
|