ProtectionForm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. /**
  3. * Page protection
  4. *
  5. * Copyright © 2005 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. */
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * Handles the page protection UI and backend
  28. */
  29. class ProtectionForm {
  30. /** @var array A map of action to restriction level, from request or default */
  31. protected $mRestrictions = [];
  32. /** @var string The custom/additional protection reason */
  33. protected $mReason = '';
  34. /** @var string The reason selected from the list, blank for other/additional */
  35. protected $mReasonSelection = '';
  36. /** @var bool True if the restrictions are cascading, from request or existing protection */
  37. protected $mCascade = false;
  38. /** @var array Map of action to "other" expiry time. Used in preference to mExpirySelection. */
  39. protected $mExpiry = [];
  40. /**
  41. * @var array Map of action to value selected in expiry drop-down list.
  42. * Will be set to 'othertime' whenever mExpiry is set.
  43. */
  44. protected $mExpirySelection = [];
  45. /** @var array Permissions errors for the protect action */
  46. protected $mPermErrors = [];
  47. /** @var array Types (i.e. actions) for which levels can be selected */
  48. protected $mApplicableTypes = [];
  49. /** @var array Map of action to the expiry time of the existing protection */
  50. protected $mExistingExpiry = [];
  51. /** @var Article */
  52. protected $mArticle;
  53. /** @var Title */
  54. protected $mTitle;
  55. /** @var bool */
  56. protected $disabled;
  57. /** @var array */
  58. protected $disabledAttrib;
  59. /** @var IContextSource */
  60. private $mContext;
  61. function __construct( Article $article ) {
  62. // Set instance variables.
  63. $this->mArticle = $article;
  64. $this->mTitle = $article->getTitle();
  65. $this->mApplicableTypes = $this->mTitle->getRestrictionTypes();
  66. $this->mContext = $article->getContext();
  67. // Check if the form should be disabled.
  68. // If it is, the form will be available in read-only to show levels.
  69. $this->mPermErrors = $this->mTitle->getUserPermissionsErrors(
  70. 'protect',
  71. $this->mContext->getUser(),
  72. $this->mContext->getRequest()->wasPosted() ? 'secure' : 'full' // T92357
  73. );
  74. if ( wfReadOnly() ) {
  75. $this->mPermErrors[] = [ 'readonlytext', wfReadOnlyReason() ];
  76. }
  77. $this->disabled = $this->mPermErrors !== [];
  78. $this->disabledAttrib = $this->disabled
  79. ? [ 'disabled' => 'disabled' ]
  80. : [];
  81. $this->loadData();
  82. }
  83. /**
  84. * Loads the current state of protection into the object.
  85. */
  86. function loadData() {
  87. $levels = MediaWikiServices::getInstance()->getPermissionManager()->getNamespaceRestrictionLevels(
  88. $this->mTitle->getNamespace(), $this->mContext->getUser()
  89. );
  90. $this->mCascade = $this->mTitle->areRestrictionsCascading();
  91. $request = $this->mContext->getRequest();
  92. $this->mReason = $request->getText( 'mwProtect-reason' );
  93. $this->mReasonSelection = $request->getText( 'wpProtectReasonSelection' );
  94. $this->mCascade = $request->getBool( 'mwProtect-cascade', $this->mCascade );
  95. foreach ( $this->mApplicableTypes as $action ) {
  96. // @todo FIXME: This form currently requires individual selections,
  97. // but the db allows multiples separated by commas.
  98. // Pull the actual restriction from the DB
  99. $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
  100. if ( !$this->mRestrictions[$action] ) {
  101. // No existing expiry
  102. $existingExpiry = '';
  103. } else {
  104. $existingExpiry = $this->mTitle->getRestrictionExpiry( $action );
  105. }
  106. $this->mExistingExpiry[$action] = $existingExpiry;
  107. $requestExpiry = $request->getText( "mwProtect-expiry-$action" );
  108. $requestExpirySelection = $request->getVal( "wpProtectExpirySelection-$action" );
  109. if ( $requestExpiry ) {
  110. // Custom expiry takes precedence
  111. $this->mExpiry[$action] = $requestExpiry;
  112. $this->mExpirySelection[$action] = 'othertime';
  113. } elseif ( $requestExpirySelection ) {
  114. // Expiry selected from list
  115. $this->mExpiry[$action] = '';
  116. $this->mExpirySelection[$action] = $requestExpirySelection;
  117. } elseif ( $existingExpiry ) {
  118. // Use existing expiry in its own list item
  119. $this->mExpiry[$action] = '';
  120. $this->mExpirySelection[$action] = $existingExpiry;
  121. } else {
  122. // Catches 'infinity' - Existing expiry is infinite, use "infinite" in drop-down
  123. // Final default: infinite
  124. $this->mExpiry[$action] = '';
  125. $this->mExpirySelection[$action] = 'infinite';
  126. }
  127. $val = $request->getVal( "mwProtect-level-$action" );
  128. if ( isset( $val ) && in_array( $val, $levels ) ) {
  129. $this->mRestrictions[$action] = $val;
  130. }
  131. }
  132. }
  133. /**
  134. * Get the expiry time for a given action, by combining the relevant inputs.
  135. *
  136. * @param string $action
  137. *
  138. * @return string|false 14-char timestamp or "infinity", or false if the input was invalid
  139. */
  140. function getExpiry( $action ) {
  141. if ( $this->mExpirySelection[$action] == 'existing' ) {
  142. return $this->mExistingExpiry[$action];
  143. } elseif ( $this->mExpirySelection[$action] == 'othertime' ) {
  144. $value = $this->mExpiry[$action];
  145. } else {
  146. $value = $this->mExpirySelection[$action];
  147. }
  148. if ( wfIsInfinity( $value ) ) {
  149. $time = 'infinity';
  150. } else {
  151. $unix = strtotime( $value );
  152. if ( !$unix || $unix === -1 ) {
  153. return false;
  154. }
  155. // @todo FIXME: Non-qualified absolute times are not in users specified timezone
  156. // and there isn't notice about it in the ui
  157. $time = wfTimestamp( TS_MW, $unix );
  158. }
  159. return $time;
  160. }
  161. /**
  162. * Main entry point for action=protect and action=unprotect
  163. */
  164. function execute() {
  165. if (
  166. MediaWikiServices::getInstance()->getPermissionManager()->getNamespaceRestrictionLevels(
  167. $this->mTitle->getNamespace()
  168. ) === [ '' ]
  169. ) {
  170. throw new ErrorPageError( 'protect-badnamespace-title', 'protect-badnamespace-text' );
  171. }
  172. if ( $this->mContext->getRequest()->wasPosted() ) {
  173. if ( $this->save() ) {
  174. $q = $this->mArticle->isRedirect() ? 'redirect=no' : '';
  175. $this->mContext->getOutput()->redirect( $this->mTitle->getFullURL( $q ) );
  176. }
  177. } else {
  178. $this->show();
  179. }
  180. }
  181. /**
  182. * Show the input form with optional error message
  183. *
  184. * @param string|string[]|null $err Error message or null if there's no error
  185. */
  186. function show( $err = null ) {
  187. $out = $this->mContext->getOutput();
  188. $out->setRobotPolicy( 'noindex,nofollow' );
  189. $out->addBacklinkSubtitle( $this->mTitle );
  190. if ( is_array( $err ) ) {
  191. $out->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", $err );
  192. } elseif ( is_string( $err ) ) {
  193. $out->addHTML( "<div class='error'>{$err}</div>\n" );
  194. }
  195. if ( $this->mTitle->getRestrictionTypes() === [] ) {
  196. // No restriction types available for the current title
  197. // this might happen if an extension alters the available types
  198. $out->setPageTitle( $this->mContext->msg(
  199. 'protect-norestrictiontypes-title',
  200. $this->mTitle->getPrefixedText()
  201. ) );
  202. $out->addWikiTextAsInterface(
  203. $this->mContext->msg( 'protect-norestrictiontypes-text' )->plain()
  204. );
  205. // Show the log in case protection was possible once
  206. $this->showLogExtract( $out );
  207. // return as there isn't anything else we can do
  208. return;
  209. }
  210. list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
  211. if ( $cascadeSources && count( $cascadeSources ) > 0 ) {
  212. $titles = '';
  213. foreach ( $cascadeSources as $title ) {
  214. $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
  215. }
  216. /** @todo FIXME: i18n issue, should use formatted number. */
  217. $out->wrapWikiMsg(
  218. "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles . "</div>",
  219. [ 'protect-cascadeon', count( $cascadeSources ) ]
  220. );
  221. }
  222. # Show an appropriate message if the user isn't allowed or able to change
  223. # the protection settings at this time
  224. if ( $this->disabled ) {
  225. $out->setPageTitle(
  226. $this->mContext->msg( 'protect-title-notallowed',
  227. $this->mTitle->getPrefixedText() )
  228. );
  229. $out->addWikiTextAsInterface( $out->formatPermissionsErrorMessage(
  230. $this->mPermErrors, 'protect'
  231. ) );
  232. } else {
  233. $out->setPageTitle( $this->mContext->msg( 'protect-title', $this->mTitle->getPrefixedText() ) );
  234. $out->addWikiMsg( 'protect-text',
  235. wfEscapeWikiText( $this->mTitle->getPrefixedText() ) );
  236. }
  237. $out->addHTML( $this->buildForm() );
  238. $this->showLogExtract( $out );
  239. }
  240. /**
  241. * Save submitted protection form
  242. *
  243. * @return bool Success
  244. */
  245. function save() {
  246. # Permission check!
  247. if ( $this->disabled ) {
  248. $this->show();
  249. return false;
  250. }
  251. $request = $this->mContext->getRequest();
  252. $user = $this->mContext->getUser();
  253. $out = $this->mContext->getOutput();
  254. $token = $request->getVal( 'wpEditToken' );
  255. if ( !$user->matchEditToken( $token, [ 'protect', $this->mTitle->getPrefixedDBkey() ] ) ) {
  256. $this->show( [ 'sessionfailure' ] );
  257. return false;
  258. }
  259. # Create reason string. Use list and/or custom string.
  260. $reasonstr = $this->mReasonSelection;
  261. if ( $reasonstr != 'other' && $this->mReason != '' ) {
  262. // Entry from drop down menu + additional comment
  263. $reasonstr .= $this->mContext->msg( 'colon-separator' )->text() . $this->mReason;
  264. } elseif ( $reasonstr == 'other' ) {
  265. $reasonstr = $this->mReason;
  266. }
  267. $expiry = [];
  268. foreach ( $this->mApplicableTypes as $action ) {
  269. $expiry[$action] = $this->getExpiry( $action );
  270. if ( empty( $this->mRestrictions[$action] ) ) {
  271. continue; // unprotected
  272. }
  273. if ( !$expiry[$action] ) {
  274. $this->show( [ 'protect_expiry_invalid' ] );
  275. return false;
  276. }
  277. if ( $expiry[$action] < wfTimestampNow() ) {
  278. $this->show( [ 'protect_expiry_old' ] );
  279. return false;
  280. }
  281. }
  282. $this->mCascade = $request->getBool( 'mwProtect-cascade' );
  283. $status = $this->mArticle->doUpdateRestrictions(
  284. $this->mRestrictions,
  285. $expiry,
  286. $this->mCascade,
  287. $reasonstr,
  288. $user
  289. );
  290. if ( !$status->isOK() ) {
  291. $this->show( $out->parseInlineAsInterface(
  292. $status->getWikiText( false, false, $this->mContext->getLanguage() )
  293. ) );
  294. return false;
  295. }
  296. /**
  297. * Give extensions a change to handle added form items
  298. *
  299. * @since 1.19 you can (and you should) return false to abort saving;
  300. * you can also return an array of message name and its parameters
  301. */
  302. $errorMsg = '';
  303. if ( !Hooks::run( 'ProtectionForm::save', [ $this->mArticle, &$errorMsg, $reasonstr ] ) ) {
  304. if ( $errorMsg == '' ) {
  305. $errorMsg = [ 'hookaborted' ];
  306. }
  307. }
  308. if ( $errorMsg != '' ) {
  309. $this->show( $errorMsg );
  310. return false;
  311. }
  312. WatchAction::doWatchOrUnwatch( $request->getCheck( 'mwProtectWatch' ), $this->mTitle, $user );
  313. return true;
  314. }
  315. /**
  316. * Build the input form
  317. *
  318. * @return string HTML form
  319. */
  320. function buildForm() {
  321. $context = $this->mContext;
  322. $user = $context->getUser();
  323. $output = $context->getOutput();
  324. $lang = $context->getLanguage();
  325. $out = '';
  326. if ( !$this->disabled ) {
  327. $output->addModules( 'mediawiki.legacy.protect' );
  328. $out .= Xml::openElement( 'form', [ 'method' => 'post',
  329. 'action' => $this->mTitle->getLocalURL( 'action=protect' ),
  330. 'id' => 'mw-Protect-Form' ] );
  331. }
  332. $out .= Xml::openElement( 'fieldset' ) .
  333. Xml::element( 'legend', null, $context->msg( 'protect-legend' )->text() ) .
  334. Xml::openElement( 'table', [ 'id' => 'mwProtectSet' ] ) .
  335. Xml::openElement( 'tbody' );
  336. $scExpiryOptions = wfMessage( 'protect-expiry-options' )->inContentLanguage()->text();
  337. $showProtectOptions = $scExpiryOptions !== '-' && !$this->disabled;
  338. // Not all languages have V_x <-> N_x relation
  339. foreach ( $this->mRestrictions as $action => $selected ) {
  340. // Messages:
  341. // restriction-edit, restriction-move, restriction-create, restriction-upload
  342. $msg = $context->msg( 'restriction-' . $action );
  343. $out .= "<tr><td>" .
  344. Xml::openElement( 'fieldset' ) .
  345. Xml::element( 'legend', null, $msg->exists() ? $msg->text() : $action ) .
  346. Xml::openElement( 'table', [ 'id' => "mw-protect-table-$action" ] ) .
  347. "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
  348. $mProtectexpiry = Xml::label(
  349. $context->msg( 'protectexpiry' )->text(),
  350. "mwProtectExpirySelection-$action"
  351. );
  352. $mProtectother = Xml::label(
  353. $context->msg( 'protect-othertime' )->text(),
  354. "mwProtect-$action-expires"
  355. );
  356. $expiryFormOptions = new XmlSelect(
  357. "wpProtectExpirySelection-$action",
  358. "mwProtectExpirySelection-$action",
  359. $this->mExpirySelection[$action]
  360. );
  361. $expiryFormOptions->setAttribute( 'tabindex', '2' );
  362. if ( $this->disabled ) {
  363. $expiryFormOptions->setAttribute( 'disabled', 'disabled' );
  364. }
  365. if ( $this->mExistingExpiry[$action] ) {
  366. if ( $this->mExistingExpiry[$action] == 'infinity' ) {
  367. $existingExpiryMessage = $context->msg( 'protect-existing-expiry-infinity' );
  368. } else {
  369. $timestamp = $lang->userTimeAndDate( $this->mExistingExpiry[$action], $user );
  370. $d = $lang->userDate( $this->mExistingExpiry[$action], $user );
  371. $t = $lang->userTime( $this->mExistingExpiry[$action], $user );
  372. $existingExpiryMessage = $context->msg(
  373. 'protect-existing-expiry',
  374. $timestamp,
  375. $d,
  376. $t
  377. );
  378. }
  379. $expiryFormOptions->addOption( $existingExpiryMessage->text(), 'existing' );
  380. }
  381. $expiryFormOptions->addOption(
  382. $context->msg( 'protect-othertime-op' )->text(),
  383. 'othertime'
  384. );
  385. foreach ( explode( ',', $scExpiryOptions ) as $option ) {
  386. if ( strpos( $option, ":" ) === false ) {
  387. $show = $value = $option;
  388. } else {
  389. list( $show, $value ) = explode( ":", $option );
  390. }
  391. $expiryFormOptions->addOption( $show, htmlspecialchars( $value ) );
  392. }
  393. # Add expiry dropdown
  394. if ( $showProtectOptions && !$this->disabled ) {
  395. $out .= "
  396. <table><tr>
  397. <td class='mw-label'>
  398. {$mProtectexpiry}
  399. </td>
  400. <td class='mw-input'>" .
  401. $expiryFormOptions->getHTML() .
  402. "</td>
  403. </tr></table>";
  404. }
  405. # Add custom expiry field
  406. $attribs = [ 'id' => "mwProtect-$action-expires" ] + $this->disabledAttrib;
  407. $out .= "<table><tr>
  408. <td class='mw-label'>" .
  409. $mProtectother .
  410. '</td>
  411. <td class="mw-input">' .
  412. Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) .
  413. '</td>
  414. </tr></table>';
  415. $out .= "</td></tr>" .
  416. Xml::closeElement( 'table' ) .
  417. Xml::closeElement( 'fieldset' ) .
  418. "</td></tr>";
  419. }
  420. # Give extensions a chance to add items to the form
  421. Hooks::run( 'ProtectionForm::buildForm', [ $this->mArticle, &$out ] );
  422. $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
  423. // JavaScript will add another row with a value-chaining checkbox
  424. if ( $this->mTitle->exists() ) {
  425. $out .= Xml::openElement( 'table', [ 'id' => 'mw-protect-table2' ] ) .
  426. Xml::openElement( 'tbody' );
  427. $out .= '<tr>
  428. <td></td>
  429. <td class="mw-input">' .
  430. Xml::checkLabel(
  431. $context->msg( 'protect-cascade' )->text(),
  432. 'mwProtect-cascade',
  433. 'mwProtect-cascade',
  434. $this->mCascade, $this->disabledAttrib
  435. ) .
  436. "</td>
  437. </tr>\n";
  438. $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
  439. }
  440. # Add manual and custom reason field/selects as well as submit
  441. if ( !$this->disabled ) {
  442. $mProtectreasonother = Xml::label(
  443. $context->msg( 'protectcomment' )->text(),
  444. 'wpProtectReasonSelection'
  445. );
  446. $mProtectreason = Xml::label(
  447. $context->msg( 'protect-otherreason' )->text(),
  448. 'mwProtect-reason'
  449. );
  450. $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection',
  451. wfMessage( 'protect-dropdown' )->inContentLanguage()->text(),
  452. wfMessage( 'protect-otherreason-op' )->inContentLanguage()->text(),
  453. $this->mReasonSelection,
  454. 'mwProtect-reason', 4 );
  455. // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
  456. // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
  457. // Unicode codepoints.
  458. // Subtract arbitrary 75 to leave some space for the autogenerated null edit's summary
  459. // and other texts chosen by dropdown menus on this page.
  460. $maxlength = CommentStore::COMMENT_CHARACTER_LIMIT - 75;
  461. $out .= Xml::openElement( 'table', [ 'id' => 'mw-protect-table3' ] ) .
  462. Xml::openElement( 'tbody' );
  463. $out .= "
  464. <tr>
  465. <td class='mw-label'>
  466. {$mProtectreasonother}
  467. </td>
  468. <td class='mw-input'>
  469. {$reasonDropDown}
  470. </td>
  471. </tr>
  472. <tr>
  473. <td class='mw-label'>
  474. {$mProtectreason}
  475. </td>
  476. <td class='mw-input'>" .
  477. Xml::input( 'mwProtect-reason', 60, $this->mReason, [ 'type' => 'text',
  478. 'id' => 'mwProtect-reason', 'maxlength' => $maxlength ] ) .
  479. "</td>
  480. </tr>";
  481. # Disallow watching is user is not logged in
  482. if ( $user->isLoggedIn() ) {
  483. $out .= "
  484. <tr>
  485. <td></td>
  486. <td class='mw-input'>" .
  487. Xml::checkLabel( $context->msg( 'watchthis' )->text(),
  488. 'mwProtectWatch', 'mwProtectWatch',
  489. $user->isWatched( $this->mTitle ) || $user->getOption( 'watchdefault' ) ) .
  490. "</td>
  491. </tr>";
  492. }
  493. $out .= "
  494. <tr>
  495. <td></td>
  496. <td class='mw-submit'>" .
  497. Xml::submitButton(
  498. $context->msg( 'confirm' )->text(),
  499. [ 'id' => 'mw-Protect-submit' ]
  500. ) .
  501. "</td>
  502. </tr>\n";
  503. $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
  504. }
  505. $out .= Xml::closeElement( 'fieldset' );
  506. if ( MediaWikiServices::getInstance()->getPermissionManager()
  507. ->userHasRight( $user, 'editinterface' ) ) {
  508. $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
  509. $link = $linkRenderer->makeKnownLink(
  510. $context->msg( 'protect-dropdown' )->inContentLanguage()->getTitle(),
  511. $context->msg( 'protect-edit-reasonlist' )->text(),
  512. [],
  513. [ 'action' => 'edit' ]
  514. );
  515. $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
  516. }
  517. if ( !$this->disabled ) {
  518. $out .= Html::hidden(
  519. 'wpEditToken',
  520. $user->getEditToken( [ 'protect', $this->mTitle->getPrefixedDBkey() ] )
  521. );
  522. $out .= Xml::closeElement( 'form' );
  523. }
  524. return $out;
  525. }
  526. /**
  527. * Build protection level selector
  528. *
  529. * @param string $action Action to protect
  530. * @param string $selected Current protection level
  531. * @return string HTML fragment
  532. */
  533. function buildSelector( $action, $selected ) {
  534. // If the form is disabled, display all relevant levels. Otherwise,
  535. // just show the ones this user can use.
  536. $levels = MediaWikiServices::getInstance()
  537. ->getPermissionManager()
  538. ->getNamespaceRestrictionLevels(
  539. $this->mTitle->getNamespace(),
  540. $this->disabled ? null : $this->mContext->getUser()
  541. );
  542. $id = 'mwProtect-level-' . $action;
  543. $select = new XmlSelect( $id, $id, $selected );
  544. $select->setAttribute( 'size', count( $levels ) );
  545. if ( $this->disabled ) {
  546. $select->setAttribute( 'disabled', 'disabled' );
  547. }
  548. foreach ( $levels as $key ) {
  549. $select->addOption( $this->getOptionLabel( $key ), $key );
  550. }
  551. return $select->getHTML();
  552. }
  553. /**
  554. * Prepare the label for a protection selector option
  555. *
  556. * @param string $permission Permission required
  557. * @return string
  558. */
  559. private function getOptionLabel( $permission ) {
  560. if ( $permission == '' ) {
  561. return $this->mContext->msg( 'protect-default' )->text();
  562. } else {
  563. // Messages: protect-level-autoconfirmed, protect-level-sysop
  564. $msg = $this->mContext->msg( "protect-level-{$permission}" );
  565. if ( $msg->exists() ) {
  566. return $msg->text();
  567. }
  568. return $this->mContext->msg( 'protect-fallback', $permission )->text();
  569. }
  570. }
  571. /**
  572. * Show protection long extracts for this page
  573. *
  574. * @param OutputPage $out
  575. */
  576. private function showLogExtract( OutputPage $out ) {
  577. # Show relevant lines from the protection log:
  578. $protectLogPage = new LogPage( 'protect' );
  579. $out->addHTML( Xml::element( 'h2', null, $protectLogPage->getName()->text() ) );
  580. LogEventsList::showLogExtract( $out, 'protect', $this->mTitle );
  581. # Let extensions add other relevant log extracts
  582. Hooks::run( 'ProtectionForm::showLogExtract', [ $this->mArticle, $out ] );
  583. }
  584. }