11use InvalidArgumentException;
22use Wikimedia\Timestamp\TimestampFormat as TS;
39 parent::__construct( $mainModule, $moduleName );
43 $this->watchlistMaxDuration =
45 $this->watchlistManager = $watchlistManager;
46 $this->watchedItemStore = $watchedItemStore;
47 $this->userOptionsLookup = $userOptionsLookup;
54 $titleObj = $pageObj->getTitle();
60 $tags = $params[
'tags'];
63 if ( $tags !==
null ) {
64 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->
getAuthority() );
65 if ( !$ableToTag->isOK() ) {
70 $expiry = (array)$params[
'expiry'];
71 if ( count( $expiry ) != count( $params[
'protections'] ) ) {
72 if ( count( $expiry ) == 1 ) {
73 $expiry = array_fill( 0, count( $params[
'protections'] ), $expiry[0] );
76 'apierror-toofewexpiries',
78 count( $params[
'protections'] )
83 $restrictionTypes = $this->restrictionStore->listApplicableRestrictionTypes( $titleObj );
85 $titleObj->getNamespace(),
91 $resultProtections = [];
92 foreach ( $params[
'protections'] as $i => $prot ) {
93 $p = explode(
'=', $prot );
94 $protections[$p[0]] = ( $p[1] ==
'all' ?
'' : $p[1] );
96 if ( $titleObj->exists() && $p[0] ==
'create' ) {
99 if ( !$titleObj->exists() && $p[0] !=
'create' ) {
100 $this->
dieWithError(
'apierror-missingtitle-createonly' );
103 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] !=
'create' ) {
106 if ( !in_array( $p[1], $levels ) && $p[1] !=
'all' ) {
111 $expiries[$p[0]] = ExpiryDef::normalizeExpiry( $expiry[$i], TS::MW );
112 }
catch ( InvalidArgumentException ) {
115 if ( $expiries[$p[0]] < MWTimestamp::now( TS::MW ) ) {
119 $resultProtections[] = [
120 $p[0] => $protections[$p[0]],
125 $cascade = $params[
'cascade'];
127 $watch = $params[
'watch'] ?
'watch' : $params[
'watchlist'];
129 $this->
setWatch( $watch, $titleObj, $user,
'watchdefault', $watchlistExpiry );
131 $status = $pageObj->doUpdateRestrictions(
140 if ( !$status->isOK() ) {
144 'title' => $titleObj->getPrefixedText(),
145 'reason' => $params[
'reason']
148 $res[
'cascade'] =
true;
150 $res[
'protections'] = $resultProtections;
170 ParamValidator::PARAM_TYPE =>
'string',
173 ParamValidator::PARAM_TYPE =>
'integer',
176 ParamValidator::PARAM_ISMULTI =>
true,
177 ParamValidator::PARAM_REQUIRED =>
true,
180 ParamValidator::PARAM_ISMULTI =>
true,
181 ParamValidator::PARAM_ALLOW_DUPLICATES =>
true,
182 ParamValidator::PARAM_DEFAULT =>
'infinite',
186 ParamValidator::PARAM_TYPE =>
'tags',
187 ParamValidator::PARAM_ISMULTI =>
true,
191 ParamValidator::PARAM_DEFAULT =>
false,
192 ParamValidator::PARAM_DEPRECATED =>
true,
204 $title = Title::newMainPage()->getPrefixedText();
205 $mp = rawurlencode( $title );
208 "action=protect&title={$mp}&token=123ABC&" .
209 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
210 =>
'apihelp-protect-example-protect',
211 "action=protect&title={$mp}&token=123ABC&" .
212 'protections=edit=all|move=all&reason=Lifting%20restrictions'
213 =>
'apihelp-protect-example-unprotect',
214 "action=protect&title={$mp}&token=123ABC&" .
215 'protections=&reason=Lifting%20restrictions'
216 =>
'apihelp-protect-example-unprotect2',
222 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protect';
227class_alias( ApiProtect::class,
'ApiProtect' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This is the main API class, used for both external and internal processing.
A class containing constants representing the names of configuration variables.
const WatchlistExpiry
Name constant for the WatchlistExpiry setting, for use with Config::get()
const WatchlistExpiryMaxDuration
Name constant for the WatchlistExpiryMaxDuration setting, for use with Config::get()
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...