37 private $restrictionStore;
53 parent::__construct( $mainModule, $moduleName );
54 $this->restrictionStore = $restrictionStore;
57 $this->watchlistExpiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
58 $this->watchlistMaxDuration =
59 $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
60 $this->watchlistManager = $watchlistManager;
61 $this->userOptionsLookup = $userOptionsLookup;
68 $titleObj = $pageObj->getTitle();
74 $tags = $params[
'tags'];
77 if ( $tags !==
null ) {
79 if ( !$ableToTag->isOK() ) {
84 $expiry = (array)$params[
'expiry'];
85 if ( count( $expiry ) != count( $params[
'protections'] ) ) {
86 if ( count( $expiry ) == 1 ) {
87 $expiry = array_fill( 0, count( $params[
'protections'] ), $expiry[0] );
90 'apierror-toofewexpiries',
92 count( $params[
'protections'] )
97 $restrictionTypes = $this->restrictionStore->listApplicableRestrictionTypes( $titleObj );
99 $titleObj->getNamespace(),
105 $resultProtections = [];
106 foreach ( $params[
'protections'] as $i => $prot ) {
107 $p = explode(
'=', $prot );
108 $protections[$p[0]] = ( $p[1] ==
'all' ?
'' : $p[1] );
110 if ( $titleObj->exists() && $p[0] ==
'create' ) {
113 if ( !$titleObj->exists() && $p[0] !=
'create' ) {
114 $this->
dieWithError(
'apierror-missingtitle-createonly' );
117 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] !=
'create' ) {
120 if ( !in_array( $p[1], $levels ) && $p[1] !=
'all' ) {
125 $expiryarray[$p[0]] =
'infinity';
127 $exp = strtotime( $expiry[$i] );
128 if ( $exp < 0 || !$exp ) {
136 $expiryarray[$p[0]] = $exp;
138 $resultProtections[] = [
139 $p[0] => $protections[$p[0]],
140 'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]],
'infinite' ),
144 $cascade = $params[
'cascade'];
146 $watch = $params[
'watch'] ?
'watch' : $params[
'watchlist'];
148 $this->
setWatch( $watch, $titleObj, $user,
'watchdefault', $watchlistExpiry );
150 $status = $pageObj->doUpdateRestrictions(
159 if ( !$status->isOK() ) {
163 'title' => $titleObj->getPrefixedText(),
164 'reason' => $params[
'reason']
167 $res[
'cascade'] =
true;
169 $res[
'protections'] = $resultProtections;
171 ApiResult::setIndexedTagName(
$res[
'protections'],
'protection' );
186 ParamValidator::PARAM_TYPE =>
'string',
189 ParamValidator::PARAM_TYPE =>
'integer',
192 ParamValidator::PARAM_ISMULTI =>
true,
193 ParamValidator::PARAM_REQUIRED =>
true,
196 ParamValidator::PARAM_ISMULTI =>
true,
197 ParamValidator::PARAM_ALLOW_DUPLICATES =>
true,
198 ParamValidator::PARAM_DEFAULT =>
'infinite',
202 ParamValidator::PARAM_TYPE =>
'tags',
203 ParamValidator::PARAM_ISMULTI =>
true,
207 ParamValidator::PARAM_DEFAULT =>
false,
208 ParamValidator::PARAM_DEPRECATED =>
true,
218 $title = Title::newMainPage()->getPrefixedText();
219 $mp = rawurlencode(
$title );
222 "action=protect&title={$mp}&token=123ABC&" .
223 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
224 =>
'apihelp-protect-example-protect',
225 "action=protect&title={$mp}&token=123ABC&" .
226 'protections=edit=all|move=all&reason=Lifting%20restrictions'
227 =>
'apihelp-protect-example-unprotect',
228 "action=protect&title={$mp}&token=123ABC&" .
229 'protections=&reason=Lifting%20restrictions'
230 =>
'apihelp-protect-example-unprotect2',
235 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protect';
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
setWatch(string $watch, Title $title, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfIsInfinity( $str)
Determine input string is represents as infinity.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
getModuleName()
Get the name of the module being executed by this instance.
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
This is the main API class, used for both external and internal processing.
isWriteMode()
Indicates whether this module requires write mode.
getExamplesMessages()
Returns usage examples for this module.
needsToken()
Returns the token type this module requires in order to execute.
__construct(ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup, RestrictionStore $restrictionStore)
mustBePosted()
Indicates whether this module must be called with a POST request.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
A class containing constants representing the names of configuration variables.
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...