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