104 $this->mArticle = $article;
105 $this->mTitle = $article->
getTitle();
107 $this->mRequest = $this->mContext->getRequest();
108 $this->mPerformer = $this->mContext->getAuthority();
109 $this->mOut = $this->mContext->getOutput();
110 $this->mLang = $this->mContext->getLanguage();
113 $this->permManager = $services->getPermissionManager();
114 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
115 $this->watchlistManager = $services->getWatchlistManager();
116 $this->titleFormatter = $services->getTitleFormatter();
117 $this->restrictionStore = $services->getRestrictionStore();
118 $this->mApplicableTypes = $this->restrictionStore->listApplicableRestrictionTypes( $this->mTitle );
122 $this->mPermStatus = PermissionStatus::newEmpty();
123 if ( $this->mRequest->wasPosted() ) {
124 $this->mPerformer->authorizeWrite(
'protect', $this->mTitle, $this->mPermStatus );
126 $this->mPerformer->authorizeRead(
'protect', $this->mTitle, $this->mPermStatus );
128 $readOnlyMode = $services->getReadOnlyMode();
129 if ( $readOnlyMode->isReadOnly() ) {
130 $this->mPermStatus->fatal(
'readonlytext', $readOnlyMode->getReason() );
132 $this->disabled = !$this->mPermStatus->isGood();
133 $this->disabledAttrib = $this->disabled ? [
'disabled' =>
'disabled' ] : [];
141 private function loadData() {
142 $levels = $this->permManager->getNamespaceRestrictionLevels(
143 $this->mTitle->getNamespace(), $this->mPerformer->getUser()
146 $this->mCascade = $this->restrictionStore->areRestrictionsCascading( $this->mTitle );
147 $this->mReason = $this->mRequest->getText(
'mwProtect-reason' );
148 $this->mReasonSelection = $this->mRequest->getText(
'wpProtectReasonSelection' );
149 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade', $this->mCascade );
151 foreach ( $this->mApplicableTypes as $action ) {
156 $this->mRestrictions[$action] = implode(
'',
157 $this->restrictionStore->getRestrictions( $this->mTitle, $action ) );
159 if ( !$this->mRestrictions[$action] ) {
161 $existingExpiry =
'';
163 $existingExpiry = $this->restrictionStore->getRestrictionExpiry( $this->mTitle, $action );
165 $this->mExistingExpiry[$action] = $existingExpiry;
167 $requestExpiry = $this->mRequest->getText(
"mwProtect-expiry-$action" );
168 $requestExpirySelection = $this->mRequest->getVal(
"wpProtectExpirySelection-$action" );
170 if ( $requestExpiry ) {
172 $this->mExpiry[$action] = $requestExpiry;
173 $this->mExpirySelection[$action] =
'othertime';
174 } elseif ( $requestExpirySelection ) {
176 $this->mExpiry[$action] =
'';
177 $this->mExpirySelection[$action] = $requestExpirySelection;
178 } elseif ( $existingExpiry ) {
180 $this->mExpiry[$action] =
'';
181 $this->mExpirySelection[$action] = $existingExpiry;
185 $this->mExpiry[$action] =
'';
186 $this->mExpirySelection[$action] =
'infinite';
189 $val = $this->mRequest->getVal(
"mwProtect-level-$action" );
190 if ( $val !==
null && in_array( $val, $levels ) ) {
191 $this->mRestrictions[$action] = $val;
204 private function getExpiry( $action ) {
205 if ( $this->mExpirySelection[$action] ==
'existing' ) {
206 return $this->mExistingExpiry[$action];
207 } elseif ( $this->mExpirySelection[$action] ==
'othertime' ) {
208 $value = $this->mExpiry[$action];
210 $value = $this->mExpirySelection[$action];
213 return ExpiryDef::normalizeExpiry( $value, TS_MW );
214 }
catch ( InvalidArgumentException $e ) {
224 $this->permManager->getNamespaceRestrictionLevels(
225 $this->mTitle->getNamespace()
228 throw new ErrorPageError(
'protect-badnamespace-title',
'protect-badnamespace-text' );
231 if ( $this->mRequest->wasPosted() ) {
232 if ( $this->save() ) {
233 $q = $this->mArticle->getPage()->isRedirect() ?
'redirect=no' :
'';
234 $this->mOut->redirect( $this->mTitle->getFullURL( $q ) );
247 private function show( $err =
null ) {
249 $out->setRobotPolicy(
'noindex,nofollow' );
250 $out->addBacklinkSubtitle( $this->mTitle );
252 if ( is_array( $err ) ) {
253 $out->addHTML( Html::errorBox( $out->msg( ...$err )->parse() ) );
254 } elseif ( is_string( $err ) ) {
255 $out->addHTML( Html::errorBox( $err ) );
258 if ( $this->mApplicableTypes === [] ) {
261 $out->setPageTitleMsg( $this->mContext->msg(
262 'protect-norestrictiontypes-title'
264 $this->mTitle->getPrefixedText()
266 $out->addWikiTextAsInterface(
267 $this->mContext->msg(
'protect-norestrictiontypes-text' )->plain()
271 $this->showLogExtract();
276 [ $cascadeSources, ] =
277 $this->restrictionStore->getCascadeProtectionSources( $this->mTitle );
278 if ( count( $cascadeSources ) > 0 ) {
281 foreach ( $cascadeSources as $pageIdentity ) {
282 $titles .=
'* [[:' . $this->titleFormatter->getPrefixedText( $pageIdentity ) .
"]]\n";
287 "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles .
"</div>",
288 [
'protect-cascadeon', count( $cascadeSources ) ]
292 # Show an appropriate message if the user isn't allowed or able to change
293 # the protection settings at this time
294 if ( $this->disabled ) {
295 $out->setPageTitleMsg(
296 $this->mContext->msg(
'protect-title-notallowed' )->plaintextParams( $this->mTitle->getPrefixedText() )
298 $out->addWikiTextAsInterface(
299 $out->formatPermissionStatus( $this->mPermStatus,
'protect' )
302 $out->setPageTitleMsg(
303 $this->mContext->msg(
'protect-title' )->plaintextParams( $this->mTitle->getPrefixedText() )
305 $out->addWikiMsg(
'protect-text',
309 $out->addHTML( $this->buildForm() );
310 $this->showLogExtract();
318 private function save() {
320 if ( $this->disabled ) {
325 $token = $this->mRequest->getVal(
'wpEditToken' );
328 ->newFromAuthority( $this->mPerformer );
329 if ( !$legacyUser->matchEditToken( $token, [
'protect', $this->mTitle->getPrefixedDBkey() ] ) ) {
330 $this->show( [
'sessionfailure' ] );
334 # Create reason string. Use list and/or custom string.
336 if ( $reasonstr !=
'other' && $this->mReason !=
'' ) {
338 $reasonstr .= $this->mContext->msg(
'colon-separator' )->text() .
$this->mReason;
339 } elseif ( $reasonstr ==
'other' ) {
344 foreach ( $this->mApplicableTypes as $action ) {
345 $expiry[$action] = $this->getExpiry( $action );
346 if ( empty( $this->mRestrictions[$action] ) ) {
350 if ( !$expiry[$action] ) {
351 $this->show( [
'protect_expiry_invalid' ] );
355 $this->show( [
'protect_expiry_old' ] );
360 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade' );
362 $status = $this->mArticle->getPage()->doUpdateRestrictions(
363 $this->mRestrictions,
367 $this->mPerformer->getUser()
370 if ( !$status->isOK() ) {
371 $this->show( $this->mOut->parseInlineAsInterface(
372 $status->getWikiText(
false,
false, $this->mLang )
384 if ( !$this->hookRunner->onProtectionForm__save( $this->mArticle, $errorMsg, $reasonstr ) ) {
385 if ( $errorMsg ==
'' ) {
386 $errorMsg = [
'hookaborted' ];
389 if ( $errorMsg !=
'' ) {
390 $this->show( $errorMsg );
394 $this->watchlistManager->setWatch(
395 $this->mRequest->getCheck(
'mwProtectWatch' ),
408 private function buildForm() {
409 $this->mOut->enableOOUI();
412 if ( !$this->disabled ) {
413 $this->mOut->addModules(
'mediawiki.action.protect' );
414 $this->mOut->addModuleStyles(
'mediawiki.action.styles' );
416 $scExpiryOptions = $this->mContext->msg(
'protect-expiry-options' )->inContentLanguage()->text();
417 $levels = $this->permManager->getNamespaceRestrictionLevels(
418 $this->mTitle->getNamespace(),
419 $this->disabled ?
null : $this->mPerformer->
getUser()
423 foreach ( $this->mRestrictions as $action => $selected ) {
426 $section =
'restriction-' . $action;
427 $id =
'mwProtect-level-' . $action;
429 foreach ( $levels as $key ) {
430 $options[$this->getOptionLabel( $key )] = $key;
436 'default' => $selected,
438 'size' => count( $levels ),
439 'options' => $options,
441 'section' => $section,
446 if ( $this->mExistingExpiry[$action] ) {
447 if ( $this->mExistingExpiry[$action] ==
'infinity' ) {
448 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry-infinity' );
450 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry' )
451 ->dateTimeParams( $this->mExistingExpiry[$action] )
452 ->dateParams( $this->mExistingExpiry[$action] )
453 ->timeParams( $this->mExistingExpiry[$action] );
455 $expiryOptions[$existingExpiryMessage->text()] =
'existing';
458 $expiryOptions[$this->mContext->msg(
'protect-othertime-op' )->text()] =
'othertime';
460 $expiryOptions = array_merge( $expiryOptions, XmlSelect::parseOptionsMessage( $scExpiryOptions ) );
462 # Add expiry dropdown
463 $fields[
"wpProtectExpirySelection-$action"] = [
465 'name' =>
"wpProtectExpirySelection-$action",
466 'id' =>
"mwProtectExpirySelection-$action",
469 'label' => $this->mContext->msg(
'protectexpiry' )->text(),
470 'options' => $expiryOptions,
471 'default' => $this->mExpirySelection[$action],
472 'section' => $section,
475 # Add custom expiry field
476 if ( !$this->disabled ) {
477 $fields[
"mwProtect-expiry-$action"] = [
479 'label' => $this->mContext->msg(
'protect-othertime' )->text(),
480 'name' =>
"mwProtect-expiry-$action",
481 'id' =>
"mwProtect-$action-expires",
483 'default' => $this->mExpiry[$action],
485 'section' => $section,
490 # Give extensions a chance to add items to the form
492 $hookFormOptions = [];
494 $this->hookRunner->onProtectionForm__buildForm( $this->mArticle, $hookFormRaw );
495 $this->hookRunner->onProtectionFormAddFormFields( $this->mArticle, $hookFormOptions );
497 # Merge forms added from addFormFields
498 $fields = array_merge( $fields, $hookFormOptions );
500 # Add raw sections added in buildForm
501 if ( $hookFormRaw ) {
502 $fields[
'rawinfo'] = [
504 'default' => $hookFormRaw,
506 'section' =>
'restriction-blank'
510 # JavaScript will add another row with a value-chaining checkbox
511 if ( $this->mTitle->exists() ) {
512 $fields[
'mwProtect-cascade'] = [
514 'label' => $this->mContext->msg(
'protect-cascade' )->text(),
515 'id' =>
'mwProtect-cascade',
516 'name' =>
'mwProtect-cascade',
522 # Add manual and custom reason field/selects as well as submit
523 if ( !$this->disabled ) {
529 $maxlength = CommentStore::COMMENT_CHARACTER_LIMIT - 75;
530 $fields[
'wpProtectReasonSelection'] = [
532 'cssclass' =>
'mwProtect-reason',
533 'label' => $this->mContext->msg(
'protectcomment' )->text(),
535 'id' =>
'wpProtectReasonSelection',
536 'name' =>
'wpProtectReasonSelection',
538 'options' => Html::listDropdownOptions(
539 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->text(),
540 [
'other' => $this->mContext->msg(
'protect-otherreason-op' )->text() ]
544 $fields[
'mwProtect-reason'] = [
546 'id' =>
'mwProtect-reason',
547 'label' => $this->mContext->msg(
'protect-otherreason' )->text(),
548 'name' =>
'mwProtect-reason',
550 'maxlength' => $maxlength,
553 # Disallow watching if user is not logged in
554 if ( $this->mPerformer->getUser()->isRegistered() ) {
555 $fields[
'mwProtectWatch'] = [
557 'id' =>
'mwProtectWatch',
558 'label' => $this->mContext->msg(
'watchthis' )->text(),
559 'name' =>
'mwProtectWatch',
561 $this->watchlistManager->isWatched( $this->mPerformer, $this->mTitle )
563 $this->mPerformer->getUser(),
571 if ( $this->mPerformer->isAllowed(
'editinterface' ) ) {
573 $link = $linkRenderer->makeKnownLink(
574 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->getTitle(),
575 $this->mContext->msg(
'protect-edit-reasonlist' )->text(),
577 [
'action' =>
'edit' ]
579 $out .=
'<p class="mw-protect-editreasons">' . $link .
'</p>';
582 $htmlForm = HTMLForm::factory(
'ooui', $fields, $this->mContext );
584 ->setMethod(
'post' )
585 ->setId(
'mw-Protect-Form' )
586 ->setTableId(
'mw-protect-table2' )
587 ->setAction( $this->mTitle->getLocalURL(
'action=protect' ) )
588 ->setSubmitID(
'mw-Protect-submit' )
589 ->setSubmitTextMsg(
'confirm' )
590 ->setTokenSalt( [
'protect', $this->mTitle->getPrefixedDBkey() ] )
591 ->suppressDefaultSubmit( $this->disabled )
592 ->setWrapperLegendMsg(
'protect-legend' )
595 return $htmlForm->getHTML(
false ) . $out;
604 private function getOptionLabel( $permission ) {
605 if ( $permission ==
'' ) {
606 return $this->mContext->msg(
'protect-default' )->text();
609 $msg = $this->mContext->msg(
"protect-level-{$permission}" );
610 if ( $msg->exists() ) {
613 return $this->mContext->msg(
'protect-fallback', $permission )->text();
620 private function showLogExtract() {
621 # Show relevant lines from the protection log:
622 $protectLogPage =
new LogPage(
'protect' );
623 $this->mOut->addHTML(
Xml::element(
'h2',
null, $protectLogPage->getName()->text() ) );
626 # Let extensions add other relevant log extracts
627 $this->hookRunner->onProtectionForm__showLogExtract( $this->mArticle, $this->mOut );