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