102 $this->mArticle = $article;
103 $this->mTitle = $article->
getTitle();
105 $this->mRequest = $this->mContext->getRequest();
106 $this->mPerformer = $this->mContext->getAuthority();
107 $this->mOut = $this->mContext->getOutput();
108 $this->mLang = $this->mContext->getLanguage();
111 $this->permManager = $services->getPermissionManager();
112 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
113 $this->watchlistManager = $services->getWatchlistManager();
114 $this->titleFormatter = $services->getTitleFormatter();
115 $this->restrictionStore = $services->getRestrictionStore();
116 $this->mApplicableTypes = $this->restrictionStore->listApplicableRestrictionTypes( $this->mTitle );
120 $this->mPermStatus = PermissionStatus::newEmpty();
121 if ( $this->mRequest->wasPosted() ) {
122 $this->mPerformer->authorizeWrite(
'protect', $this->mTitle, $this->mPermStatus );
124 $this->mPerformer->authorizeRead(
'protect', $this->mTitle, $this->mPermStatus );
126 $readOnlyMode = $services->getReadOnlyMode();
127 if ( $readOnlyMode->isReadOnly() ) {
128 $this->mPermStatus->fatal(
'readonlytext', $readOnlyMode->getReason() );
130 $this->disabled = !$this->mPermStatus->isGood();
131 $this->disabledAttrib = $this->disabled ? [
'disabled' =>
'disabled' ] : [];
139 private function loadData() {
140 $levels = $this->permManager->getNamespaceRestrictionLevels(
141 $this->mTitle->getNamespace(), $this->mPerformer->getUser()
144 $this->mCascade = $this->restrictionStore->areRestrictionsCascading( $this->mTitle );
145 $this->mReason = $this->mRequest->getText(
'mwProtect-reason' );
146 $this->mReasonSelection = $this->mRequest->getText(
'wpProtectReasonSelection' );
147 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade', $this->mCascade );
149 foreach ( $this->mApplicableTypes as $action ) {
154 $this->mRestrictions[$action] = implode(
'',
155 $this->restrictionStore->getRestrictions( $this->mTitle, $action ) );
157 if ( !$this->mRestrictions[$action] ) {
159 $existingExpiry =
'';
161 $existingExpiry = $this->restrictionStore->getRestrictionExpiry( $this->mTitle, $action );
163 $this->mExistingExpiry[$action] = $existingExpiry;
165 $requestExpiry = $this->mRequest->getText(
"mwProtect-expiry-$action" );
166 $requestExpirySelection = $this->mRequest->getVal(
"wpProtectExpirySelection-$action" );
168 if ( $requestExpiry ) {
170 $this->mExpiry[$action] = $requestExpiry;
171 $this->mExpirySelection[$action] =
'othertime';
172 } elseif ( $requestExpirySelection ) {
174 $this->mExpiry[$action] =
'';
175 $this->mExpirySelection[$action] = $requestExpirySelection;
176 } elseif ( $existingExpiry ) {
178 $this->mExpiry[$action] =
'';
179 $this->mExpirySelection[$action] = $existingExpiry;
183 $this->mExpiry[$action] =
'';
184 $this->mExpirySelection[$action] =
'infinite';
187 $val = $this->mRequest->getVal(
"mwProtect-level-$action" );
188 if ( isset( $val ) && in_array( $val, $levels ) ) {
189 $this->mRestrictions[$action] = $val;
201 private function getExpiry( $action ) {
202 if ( $this->mExpirySelection[$action] ==
'existing' ) {
203 return $this->mExistingExpiry[$action];
204 } elseif ( $this->mExpirySelection[$action] ==
'othertime' ) {
205 $value = $this->mExpiry[$action];
207 $value = $this->mExpirySelection[$action];
212 $unix = strtotime( $value );
214 if ( !$unix || $unix === -1 ) {
230 $this->permManager->getNamespaceRestrictionLevels(
231 $this->mTitle->getNamespace()
234 throw new ErrorPageError(
'protect-badnamespace-title',
'protect-badnamespace-text' );
237 if ( $this->mRequest->wasPosted() ) {
238 if ( $this->save() ) {
239 $q = $this->mArticle->getPage()->isRedirect() ?
'redirect=no' :
'';
240 $this->mOut->redirect( $this->mTitle->getFullURL( $q ) );
253 private function show( $err =
null ) {
255 $out->setRobotPolicy(
'noindex,nofollow' );
256 $out->addBacklinkSubtitle( $this->mTitle );
258 if ( is_array( $err ) ) {
259 $out->addHTML( Html::errorBox( $out->msg( ...$err )->parse() ) );
260 } elseif ( is_string( $err ) ) {
261 $out->addHTML( Html::errorBox( $err ) );
264 if ( $this->mApplicableTypes === [] ) {
267 $out->setPageTitleMsg( $this->mContext->msg(
268 'protect-norestrictiontypes-title'
270 $this->mTitle->getPrefixedText()
272 $out->addWikiTextAsInterface(
273 $this->mContext->msg(
'protect-norestrictiontypes-text' )->plain()
277 $this->showLogExtract();
282 [ $cascadeSources, ] =
283 $this->restrictionStore->getCascadeProtectionSources( $this->mTitle );
284 if ( count( $cascadeSources ) > 0 ) {
287 foreach ( $cascadeSources as $pageIdentity ) {
288 $titles .=
'* [[:' . $this->titleFormatter->getPrefixedText( $pageIdentity ) .
"]]\n";
293 "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles .
"</div>",
294 [
'protect-cascadeon', count( $cascadeSources ) ]
298 # Show an appropriate message if the user isn't allowed or able to change
299 # the protection settings at this time
300 if ( $this->disabled ) {
301 $out->setPageTitleMsg(
302 $this->mContext->msg(
'protect-title-notallowed' )->plaintextParams( $this->mTitle->getPrefixedText() )
304 $out->addWikiTextAsInterface(
305 $out->formatPermissionStatus( $this->mPermStatus,
'protect' )
308 $out->setPageTitleMsg(
309 $this->mContext->msg(
'protect-title' )->plaintextParams( $this->mTitle->getPrefixedText() )
311 $out->addWikiMsg(
'protect-text',
315 $out->addHTML( $this->buildForm() );
316 $this->showLogExtract();
324 private function save() {
326 if ( $this->disabled ) {
331 $token = $this->mRequest->getVal(
'wpEditToken' );
334 ->newFromAuthority( $this->mPerformer );
335 if ( !$legacyUser->matchEditToken( $token, [
'protect', $this->mTitle->getPrefixedDBkey() ] ) ) {
336 $this->show( [
'sessionfailure' ] );
340 # Create reason string. Use list and/or custom string.
342 if ( $reasonstr !=
'other' && $this->mReason !=
'' ) {
344 $reasonstr .= $this->mContext->msg(
'colon-separator' )->text() .
$this->mReason;
345 } elseif ( $reasonstr ==
'other' ) {
350 foreach ( $this->mApplicableTypes as $action ) {
351 $expiry[$action] = $this->getExpiry( $action );
352 if ( empty( $this->mRestrictions[$action] ) ) {
356 if ( !$expiry[$action] ) {
357 $this->show( [
'protect_expiry_invalid' ] );
361 $this->show( [
'protect_expiry_old' ] );
366 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade' );
368 $status = $this->mArticle->getPage()->doUpdateRestrictions(
369 $this->mRestrictions,
373 $this->mPerformer->getUser()
376 if ( !$status->isOK() ) {
377 $this->show( $this->mOut->parseInlineAsInterface(
378 $status->getWikiText(
false,
false, $this->mLang )
390 if ( !$this->hookRunner->onProtectionForm__save( $this->mArticle, $errorMsg, $reasonstr ) ) {
391 if ( $errorMsg ==
'' ) {
392 $errorMsg = [
'hookaborted' ];
395 if ( $errorMsg !=
'' ) {
396 $this->show( $errorMsg );
400 $this->watchlistManager->setWatch(
401 $this->mRequest->getCheck(
'mwProtectWatch' ),
414 private function buildForm() {
415 $this->mOut->enableOOUI();
418 if ( !$this->disabled ) {
419 $this->mOut->addModules(
'mediawiki.action.protect' );
420 $this->mOut->addModuleStyles(
'mediawiki.action.styles' );
422 $scExpiryOptions = $this->mContext->msg(
'protect-expiry-options' )->inContentLanguage()->text();
423 $levels = $this->permManager->getNamespaceRestrictionLevels(
424 $this->mTitle->getNamespace(),
425 $this->disabled ?
null : $this->mPerformer->
getUser()
429 foreach ( $this->mRestrictions as $action => $selected ) {
432 $section =
'restriction-' . $action;
433 $id =
'mwProtect-level-' . $action;
435 foreach ( $levels as $key ) {
436 $options[$this->getOptionLabel( $key )] = $key;
442 'default' => $selected,
444 'size' => count( $levels ),
445 'options' => $options,
447 'section' => $section,
452 if ( $this->mExistingExpiry[$action] ) {
453 if ( $this->mExistingExpiry[$action] ==
'infinity' ) {
454 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry-infinity' );
456 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry' )
457 ->dateTimeParams( $this->mExistingExpiry[$action] )
458 ->dateParams( $this->mExistingExpiry[$action] )
459 ->timeParams( $this->mExistingExpiry[$action] );
461 $expiryOptions[$existingExpiryMessage->text()] =
'existing';
464 $expiryOptions[$this->mContext->msg(
'protect-othertime-op' )->text()] =
'othertime';
466 $expiryOptions = array_merge( $expiryOptions, XmlSelect::parseOptionsMessage( $scExpiryOptions ) );
468 # Add expiry dropdown
469 $fields[
"wpProtectExpirySelection-$action"] = [
471 'name' =>
"wpProtectExpirySelection-$action",
472 'id' =>
"mwProtectExpirySelection-$action",
475 'label' => $this->mContext->msg(
'protectexpiry' )->text(),
476 'options' => $expiryOptions,
477 'default' => $this->mExpirySelection[$action],
478 'section' => $section,
481 # Add custom expiry field
482 if ( !$this->disabled ) {
483 $fields[
"mwProtect-expiry-$action"] = [
485 'label' => $this->mContext->msg(
'protect-othertime' )->text(),
486 'name' =>
"mwProtect-expiry-$action",
487 'id' =>
"mwProtect-$action-expires",
489 'default' => $this->mExpiry[$action],
491 'section' => $section,
496 # Give extensions a chance to add items to the form
498 $hookFormOptions = [];
500 $this->hookRunner->onProtectionForm__buildForm( $this->mArticle, $hookFormRaw );
501 $this->hookRunner->onProtectionFormAddFormFields( $this->mArticle, $hookFormOptions );
503 # Merge forms added from addFormFields
504 $fields = array_merge( $fields, $hookFormOptions );
506 # Add raw sections added in buildForm
507 if ( $hookFormRaw ) {
508 $fields[
'rawinfo'] = [
510 'default' => $hookFormRaw,
512 'section' =>
'restriction-blank'
516 # JavaScript will add another row with a value-chaining checkbox
517 if ( $this->mTitle->exists() ) {
518 $fields[
'mwProtect-cascade'] = [
520 'label' => $this->mContext->msg(
'protect-cascade' )->text(),
521 'id' =>
'mwProtect-cascade',
522 'name' =>
'mwProtect-cascade',
528 # Add manual and custom reason field/selects as well as submit
529 if ( !$this->disabled ) {
535 $maxlength = CommentStore::COMMENT_CHARACTER_LIMIT - 75;
536 $fields[
'wpProtectReasonSelection'] = [
538 'cssclass' =>
'mwProtect-reason',
539 'label' => $this->mContext->msg(
'protectcomment' )->text(),
541 'id' =>
'wpProtectReasonSelection',
542 'name' =>
'wpProtectReasonSelection',
544 'options' => Html::listDropdownOptions(
545 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->text(),
546 [
'other' => $this->mContext->msg(
'protect-otherreason-op' )->text() ]
550 $fields[
'mwProtect-reason'] = [
552 'id' =>
'mwProtect-reason',
553 'label' => $this->mContext->msg(
'protect-otherreason' )->text(),
554 'name' =>
'mwProtect-reason',
556 'maxlength' => $maxlength,
559 # Disallow watching if user is not logged in
560 if ( $this->mPerformer->getUser()->isRegistered() ) {
561 $fields[
'mwProtectWatch'] = [
563 'id' =>
'mwProtectWatch',
564 'label' => $this->mContext->msg(
'watchthis' )->text(),
565 'name' =>
'mwProtectWatch',
567 $this->watchlistManager->isWatched( $this->mPerformer, $this->mTitle )
569 $this->mPerformer->getUser(),
577 if ( $this->mPerformer->isAllowed(
'editinterface' ) ) {
579 $link = $linkRenderer->makeKnownLink(
580 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->getTitle(),
581 $this->mContext->msg(
'protect-edit-reasonlist' )->text(),
583 [
'action' =>
'edit' ]
585 $out .=
'<p class="mw-protect-editreasons">' . $link .
'</p>';
588 $htmlForm = HTMLForm::factory(
'ooui', $fields, $this->mContext );
590 ->setMethod(
'post' )
591 ->setId(
'mw-Protect-Form' )
592 ->setTableId(
'mw-protect-table2' )
593 ->setAction( $this->mTitle->getLocalURL(
'action=protect' ) )
594 ->setSubmitID(
'mw-Protect-submit' )
595 ->setSubmitTextMsg(
'confirm' )
596 ->setTokenSalt( [
'protect', $this->mTitle->getPrefixedDBkey() ] )
597 ->suppressDefaultSubmit( $this->disabled )
598 ->setWrapperLegendMsg(
'protect-legend' )
601 return $htmlForm->getHTML(
false ) . $out;
610 private function getOptionLabel( $permission ) {
611 if ( $permission ==
'' ) {
612 return $this->mContext->msg(
'protect-default' )->text();
615 $msg = $this->mContext->msg(
"protect-level-{$permission}" );
616 if ( $msg->exists() ) {
619 return $this->mContext->msg(
'protect-fallback', $permission )->text();
626 private function showLogExtract() {
627 # Show relevant lines from the protection log:
628 $protectLogPage =
new LogPage(
'protect' );
629 $this->mOut->addHTML(
Xml::element(
'h2',
null, $protectLogPage->getName()->text() ) );
632 # Let extensions add other relevant log extracts
633 $this->hookRunner->onProtectionForm__showLogExtract( $this->mArticle, $this->mOut );