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