88 $this->mArticle = $article;
89 $this->mTitle = $article->
getTitle();
91 $this->mRequest = $this->mContext->getRequest();
92 $this->mPerformer = $this->mContext->getAuthority();
93 $this->mOut = $this->mContext->getOutput();
94 $this->mLang = $this->mContext->getLanguage();
97 $this->permManager = $services->getPermissionManager();
98 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
99 $this->watchlistManager = $services->getWatchlistManager();
100 $this->titleFormatter = $services->getTitleFormatter();
101 $this->restrictionStore = $services->getRestrictionStore();
102 $this->mApplicableTypes = $this->restrictionStore->listApplicableRestrictionTypes( $this->mTitle );
106 $this->mPermStatus = PermissionStatus::newEmpty();
107 if ( $this->mRequest->wasPosted() ) {
108 $this->mPerformer->authorizeWrite(
'protect', $this->mTitle, $this->mPermStatus );
110 $this->mPerformer->authorizeRead(
'protect', $this->mTitle, $this->mPermStatus );
112 $readOnlyMode = $services->getReadOnlyMode();
113 if ( $readOnlyMode->isReadOnly() ) {
114 $this->mPermStatus->fatal(
'readonlytext', $readOnlyMode->getReason() );
116 $this->disabled = !$this->mPermStatus->isGood();
117 $this->disabledAttrib = $this->disabled ? [
'disabled' =>
'disabled' ] : [];
125 private function loadData() {
126 $levels = $this->permManager->getNamespaceRestrictionLevels(
127 $this->mTitle->getNamespace(), $this->mPerformer->getUser()
130 $this->mCascade = $this->restrictionStore->areRestrictionsCascading( $this->mTitle );
131 $this->mReason = $this->mRequest->getText(
'mwProtect-reason' );
132 $this->mReasonSelection = $this->mRequest->getText(
'wpProtectReasonSelection' );
133 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade', $this->mCascade );
135 foreach ( $this->mApplicableTypes as $action ) {
140 $this->mRestrictions[$action] = implode(
'',
141 $this->restrictionStore->getRestrictions( $this->mTitle, $action ) );
143 if ( !$this->mRestrictions[$action] ) {
145 $existingExpiry =
'';
147 $existingExpiry = $this->restrictionStore->getRestrictionExpiry( $this->mTitle, $action );
149 $this->mExistingExpiry[$action] = $existingExpiry;
151 $requestExpiry = $this->mRequest->getText(
"mwProtect-expiry-$action" );
152 $requestExpirySelection = $this->mRequest->getVal(
"wpProtectExpirySelection-$action" );
154 if ( $requestExpiry ) {
156 $this->mExpiry[$action] = $requestExpiry;
157 $this->mExpirySelection[$action] =
'othertime';
158 } elseif ( $requestExpirySelection ) {
160 $this->mExpiry[$action] =
'';
161 $this->mExpirySelection[$action] = $requestExpirySelection;
162 } elseif ( $existingExpiry ) {
164 $this->mExpiry[$action] =
'';
165 $this->mExpirySelection[$action] = $existingExpiry;
169 $this->mExpiry[$action] =
'';
170 $this->mExpirySelection[$action] =
'infinite';
173 $val = $this->mRequest->getVal(
"mwProtect-level-$action" );
174 if ( $val !==
null && in_array( $val, $levels ) ) {
175 $this->mRestrictions[$action] = $val;
188 private function getExpiry( $action ) {
189 if ( $this->mExpirySelection[$action] ==
'existing' ) {
190 return $this->mExistingExpiry[$action];
191 } elseif ( $this->mExpirySelection[$action] ==
'othertime' ) {
192 $value = $this->mExpiry[$action];
194 $value = $this->mExpirySelection[$action];
197 return ExpiryDef::normalizeExpiry( $value, TS_MW );
198 }
catch ( InvalidArgumentException ) {
208 $this->permManager->getNamespaceRestrictionLevels(
209 $this->mTitle->getNamespace()
212 throw new ErrorPageError(
'protect-badnamespace-title',
'protect-badnamespace-text' );
215 if ( $this->mRequest->wasPosted() ) {
216 if ( $this->save() ) {
217 $q = $this->mArticle->getPage()->isRedirect() ?
'redirect=no' :
'';
218 $this->mOut->redirect( $this->mTitle->getFullURL( $q ) );
231 private function show( $err =
null ) {
233 $out->setRobotPolicy(
'noindex,nofollow' );
234 $out->addBacklinkSubtitle( $this->mTitle );
236 if ( is_array( $err ) ) {
237 $out->addHTML( Html::errorBox( $out->msg( ...$err )->parse() ) );
238 } elseif ( is_string( $err ) ) {
239 $out->addHTML( Html::errorBox( $err ) );
242 if ( $this->mApplicableTypes === [] ) {
245 $out->setPageTitleMsg( $this->mContext->msg(
246 'protect-norestrictiontypes-title'
248 $this->mTitle->getPrefixedText()
250 $out->addWikiTextAsInterface(
251 $this->mContext->msg(
'protect-norestrictiontypes-text' )->plain()
255 $this->showLogExtract();
260 [ $cascadeSources, ] =
261 $this->restrictionStore->getCascadeProtectionSources( $this->mTitle );
262 if ( count( $cascadeSources ) > 0 ) {
265 foreach ( $cascadeSources as $pageIdentity ) {
266 $titles .=
'* [[:' . $this->titleFormatter->getPrefixedText( $pageIdentity ) .
"]]\n";
271 "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles .
"</div>",
272 [
'protect-cascadeon', count( $cascadeSources ) ]
276 # Show an appropriate message if the user isn't allowed or able to change
277 # the protection settings at this time
278 if ( $this->disabled ) {
279 $out->setPageTitleMsg(
280 $this->mContext->msg(
'protect-title-notallowed' )->plaintextParams( $this->mTitle->getPrefixedText() )
282 $out->addWikiTextAsInterface(
283 $out->formatPermissionStatus( $this->mPermStatus,
'protect' )
286 $out->setPageTitleMsg(
287 $this->mContext->msg(
'protect-title' )->plaintextParams( $this->mTitle->getPrefixedText() )
289 $out->addWikiMsg(
'protect-text',
293 $out->addHTML( $this->buildForm() );
294 $this->showLogExtract();
302 private function save() {
304 if ( $this->disabled ) {
309 $token = $this->mRequest->getVal(
'wpEditToken' );
312 ->newFromAuthority( $this->mPerformer );
313 if ( !$legacyUser->matchEditToken( $token, [
'protect', $this->mTitle->getPrefixedDBkey() ] ) ) {
314 $this->show( [
'sessionfailure' ] );
318 # Create reason string. Use list and/or custom string.
320 if ( $reasonstr !=
'other' && $this->mReason !=
'' ) {
322 $reasonstr .= $this->mContext->msg(
'colon-separator' )->text() .
$this->mReason;
323 } elseif ( $reasonstr ==
'other' ) {
328 foreach ( $this->mApplicableTypes as $action ) {
329 $expiry[$action] = $this->getExpiry( $action );
330 if ( empty( $this->mRestrictions[$action] ) ) {
334 if ( !$expiry[$action] ) {
335 $this->show( [
'protect_expiry_invalid' ] );
339 $this->show( [
'protect_expiry_old' ] );
344 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade' );
346 $status = $this->mArticle->getPage()->doUpdateRestrictions(
347 $this->mRestrictions,
351 $this->mPerformer->getUser()
354 if ( !$status->isOK() ) {
355 $this->show( $this->mOut->parseInlineAsInterface(
356 $status->getWikiText(
false,
false, $this->mLang )
368 if ( !$this->hookRunner->onProtectionForm__save( $this->mArticle, $errorMsg, $reasonstr ) ) {
369 if ( $errorMsg ==
'' ) {
370 $errorMsg = [
'hookaborted' ];
373 if ( $errorMsg !=
'' ) {
374 $this->show( $errorMsg );
378 $this->watchlistManager->setWatch(
379 $this->mRequest->getCheck(
'mwProtectWatch' ),
392 private function buildForm() {
393 $this->mOut->enableOOUI();
396 if ( !$this->disabled ) {
397 $this->mOut->addModules(
'mediawiki.action.protect' );
398 $this->mOut->addModuleStyles(
'mediawiki.action.styles' );
400 $scExpiryOptions = $this->mContext->msg(
'protect-expiry-options' )->inContentLanguage()->text();
401 $levels = $this->permManager->getNamespaceRestrictionLevels(
402 $this->mTitle->getNamespace(),
403 $this->disabled ?
null : $this->mPerformer->
getUser()
407 foreach ( $this->mRestrictions as $action => $selected ) {
410 $section =
'restriction-' . $action;
411 $id =
'mwProtect-level-' . $action;
413 foreach ( $levels as $key ) {
414 $options[$this->getOptionLabel( $key )] = $key;
420 'default' => $selected,
422 'size' => count( $levels ),
423 'options' => $options,
425 'section' => $section,
430 if ( $this->mExistingExpiry[$action] ) {
431 if ( $this->mExistingExpiry[$action] ==
'infinity' ) {
432 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry-infinity' );
434 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry' )
435 ->dateTimeParams( $this->mExistingExpiry[$action] )
436 ->dateParams( $this->mExistingExpiry[$action] )
437 ->timeParams( $this->mExistingExpiry[$action] );
439 $expiryOptions[$existingExpiryMessage->text()] =
'existing';
442 $expiryOptions[$this->mContext->msg(
'protect-othertime-op' )->text()] =
'othertime';
444 $expiryOptions = array_merge( $expiryOptions, XmlSelect::parseOptionsMessage( $scExpiryOptions ) );
446 # Add expiry dropdown
447 $fields[
"wpProtectExpirySelection-$action"] = [
449 'name' =>
"wpProtectExpirySelection-$action",
450 'id' =>
"mwProtectExpirySelection-$action",
453 'label' => $this->mContext->msg(
'protectexpiry' )->text(),
454 'options' => $expiryOptions,
455 'default' => $this->mExpirySelection[$action],
456 'section' => $section,
459 # Add custom expiry field
460 if ( !$this->disabled ) {
461 $fields[
"mwProtect-expiry-$action"] = [
463 'label' => $this->mContext->msg(
'protect-othertime' )->text(),
464 'name' =>
"mwProtect-expiry-$action",
465 'id' =>
"mwProtect-$action-expires",
467 'default' => $this->mExpiry[$action],
469 'section' => $section,
474 # Give extensions a chance to add items to the form
476 $hookFormOptions = [];
478 $this->hookRunner->onProtectionForm__buildForm( $this->mArticle, $hookFormRaw );
479 $this->hookRunner->onProtectionFormAddFormFields( $this->mArticle, $hookFormOptions );
481 # Merge forms added from addFormFields
482 $fields = array_merge( $fields, $hookFormOptions );
484 # Add raw sections added in buildForm
485 if ( $hookFormRaw ) {
486 $fields[
'rawinfo'] = [
488 'default' => $hookFormRaw,
490 'section' =>
'restriction-blank'
494 # JavaScript will add another row with a value-chaining checkbox
495 if ( $this->mTitle->exists() ) {
496 $fields[
'mwProtect-cascade'] = [
498 'label' => $this->mContext->msg(
'protect-cascade' )->text(),
499 'id' =>
'mwProtect-cascade',
500 'name' =>
'mwProtect-cascade',
506 # Add manual and custom reason field/selects as well as submit
507 if ( !$this->disabled ) {
513 $maxlength = CommentStore::COMMENT_CHARACTER_LIMIT - 75;
514 $fields[
'wpProtectReasonSelection'] = [
516 'cssclass' =>
'mwProtect-reason',
517 'label' => $this->mContext->msg(
'protectcomment' )->text(),
519 'id' =>
'wpProtectReasonSelection',
520 'name' =>
'wpProtectReasonSelection',
522 'options' => Html::listDropdownOptions(
523 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->text(),
524 [
'other' => $this->mContext->msg(
'protect-otherreason-op' )->text() ]
528 $fields[
'mwProtect-reason'] = [
530 'id' =>
'mwProtect-reason',
531 'label' => $this->mContext->msg(
'protect-otherreason' )->text(),
532 'name' =>
'mwProtect-reason',
534 'maxlength' => $maxlength,
537 # Disallow watching if user is not logged in
538 if ( $this->mPerformer->getUser()->isRegistered() ) {
539 $fields[
'mwProtectWatch'] = [
541 'id' =>
'mwProtectWatch',
542 'label' => $this->mContext->msg(
'watchthis' )->text(),
543 'name' =>
'mwProtectWatch',
545 $this->watchlistManager->isWatched( $this->mPerformer, $this->mTitle )
547 $this->mPerformer->getUser(),
555 if ( $this->mPerformer->isAllowed(
'editinterface' ) ) {
557 $link = $linkRenderer->makeKnownLink(
558 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->getTitle(),
559 $this->mContext->msg(
'protect-edit-reasonlist' )->text(),
561 [
'action' =>
'edit' ]
563 $out .=
'<p class="mw-protect-editreasons">' . $link .
'</p>';
566 $htmlForm = HTMLForm::factory(
'ooui', $fields, $this->mContext );
568 ->setMethod(
'post' )
569 ->setId(
'mw-Protect-Form' )
570 ->setTableId(
'mw-protect-table2' )
571 ->setAction( $this->mTitle->getLocalURL(
'action=protect' ) )
572 ->setSubmitID(
'mw-Protect-submit' )
573 ->setSubmitTextMsg(
'confirm' )
574 ->setTokenSalt( [
'protect', $this->mTitle->getPrefixedDBkey() ] )
575 ->suppressDefaultSubmit( $this->disabled )
576 ->setWrapperLegendMsg(
'protect-legend' )
579 return $htmlForm->getHTML(
false ) . $out;
588 private function getOptionLabel( $permission ) {
589 if ( $permission ==
'' ) {
590 return $this->mContext->msg(
'protect-default' )->text();
593 $msg = $this->mContext->msg(
"protect-level-{$permission}" );
594 if ( $msg->exists() ) {
597 return $this->mContext->msg(
'protect-fallback', $permission )->text();
604 private function showLogExtract() {
605 # Show relevant lines from the protection log:
606 $protectLogPage =
new LogPage(
'protect' );
607 $this->mOut->addHTML(
Html::element(
'h2', [], $protectLogPage->getName()->text() ) );
609 LogEventsList::showLogExtract( $this->mOut,
'protect', $this->mTitle );
610 # Let extensions add other relevant log extracts
611 $this->hookRunner->onProtectionForm__showLogExtract( $this->mArticle, $this->mOut );