MediaWiki  1.34.0
ApiQueryTitleBlacklist.php
Go to the documentation of this file.
1 <?php
31 
32  public function __construct( $query, $moduleName ) {
33  parent::__construct( $query, $moduleName, 'tb' );
34  }
35 
36  public function execute() {
37  $params = $this->extractRequestParams();
38  $action = $params['action'];
39  $override = !$params['nooverride'];
40 
41  // createtalk and createpage are useless as they're treated exactly like create
42  if ( $action === 'createpage' || $action === 'createtalk' ) {
43  $action = 'create';
44  }
45 
46  $title = Title::newFromText( $params['title'] );
47  if ( !$title ) {
48  $this->dieWithError(
49  [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ]
50  );
51  }
52 
53  $blacklisted = TitleBlacklist::singleton()->userCannot(
54  $title, $this->getUser(), $action, $override
55  );
56  if ( $blacklisted instanceof TitleBlacklistEntry ) {
57  // this title is blacklisted.
58  $result = [
59  htmlspecialchars( $blacklisted->getRaw() ),
60  htmlspecialchars( $params['title'] ),
61  ];
62 
63  $res = $this->getResult();
64  $res->addValue( 'titleblacklist', 'result', 'blacklisted' );
65  // there aren't any messages for create(talk|page), using edit for those instead
66  $message = $blacklisted->getErrorMessage( $action !== 'create' ? $action : 'edit' );
67  $res->addValue( 'titleblacklist', 'reason', wfMessage( $message, $result )->text() );
68  $res->addValue( 'titleblacklist', 'message', $message );
69  $res->addValue( 'titleblacklist', 'line', htmlspecialchars( $blacklisted->getRaw() ) );
70  } else {
71  // not blacklisted
72  $this->getResult()->addValue( 'titleblacklist', 'result', 'ok' );
73  }
74  }
75 
76  public function getAllowedParams() {
77  return [
78  'title' => [
80  ],
81  'action' => [
82  ApiBase::PARAM_DFLT => 'edit',
83  ApiBase::PARAM_ISMULTI => false,
85  // createtalk and createpage are useless as they're treated exactly like create
86  'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account'
87  ],
88  ],
89  'nooverride' => [
90  ApiBase::PARAM_DFLT => false,
91  ]
92  ];
93  }
94 
99  protected function getExamplesMessages() {
100  return [
101  'action=titleblacklist&tbtitle=Foo'
102  => 'apihelp-titleblacklist-example-1',
103  'action=titleblacklist&tbtitle=Bar&tbaction=edit'
104  => 'apihelp-titleblacklist-example-2',
105  ];
106  }
107 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
TitleBlacklist\singleton
static singleton()
Get an instance of this class.
Definition: TitleBlacklist.php:36
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
$res
$res
Definition: testCompression.php:52
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
TitleBlacklistEntry
Represents a title blacklist entry.
Definition: TitleBlacklistEntry.php:19
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1551
ApiQueryTitleBlacklist\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryTitleBlacklist.php:36
ApiQueryTitleBlacklist\__construct
__construct( $query, $moduleName)
Definition: ApiQueryTitleBlacklist.php:32
ApiQueryTitleBlacklist
TitleBlacklist extension API.
Definition: ApiQueryTitleBlacklist.php:30
ApiQueryTitleBlacklist\getExamplesMessages
getExamplesMessages()
Definition: ApiQueryTitleBlacklist.php:99
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
ApiQueryTitleBlacklist\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryTitleBlacklist.php:76