SpecialBlock.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. <?php
  2. /**
  3. * Implements Special:Block
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\Block\DatabaseBlock;
  24. use MediaWiki\Block\Restriction\PageRestriction;
  25. use MediaWiki\Block\Restriction\NamespaceRestriction;
  26. use MediaWiki\MediaWikiServices;
  27. use MediaWiki\User\UserIdentity;
  28. /**
  29. * A special page that allows users with 'block' right to block users from
  30. * editing pages and other actions
  31. *
  32. * @ingroup SpecialPage
  33. */
  34. class SpecialBlock extends FormSpecialPage {
  35. /** @var User|string|null User to be blocked, as passed either by parameter (url?wpTarget=Foo)
  36. * or as subpage (Special:Block/Foo)
  37. */
  38. protected $target;
  39. /** @var int DatabaseBlock::TYPE_ constant */
  40. protected $type;
  41. /** @var User|string The previous block target */
  42. protected $previousTarget;
  43. /** @var bool Whether the previous submission of the form asked for HideUser */
  44. protected $requestedHideUser;
  45. /** @var bool */
  46. protected $alreadyBlocked;
  47. /** @var array */
  48. protected $preErrors = [];
  49. public function __construct() {
  50. parent::__construct( 'Block', 'block' );
  51. }
  52. public function doesWrites() {
  53. return true;
  54. }
  55. /**
  56. * Checks that the user can unblock themselves if they are trying to do so
  57. *
  58. * @param User $user
  59. * @throws ErrorPageError
  60. */
  61. protected function checkExecutePermissions( User $user ) {
  62. parent::checkExecutePermissions( $user );
  63. # T17810: blocked admins should have limited access here
  64. $status = self::checkUnblockSelf( $this->target, $user );
  65. if ( $status !== true ) {
  66. throw new ErrorPageError( 'badaccess', $status );
  67. }
  68. }
  69. /**
  70. * We allow certain special cases where user is blocked
  71. *
  72. * @return bool
  73. */
  74. public function requiresUnblock() {
  75. return false;
  76. }
  77. /**
  78. * Handle some magic here
  79. *
  80. * @param string $par
  81. */
  82. protected function setParameter( $par ) {
  83. # Extract variables from the request. Try not to get into a situation where we
  84. # need to extract *every* variable from the form just for processing here, but
  85. # there are legitimate uses for some variables
  86. $request = $this->getRequest();
  87. list( $this->target, $this->type ) = self::getTargetAndType( $par, $request );
  88. if ( $this->target instanceof User ) {
  89. # Set the 'relevant user' in the skin, so it displays links like Contributions,
  90. # User logs, UserRights, etc.
  91. $this->getSkin()->setRelevantUser( $this->target );
  92. }
  93. list( $this->previousTarget, /*...*/ ) =
  94. DatabaseBlock::parseTarget( $request->getVal( 'wpPreviousTarget' ) );
  95. $this->requestedHideUser = $request->getBool( 'wpHideUser' );
  96. }
  97. /**
  98. * Customizes the HTMLForm a bit
  99. *
  100. * @param HTMLForm $form
  101. */
  102. protected function alterForm( HTMLForm $form ) {
  103. $form->setHeaderText( '' );
  104. $form->setSubmitDestructive();
  105. $msg = $this->alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit';
  106. $form->setSubmitTextMsg( $msg );
  107. $this->addHelpLink( 'Help:Blocking users' );
  108. # Don't need to do anything if the form has been posted
  109. if ( !$this->getRequest()->wasPosted() && $this->preErrors ) {
  110. $s = $form->formatErrors( $this->preErrors );
  111. if ( $s ) {
  112. $form->addHeaderText( Html::rawElement(
  113. 'div',
  114. [ 'class' => 'error' ],
  115. $s
  116. ) );
  117. }
  118. }
  119. }
  120. protected function getDisplayFormat() {
  121. return 'ooui';
  122. }
  123. /**
  124. * Get the HTMLForm descriptor array for the block form
  125. * @return array
  126. */
  127. protected function getFormFields() {
  128. $conf = $this->getConfig();
  129. $enablePartialBlocks = $conf->get( 'EnablePartialBlocks' );
  130. $blockAllowsUTEdit = $conf->get( 'BlockAllowsUTEdit' );
  131. $this->getOutput()->enableOOUI();
  132. $user = $this->getUser();
  133. $suggestedDurations = self::getSuggestedDurations();
  134. $a = [];
  135. $a['Target'] = [
  136. 'type' => 'user',
  137. 'ipallowed' => true,
  138. 'iprange' => true,
  139. 'id' => 'mw-bi-target',
  140. 'size' => '45',
  141. 'autofocus' => true,
  142. 'required' => true,
  143. 'validation-callback' => [ __CLASS__, 'validateTargetField' ],
  144. 'section' => 'target',
  145. ];
  146. $a['Editing'] = [
  147. 'type' => 'check',
  148. 'label-message' => 'block-prevent-edit',
  149. 'default' => true,
  150. 'section' => 'actions',
  151. 'disabled' => $enablePartialBlocks ? false : true,
  152. ];
  153. if ( $enablePartialBlocks ) {
  154. $a['EditingRestriction'] = [
  155. 'type' => 'radio',
  156. 'cssclass' => 'mw-block-editing-restriction',
  157. 'options' => [
  158. $this->msg( 'ipb-sitewide' )->escaped() .
  159. new \OOUI\LabelWidget( [
  160. 'classes' => [ 'oo-ui-inline-help' ],
  161. 'label' => $this->msg( 'ipb-sitewide-help' )->text(),
  162. ] ) => 'sitewide',
  163. $this->msg( 'ipb-partial' )->escaped() .
  164. new \OOUI\LabelWidget( [
  165. 'classes' => [ 'oo-ui-inline-help' ],
  166. 'label' => $this->msg( 'ipb-partial-help' )->text(),
  167. ] ) => 'partial',
  168. ],
  169. 'section' => 'actions',
  170. ];
  171. $a['PageRestrictions'] = [
  172. 'type' => 'titlesmultiselect',
  173. 'label' => $this->msg( 'ipb-pages-label' )->text(),
  174. 'exists' => true,
  175. 'max' => 10,
  176. 'cssclass' => 'mw-block-restriction',
  177. 'showMissing' => false,
  178. 'excludeDynamicNamespaces' => true,
  179. 'input' => [
  180. 'autocomplete' => false
  181. ],
  182. 'section' => 'actions',
  183. ];
  184. $a['NamespaceRestrictions'] = [
  185. 'type' => 'namespacesmultiselect',
  186. 'label' => $this->msg( 'ipb-namespaces-label' )->text(),
  187. 'exists' => true,
  188. 'cssclass' => 'mw-block-restriction',
  189. 'input' => [
  190. 'autocomplete' => false
  191. ],
  192. 'section' => 'actions',
  193. ];
  194. }
  195. $a['CreateAccount'] = [
  196. 'type' => 'check',
  197. 'label-message' => 'ipbcreateaccount',
  198. 'default' => true,
  199. 'section' => 'actions',
  200. ];
  201. if ( self::canBlockEmail( $user ) ) {
  202. $a['DisableEmail'] = [
  203. 'type' => 'check',
  204. 'label-message' => 'ipbemailban',
  205. 'section' => 'actions',
  206. ];
  207. }
  208. if ( $blockAllowsUTEdit ) {
  209. $a['DisableUTEdit'] = [
  210. 'type' => 'check',
  211. 'label-message' => 'ipb-disableusertalk',
  212. 'default' => false,
  213. 'section' => 'actions',
  214. ];
  215. }
  216. $a['Expiry'] = [
  217. 'type' => 'expiry',
  218. 'required' => true,
  219. 'options' => $suggestedDurations,
  220. 'default' => $this->msg( 'ipb-default-expiry' )->inContentLanguage()->text(),
  221. 'section' => 'expiry',
  222. ];
  223. $a['Reason'] = [
  224. 'type' => 'selectandother',
  225. // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
  226. // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
  227. // Unicode codepoints.
  228. 'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
  229. 'maxlength-unit' => 'codepoints',
  230. 'options-message' => 'ipbreason-dropdown',
  231. 'section' => 'reason',
  232. ];
  233. $a['AutoBlock'] = [
  234. 'type' => 'check',
  235. 'label-message' => 'ipbenableautoblock',
  236. 'default' => true,
  237. 'section' => 'options',
  238. ];
  239. # Allow some users to hide name from block log, blocklist and listusers
  240. if ( MediaWikiServices::getInstance()
  241. ->getPermissionManager()
  242. ->userHasRight( $user, 'hideuser' )
  243. ) {
  244. $a['HideUser'] = [
  245. 'type' => 'check',
  246. 'label-message' => 'ipbhidename',
  247. 'cssclass' => 'mw-block-hideuser',
  248. 'section' => 'options',
  249. ];
  250. }
  251. # Watchlist their user page? (Only if user is logged in)
  252. if ( $user->isLoggedIn() ) {
  253. $a['Watch'] = [
  254. 'type' => 'check',
  255. 'label-message' => 'ipbwatchuser',
  256. 'section' => 'options',
  257. ];
  258. }
  259. $a['HardBlock'] = [
  260. 'type' => 'check',
  261. 'label-message' => 'ipb-hardblock',
  262. 'default' => false,
  263. 'section' => 'options',
  264. ];
  265. # This is basically a copy of the Target field, but the user can't change it, so we
  266. # can see if the warnings we maybe showed to the user before still apply
  267. $a['PreviousTarget'] = [
  268. 'type' => 'hidden',
  269. 'default' => false,
  270. ];
  271. # We'll turn this into a checkbox if we need to
  272. $a['Confirm'] = [
  273. 'type' => 'hidden',
  274. 'default' => '',
  275. 'label-message' => 'ipb-confirm',
  276. 'cssclass' => 'mw-block-confirm',
  277. ];
  278. $this->maybeAlterFormDefaults( $a );
  279. // Allow extensions to add more fields
  280. Hooks::run( 'SpecialBlockModifyFormFields', [ $this, &$a ] );
  281. return $a;
  282. }
  283. /**
  284. * If the user has already been blocked with similar settings, load that block
  285. * and change the defaults for the form fields to match the existing settings.
  286. * @param array &$fields HTMLForm descriptor array
  287. */
  288. protected function maybeAlterFormDefaults( &$fields ) {
  289. # This will be overwritten by request data
  290. $fields['Target']['default'] = (string)$this->target;
  291. if ( $this->target ) {
  292. $status = self::validateTarget( $this->target, $this->getUser() );
  293. if ( !$status->isOK() ) {
  294. $errors = $status->getErrorsArray();
  295. $this->preErrors = array_merge( $this->preErrors, $errors );
  296. }
  297. }
  298. # This won't be
  299. $fields['PreviousTarget']['default'] = (string)$this->target;
  300. $block = DatabaseBlock::newFromTarget( $this->target );
  301. // Populate fields if there is a block that is not an autoblock; if it is a range
  302. // block, only populate the fields if the range is the same as $this->target
  303. if ( $block instanceof DatabaseBlock && $block->getType() !== DatabaseBlock::TYPE_AUTO
  304. && ( $this->type != DatabaseBlock::TYPE_RANGE
  305. || $block->getTarget() == $this->target )
  306. ) {
  307. $fields['HardBlock']['default'] = $block->isHardblock();
  308. $fields['CreateAccount']['default'] = $block->isCreateAccountBlocked();
  309. $fields['AutoBlock']['default'] = $block->isAutoblocking();
  310. if ( isset( $fields['DisableEmail'] ) ) {
  311. $fields['DisableEmail']['default'] = $block->isEmailBlocked();
  312. }
  313. if ( isset( $fields['HideUser'] ) ) {
  314. $fields['HideUser']['default'] = $block->getHideName();
  315. }
  316. if ( isset( $fields['DisableUTEdit'] ) ) {
  317. $fields['DisableUTEdit']['default'] = !$block->isUsertalkEditAllowed();
  318. }
  319. // If the username was hidden (ipb_deleted == 1), don't show the reason
  320. // unless this user also has rights to hideuser: T37839
  321. if ( !$block->getHideName() || MediaWikiServices::getInstance()
  322. ->getPermissionManager()
  323. ->userHasRight( $this->getUser(), 'hideuser' )
  324. ) {
  325. $fields['Reason']['default'] = $block->getReason();
  326. } else {
  327. $fields['Reason']['default'] = '';
  328. }
  329. if ( $this->getRequest()->wasPosted() ) {
  330. # Ok, so we got a POST submission asking us to reblock a user. So show the
  331. # confirm checkbox; the user will only see it if they haven't previously
  332. $fields['Confirm']['type'] = 'check';
  333. } else {
  334. # We got a target, but it wasn't a POST request, so the user must have gone
  335. # to a link like [[Special:Block/User]]. We don't need to show the checkbox
  336. # as long as they go ahead and block *that* user
  337. $fields['Confirm']['default'] = 1;
  338. }
  339. if ( $block->getExpiry() == 'infinity' ) {
  340. $fields['Expiry']['default'] = 'infinite';
  341. } else {
  342. $fields['Expiry']['default'] = wfTimestamp( TS_RFC2822, $block->getExpiry() );
  343. }
  344. $this->alreadyBlocked = true;
  345. $this->preErrors[] = [ 'ipb-needreblock', wfEscapeWikiText( (string)$block->getTarget() ) ];
  346. }
  347. if ( $this->alreadyBlocked || $this->getRequest()->wasPosted()
  348. || $this->getRequest()->getCheck( 'wpCreateAccount' )
  349. ) {
  350. $this->getOutput()->addJsConfigVars( 'wgCreateAccountDirty', true );
  351. }
  352. # We always need confirmation to do HideUser
  353. if ( $this->requestedHideUser ) {
  354. $fields['Confirm']['type'] = 'check';
  355. unset( $fields['Confirm']['default'] );
  356. $this->preErrors[] = [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
  357. }
  358. # Or if the user is trying to block themselves
  359. if ( (string)$this->target === $this->getUser()->getName() ) {
  360. $fields['Confirm']['type'] = 'check';
  361. unset( $fields['Confirm']['default'] );
  362. $this->preErrors[] = [ 'ipb-blockingself', 'ipb-confirmaction' ];
  363. }
  364. if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
  365. if ( $block instanceof DatabaseBlock && !$block->isSitewide() ) {
  366. $fields['EditingRestriction']['default'] = 'partial';
  367. } else {
  368. $fields['EditingRestriction']['default'] = 'sitewide';
  369. }
  370. if ( $block instanceof DatabaseBlock ) {
  371. $pageRestrictions = [];
  372. $namespaceRestrictions = [];
  373. foreach ( $block->getRestrictions() as $restriction ) {
  374. switch ( $restriction->getType() ) {
  375. case PageRestriction::TYPE:
  376. /** @var PageRestriction $restriction */
  377. '@phan-var PageRestriction $restriction';
  378. if ( $restriction->getTitle() ) {
  379. $pageRestrictions[] = $restriction->getTitle()->getPrefixedText();
  380. }
  381. break;
  382. case NamespaceRestriction::TYPE:
  383. $namespaceRestrictions[] = $restriction->getValue();
  384. break;
  385. }
  386. }
  387. if (
  388. !$block->isSitewide() &&
  389. empty( $pageRestrictions ) &&
  390. empty( $namespaceRestrictions )
  391. ) {
  392. $fields['Editing']['default'] = false;
  393. }
  394. // Sort the restrictions so they are in alphabetical order.
  395. sort( $pageRestrictions );
  396. $fields['PageRestrictions']['default'] = implode( "\n", $pageRestrictions );
  397. sort( $namespaceRestrictions );
  398. $fields['NamespaceRestrictions']['default'] = implode( "\n", $namespaceRestrictions );
  399. }
  400. }
  401. }
  402. /**
  403. * Add header elements like block log entries, etc.
  404. * @return string
  405. */
  406. protected function preText() {
  407. $this->getOutput()->addModuleStyles( [
  408. 'mediawiki.widgets.TagMultiselectWidget.styles',
  409. 'mediawiki.special',
  410. ] );
  411. $this->getOutput()->addModules( [ 'mediawiki.special.block' ] );
  412. $blockCIDRLimit = $this->getConfig()->get( 'BlockCIDRLimit' );
  413. $text = $this->msg( 'blockiptext', $blockCIDRLimit['IPv4'], $blockCIDRLimit['IPv6'] )->parse();
  414. $otherBlockMessages = [];
  415. if ( $this->target !== null ) {
  416. $targetName = $this->target;
  417. if ( $this->target instanceof User ) {
  418. $targetName = $this->target->getName();
  419. }
  420. # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
  421. Hooks::run( 'OtherBlockLogLink', [ &$otherBlockMessages, $targetName ] );
  422. if ( count( $otherBlockMessages ) ) {
  423. $s = Html::rawElement(
  424. 'h2',
  425. [],
  426. $this->msg( 'ipb-otherblocks-header', count( $otherBlockMessages ) )->parse()
  427. ) . "\n";
  428. $list = '';
  429. foreach ( $otherBlockMessages as $link ) {
  430. $list .= Html::rawElement( 'li', [], $link ) . "\n";
  431. }
  432. $s .= Html::rawElement(
  433. 'ul',
  434. [ 'class' => 'mw-blockip-alreadyblocked' ],
  435. $list
  436. ) . "\n";
  437. $text .= $s;
  438. }
  439. }
  440. return $text;
  441. }
  442. /**
  443. * Add footer elements to the form
  444. * @return string
  445. */
  446. protected function postText() {
  447. $links = [];
  448. $this->getOutput()->addModuleStyles( 'mediawiki.special' );
  449. $linkRenderer = $this->getLinkRenderer();
  450. # Link to the user's contributions, if applicable
  451. if ( $this->target instanceof User ) {
  452. $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
  453. $links[] = $linkRenderer->makeLink(
  454. $contribsPage,
  455. $this->msg( 'ipb-blocklist-contribs', $this->target->getName() )->text()
  456. );
  457. }
  458. # Link to unblock the specified user, or to a blank unblock form
  459. if ( $this->target instanceof User ) {
  460. $message = $this->msg(
  461. 'ipb-unblock-addr',
  462. wfEscapeWikiText( $this->target->getName() )
  463. )->parse();
  464. $list = SpecialPage::getTitleFor( 'Unblock', $this->target->getName() );
  465. } else {
  466. $message = $this->msg( 'ipb-unblock' )->parse();
  467. $list = SpecialPage::getTitleFor( 'Unblock' );
  468. }
  469. $links[] = $linkRenderer->makeKnownLink(
  470. $list,
  471. new HtmlArmor( $message )
  472. );
  473. # Link to the block list
  474. $links[] = $linkRenderer->makeKnownLink(
  475. SpecialPage::getTitleFor( 'BlockList' ),
  476. $this->msg( 'ipb-blocklist' )->text()
  477. );
  478. $user = $this->getUser();
  479. # Link to edit the block dropdown reasons, if applicable
  480. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  481. if ( $permissionManager->userHasRight( $user, 'editinterface' ) ) {
  482. $links[] = $linkRenderer->makeKnownLink(
  483. $this->msg( 'ipbreason-dropdown' )->inContentLanguage()->getTitle(),
  484. $this->msg( 'ipb-edit-dropdown' )->text(),
  485. [],
  486. [ 'action' => 'edit' ]
  487. );
  488. }
  489. $text = Html::rawElement(
  490. 'p',
  491. [ 'class' => 'mw-ipb-conveniencelinks' ],
  492. $this->getLanguage()->pipeList( $links )
  493. );
  494. $userTitle = self::getTargetUserTitle( $this->target );
  495. if ( $userTitle ) {
  496. # Get relevant extracts from the block and suppression logs, if possible
  497. $out = '';
  498. LogEventsList::showLogExtract(
  499. $out,
  500. 'block',
  501. $userTitle,
  502. '',
  503. [
  504. 'lim' => 10,
  505. 'msgKey' => [ 'blocklog-showlog', $userTitle->getText() ],
  506. 'showIfEmpty' => false
  507. ]
  508. );
  509. $text .= $out;
  510. # Add suppression block entries if allowed
  511. if ( $permissionManager->userHasRight( $user, 'suppressionlog' ) ) {
  512. LogEventsList::showLogExtract(
  513. $out,
  514. 'suppress',
  515. $userTitle,
  516. '',
  517. [
  518. 'lim' => 10,
  519. 'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ],
  520. 'msgKey' => [ 'blocklog-showsuppresslog', $userTitle->getText() ],
  521. 'showIfEmpty' => false
  522. ]
  523. );
  524. $text .= $out;
  525. }
  526. }
  527. return $text;
  528. }
  529. /**
  530. * Get a user page target for things like logs.
  531. * This handles account and IP range targets.
  532. * @param User|string $target
  533. * @return Title|null
  534. */
  535. protected static function getTargetUserTitle( $target ) {
  536. if ( $target instanceof User ) {
  537. return $target->getUserPage();
  538. } elseif ( IP::isIPAddress( $target ) ) {
  539. return Title::makeTitleSafe( NS_USER, $target );
  540. }
  541. return null;
  542. }
  543. /**
  544. * Determine the target of the block, and the type of target
  545. * @todo Should be in DatabaseBlock.php?
  546. * @param string $par Subpage parameter passed to setup, or data value from
  547. * the HTMLForm
  548. * @param WebRequest|null $request Optionally try and get data from a request too
  549. * @return array [ User|string|null, DatabaseBlock::TYPE_ constant|null ]
  550. * @phan-return array{0:User|string|null,1:int|null}
  551. */
  552. public static function getTargetAndType( $par, WebRequest $request = null ) {
  553. $i = 0;
  554. $target = null;
  555. while ( true ) {
  556. switch ( $i++ ) {
  557. case 0:
  558. # The HTMLForm will check wpTarget first and only if it doesn't get
  559. # a value use the default, which will be generated from the options
  560. # below; so this has to have a higher precedence here than $par, or
  561. # we could end up with different values in $this->target and the HTMLForm!
  562. if ( $request instanceof WebRequest ) {
  563. $target = $request->getText( 'wpTarget', null );
  564. }
  565. break;
  566. case 1:
  567. $target = $par;
  568. break;
  569. case 2:
  570. if ( $request instanceof WebRequest ) {
  571. $target = $request->getText( 'ip', null );
  572. }
  573. break;
  574. case 3:
  575. # B/C @since 1.18
  576. if ( $request instanceof WebRequest ) {
  577. $target = $request->getText( 'wpBlockAddress', null );
  578. }
  579. break;
  580. case 4:
  581. break 2;
  582. }
  583. list( $target, $type ) = DatabaseBlock::parseTarget( $target );
  584. if ( $type !== null ) {
  585. return [ $target, $type ];
  586. }
  587. }
  588. return [ null, null ];
  589. }
  590. /**
  591. * HTMLForm field validation-callback for Target field.
  592. * @since 1.18
  593. * @param string $value
  594. * @param array $alldata
  595. * @param HTMLForm $form
  596. * @return Message
  597. */
  598. public static function validateTargetField( $value, $alldata, $form ) {
  599. $status = self::validateTarget( $value, $form->getUser() );
  600. if ( !$status->isOK() ) {
  601. $errors = $status->getErrorsArray();
  602. return $form->msg( ...$errors[0] );
  603. } else {
  604. return true;
  605. }
  606. }
  607. /**
  608. * Validate a block target.
  609. *
  610. * @since 1.21
  611. * @param string $value Block target to check
  612. * @param User $user Performer of the block
  613. * @return Status
  614. */
  615. public static function validateTarget( $value, User $user ) {
  616. global $wgBlockCIDRLimit;
  617. /** @var User $target */
  618. list( $target, $type ) = self::getTargetAndType( $value );
  619. $status = Status::newGood( $target );
  620. if ( $type == DatabaseBlock::TYPE_USER ) {
  621. if ( $target->isAnon() ) {
  622. $status->fatal(
  623. 'nosuchusershort',
  624. wfEscapeWikiText( $target->getName() )
  625. );
  626. }
  627. $unblockStatus = self::checkUnblockSelf( $target, $user );
  628. if ( $unblockStatus !== true ) {
  629. $status->fatal( 'badaccess', $unblockStatus );
  630. }
  631. } elseif ( $type == DatabaseBlock::TYPE_RANGE ) {
  632. list( $ip, $range ) = explode( '/', $target, 2 );
  633. if (
  634. ( IP::isIPv4( $ip ) && $wgBlockCIDRLimit['IPv4'] == 32 ) ||
  635. ( IP::isIPv6( $ip ) && $wgBlockCIDRLimit['IPv6'] == 128 )
  636. ) {
  637. // Range block effectively disabled
  638. $status->fatal( 'range_block_disabled' );
  639. }
  640. if (
  641. ( IP::isIPv4( $ip ) && $range > 32 ) ||
  642. ( IP::isIPv6( $ip ) && $range > 128 )
  643. ) {
  644. // Dodgy range
  645. $status->fatal( 'ip_range_invalid' );
  646. }
  647. if ( IP::isIPv4( $ip ) && $range < $wgBlockCIDRLimit['IPv4'] ) {
  648. $status->fatal( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
  649. }
  650. if ( IP::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
  651. $status->fatal( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
  652. }
  653. } elseif ( $type == DatabaseBlock::TYPE_IP ) {
  654. # All is well
  655. } else {
  656. $status->fatal( 'badipaddress' );
  657. }
  658. return $status;
  659. }
  660. /**
  661. * Given the form data, actually implement a block. This is also called from ApiBlock.
  662. *
  663. * @param array $data
  664. * @param IContextSource $context
  665. * @return bool|array
  666. */
  667. public static function processForm( array $data, IContextSource $context ) {
  668. $performer = $context->getUser();
  669. $enablePartialBlocks = $context->getConfig()->get( 'EnablePartialBlocks' );
  670. $isPartialBlock = $enablePartialBlocks &&
  671. isset( $data['EditingRestriction'] ) &&
  672. $data['EditingRestriction'] === 'partial';
  673. # This might have been a hidden field or a checkbox, so interesting data
  674. # can come from it
  675. $data['Confirm'] = !in_array( $data['Confirm'], [ '', '0', null, false ], true );
  676. /** @var User $target */
  677. list( $target, $type ) = self::getTargetAndType( $data['Target'] );
  678. if ( $type == DatabaseBlock::TYPE_USER ) {
  679. $user = $target;
  680. $target = $user->getName();
  681. $userId = $user->getId();
  682. # Give admins a heads-up before they go and block themselves. Much messier
  683. # to do this for IPs, but it's pretty unlikely they'd ever get the 'block'
  684. # permission anyway, although the code does allow for it.
  685. # Note: Important to use $target instead of $data['Target']
  686. # since both $data['PreviousTarget'] and $target are normalized
  687. # but $data['target'] gets overridden by (non-normalized) request variable
  688. # from previous request.
  689. if ( $target === $performer->getName() &&
  690. ( $data['PreviousTarget'] !== $target || !$data['Confirm'] )
  691. ) {
  692. return [ 'ipb-blockingself', 'ipb-confirmaction' ];
  693. }
  694. } elseif ( $type == DatabaseBlock::TYPE_RANGE ) {
  695. $user = null;
  696. $userId = 0;
  697. } elseif ( $type == DatabaseBlock::TYPE_IP ) {
  698. $user = null;
  699. $target = $target->getName();
  700. $userId = 0;
  701. } else {
  702. # This should have been caught in the form field validation
  703. return [ 'badipaddress' ];
  704. }
  705. $expiryTime = self::parseExpiryInput( $data['Expiry'] );
  706. if (
  707. // an expiry time is needed
  708. ( strlen( $data['Expiry'] ) == 0 ) ||
  709. // can't be a larger string as 50 (it should be a time format in any way)
  710. ( strlen( $data['Expiry'] ) > 50 ) ||
  711. // check, if the time could be parsed
  712. !$expiryTime
  713. ) {
  714. return [ 'ipb_expiry_invalid' ];
  715. }
  716. // an expiry time should be in the future, not in the
  717. // past (wouldn't make any sense) - bug T123069
  718. if ( $expiryTime < wfTimestampNow() ) {
  719. return [ 'ipb_expiry_old' ];
  720. }
  721. if ( !isset( $data['DisableEmail'] ) ) {
  722. $data['DisableEmail'] = false;
  723. }
  724. # If the user has done the form 'properly', they won't even have been given the
  725. # option to suppress-block unless they have the 'hideuser' permission
  726. if ( !isset( $data['HideUser'] ) ) {
  727. $data['HideUser'] = false;
  728. }
  729. if ( $data['HideUser'] ) {
  730. if ( !MediaWikiServices::getInstance()
  731. ->getPermissionManager()
  732. ->userHasRight( $performer, 'hideuser' )
  733. ) {
  734. # this codepath is unreachable except by a malicious user spoofing forms,
  735. # or by race conditions (user has hideuser and block rights, loads block form,
  736. # and loses hideuser rights before submission); so need to fail completely
  737. # rather than just silently disable hiding
  738. return [ 'badaccess-group0' ];
  739. }
  740. if ( $isPartialBlock ) {
  741. return [ 'ipb_hide_partial' ];
  742. }
  743. # Recheck params here...
  744. $hideUserContribLimit = $context->getConfig()->get( 'HideUserContribLimit' );
  745. if ( $type != DatabaseBlock::TYPE_USER ) {
  746. $data['HideUser'] = false; # IP users should not be hidden
  747. } elseif ( !wfIsInfinity( $data['Expiry'] ) ) {
  748. # Bad expiry.
  749. return [ 'ipb_expiry_temp' ];
  750. } elseif ( $hideUserContribLimit !== false
  751. && $user->getEditCount() > $hideUserContribLimit
  752. ) {
  753. # Typically, the user should have a handful of edits.
  754. # Disallow hiding users with many edits for performance.
  755. return [ [ 'ipb_hide_invalid',
  756. Message::numParam( $hideUserContribLimit ) ] ];
  757. } elseif ( !$data['Confirm'] ) {
  758. return [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
  759. }
  760. }
  761. $blockAllowsUTEdit = $context->getConfig()->get( 'BlockAllowsUTEdit' );
  762. $userTalkEditAllowed = !$blockAllowsUTEdit || !$data['DisableUTEdit'];
  763. if ( !$userTalkEditAllowed &&
  764. $isPartialBlock &&
  765. !in_array( NS_USER_TALK, explode( "\n", $data['NamespaceRestrictions'] ) )
  766. ) {
  767. return [ 'ipb-prevent-user-talk-edit' ];
  768. }
  769. # Create block object.
  770. $block = new DatabaseBlock();
  771. $block->setTarget( $target );
  772. $block->setBlocker( $performer );
  773. $block->setReason( $data['Reason'][0] );
  774. $block->setExpiry( $expiryTime );
  775. $block->isCreateAccountBlocked( $data['CreateAccount'] );
  776. $block->isUsertalkEditAllowed( $userTalkEditAllowed );
  777. $block->isEmailBlocked( $data['DisableEmail'] );
  778. $block->isHardblock( $data['HardBlock'] );
  779. $block->isAutoblocking( $data['AutoBlock'] );
  780. $block->setHideName( $data['HideUser'] );
  781. if ( $isPartialBlock ) {
  782. $block->isSitewide( false );
  783. }
  784. $reason = [ 'hookaborted' ];
  785. if ( !Hooks::run( 'BlockIp', [ &$block, &$performer, &$reason ] ) ) {
  786. return $reason;
  787. }
  788. $pageRestrictions = [];
  789. $namespaceRestrictions = [];
  790. if ( $enablePartialBlocks ) {
  791. if ( $data['PageRestrictions'] !== '' ) {
  792. $pageRestrictions = array_map( function ( $text ) {
  793. $title = Title::newFromText( $text );
  794. // Use the link cache since the title has already been loaded when
  795. // the field was validated.
  796. $restriction = new PageRestriction( 0, $title->getArticleID() );
  797. $restriction->setTitle( $title );
  798. return $restriction;
  799. }, explode( "\n", $data['PageRestrictions'] ) );
  800. }
  801. if ( $data['NamespaceRestrictions'] !== '' ) {
  802. $namespaceRestrictions = array_map( function ( $id ) {
  803. return new NamespaceRestriction( 0, $id );
  804. }, explode( "\n", $data['NamespaceRestrictions'] ) );
  805. }
  806. $restrictions = ( array_merge( $pageRestrictions, $namespaceRestrictions ) );
  807. $block->setRestrictions( $restrictions );
  808. }
  809. $priorBlock = null;
  810. # Try to insert block. Is there a conflicting block?
  811. $status = $block->insert();
  812. if ( !$status ) {
  813. # Indicates whether the user is confirming the block and is aware of
  814. # the conflict (did not change the block target in the meantime)
  815. $blockNotConfirmed = !$data['Confirm'] || ( array_key_exists( 'PreviousTarget', $data )
  816. && $data['PreviousTarget'] !== $target );
  817. # Special case for API - T34434
  818. $reblockNotAllowed = ( array_key_exists( 'Reblock', $data ) && !$data['Reblock'] );
  819. # Show form unless the user is already aware of this...
  820. if ( $blockNotConfirmed || $reblockNotAllowed ) {
  821. return [ [ 'ipb_already_blocked', $block->getTarget() ] ];
  822. # Otherwise, try to update the block...
  823. } else {
  824. # This returns direct blocks before autoblocks/rangeblocks, since we should
  825. # be sure the user is blocked by now it should work for our purposes
  826. $currentBlock = DatabaseBlock::newFromTarget( $target );
  827. if ( $block->equals( $currentBlock ) ) {
  828. return [ [ 'ipb_already_blocked', $block->getTarget() ] ];
  829. }
  830. # If the name was hidden and the blocking user cannot hide
  831. # names, then don't allow any block changes...
  832. if ( $currentBlock->getHideName() && !MediaWikiServices::getInstance()
  833. ->getPermissionManager()
  834. ->userHasRight( $performer, 'hideuser' )
  835. ) {
  836. return [ 'cant-see-hidden-user' ];
  837. }
  838. $priorBlock = clone $currentBlock;
  839. $currentBlock->isHardblock( $block->isHardblock() );
  840. $currentBlock->isCreateAccountBlocked( $block->isCreateAccountBlocked() );
  841. $currentBlock->setExpiry( $block->getExpiry() );
  842. $currentBlock->isAutoblocking( $block->isAutoblocking() );
  843. $currentBlock->setHideName( $block->getHideName() );
  844. $currentBlock->isEmailBlocked( $block->isEmailBlocked() );
  845. $currentBlock->isUsertalkEditAllowed( $block->isUsertalkEditAllowed() );
  846. $currentBlock->setReason( $block->getReason() );
  847. if ( $enablePartialBlocks ) {
  848. // Maintain the sitewide status. If partial blocks is not enabled,
  849. // saving the block will result in a sitewide block.
  850. $currentBlock->isSitewide( $block->isSitewide() );
  851. // Set the block id of the restrictions.
  852. $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
  853. $currentBlock->setRestrictions(
  854. $blockRestrictionStore->setBlockId( $currentBlock->getId(), $restrictions )
  855. );
  856. }
  857. $status = $currentBlock->update();
  858. // TODO handle failure
  859. $logaction = 'reblock';
  860. # Unset _deleted fields if requested
  861. if ( $currentBlock->getHideName() && !$data['HideUser'] ) {
  862. RevisionDeleteUser::unsuppressUserName( $target, $userId );
  863. }
  864. # If hiding/unhiding a name, this should go in the private logs
  865. if ( (bool)$currentBlock->getHideName() ) {
  866. $data['HideUser'] = true;
  867. }
  868. $block = $currentBlock;
  869. }
  870. } else {
  871. $logaction = 'block';
  872. }
  873. Hooks::run( 'BlockIpComplete', [ $block, $performer, $priorBlock ] );
  874. # Set *_deleted fields if requested
  875. if ( $data['HideUser'] ) {
  876. RevisionDeleteUser::suppressUserName( $target, $userId );
  877. }
  878. # Can't watch a rangeblock
  879. if ( $type != DatabaseBlock::TYPE_RANGE && $data['Watch'] ) {
  880. WatchAction::doWatch(
  881. Title::makeTitle( NS_USER, $target ),
  882. $performer,
  883. User::IGNORE_USER_RIGHTS
  884. );
  885. }
  886. # DatabaseBlock constructor sanitizes certain block options on insert
  887. $data['BlockEmail'] = $block->isEmailBlocked();
  888. $data['AutoBlock'] = $block->isAutoblocking();
  889. # Prepare log parameters
  890. $logParams = [];
  891. $logParams['5::duration'] = $data['Expiry'];
  892. $logParams['6::flags'] = self::blockLogFlags( $data, $type );
  893. $logParams['sitewide'] = $block->isSitewide();
  894. if ( $enablePartialBlocks && !$block->isSitewide() ) {
  895. if ( $data['PageRestrictions'] !== '' ) {
  896. $logParams['7::restrictions']['pages'] = explode( "\n", $data['PageRestrictions'] );
  897. }
  898. if ( $data['NamespaceRestrictions'] !== '' ) {
  899. $logParams['7::restrictions']['namespaces'] = explode( "\n", $data['NamespaceRestrictions'] );
  900. }
  901. }
  902. # Make log entry, if the name is hidden, put it in the suppression log
  903. $log_type = $data['HideUser'] ? 'suppress' : 'block';
  904. $logEntry = new ManualLogEntry( $log_type, $logaction );
  905. $logEntry->setTarget( Title::makeTitle( NS_USER, $target ) );
  906. $logEntry->setComment( $data['Reason'][0] );
  907. $logEntry->setPerformer( $performer );
  908. $logEntry->setParameters( $logParams );
  909. # Relate log ID to block ID (T27763)
  910. $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
  911. $logId = $logEntry->insert();
  912. if ( !empty( $data['Tags'] ) ) {
  913. $logEntry->addTags( $data['Tags'] );
  914. }
  915. $logEntry->publish( $logId );
  916. return true;
  917. }
  918. /**
  919. * Get an array of suggested block durations from MediaWiki:Ipboptions
  920. * @todo FIXME: This uses a rather odd syntax for the options, should it be converted
  921. * to the standard "**<duration>|<displayname>" format?
  922. * @param Language|null $lang The language to get the durations in, or null to use
  923. * the wiki's content language
  924. * @param bool $includeOther Whether to include the 'other' option in the list of
  925. * suggestions
  926. * @return array
  927. */
  928. public static function getSuggestedDurations( Language $lang = null, $includeOther = true ) {
  929. $a = [];
  930. $msg = $lang === null
  931. ? wfMessage( 'ipboptions' )->inContentLanguage()->text()
  932. : wfMessage( 'ipboptions' )->inLanguage( $lang )->text();
  933. if ( $msg == '-' ) {
  934. return [];
  935. }
  936. foreach ( explode( ',', $msg ) as $option ) {
  937. if ( strpos( $option, ':' ) === false ) {
  938. $option = "$option:$option";
  939. }
  940. list( $show, $value ) = explode( ':', $option );
  941. $a[$show] = $value;
  942. }
  943. if ( $a && $includeOther ) {
  944. // if options exist, add other to the end instead of the begining (which
  945. // is what happens by default).
  946. $a[ wfMessage( 'ipbother' )->text() ] = 'other';
  947. }
  948. return $a;
  949. }
  950. /**
  951. * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
  952. * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
  953. *
  954. * @todo strtotime() only accepts English strings. This means the expiry input
  955. * can only be specified in English.
  956. * @see https://www.php.net/manual/en/function.strtotime.php
  957. *
  958. * @param string $expiry Whatever was typed into the form
  959. * @return string|bool Timestamp or 'infinity' or false on error.
  960. */
  961. public static function parseExpiryInput( $expiry ) {
  962. if ( wfIsInfinity( $expiry ) ) {
  963. return 'infinity';
  964. }
  965. $expiry = strtotime( $expiry );
  966. if ( $expiry < 0 || $expiry === false ) {
  967. return false;
  968. }
  969. return wfTimestamp( TS_MW, $expiry );
  970. }
  971. /**
  972. * Can we do an email block?
  973. * @param UserIdentity $user The sysop wanting to make a block
  974. * @return bool
  975. */
  976. public static function canBlockEmail( UserIdentity $user ) {
  977. global $wgEnableUserEmail;
  978. return ( $wgEnableUserEmail && MediaWikiServices::getInstance()
  979. ->getPermissionManager()
  980. ->userHasRight( $user, 'blockemail' ) );
  981. }
  982. /**
  983. * T17810: blocked admins should not be able to block/unblock
  984. * others, and probably shouldn't be able to unblock themselves
  985. * either.
  986. *
  987. * Exception: Users can block the user who blocked them, to reduce
  988. * advantage of a malicious account blocking all admins (T150826)
  989. *
  990. * @param User|int|string|null $target Target to block or unblock; could be a User object,
  991. * or a user ID or username, or null when the target is not known yet (e.g. when
  992. * displaying Special:Block)
  993. * @param User $performer User doing the request
  994. * @return bool|string True or error message key
  995. */
  996. public static function checkUnblockSelf( $target, User $performer ) {
  997. if ( is_int( $target ) ) {
  998. $target = User::newFromId( $target );
  999. } elseif ( is_string( $target ) ) {
  1000. $target = User::newFromName( $target );
  1001. }
  1002. if ( $performer->getBlock() ) {
  1003. if ( $target instanceof User && $target->getId() == $performer->getId() ) {
  1004. # User is trying to unblock themselves
  1005. if ( MediaWikiServices::getInstance()
  1006. ->getPermissionManager()
  1007. ->userHasRight( $performer, 'unblockself' )
  1008. ) {
  1009. return true;
  1010. # User blocked themselves and is now trying to reverse it
  1011. } elseif ( $performer->blockedBy() === $performer->getName() ) {
  1012. return true;
  1013. } else {
  1014. return 'ipbnounblockself';
  1015. }
  1016. } elseif (
  1017. $target instanceof User &&
  1018. $performer->getBlock() instanceof DatabaseBlock &&
  1019. $performer->getBlock()->getBy() &&
  1020. $performer->getBlock()->getBy() === $target->getId()
  1021. ) {
  1022. // Allow users to block the user that blocked them.
  1023. // This is to prevent a situation where a malicious user
  1024. // blocks all other users. This way, the non-malicious
  1025. // user can block the malicious user back, resulting
  1026. // in a stalemate.
  1027. return true;
  1028. } else {
  1029. # User is trying to block/unblock someone else
  1030. return 'ipbblocked';
  1031. }
  1032. } else {
  1033. return true;
  1034. }
  1035. }
  1036. /**
  1037. * Return a comma-delimited list of "flags" to be passed to the log
  1038. * reader for this block, to provide more information in the logs
  1039. * @param array $data From HTMLForm data
  1040. * @param int $type DatabaseBlock::TYPE_ constant (USER, RANGE, or IP)
  1041. * @return string
  1042. */
  1043. protected static function blockLogFlags( array $data, $type ) {
  1044. $config = RequestContext::getMain()->getConfig();
  1045. $blockAllowsUTEdit = $config->get( 'BlockAllowsUTEdit' );
  1046. $flags = [];
  1047. # when blocking a user the option 'anononly' is not available/has no effect
  1048. # -> do not write this into log
  1049. if ( !$data['HardBlock'] && $type != DatabaseBlock::TYPE_USER ) {
  1050. // For grepping: message block-log-flags-anononly
  1051. $flags[] = 'anononly';
  1052. }
  1053. if ( $data['CreateAccount'] ) {
  1054. // For grepping: message block-log-flags-nocreate
  1055. $flags[] = 'nocreate';
  1056. }
  1057. # Same as anononly, this is not displayed when blocking an IP address
  1058. if ( !$data['AutoBlock'] && $type == DatabaseBlock::TYPE_USER ) {
  1059. // For grepping: message block-log-flags-noautoblock
  1060. $flags[] = 'noautoblock';
  1061. }
  1062. if ( $data['DisableEmail'] ) {
  1063. // For grepping: message block-log-flags-noemail
  1064. $flags[] = 'noemail';
  1065. }
  1066. if ( $blockAllowsUTEdit && $data['DisableUTEdit'] ) {
  1067. // For grepping: message block-log-flags-nousertalk
  1068. $flags[] = 'nousertalk';
  1069. }
  1070. if ( $data['HideUser'] ) {
  1071. // For grepping: message block-log-flags-hiddenname
  1072. $flags[] = 'hiddenname';
  1073. }
  1074. return implode( ',', $flags );
  1075. }
  1076. /**
  1077. * Process the form on POST submission.
  1078. * @param array $data
  1079. * @param HTMLForm|null $form
  1080. * @return bool|array True for success, false for didn't-try, array of errors on failure
  1081. */
  1082. public function onSubmit( array $data, HTMLForm $form = null ) {
  1083. // If "Editing" checkbox is unchecked, the block must be a partial block affecting
  1084. // actions other than editing, and there must be no restrictions.
  1085. if ( isset( $data['Editing'] ) && $data['Editing'] === false ) {
  1086. $data['EditingRestriction'] = 'partial';
  1087. $data['PageRestrictions'] = '';
  1088. $data['NamespaceRestrictions'] = '';
  1089. }
  1090. return self::processForm( $data, $form->getContext() );
  1091. }
  1092. /**
  1093. * Do something exciting on successful processing of the form, most likely to show a
  1094. * confirmation message
  1095. */
  1096. public function onSuccess() {
  1097. $out = $this->getOutput();
  1098. $out->setPageTitle( $this->msg( 'blockipsuccesssub' ) );
  1099. $out->addWikiMsg( 'blockipsuccesstext', wfEscapeWikiText( $this->target ) );
  1100. }
  1101. /**
  1102. * Return an array of subpages beginning with $search that this special page will accept.
  1103. *
  1104. * @param string $search Prefix to search for
  1105. * @param int $limit Maximum number of results to return (usually 10)
  1106. * @param int $offset Number of results to skip (usually 0)
  1107. * @return string[] Matching subpages
  1108. */
  1109. public function prefixSearchSubpages( $search, $limit, $offset ) {
  1110. $user = User::newFromName( $search );
  1111. if ( !$user ) {
  1112. // No prefix suggestion for invalid user
  1113. return [];
  1114. }
  1115. // Autocomplete subpage as user list - public to allow caching
  1116. return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
  1117. }
  1118. protected function getGroupName() {
  1119. return 'users';
  1120. }
  1121. }