ProtectionForm.php 20 KB

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