Select "move subpages" by default when moving a page
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * A special page that allows users to change page titles
28 *
29 * @ingroup SpecialPage
30 */
31 class MovePageForm extends UnlistedSpecialPage {
32 /** @var Title */
33 protected $oldTitle = null;
34
35 /** @var Title */
36 protected $newTitle;
37
38 /** @var string Text input */
39 protected $reason;
40
41 // Checks
42
43 /** @var bool */
44 protected $moveTalk;
45
46 /** @var bool */
47 protected $deleteAndMove;
48
49 /** @var bool */
50 protected $moveSubpages;
51
52 /** @var bool */
53 protected $fixRedirects;
54
55 /** @var bool */
56 protected $leaveRedirect;
57
58 /** @var bool */
59 protected $moveOverShared;
60
61 private $watch = false;
62
63 public function __construct() {
64 parent::__construct( 'Movepage' );
65 }
66
67 public function doesWrites() {
68 return true;
69 }
70
71 public function execute( $par ) {
72 $this->useTransactionalTimeLimit();
73
74 $this->checkReadOnly();
75
76 $this->setHeaders();
77 $this->outputHeader();
78
79 $request = $this->getRequest();
80 $target = $par ?? $request->getVal( 'target' );
81
82 // Yes, the use of getVal() and getText() is wanted, see T22365
83
84 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
85 $this->oldTitle = Title::newFromText( $oldTitleText );
86
87 if ( !$this->oldTitle ) {
88 // Either oldTitle wasn't passed, or newFromText returned null
89 throw new ErrorPageError( 'notargettitle', 'notargettext' );
90 }
91 if ( !$this->oldTitle->exists() ) {
92 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
93 }
94
95 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
96 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle->getNamespace() );
97 // Backwards compatibility for forms submitting here from other sources
98 // which is more common than it should be..
99 $newTitleText_bc = $request->getText( 'wpNewTitle' );
100 $this->newTitle = strlen( $newTitleText_bc ) > 0
101 ? Title::newFromText( $newTitleText_bc )
102 : Title::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
103
104 $user = $this->getUser();
105
106 # Check rights
107 $permErrors = $this->oldTitle->getUserPermissionsErrors( 'move', $user );
108 if ( count( $permErrors ) ) {
109 // Auto-block user's IP if the account was "hard" blocked
110 DeferredUpdates::addCallableUpdate( function () use ( $user ) {
111 $user->spreadAnyEditBlock();
112 } );
113 throw new PermissionsError( 'move', $permErrors );
114 }
115
116 $def = !$request->wasPosted();
117
118 $this->reason = $request->getText( 'wpReason' );
119 $this->moveTalk = $request->getBool( 'wpMovetalk', $def );
120 $this->fixRedirects = $request->getBool( 'wpFixRedirects', $def );
121 $this->leaveRedirect = $request->getBool( 'wpLeaveRedirect', $def );
122 $this->moveSubpages = $request->getBool( 'wpMovesubpages' );
123 $this->deleteAndMove = $request->getBool( 'wpDeleteAndMove' );
124 $this->moveOverShared = $request->getBool( 'wpMoveOverSharedFile' );
125 $this->watch = $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
126
127 if ( $request->getVal( 'action' ) == 'submit' && $request->wasPosted()
128 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
129 ) {
130 $this->doSubmit();
131 } else {
132 $this->showForm( [] );
133 }
134 }
135
136 /**
137 * Show the form
138 *
139 * @param array $err Error messages. Each item is an error message.
140 * It may either be a string message name or array message name and
141 * parameters, like the second argument to OutputPage::wrapWikiMsg().
142 * @param bool $isPermError Whether the error message is about user permissions.
143 */
144 function showForm( $err, $isPermError = false ) {
145 $this->getSkin()->setRelevantTitle( $this->oldTitle );
146
147 $out = $this->getOutput();
148 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle->getPrefixedText() ) );
149 $out->addModuleStyles( 'mediawiki.special' );
150 $out->addModules( 'mediawiki.special.movePage' );
151 $this->addHelpLink( 'Help:Moving a page' );
152
153 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
154 'movepagetext' :
155 'movepagetext-noredirectfixer'
156 );
157
158 if ( $this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) {
159 $out->wrapWikiMsg(
160 "<div class=\"warningbox mw-moveuserpage-warning\">\n$1\n</div>",
161 'moveuserpage-warning'
162 );
163 } elseif ( $this->oldTitle->getNamespace() == NS_CATEGORY ) {
164 $out->wrapWikiMsg(
165 "<div class=\"warningbox mw-movecategorypage-warning\">\n$1\n</div>",
166 'movecategorypage-warning'
167 );
168 }
169
170 $deleteAndMove = false;
171 $moveOverShared = false;
172
173 $newTitle = $this->newTitle;
174
175 if ( !$newTitle ) {
176 # Show the current title as a default
177 # when the form is first opened.
178 $newTitle = $this->oldTitle;
179 } elseif ( !count( $err ) ) {
180 # If a title was supplied, probably from the move log revert
181 # link, check for validity. We can then show some diagnostic
182 # information and save a click.
183 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
184 if ( is_array( $newerr ) ) {
185 $err = $newerr;
186 }
187 }
188
189 $user = $this->getUser();
190
191 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
192 && $newTitle->quickUserCan( 'delete', $user )
193 ) {
194 $out->wrapWikiMsg(
195 "<div class='warningbox'>\n$1\n</div>\n",
196 [ 'delete_and_move_text', $newTitle->getPrefixedText() ]
197 );
198 $deleteAndMove = true;
199 $err = [];
200 }
201
202 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
203 && $user->isAllowed( 'reupload-shared' )
204 ) {
205 $out->wrapWikiMsg(
206 "<div class='warningbox'>\n$1\n</div>\n",
207 [
208 'move-over-sharedrepo',
209 $newTitle->getPrefixedText()
210 ]
211 );
212 $moveOverShared = true;
213 $err = [];
214 }
215
216 $oldTalk = $this->oldTitle->getTalkPage();
217 $oldTitleSubpages = $this->oldTitle->hasSubpages();
218 $oldTitleTalkSubpages = $this->oldTitle->getTalkPage()->hasSubpages();
219
220 $canMoveSubpage = ( $oldTitleSubpages || $oldTitleTalkSubpages ) &&
221 !count( $this->oldTitle->getUserPermissionsErrors( 'move-subpages', $user ) );
222
223 # We also want to be able to move assoc. subpage talk-pages even if base page
224 # has no associated talk page, so || with $oldTitleTalkSubpages.
225 $considerTalk = !$this->oldTitle->isTalkPage() &&
226 ( $oldTalk->exists()
227 || ( $oldTitleTalkSubpages && $canMoveSubpage ) );
228
229 $dbr = wfGetDB( DB_REPLICA );
230 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
231 $hasRedirects = $dbr->selectField( 'redirect', '1',
232 [
233 'rd_namespace' => $this->oldTitle->getNamespace(),
234 'rd_title' => $this->oldTitle->getDBkey(),
235 ], __METHOD__ );
236 } else {
237 $hasRedirects = false;
238 }
239
240 if ( count( $err ) ) {
241 if ( $isPermError ) {
242 $action_desc = $this->msg( 'action-move' )->plain();
243 $errMsgHtml = $this->msg( 'permissionserrorstext-withaction',
244 count( $err ), $action_desc )->parseAsBlock();
245 } else {
246 $errMsgHtml = $this->msg( 'cannotmove', count( $err ) )->parseAsBlock();
247 }
248
249 if ( count( $err ) == 1 ) {
250 $errMsg = $err[0];
251 $errMsgName = array_shift( $errMsg );
252
253 if ( $errMsgName == 'hookaborted' ) {
254 $errMsgHtml .= "<p>{$errMsg[0]}</p>\n";
255 } else {
256 $errMsgHtml .= $this->msg( $errMsgName, $errMsg )->parseAsBlock();
257 }
258 } else {
259 $errStr = [];
260
261 foreach ( $err as $errMsg ) {
262 if ( $errMsg[0] == 'hookaborted' ) {
263 $errStr[] = $errMsg[1];
264 } else {
265 $errMsgName = array_shift( $errMsg );
266 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
267 }
268 }
269
270 $errMsgHtml .= '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n";
271 }
272 $out->addHTML( Html::errorBox( $errMsgHtml ) );
273 }
274
275 if ( $this->oldTitle->isProtected( 'move' ) ) {
276 # Is the title semi-protected?
277 if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
278 $noticeMsg = 'semiprotectedpagemovewarning';
279 $classes[] = 'mw-textarea-sprotected';
280 } else {
281 # Then it must be protected based on static groups (regular)
282 $noticeMsg = 'protectedpagemovewarning';
283 $classes[] = 'mw-textarea-protected';
284 }
285 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
286 $out->addWikiMsg( $noticeMsg );
287 LogEventsList::showLogExtract(
288 $out,
289 'protect',
290 $this->oldTitle,
291 '',
292 [ 'lim' => 1 ]
293 );
294 $out->addHTML( "</div>\n" );
295 }
296
297 // Length limit for wpReason and wpNewTitleMain is enforced in the
298 // mediawiki.special.movePage module
299
300 $immovableNamespaces = [];
301 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
302 if ( !MediaWikiServices::getInstance()->getNamespaceInfo()->isMovable( $nsId ) ) {
303 $immovableNamespaces[] = $nsId;
304 }
305 }
306
307 $handler = ContentHandler::getForTitle( $this->oldTitle );
308
309 $out->enableOOUI();
310 $fields = [];
311
312 $fields[] = new OOUI\FieldLayout(
313 new MediaWiki\Widget\ComplexTitleInputWidget( [
314 'id' => 'wpNewTitle',
315 'namespace' => [
316 'id' => 'wpNewTitleNs',
317 'name' => 'wpNewTitleNs',
318 'value' => $newTitle->getNamespace(),
319 'exclude' => $immovableNamespaces,
320 ],
321 'title' => [
322 'id' => 'wpNewTitleMain',
323 'name' => 'wpNewTitleMain',
324 'value' => $newTitle->getText(),
325 // Inappropriate, since we're expecting the user to input a non-existent page's title
326 'suggestions' => false,
327 ],
328 'infusable' => true,
329 ] ),
330 [
331 'label' => $this->msg( 'newtitle' )->text(),
332 'align' => 'top',
333 ]
334 );
335
336 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
337 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
338 // Unicode codepoints.
339 $fields[] = new OOUI\FieldLayout(
340 new OOUI\TextInputWidget( [
341 'name' => 'wpReason',
342 'id' => 'wpReason',
343 'maxLength' => CommentStore::COMMENT_CHARACTER_LIMIT,
344 'infusable' => true,
345 'value' => $this->reason,
346 ] ),
347 [
348 'label' => $this->msg( 'movereason' )->text(),
349 'align' => 'top',
350 ]
351 );
352
353 if ( $considerTalk ) {
354 $fields[] = new OOUI\FieldLayout(
355 new OOUI\CheckboxInputWidget( [
356 'name' => 'wpMovetalk',
357 'id' => 'wpMovetalk',
358 'value' => '1',
359 'selected' => $this->moveTalk,
360 ] ),
361 [
362 'label' => $this->msg( 'movetalk' )->text(),
363 'help' => new OOUI\HtmlSnippet( $this->msg( 'movepagetalktext' )->parseAsBlock() ),
364 'helpInline' => true,
365 'align' => 'inline',
366 'id' => 'wpMovetalk-field',
367 ]
368 );
369 }
370
371 if ( $user->isAllowed( 'suppressredirect' ) ) {
372 if ( $handler->supportsRedirects() ) {
373 $isChecked = $this->leaveRedirect;
374 $isDisabled = false;
375 } else {
376 $isChecked = false;
377 $isDisabled = true;
378 }
379 $fields[] = new OOUI\FieldLayout(
380 new OOUI\CheckboxInputWidget( [
381 'name' => 'wpLeaveRedirect',
382 'id' => 'wpLeaveRedirect',
383 'value' => '1',
384 'selected' => $isChecked,
385 'disabled' => $isDisabled,
386 ] ),
387 [
388 'label' => $this->msg( 'move-leave-redirect' )->text(),
389 'align' => 'inline',
390 ]
391 );
392 }
393
394 if ( $hasRedirects ) {
395 $fields[] = new OOUI\FieldLayout(
396 new OOUI\CheckboxInputWidget( [
397 'name' => 'wpFixRedirects',
398 'id' => 'wpFixRedirects',
399 'value' => '1',
400 'selected' => $this->fixRedirects,
401 ] ),
402 [
403 'label' => $this->msg( 'fix-double-redirects' )->text(),
404 'align' => 'inline',
405 ]
406 );
407 }
408
409 if ( $canMoveSubpage ) {
410 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
411 $fields[] = new OOUI\FieldLayout(
412 new OOUI\CheckboxInputWidget( [
413 'name' => 'wpMovesubpages',
414 'id' => 'wpMovesubpages',
415 'value' => '1',
416 'selected' => true, // T222953 Always check the box
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 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
668 $dbr = wfGetDB( DB_MASTER );
669 if ( $this->moveSubpages && (
670 $nsInfo->hasSubpages( $nt->getNamespace() ) || (
671 $this->moveTalk
672 && $nsInfo->hasSubpages( $nt->getTalkPage()->getNamespace() )
673 )
674 ) ) {
675 $conds = [
676 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
677 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
678 ];
679 $conds['page_namespace'] = [];
680 if ( $nsInfo->hasSubpages( $nt->getNamespace() ) ) {
681 $conds['page_namespace'][] = $ot->getNamespace();
682 }
683 if ( $this->moveTalk &&
684 $nsInfo->hasSubpages( $nt->getTalkPage()->getNamespace() )
685 ) {
686 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
687 }
688 } elseif ( $this->moveTalk ) {
689 $conds = [
690 'page_namespace' => $ot->getTalkPage()->getNamespace(),
691 'page_title' => $ot->getDBkey()
692 ];
693 } else {
694 # Skip the query
695 $conds = null;
696 }
697
698 $extraPages = [];
699 if ( !is_null( $conds ) ) {
700 $extraPages = TitleArray::newFromResult(
701 $dbr->select( 'page',
702 [ 'page_id', 'page_namespace', 'page_title' ],
703 $conds,
704 __METHOD__
705 )
706 );
707 }
708
709 $extraOutput = [];
710 $count = 1;
711 foreach ( $extraPages as $oldSubpage ) {
712 if ( $ot->equals( $oldSubpage ) || $nt->equals( $oldSubpage ) ) {
713 # Already did this one.
714 continue;
715 }
716
717 $newPageName = preg_replace(
718 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
719 StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # T23234
720 $oldSubpage->getDBkey()
721 );
722
723 if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) {
724 // Moving a subpage from a subject namespace to a talk namespace or vice-versa
725 $newNs = $nt->getNamespace();
726 } elseif ( $oldSubpage->isTalkPage() ) {
727 $newNs = $nt->getTalkPage()->getNamespace();
728 } else {
729 $newNs = $nt->getSubjectPage()->getNamespace();
730 }
731
732 # T16385: we need makeTitleSafe because the new page names may
733 # be longer than 255 characters.
734 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
735 if ( !$newSubpage ) {
736 $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
737 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
738 ->params( Title::makeName( $newNs, $newPageName ) )->escaped();
739 continue;
740 }
741
742 # This was copy-pasted from Renameuser, bleh.
743 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
744 $link = $linkRenderer->makeKnownLink( $newSubpage );
745 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
746 } else {
747 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
748
749 if ( $success === true ) {
750 if ( $this->fixRedirects ) {
751 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
752 }
753 $oldLink = $linkRenderer->makeLink(
754 $oldSubpage,
755 null,
756 [],
757 [ 'redirect' => 'no' ]
758 );
759
760 $newLink = $linkRenderer->makeKnownLink( $newSubpage );
761 $extraOutput[] = $this->msg( 'movepage-page-moved' )
762 ->rawParams( $oldLink, $newLink )->escaped();
763 ++$count;
764
765 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
766 if ( $count >= $maximumMovedPages ) {
767 $extraOutput[] = $this->msg( 'movepage-max-pages' )
768 ->numParams( $maximumMovedPages )->escaped();
769 break;
770 }
771 } else {
772 $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
773 $newLink = $linkRenderer->makeLink( $newSubpage );
774 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
775 ->rawParams( $oldLink, $newLink )->escaped();
776 }
777 }
778 }
779
780 if ( $extraOutput !== [] ) {
781 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
782 }
783
784 # Deal with watches (we don't watch subpages)
785 WatchAction::doWatchOrUnwatch( $this->watch, $ot, $user );
786 WatchAction::doWatchOrUnwatch( $this->watch, $nt, $user );
787 }
788
789 function showLogFragment( $title ) {
790 $moveLogPage = new LogPage( 'move' );
791 $out = $this->getOutput();
792 $out->addHTML( Xml::element( 'h2', null, $moveLogPage->getName()->text() ) );
793 LogEventsList::showLogExtract( $out, 'move', $title );
794 }
795
796 /**
797 * Show subpages of the page being moved. Section is not shown if both current
798 * namespace does not support subpages and no talk subpages were found.
799 *
800 * @param Title $title Page being moved.
801 */
802 function showSubpages( $title ) {
803 $nsHasSubpages = MediaWikiServices::getInstance()->getNamespaceInfo()->
804 hasSubpages( $title->getNamespace() );
805 $subpages = $title->getSubpages();
806 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
807
808 $titleIsTalk = $title->isTalkPage();
809 $subpagesTalk = $title->getTalkPage()->getSubpages();
810 $countTalk = $subpagesTalk instanceof TitleArray ? $subpagesTalk->count() : 0;
811 $totalCount = $count + $countTalk;
812
813 if ( !$nsHasSubpages && $countTalk == 0 ) {
814 return;
815 }
816
817 $this->getOutput()->wrapWikiMsg(
818 '== $1 ==',
819 [ 'movesubpage', ( $titleIsTalk ? $count : $totalCount ) ]
820 );
821
822 if ( $nsHasSubpages ) {
823 $this->showSubpagesList( $subpages, $count, 'movesubpagetext', true );
824 }
825
826 if ( !$titleIsTalk && $countTalk > 0 ) {
827 $this->showSubpagesList( $subpagesTalk, $countTalk, 'movesubpagetalktext' );
828 }
829 }
830
831 function showSubpagesList( $subpages, $pagecount, $wikiMsg, $noSubpageMsg = false ) {
832 $out = $this->getOutput();
833
834 # No subpages.
835 if ( $pagecount == 0 && $noSubpageMsg ) {
836 $out->addWikiMsg( 'movenosubpage' );
837 return;
838 }
839
840 $out->addWikiMsg( $wikiMsg, $this->getLanguage()->formatNum( $pagecount ) );
841 $out->addHTML( "<ul>\n" );
842
843 $linkBatch = new LinkBatch( $subpages );
844 $linkBatch->setCaller( __METHOD__ );
845 $linkBatch->execute();
846 $linkRenderer = $this->getLinkRenderer();
847
848 foreach ( $subpages as $subpage ) {
849 $link = $linkRenderer->makeLink( $subpage );
850 $out->addHTML( "<li>$link</li>\n" );
851 }
852 $out->addHTML( "</ul>\n" );
853 }
854
855 /**
856 * Return an array of subpages beginning with $search that this special page will accept.
857 *
858 * @param string $search Prefix to search for
859 * @param int $limit Maximum number of results to return (usually 10)
860 * @param int $offset Number of results to skip (usually 0)
861 * @return string[] Matching subpages
862 */
863 public function prefixSearchSubpages( $search, $limit, $offset ) {
864 return $this->prefixSearchString( $search, $limit, $offset );
865 }
866
867 protected function getGroupName() {
868 return 'pagetools';
869 }
870 }