| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
|
| 66 |
$result = [ |
| 67 |
htmlspecialchars( $blacklisted->getRaw() ), |
| 68 |
htmlspecialchars( $params['title'] ), |
| 69 |
]; |
| 70 |
|
| 71 |
$res = $this->getResult(); |
| 72 |
$res->addValue( 'titleblacklist', 'result', 'blacklisted' ); |
| 73 |
|
| 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 |
|
| 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 |
|
| 99 |
'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account' |
| 100 |
], |
| 101 |
], |
| 102 |
'nooverride' => [ |
| 103 |
ParamValidator::PARAM_DEFAULT => false, |
| 104 |
] |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
} |