| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use ChangeTags; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\Permissions\RestrictionStore; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 30 |
use MediaWiki\Watchlist\WatchlistManager; |
| 31 |
use Wikimedia\ParamValidator\ParamValidator; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ApiProtect extends ApiBase { |
| 37 |
|
| 38 |
use ApiWatchlistTrait; |
| 39 |
|
| 40 |
private RestrictionStore $restrictionStore; |
| 41 |
|
| 42 |
public function __construct( |
| 43 |
ApiMain $mainModule, |
| 44 |
string $moduleName, |
| 45 |
WatchlistManager $watchlistManager, |
| 46 |
UserOptionsLookup $userOptionsLookup, |
| 47 |
RestrictionStore $restrictionStore |
| 48 |
) { |
| 49 |
parent::__construct( $mainModule, $moduleName ); |
| 50 |
$this->restrictionStore = $restrictionStore; |
| 51 |
|
| 52 |
|
| 53 |
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 54 |
$this->watchlistMaxDuration = |
| 55 |
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 56 |
$this->watchlistManager = $watchlistManager; |
| 57 |
$this->userOptionsLookup = $userOptionsLookup; |
| 58 |
} |
| 59 |
|
| 60 |
public function execute() { |
| 61 |
$params = $this->extractRequestParams(); |
| 62 |
|
| 63 |
$pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); |
| 64 |
$titleObj = $pageObj->getTitle(); |
| 65 |
$this->getErrorFormatter()->setContextTitle( $titleObj ); |
| 66 |
|
| 67 |
$this->checkTitleUserPermissions( $titleObj, 'protect' ); |
| 68 |
|
| 69 |
$user = $this->getUser(); |
| 70 |
$tags = $params['tags']; |
| 71 |
|
| 72 |
|
| 73 |
if ( $tags !== null ) { |
| 74 |
$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() ); |
| 75 |
if ( !$ableToTag->isOK() ) { |
| 76 |
$this->dieStatus( $ableToTag ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$expiry = (array)$params['expiry']; |
| 81 |
if ( count( $expiry ) != count( $params['protections'] ) ) { |
| 82 |
if ( count( $expiry ) == 1 ) { |
| 83 |
$expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] ); |
| 84 |
} else { |
| 85 |
$this->dieWithError( [ |
| 86 |
'apierror-toofewexpiries', |
| 87 |
count( $expiry ), |
| 88 |
count( $params['protections'] ) |
| 89 |
] ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$restrictionTypes = $this->restrictionStore->listApplicableRestrictionTypes( $titleObj ); |
| 94 |
$levels = $this->getPermissionManager()->getNamespaceRestrictionLevels( |
| 95 |
$titleObj->getNamespace(), |
| 96 |
$user |
| 97 |
); |
| 98 |
|
| 99 |
$protections = []; |
| 100 |
$expiryarray = []; |
| 101 |
$resultProtections = []; |
| 102 |
foreach ( $params['protections'] as $i => $prot ) { |
| 103 |
$p = explode( '=', $prot ); |
| 104 |
$protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] ); |
| 105 |
|
| 106 |
if ( $titleObj->exists() && $p[0] == 'create' ) { |
| 107 |
$this->dieWithError( 'apierror-create-titleexists' ); |
| 108 |
} |
| 109 |
if ( !$titleObj->exists() && $p[0] != 'create' ) { |
| 110 |
$this->dieWithError( 'apierror-missingtitle-createonly' ); |
| 111 |
} |
| 112 |
|
| 113 |
if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) { |
| 114 |
$this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] ); |
| 115 |
} |
| 116 |
if ( !in_array( $p[1], $levels ) && $p[1] != 'all' ) { |
| 117 |
$this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( wfIsInfinity( $expiry[$i] ) ) { |
| 121 |
$expiryarray[$p[0]] = 'infinity'; |
| 122 |
} else { |
| 123 |
$exp = strtotime( $expiry[$i] ); |
| 124 |
if ( $exp < 0 || !$exp ) { |
| 125 |
$this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] ); |
| 126 |
} |
| 127 |
|
| 128 |
$exp = wfTimestamp( TS_MW, $exp ); |
| 129 |
if ( $exp < wfTimestampNow() ) { |
| 130 |
$this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] ); |
| 131 |
} |
| 132 |
$expiryarray[$p[0]] = $exp; |
| 133 |
} |
| 134 |
$resultProtections[] = [ |
| 135 |
$p[0] => $protections[$p[0]], |
| 136 |
'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]], 'infinite' ), |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
$cascade = $params['cascade']; |
| 141 |
|
| 142 |
$watch = $params['watch'] ? 'watch' : $params['watchlist']; |
| 143 |
$watchlistExpiry = $this->getExpiryFromParams( $params ); |
| 144 |
$this->setWatch( $watch, $titleObj, $user, 'watchdefault', $watchlistExpiry ); |
| 145 |
|
| 146 |
$status = $pageObj->doUpdateRestrictions( |
| 147 |
$protections, |
| 148 |
$expiryarray, |
| 149 |
$cascade, |
| 150 |
$params['reason'], |
| 151 |
$user, |
| 152 |
$tags ?? [] |
| 153 |
); |
| 154 |
|
| 155 |
if ( !$status->isOK() ) { |
| 156 |
$this->dieStatus( $status ); |
| 157 |
} |
| 158 |
$res = [ |
| 159 |
'title' => $titleObj->getPrefixedText(), |
| 160 |
'reason' => $params['reason'] |
| 161 |
]; |
| 162 |
if ( $cascade ) { |
| 163 |
$res['cascade'] = true; |
| 164 |
} |
| 165 |
$res['protections'] = $resultProtections; |
| 166 |
$result = $this->getResult(); |
| 167 |
ApiResult::setIndexedTagName( $res['protections'], 'protection' ); |
| 168 |
$result->addValue( null, $this->getModuleName(), $res ); |
| 169 |
} |
| 170 |
|
| 171 |
public function mustBePosted() { |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
public function isWriteMode() { |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
public function getAllowedParams() { |
| 180 |
return [ |
| 181 |
'title' => [ |
| 182 |
ParamValidator::PARAM_TYPE => 'string', |
| 183 |
], |
| 184 |
'pageid' => [ |
| 185 |
ParamValidator::PARAM_TYPE => 'integer', |
| 186 |
], |
| 187 |
'protections' => [ |
| 188 |
ParamValidator::PARAM_ISMULTI => true, |
| 189 |
ParamValidator::PARAM_REQUIRED => true, |
| 190 |
], |
| 191 |
'expiry' => [ |
| 192 |
ParamValidator::PARAM_ISMULTI => true, |
| 193 |
ParamValidator::PARAM_ALLOW_DUPLICATES => true, |
| 194 |
ParamValidator::PARAM_DEFAULT => 'infinite', |
| 195 |
], |
| 196 |
'reason' => '', |
| 197 |
'tags' => [ |
| 198 |
ParamValidator::PARAM_TYPE => 'tags', |
| 199 |
ParamValidator::PARAM_ISMULTI => true, |
| 200 |
], |
| 201 |
'cascade' => false, |
| 202 |
'watch' => [ |
| 203 |
ParamValidator::PARAM_DEFAULT => false, |
| 204 |
ParamValidator::PARAM_DEPRECATED => true, |
| 205 |
], |
| 206 |
] + $this->getWatchlistParams(); |
| 207 |
} |
| 208 |
|
| 209 |
public function needsToken() { |
| 210 |
return 'csrf'; |
| 211 |
} |
| 212 |
|
| 213 |
protected function getExamplesMessages() { |
| 214 |
$title = Title::newMainPage()->getPrefixedText(); |
| 215 |
$mp = rawurlencode( $title ); |
| 216 |
|
| 217 |
return [ |
| 218 |
"action=protect&title={$mp}&token=123ABC&" . |
| 219 |
'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never' |
| 220 |
=> 'apihelp-protect-example-protect', |
| 221 |
"action=protect&title={$mp}&token=123ABC&" . |
| 222 |
'protections=edit=all|move=all&reason=Lifting%20restrictions' |
| 223 |
=> 'apihelp-protect-example-unprotect', |
| 224 |
"action=protect&title={$mp}&token=123ABC&" . |
| 225 |
'protections=&reason=Lifting%20restrictions' |
| 226 |
=> 'apihelp-protect-example-unprotect2', |
| 227 |
]; |
| 228 |
} |
| 229 |
|
| 230 |
public function getHelpUrls() { |
| 231 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protect'; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
class_alias( ApiProtect::class, 'ApiProtect' ); |