89 $this->mArticle = $article;
90 $this->mTitle = $article->
getTitle();
92 $this->mRequest = $this->mContext->getRequest();
93 $this->mPerformer = $this->mContext->getAuthority();
94 $this->mOut = $this->mContext->getOutput();
95 $this->mLang = $this->mContext->getLanguage();
98 $this->permManager = $services->getPermissionManager();
99 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
100 $this->watchlistManager = $services->getWatchlistManager();
101 $this->titleFormatter = $services->getTitleFormatter();
102 $this->restrictionStore = $services->getRestrictionStore();
103 $this->mApplicableTypes = $this->restrictionStore->listApplicableRestrictionTypes( $this->mTitle );
107 $this->mPermStatus = PermissionStatus::newEmpty();
108 if ( $this->mRequest->wasPosted() ) {
109 $this->mPerformer->authorizeWrite(
'protect', $this->mTitle, $this->mPermStatus );
111 $this->mPerformer->authorizeRead(
'protect', $this->mTitle, $this->mPermStatus );
113 $readOnlyMode = $services->getReadOnlyMode();
114 if ( $readOnlyMode->isReadOnly() ) {
115 $this->mPermStatus->fatal(
'readonlytext', $readOnlyMode->getReason() );
117 $this->disabled = !$this->mPermStatus->isGood();
118 $this->disabledAttrib = $this->disabled ? [
'disabled' =>
'disabled' ] : [];
126 private function loadData() {
127 $levels = $this->permManager->getNamespaceRestrictionLevels(
128 $this->mTitle->getNamespace(), $this->mPerformer->getUser()
131 $this->mCascade = $this->restrictionStore->areRestrictionsCascading( $this->mTitle );
132 $this->mReason = $this->mRequest->getText(
'mwProtect-reason' );
133 $this->mReasonSelection = $this->mRequest->getText(
'wpProtectReasonSelection' );
134 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade', $this->mCascade );
136 foreach ( $this->mApplicableTypes as $action ) {
141 $this->mRestrictions[$action] = implode(
'',
142 $this->restrictionStore->getRestrictions( $this->mTitle, $action ) );
144 if ( !$this->mRestrictions[$action] ) {
146 $existingExpiry =
'';
148 $existingExpiry = $this->restrictionStore->getRestrictionExpiry( $this->mTitle, $action );
150 $this->mExistingExpiry[$action] = $existingExpiry;
152 $requestExpiry = $this->mRequest->getText(
"mwProtect-expiry-$action" );
153 $requestExpirySelection = $this->mRequest->getVal(
"wpProtectExpirySelection-$action" );
155 if ( $requestExpiry ) {
157 $this->mExpiry[$action] = $requestExpiry;
158 $this->mExpirySelection[$action] =
'othertime';
159 } elseif ( $requestExpirySelection ) {
161 $this->mExpiry[$action] =
'';
162 $this->mExpirySelection[$action] = $requestExpirySelection;
163 } elseif ( $existingExpiry ) {
165 $this->mExpiry[$action] =
'';
166 $this->mExpirySelection[$action] = $existingExpiry;
170 $this->mExpiry[$action] =
'';
171 $this->mExpirySelection[$action] =
'infinite';
174 $val = $this->mRequest->getVal(
"mwProtect-level-$action" );
175 if ( $val !==
null && in_array( $val, $levels ) ) {
176 $this->mRestrictions[$action] = $val;
189 private function getExpiry( $action ) {
190 if ( $this->mExpirySelection[$action] ==
'existing' ) {
191 return $this->mExistingExpiry[$action];
192 } elseif ( $this->mExpirySelection[$action] ==
'othertime' ) {
193 $value = $this->mExpiry[$action];
195 $value = $this->mExpirySelection[$action];
198 return ExpiryDef::normalizeExpiry( $value, TS::MW );
199 }
catch ( InvalidArgumentException ) {
209 $this->permManager->getNamespaceRestrictionLevels(
210 $this->mTitle->getNamespace()
213 throw new ErrorPageError(
'protect-badnamespace-title',
'protect-badnamespace-text' );
216 if ( $this->mRequest->wasPosted() ) {
217 if ( $this->save() ) {
218 $q = $this->mArticle->getPage()->isRedirect() ?
'redirect=no' :
'';
219 $this->mOut->redirect( $this->mTitle->getFullURL( $q ) );
232 private function show( $err =
null ) {
234 $out->setRobotPolicy(
'noindex,nofollow' );
235 $out->addBacklinkSubtitle( $this->mTitle );
237 if ( is_array( $err ) ) {
238 $out->addHTML( Html::errorBox( $out->msg( ...$err )->parse() ) );
239 } elseif ( is_string( $err ) ) {
240 $out->addHTML( Html::errorBox( $err ) );
243 if ( $this->mApplicableTypes === [] ) {
246 $out->setPageTitleMsg( $this->mContext->msg(
247 'protect-norestrictiontypes-title'
249 $this->mTitle->getPrefixedText()
251 $out->addWikiTextAsInterface(
252 $this->mContext->msg(
'protect-norestrictiontypes-text' )->plain()
256 $this->showLogExtract();
261 [ $cascadeSources, ] =
262 $this->restrictionStore->getCascadeProtectionSources( $this->mTitle );
263 if ( count( $cascadeSources ) > 0 ) {
266 foreach ( $cascadeSources as $pageIdentity ) {
267 $titles .=
'* [[:' . $this->titleFormatter->getPrefixedText( $pageIdentity ) .
"]]\n";
272 "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles .
"</div>",
273 [
'protect-cascadeon', count( $cascadeSources ) ]
277 # Show an appropriate message if the user isn't allowed or able to change
278 # the protection settings at this time
279 if ( $this->disabled ) {
280 $out->setPageTitleMsg(
281 $this->mContext->msg(
'protect-title-notallowed' )->plaintextParams( $this->mTitle->getPrefixedText() )
283 $out->addWikiTextAsInterface(
284 $out->formatPermissionStatus( $this->mPermStatus,
'protect' )
287 $out->setPageTitleMsg(
288 $this->mContext->msg(
'protect-title' )->plaintextParams( $this->mTitle->getPrefixedText() )
290 $out->addWikiMsg(
'protect-text',
294 $out->addHTML( $this->buildForm() );
295 $this->showLogExtract();
303 private function save() {
305 if ( $this->disabled ) {
310 $token = $this->mRequest->getVal(
'wpEditToken' );
313 ->newFromAuthority( $this->mPerformer );
314 if ( !$legacyUser->matchEditToken( $token, [
'protect', $this->mTitle->getPrefixedDBkey() ] ) ) {
315 $this->show( [
'sessionfailure' ] );
319 # Create reason string. Use list and/or custom string.
321 if ( $reasonstr !=
'other' && $this->mReason !=
'' ) {
323 $reasonstr .= $this->mContext->msg(
'colon-separator' )->text() .
$this->mReason;
324 } elseif ( $reasonstr ==
'other' ) {
329 foreach ( $this->mApplicableTypes as $action ) {
330 $expiry[$action] = $this->getExpiry( $action );
331 if ( empty( $this->mRestrictions[$action] ) ) {
335 if ( !$expiry[$action] ) {
336 $this->show( [
'protect_expiry_invalid' ] );
340 $this->show( [
'protect_expiry_old' ] );
345 $this->mCascade = $this->mRequest->getBool(
'mwProtect-cascade' );
347 $status = $this->mArticle->getPage()->doUpdateRestrictions(
348 $this->mRestrictions,
352 $this->mPerformer->getUser()
355 if ( !$status->isOK() ) {
356 $this->show( $this->mOut->parseInlineAsInterface(
357 $status->getWikiText(
false,
false, $this->mLang )
369 if ( !$this->hookRunner->onProtectionForm__save( $this->mArticle, $errorMsg, $reasonstr ) ) {
370 if ( $errorMsg ==
'' ) {
371 $errorMsg = [
'hookaborted' ];
374 if ( $errorMsg !=
'' ) {
375 $this->show( $errorMsg );
379 $this->watchlistManager->setWatch(
380 $this->mRequest->getCheck(
'mwProtectWatch' ),
393 private function buildForm() {
394 $this->mOut->enableOOUI();
397 if ( !$this->disabled ) {
398 $this->mOut->addModules(
'mediawiki.action.protect' );
399 $this->mOut->addModuleStyles(
'mediawiki.action.styles' );
401 $scExpiryOptions = $this->mContext->msg(
'protect-expiry-options' )->inContentLanguage()->text();
402 $levels = $this->permManager->getNamespaceRestrictionLevels(
403 $this->mTitle->getNamespace(),
404 $this->disabled ?
null : $this->mPerformer->
getUser()
408 foreach ( $this->mRestrictions as $action => $selected ) {
411 $section =
'restriction-' . $action;
412 $id =
'mwProtect-level-' . $action;
414 foreach ( $levels as $key ) {
415 $options[$this->getOptionLabel( $key )] = $key;
421 'default' => $selected,
423 'size' => count( $levels ),
424 'options' => $options,
426 'section' => $section,
431 if ( $this->mExistingExpiry[$action] ) {
432 if ( $this->mExistingExpiry[$action] ==
'infinity' ) {
433 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry-infinity' );
435 $existingExpiryMessage = $this->mContext->msg(
'protect-existing-expiry' )
436 ->dateTimeParams( $this->mExistingExpiry[$action] )
437 ->dateParams( $this->mExistingExpiry[$action] )
438 ->timeParams( $this->mExistingExpiry[$action] );
440 $expiryOptions[$existingExpiryMessage->text()] =
'existing';
443 $expiryOptions[$this->mContext->msg(
'protect-othertime-op' )->text()] =
'othertime';
445 $expiryOptions = array_merge( $expiryOptions, XmlSelect::parseOptionsMessage( $scExpiryOptions ) );
447 # Add expiry dropdown
448 $fields[
"wpProtectExpirySelection-$action"] = [
450 'name' =>
"wpProtectExpirySelection-$action",
451 'id' =>
"mwProtectExpirySelection-$action",
454 'label' => $this->mContext->msg(
'protectexpiry' )->text(),
455 'options' => $expiryOptions,
456 'default' => $this->mExpirySelection[$action],
457 'section' => $section,
460 # Add custom expiry field
461 if ( !$this->disabled ) {
462 $fields[
"mwProtect-expiry-$action"] = [
464 'label' => $this->mContext->msg(
'protect-othertime' )->text(),
465 'name' =>
"mwProtect-expiry-$action",
466 'id' =>
"mwProtect-$action-expires",
468 'default' => $this->mExpiry[$action],
470 'section' => $section,
475 # Give extensions a chance to add items to the form
477 $hookFormOptions = [];
479 $this->hookRunner->onProtectionForm__buildForm( $this->mArticle, $hookFormRaw );
480 $this->hookRunner->onProtectionFormAddFormFields( $this->mArticle, $hookFormOptions );
482 # Merge forms added from addFormFields
483 $fields = array_merge( $fields, $hookFormOptions );
485 # Add raw sections added in buildForm
486 if ( $hookFormRaw ) {
487 $fields[
'rawinfo'] = [
489 'default' => $hookFormRaw,
491 'section' =>
'restriction-blank'
495 # JavaScript will add another row with a value-chaining checkbox
496 if ( $this->mTitle->exists() ) {
497 $fields[
'mwProtect-cascade'] = [
499 'label' => $this->mContext->msg(
'protect-cascade' )->text(),
500 'id' =>
'mwProtect-cascade',
501 'name' =>
'mwProtect-cascade',
507 # Add manual and custom reason field/selects as well as submit
508 if ( !$this->disabled ) {
514 $maxlength = CommentStore::COMMENT_CHARACTER_LIMIT - 75;
515 $fields[
'wpProtectReasonSelection'] = [
517 'cssclass' =>
'mwProtect-reason',
518 'label' => $this->mContext->msg(
'protectcomment' )->text(),
520 'id' =>
'wpProtectReasonSelection',
521 'name' =>
'wpProtectReasonSelection',
523 'options' => Html::listDropdownOptions(
524 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->text(),
525 [
'other' => $this->mContext->msg(
'protect-otherreason-op' )->text() ]
529 $fields[
'mwProtect-reason'] = [
531 'id' =>
'mwProtect-reason',
532 'label' => $this->mContext->msg(
'protect-otherreason' )->text(),
533 'name' =>
'mwProtect-reason',
535 'maxlength' => $maxlength,
538 # Disallow watching if user is not logged in
539 if ( $this->mPerformer->getUser()->isRegistered() ) {
540 $fields[
'mwProtectWatch'] = [
542 'id' =>
'mwProtectWatch',
543 'label' => $this->mContext->msg(
'watchthis' )->text(),
544 'name' =>
'mwProtectWatch',
546 $this->watchlistManager->isWatched( $this->mPerformer, $this->mTitle )
548 $this->mPerformer->getUser(),
556 if ( $this->mPerformer->isAllowed(
'editinterface' ) ) {
558 $link = $linkRenderer->makeKnownLink(
559 $this->mContext->msg(
'protect-dropdown' )->inContentLanguage()->getTitle(),
560 $this->mContext->msg(
'protect-edit-reasonlist' )->text(),
562 [
'action' =>
'edit' ]
564 $out .=
'<p class="mw-protect-editreasons">' . $link .
'</p>';
567 $htmlForm = HTMLForm::factory(
'ooui', $fields, $this->mContext );
569 ->setMethod(
'post' )
570 ->setId(
'mw-Protect-Form' )
571 ->setTableId(
'mw-protect-table2' )
572 ->setAction( $this->mTitle->getLocalURL(
'action=protect' ) )
573 ->setSubmitID(
'mw-Protect-submit' )
574 ->setSubmitTextMsg(
'confirm' )
575 ->setTokenSalt( [
'protect', $this->mTitle->getPrefixedDBkey() ] )
576 ->suppressDefaultSubmit( $this->disabled )
577 ->setWrapperLegendMsg(
'protect-legend' )
580 return $htmlForm->getHTML(
false ) . $out;
589 private function getOptionLabel( $permission ) {
590 if ( $permission ==
'' ) {
591 return $this->mContext->msg(
'protect-default' )->text();
594 $msg = $this->mContext->msg(
"protect-level-{$permission}" );
595 if ( $msg->exists() ) {
598 return $this->mContext->msg(
'protect-fallback', $permission )->text();
605 private function showLogExtract() {
606 # Show relevant lines from the protection log:
607 $protectLogPage =
new LogPage(
'protect' );
608 $this->mOut->addHTML(
Html::element(
'h2', [], $protectLogPage->getName()->text() ) );
610 LogEventsList::showLogExtract( $this->mOut,
'protect', $this->mTitle );
611 # Let extensions add other relevant log extracts
612 $this->hookRunner->onProtectionForm__showLogExtract( $this->mArticle, $this->mOut );