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