Merge "MovePage methods need to run safety checks"
[lhc/web/wiklou.git] / includes / specials / SpecialMovepage.php
1 <?php
2 /**
3 * Implements Special:Movepage
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
24 /**
25 * A special page that allows users to change page titles
26 *
27 * @ingroup SpecialPage
28 */
29 class MovePageForm extends UnlistedSpecialPage {
30 /** @var Title */
31 protected $oldTitle = null;
32
33 /** @var Title */
34 protected $newTitle;
35
36 /** @var string Text input */
37 protected $reason;
38
39 // Checks
40
41 /** @var bool */
42 protected $moveTalk;
43
44 /** @var bool */
45 protected $deleteAndMove;
46
47 /** @var bool */
48 protected $moveSubpages;
49
50 /** @var bool */
51 protected $fixRedirects;
52
53 /** @var bool */
54 protected $leaveRedirect;
55
56 /** @var bool */
57 protected $moveOverShared;
58
59 private $watch = false;
60
61 public function __construct() {
62 parent::__construct( 'Movepage' );
63 }
64
65 public function doesWrites() {
66 return true;
67 }
68
69 public function execute( $par ) {
70 $this->useTransactionalTimeLimit();
71
72 $this->checkReadOnly();
73
74 $this->setHeaders();
75 $this->outputHeader();
76
77 $request = $this->getRequest();
78 $target = $par ?? $request->getVal( 'target' );
79
80 // Yes, the use of getVal() and getText() is wanted, see T22365
81
82 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
83 $this->oldTitle = Title::newFromText( $oldTitleText );
84
85 if ( !$this->oldTitle ) {
86 // Either oldTitle wasn't passed, or newFromText returned null
87 throw new ErrorPageError( 'notargettitle', 'notargettext' );
88 }
89 if ( !$this->oldTitle->exists() ) {
90 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
91 }
92
93 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
94 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle->getNamespace() );
95 // Backwards compatibility for forms submitting here from other sources
96 // which is more common than it should be..
97 $newTitleText_bc = $request->getText( 'wpNewTitle' );
98 $this->newTitle = strlen( $newTitleText_bc ) > 0
99 ? Title::newFromText( $newTitleText_bc )
100 : Title::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
101
102 $user = $this->getUser();
103
104 # Check rights
105 $permErrors = $this->oldTitle->getUserPermissionsErrors( 'move', $user );
106 if ( count( $permErrors ) ) {
107 // Auto-block user's IP if the account was "hard" blocked
108 DeferredUpdates::addCallableUpdate( function () use ( $user ) {
109 $user->spreadAnyEditBlock();
110 } );
111 throw new PermissionsError( 'move', $permErrors );
112 }
113
114 $def = !$request->wasPosted();
115
116 $this->reason = $request->getText( 'wpReason' );
117 $this->moveTalk = $request->getBool( 'wpMovetalk', $def );
118 $this->fixRedirects = $request->getBool( 'wpFixRedirects', $def );
119 $this->leaveRedirect = $request->getBool( 'wpLeaveRedirect', $def );
120 $this->moveSubpages = $request->getBool( 'wpMovesubpages' );
121 $this->deleteAndMove = $request->getBool( 'wpDeleteAndMove' );
122 $this->moveOverShared = $request->getBool( 'wpMoveOverSharedFile' );
123 $this->watch = $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
124
125 if ( $request->getVal( 'action' ) == 'submit' && $request->wasPosted()
126 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
127 ) {
128 $this->doSubmit();
129 } else {
130 $this->showForm( [] );
131 }
132 }
133
134 /**
135 * Show the form
136 *
137 * @param array $err Error messages. Each item is an error message.
138 * It may either be a string message name or array message name and
139 * parameters, like the second argument to OutputPage::wrapWikiMsg().
140 * @param bool $isPermError Whether the error message is about user permissions.
141 */
142 function showForm( $err, $isPermError = false ) {
143 $this->getSkin()->setRelevantTitle( $this->oldTitle );
144
145 $out = $this->getOutput();
146 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle->getPrefixedText() ) );
147 $out->addModuleStyles( 'mediawiki.special' );
148 $out->addModules( 'mediawiki.special.movePage' );
149 $this->addHelpLink( 'Help:Moving a page' );
150
151 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
152 'movepagetext' :
153 'movepagetext-noredirectfixer'
154 );
155
156 if ( $this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) {
157 $out->wrapWikiMsg(
158 "<div class=\"warningbox mw-moveuserpage-warning\">\n$1\n</div>",
159 'moveuserpage-warning'
160 );
161 } elseif ( $this->oldTitle->getNamespace() == NS_CATEGORY ) {
162 $out->wrapWikiMsg(
163 "<div class=\"warningbox mw-movecategorypage-warning\">\n$1\n</div>",
164 'movecategorypage-warning'
165 );
166 }
167
168 $deleteAndMove = false;
169 $moveOverShared = false;
170
171 $newTitle = $this->newTitle;
172
173 if ( !$newTitle ) {
174 # Show the current title as a default
175 # when the form is first opened.
176 $newTitle = $this->oldTitle;
177 } elseif ( !count( $err ) ) {
178 # If a title was supplied, probably from the move log revert
179 # link, check for validity. We can then show some diagnostic
180 # information and save a click.
181 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
182 if ( is_array( $newerr ) ) {
183 $err = $newerr;
184 }
185 }
186
187 $user = $this->getUser();
188
189 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
190 && $newTitle->quickUserCan( 'delete', $user )
191 ) {
192 $out->wrapWikiMsg(
193 "<div class='warningbox'>\n$1\n</div>\n",
194 [ 'delete_and_move_text', $newTitle->getPrefixedText() ]
195 );
196 $deleteAndMove = true;
197 $err = [];
198 }
199
200 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
201 && $user->isAllowed( 'reupload-shared' )
202 ) {
203 $out->wrapWikiMsg(
204 "<div class='warningbox'>\n$1\n</div>\n",
205 [
206 'move-over-sharedrepo',
207 $newTitle->getPrefixedText()
208 ]
209 );
210 $moveOverShared = true;
211 $err = [];
212 }
213
214 $oldTalk = $this->oldTitle->getTalkPage();
215 $oldTitleSubpages = $this->oldTitle->hasSubpages();
216 $oldTitleTalkSubpages = $this->oldTitle->getTalkPage()->hasSubpages();
217
218 $canMoveSubpage = ( $oldTitleSubpages || $oldTitleTalkSubpages ) &&
219 !count( $this->oldTitle->getUserPermissionsErrors( 'move-subpages', $user ) );
220
221 # We also want to be able to move assoc. subpage talk-pages even if base page
222 # has no associated talk page, so || with $oldTitleTalkSubpages.
223 $considerTalk = !$this->oldTitle->isTalkPage() &&
224 ( $oldTalk->exists()
225 || ( $oldTitleTalkSubpages && $canMoveSubpage ) );
226
227 $dbr = wfGetDB( DB_REPLICA );
228 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
229 $hasRedirects = $dbr->selectField( 'redirect', '1',
230 [
231 'rd_namespace' => $this->oldTitle->getNamespace(),
232 'rd_title' => $this->oldTitle->getDBkey(),
233 ], __METHOD__ );
234 } else {
235 $hasRedirects = false;
236 }
237
238 if ( count( $err ) ) {
239 if ( $isPermError ) {
240 $action_desc = $this->msg( 'action-move' )->plain();
241 $errMsgHtml = $this->msg( 'permissionserrorstext-withaction',
242 count( $err ), $action_desc )->parseAsBlock();
243 } else {
244 $errMsgHtml = $this->msg( 'cannotmove', count( $err ) )->parseAsBlock();
245 }
246
247 if ( count( $err ) == 1 ) {
248 $errMsg = $err[0];
249 $errMsgName = array_shift( $errMsg );
250
251 if ( $errMsgName == 'hookaborted' ) {
252 $errMsgHtml .= "<p>{$errMsg[0]}</p>\n";
253 } else {
254 $errMsgHtml .= $this->msg( $errMsgName, $errMsg )->parseAsBlock();
255 }
256 } else {
257 $errStr = [];
258
259 foreach ( $err as $errMsg ) {
260 if ( $errMsg[0] == 'hookaborted' ) {
261 $errStr[] = $errMsg[1];
262 } else {
263 $errMsgName = array_shift( $errMsg );
264 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
265 }
266 }
267
268 $errMsgHtml .= '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n";
269 }
270 $out->addHTML( Html::errorBox( $errMsgHtml ) );
271 }
272
273 if ( $this->oldTitle->isProtected( 'move' ) ) {
274 # Is the title semi-protected?
275 if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
276 $noticeMsg = 'semiprotectedpagemovewarning';
277 $classes[] = 'mw-textarea-sprotected';
278 } else {
279 # Then it must be protected based on static groups (regular)
280 $noticeMsg = 'protectedpagemovewarning';
281 $classes[] = 'mw-textarea-protected';
282 }
283 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
284 $out->addWikiMsg( $noticeMsg );
285 LogEventsList::showLogExtract(
286 $out,
287 'protect',
288 $this->oldTitle,
289 '',
290 [ 'lim' => 1 ]
291 );
292 $out->addHTML( "</div>\n" );
293 }
294
295 // Length limit for wpReason and wpNewTitleMain is enforced in the
296 // mediawiki.special.movePage module
297
298 $immovableNamespaces = [];
299 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
300 if ( !MWNamespace::isMovable( $nsId ) ) {
301 $immovableNamespaces[] = $nsId;
302 }
303 }
304
305 $handler = ContentHandler::getForTitle( $this->oldTitle );
306
307 $out->enableOOUI();
308 $fields = [];
309
310 $fields[] = new OOUI\FieldLayout(
311 new MediaWiki\Widget\ComplexTitleInputWidget( [
312 'id' => 'wpNewTitle',
313 'namespace' => [
314 'id' => 'wpNewTitleNs',
315 'name' => 'wpNewTitleNs',
316 'value' => $newTitle->getNamespace(),
317 'exclude' => $immovableNamespaces,
318 ],
319 'title' => [
320 'id' => 'wpNewTitleMain',
321 'name' => 'wpNewTitleMain',
322 'value' => $newTitle->getText(),
323 // Inappropriate, since we're expecting the user to input a non-existent page's title
324 'suggestions' => false,
325 ],
326 'infusable' => true,
327 ] ),
328 [
329 'label' => $this->msg( 'newtitle' )->text(),
330 'align' => 'top',
331 ]
332 );
333
334 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
335 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
336 // Unicode codepoints.
337 $fields[] = new OOUI\FieldLayout(
338 new OOUI\TextInputWidget( [
339 'name' => 'wpReason',
340 'id' => 'wpReason',
341 'maxLength' => CommentStore::COMMENT_CHARACTER_LIMIT,
342 'infusable' => true,
343 'value' => $this->reason,
344 ] ),
345 [
346 'label' => $this->msg( 'movereason' )->text(),
347 'align' => 'top',
348 ]
349 );
350
351 if ( $considerTalk ) {
352 $fields[] = new OOUI\FieldLayout(
353 new OOUI\CheckboxInputWidget( [
354 'name' => 'wpMovetalk',
355 'id' => 'wpMovetalk',
356 'value' => '1',
357 'selected' => $this->moveTalk,
358 ] ),
359 [
360 'label' => $this->msg( 'movetalk' )->text(),
361 'help' => new OOUI\HtmlSnippet( $this->msg( 'movepagetalktext' )->parseAsBlock() ),
362 'helpInline' => true,
363 'align' => 'inline',
364 'id' => 'wpMovetalk-field',
365 ]
366 );
367 }
368
369 if ( $user->isAllowed( 'suppressredirect' ) ) {
370 if ( $handler->supportsRedirects() ) {
371 $isChecked = $this->leaveRedirect;
372 $isDisabled = false;
373 } else {
374 $isChecked = false;
375 $isDisabled = true;
376 }
377 $fields[] = new OOUI\FieldLayout(
378 new OOUI\CheckboxInputWidget( [
379 'name' => 'wpLeaveRedirect',
380 'id' => 'wpLeaveRedirect',
381 'value' => '1',
382 'selected' => $isChecked,
383 'disabled' => $isDisabled,
384 ] ),
385 [
386 'label' => $this->msg( 'move-leave-redirect' )->text(),
387 'align' => 'inline',
388 ]
389 );
390 }
391
392 if ( $hasRedirects ) {
393 $fields[] = new OOUI\FieldLayout(
394 new OOUI\CheckboxInputWidget( [
395 'name' => 'wpFixRedirects',
396 'id' => 'wpFixRedirects',
397 'value' => '1',
398 'selected' => $this->fixRedirects,
399 ] ),
400 [
401 'label' => $this->msg( 'fix-double-redirects' )->text(),
402 'align' => 'inline',
403 ]
404 );
405 }
406
407 if ( $canMoveSubpage ) {
408 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
409 $fields[] = new OOUI\FieldLayout(
410 new OOUI\CheckboxInputWidget( [
411 'name' => 'wpMovesubpages',
412 'id' => 'wpMovesubpages',
413 'value' => '1',
414 # Don't check the box if we only have talk subpages to
415 # move and we aren't moving the talk page.
416 'selected' => $this->moveSubpages && ( $this->oldTitle->hasSubpages() || $this->moveTalk ),
417 ] ),
418 [
419 'label' => new OOUI\HtmlSnippet(
420 $this->msg(
421 ( $this->oldTitle->hasSubpages()
422 ? 'move-subpages'
423 : 'move-talk-subpages' )
424 )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
425 ),
426 'align' => 'inline',
427 ]
428 );
429 }
430
431 # Don't allow watching if user is not logged in
432 if ( $user->isLoggedIn() ) {
433 $watchChecked = $user->isLoggedIn() && ( $this->watch || $user->getBoolOption( 'watchmoves' )
434 || $user->isWatched( $this->oldTitle ) );
435 $fields[] = new OOUI\FieldLayout(
436 new OOUI\CheckboxInputWidget( [
437 'name' => 'wpWatch',
438 'id' => 'watch', # ew
439 'value' => '1',
440 'selected' => $watchChecked,
441 ] ),
442 [
443 'label' => $this->msg( 'move-watch' )->text(),
444 'align' => 'inline',
445 ]
446 );
447 }
448
449 $hiddenFields = '';
450 if ( $moveOverShared ) {
451 $hiddenFields .= Html::hidden( 'wpMoveOverSharedFile', '1' );
452 }
453
454 if ( $deleteAndMove ) {
455 $fields[] = new OOUI\FieldLayout(
456 new OOUI\CheckboxInputWidget( [
457 'name' => 'wpDeleteAndMove',
458 'id' => 'wpDeleteAndMove',
459 'value' => '1',
460 ] ),
461 [
462 'label' => $this->msg( 'delete_and_move_confirm' )->text(),
463 'align' => 'inline',
464 ]
465 );
466 }
467
468 $fields[] = new OOUI\FieldLayout(
469 new OOUI\ButtonInputWidget( [
470 'name' => 'wpMove',
471 'value' => $this->msg( 'movepagebtn' )->text(),
472 'label' => $this->msg( 'movepagebtn' )->text(),
473 'flags' => [ 'primary', 'progressive' ],
474 'type' => 'submit',
475 ] ),
476 [
477 'align' => 'top',
478 ]
479 );
480
481 $fieldset = new OOUI\FieldsetLayout( [
482 'label' => $this->msg( 'move-page-legend' )->text(),
483 'id' => 'mw-movepage-table',
484 'items' => $fields,
485 ] );
486
487 $form = new OOUI\FormLayout( [
488 'method' => 'post',
489 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
490 'id' => 'movepage',
491 ] );
492 $form->appendContent(
493 $fieldset,
494 new OOUI\HtmlSnippet(
495 $hiddenFields .
496 Html::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
497 Html::hidden( 'wpEditToken', $user->getEditToken() )
498 )
499 );
500
501 $out->addHTML(
502 new OOUI\PanelLayout( [
503 'classes' => [ 'movepage-wrapper' ],
504 'expanded' => false,
505 'padded' => true,
506 'framed' => true,
507 'content' => $form,
508 ] )
509 );
510
511 $this->showLogFragment( $this->oldTitle );
512 $this->showSubpages( $this->oldTitle );
513 }
514
515 function doSubmit() {
516 $user = $this->getUser();
517
518 if ( $user->pingLimiter( 'move' ) ) {
519 throw new ThrottledError;
520 }
521
522 $ot = $this->oldTitle;
523 $nt = $this->newTitle;
524
525 # don't allow moving to pages with # in
526 if ( !$nt || $nt->hasFragment() ) {
527 $this->showForm( [ [ 'badtitletext' ] ] );
528
529 return;
530 }
531
532 # Show a warning if the target file exists on a shared repo
533 if ( $nt->getNamespace() == NS_FILE
534 && !( $this->moveOverShared && $user->isAllowed( 'reupload-shared' ) )
535 && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt )
536 && wfFindFile( $nt )
537 ) {
538 $this->showForm( [ [ 'file-exists-sharedrepo' ] ] );
539
540 return;
541 }
542
543 # Delete to make way if requested
544 if ( $this->deleteAndMove ) {
545 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
546 if ( count( $permErrors ) ) {
547 # Only show the first error
548 $this->showForm( $permErrors, true );
549
550 return;
551 }
552
553 $page = WikiPage::factory( $nt );
554
555 // Small safety margin to guard against concurrent edits
556 if ( $page->isBatchedDelete( 5 ) ) {
557 $this->showForm( [ [ 'movepage-delete-first' ] ] );
558
559 return;
560 }
561
562 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
563
564 // Delete an associated image if there is
565 if ( $nt->getNamespace() == NS_FILE ) {
566 $file = wfLocalFile( $nt );
567 $file->load( File::READ_LATEST );
568 if ( $file->exists() ) {
569 $file->delete( $reason, false, $user );
570 }
571 }
572
573 $error = ''; // passed by ref
574 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
575 if ( !$deleteStatus->isGood() ) {
576 $this->showForm( $deleteStatus->getErrorsArray() );
577
578 return;
579 }
580 }
581
582 $handler = ContentHandler::getForTitle( $ot );
583
584 if ( !$handler->supportsRedirects() ) {
585 $createRedirect = false;
586 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
587 $createRedirect = $this->leaveRedirect;
588 } else {
589 $createRedirect = true;
590 }
591
592 # Do the actual move.
593 $mp = new MovePage( $ot, $nt );
594
595 $userPermitted = $mp->checkPermissions( $user, $this->reason )->isOK();
596
597 $status = $mp->moveIfAllowed( $user, $this->reason, $createRedirect );
598 if ( !$status->isOK() ) {
599 $this->showForm( $status->getErrorsArray(), !$userPermitted );
600 return;
601 }
602
603 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects ) {
604 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
605 }
606
607 $out = $this->getOutput();
608 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
609
610 $linkRenderer = $this->getLinkRenderer();
611 $oldLink = $linkRenderer->makeLink(
612 $ot,
613 null,
614 [ 'id' => 'movepage-oldlink' ],
615 [ 'redirect' => 'no' ]
616 );
617 $newLink = $linkRenderer->makeKnownLink(
618 $nt,
619 null,
620 [ 'id' => 'movepage-newlink' ]
621 );
622 $oldText = $ot->getPrefixedText();
623 $newText = $nt->getPrefixedText();
624
625 if ( $ot->exists() ) {
626 // NOTE: we assume that if the old title exists, it's because it was re-created as
627 // a redirect to the new title. This is not safe, but what we did before was
628 // even worse: we just determined whether a redirect should have been created,
629 // and reported that it was created if it should have, without any checks.
630 // Also note that isRedirect() is unreliable because of T39209.
631 $msgName = 'movepage-moved-redirect';
632 } else {
633 $msgName = 'movepage-moved-noredirect';
634 }
635
636 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
637 $newLink )->params( $oldText, $newText )->parseAsBlock() );
638 $out->addWikiMsg( $msgName );
639
640 // Avoid PHP 7.1 warning from passing $this by reference
641 $movePage = $this;
642 Hooks::run( 'SpecialMovepageAfterMove', [ &$movePage, &$ot, &$nt ] );
643
644 # Now we move extra pages we've been asked to move: subpages and talk
645 # pages. First, if the old page or the new page is a talk page, we
646 # can't move any talk pages: cancel that.
647 if ( $ot->isTalkPage() || $nt->isTalkPage() ) {
648 $this->moveTalk = false;
649 }
650
651 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
652 $this->moveSubpages = false;
653 }
654
655 /**
656 * Next make a list of id's. This might be marginally less efficient
657 * than a more direct method, but this is not a highly performance-cri-
658 * tical code path and readable code is more important here.
659 *
660 * If the target namespace doesn't allow subpages, moving with subpages
661 * would mean that you couldn't move them back in one operation, which
662 * is bad.
663 * @todo FIXME: A specific error message should be given in this case.
664 */
665
666 // @todo FIXME: Use Title::moveSubpages() here
667 $dbr = wfGetDB( DB_MASTER );
668 if ( $this->moveSubpages && (
669 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
670 $this->moveTalk
671 && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
672 )
673 ) ) {
674 $conds = [
675 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
676 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
677 ];
678 $conds['page_namespace'] = [];
679 if ( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
680 $conds['page_namespace'][] = $ot->getNamespace();
681 }
682 if ( $this->moveTalk &&
683 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
684 ) {
685 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
686 }
687 } elseif ( $this->moveTalk ) {
688 $conds = [
689 'page_namespace' => $ot->getTalkPage()->getNamespace(),
690 'page_title' => $ot->getDBkey()
691 ];
692 } else {
693 # Skip the query
694 $conds = null;
695 }
696
697 $extraPages = [];
698 if ( !is_null( $conds ) ) {
699 $extraPages = TitleArray::newFromResult(
700 $dbr->select( 'page',
701 [ 'page_id', 'page_namespace', 'page_title' ],
702 $conds,
703 __METHOD__
704 )
705 );
706 }
707
708 $extraOutput = [];
709 $count = 1;
710 foreach ( $extraPages as $oldSubpage ) {
711 if ( $ot->equals( $oldSubpage ) || $nt->equals( $oldSubpage ) ) {
712 # Already did this one.
713 continue;
714 }
715
716 $newPageName = preg_replace(
717 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
718 StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # T23234
719 $oldSubpage->getDBkey()
720 );
721
722 if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) {
723 // Moving a subpage from a subject namespace to a talk namespace or vice-versa
724 $newNs = $nt->getNamespace();
725 } elseif ( $oldSubpage->isTalkPage() ) {
726 $newNs = $nt->getTalkPage()->getNamespace();
727 } else {
728 $newNs = $nt->getSubjectPage()->getNamespace();
729 }
730
731 # T16385: we need makeTitleSafe because the new page names may
732 # be longer than 255 characters.
733 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
734 if ( !$newSubpage ) {
735 $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
736 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
737 ->params( Title::makeName( $newNs, $newPageName ) )->escaped();
738 continue;
739 }
740
741 # This was copy-pasted from Renameuser, bleh.
742 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
743 $link = $linkRenderer->makeKnownLink( $newSubpage );
744 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
745 } else {
746 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
747
748 if ( $success === true ) {
749 if ( $this->fixRedirects ) {
750 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
751 }
752 $oldLink = $linkRenderer->makeLink(
753 $oldSubpage,
754 null,
755 [],
756 [ 'redirect' => 'no' ]
757 );
758
759 $newLink = $linkRenderer->makeKnownLink( $newSubpage );
760 $extraOutput[] = $this->msg( 'movepage-page-moved' )
761 ->rawParams( $oldLink, $newLink )->escaped();
762 ++$count;
763
764 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
765 if ( $count >= $maximumMovedPages ) {
766 $extraOutput[] = $this->msg( 'movepage-max-pages' )
767 ->numParams( $maximumMovedPages )->escaped();
768 break;
769 }
770 } else {
771 $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
772 $newLink = $linkRenderer->makeLink( $newSubpage );
773 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
774 ->rawParams( $oldLink, $newLink )->escaped();
775 }
776 }
777 }
778
779 if ( $extraOutput !== [] ) {
780 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
781 }
782
783 # Deal with watches (we don't watch subpages)
784 WatchAction::doWatchOrUnwatch( $this->watch, $ot, $user );
785 WatchAction::doWatchOrUnwatch( $this->watch, $nt, $user );
786 }
787
788 function showLogFragment( $title ) {
789 $moveLogPage = new LogPage( 'move' );
790 $out = $this->getOutput();
791 $out->addHTML( Xml::element( 'h2', null, $moveLogPage->getName()->text() ) );
792 LogEventsList::showLogExtract( $out, 'move', $title );
793 }
794
795 /**
796 * Show subpages of the page being moved. Section is not shown if both current
797 * namespace does not support subpages and no talk subpages were found.
798 *
799 * @param Title $title Page being moved.
800 */
801 function showSubpages( $title ) {
802 $nsHasSubpages = MWNamespace::hasSubpages( $title->getNamespace() );
803 $subpages = $title->getSubpages();
804 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
805
806 $titleIsTalk = $title->isTalkPage();
807 $subpagesTalk = $title->getTalkPage()->getSubpages();
808 $countTalk = $subpagesTalk instanceof TitleArray ? $subpagesTalk->count() : 0;
809 $totalCount = $count + $countTalk;
810
811 if ( !$nsHasSubpages && $countTalk == 0 ) {
812 return;
813 }
814
815 $this->getOutput()->wrapWikiMsg(
816 '== $1 ==',
817 [ 'movesubpage', ( $titleIsTalk ? $count : $totalCount ) ]
818 );
819
820 if ( $nsHasSubpages ) {
821 $this->showSubpagesList( $subpages, $count, 'movesubpagetext', true );
822 }
823
824 if ( !$titleIsTalk && $countTalk > 0 ) {
825 $this->showSubpagesList( $subpagesTalk, $countTalk, 'movesubpagetalktext' );
826 }
827 }
828
829 function showSubpagesList( $subpages, $pagecount, $wikiMsg, $noSubpageMsg = false ) {
830 $out = $this->getOutput();
831
832 # No subpages.
833 if ( $pagecount == 0 && $noSubpageMsg ) {
834 $out->addWikiMsg( 'movenosubpage' );
835 return;
836 }
837
838 $out->addWikiMsg( $wikiMsg, $this->getLanguage()->formatNum( $pagecount ) );
839 $out->addHTML( "<ul>\n" );
840
841 $linkBatch = new LinkBatch( $subpages );
842 $linkBatch->setCaller( __METHOD__ );
843 $linkBatch->execute();
844 $linkRenderer = $this->getLinkRenderer();
845
846 foreach ( $subpages as $subpage ) {
847 $link = $linkRenderer->makeLink( $subpage );
848 $out->addHTML( "<li>$link</li>\n" );
849 }
850 $out->addHTML( "</ul>\n" );
851 }
852
853 /**
854 * Return an array of subpages beginning with $search that this special page will accept.
855 *
856 * @param string $search Prefix to search for
857 * @param int $limit Maximum number of results to return (usually 10)
858 * @param int $offset Number of results to skip (usually 0)
859 * @return string[] Matching subpages
860 */
861 public function prefixSearchSubpages( $search, $limit, $offset ) {
862 return $this->prefixSearchString( $search, $limit, $offset );
863 }
864
865 protected function getGroupName() {
866 return 'pagetools';
867 }
868 }