38 private $restrictionStore;
54 parent::__construct( $mainModule, $moduleName );
55 $this->restrictionStore = $restrictionStore;
58 $this->watchlistExpiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
59 $this->watchlistMaxDuration =
60 $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
61 $this->watchlistManager = $watchlistManager;
62 $this->userOptionsLookup = $userOptionsLookup;
69 $titleObj = $pageObj->getTitle();
75 $tags = $params[
'tags'];
78 if ( $tags !==
null ) {
80 if ( !$ableToTag->isOK() ) {
85 $expiry = (array)$params[
'expiry'];
86 if ( count( $expiry ) != count( $params[
'protections'] ) ) {
87 if ( count( $expiry ) == 1 ) {
88 $expiry = array_fill( 0, count( $params[
'protections'] ), $expiry[0] );
91 'apierror-toofewexpiries',
93 count( $params[
'protections'] )
98 $restrictionTypes = $this->restrictionStore->listApplicableRestrictionTypes( $titleObj );
100 $titleObj->getNamespace(),
106 $resultProtections = [];
107 foreach ( $params[
'protections'] as $i => $prot ) {
108 $p = explode(
'=', $prot );
109 $protections[$p[0]] = ( $p[1] ==
'all' ?
'' : $p[1] );
111 if ( $titleObj->exists() && $p[0] ==
'create' ) {
114 if ( !$titleObj->exists() && $p[0] !=
'create' ) {
115 $this->
dieWithError(
'apierror-missingtitle-createonly' );
118 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] !=
'create' ) {
121 if ( !in_array( $p[1], $levels ) && $p[1] !=
'all' ) {
126 $expiryarray[$p[0]] =
'infinity';
128 $exp = strtotime( $expiry[$i] );
129 if ( $exp < 0 || !$exp ) {
137 $expiryarray[$p[0]] = $exp;
139 $resultProtections[] = [
140 $p[0] => $protections[$p[0]],
141 'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]],
'infinite' ),
145 $cascade = $params[
'cascade'];
147 $watch = $params[
'watch'] ?
'watch' : $params[
'watchlist'];
149 $this->
setWatch( $watch, $titleObj, $user,
'watchdefault', $watchlistExpiry );
151 $status = $pageObj->doUpdateRestrictions(
160 if ( !$status->isOK() ) {
164 'title' => $titleObj->getPrefixedText(),
165 'reason' => $params[
'reason']
168 $res[
'cascade'] =
true;
170 $res[
'protections'] = $resultProtections;
172 ApiResult::setIndexedTagName(
$res[
'protections'],
'protection' );
187 ParamValidator::PARAM_TYPE =>
'string',
190 ParamValidator::PARAM_TYPE =>
'integer',
193 ParamValidator::PARAM_ISMULTI =>
true,
194 ParamValidator::PARAM_REQUIRED =>
true,
197 ParamValidator::PARAM_ISMULTI =>
true,
198 ParamValidator::PARAM_ALLOW_DUPLICATES =>
true,
199 ParamValidator::PARAM_DEFAULT =>
'infinite',
203 ParamValidator::PARAM_TYPE =>
'tags',
204 ParamValidator::PARAM_ISMULTI =>
true,
208 ParamValidator::PARAM_DEFAULT =>
false,
209 ParamValidator::PARAM_DEPRECATED =>
true,
219 $title = Title::newMainPage()->getPrefixedText();
220 $mp = rawurlencode(
$title );
223 "action=protect&title={$mp}&token=123ABC&" .
224 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
225 =>
'apihelp-protect-example-protect',
226 "action=protect&title={$mp}&token=123ABC&" .
227 'protections=edit=all|move=all&reason=Lifting%20restrictions'
228 =>
'apihelp-protect-example-unprotect',
229 "action=protect&title={$mp}&token=123ABC&" .
230 'protections=&reason=Lifting%20restrictions'
231 =>
'apihelp-protect-example-unprotect2',
236 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...