Merge "Only store currently-existing categories in the categories table"
[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 /**
25 * A special page that allows users to change page titles
26 *
27 * @ingroup SpecialPage
28 */
29 class MovePageForm extends UnlistedSpecialPage {
30 /** @var Title */
31 protected $oldTitle = null;
32
33 /** @var Title */
34 protected $newTitle;
35
36 /** @var string Text input */
37 protected $reason;
38
39 // Checks
40
41 /** @var bool */
42 protected $moveTalk;
43
44 /** @var bool */
45 protected $deleteAndMove;
46
47 /** @var bool */
48 protected $moveSubpages;
49
50 /** @var bool */
51 protected $fixRedirects;
52
53 /** @var bool */
54 protected $leaveRedirect;
55
56 /** @var bool */
57 protected $moveOverShared;
58
59 private $watch = false;
60
61 public function __construct() {
62 parent::__construct( 'Movepage' );
63 }
64
65 public function doesWrites() {
66 return true;
67 }
68
69 public function execute( $par ) {
70 $this->useTransactionalTimeLimit();
71
72 $this->checkReadOnly();
73
74 $this->setHeaders();
75 $this->outputHeader();
76
77 $request = $this->getRequest();
78 $target = !is_null( $par ) ? $par : $request->getVal( 'target' );
79
80 // Yes, the use of getVal() and getText() is wanted, see bug 20365
81
82 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
83 $this->oldTitle = Title::newFromText( $oldTitleText );
84
85 if ( !$this->oldTitle ) {
86 // Either oldTitle wasn't passed, or newFromText returned null
87 throw new ErrorPageError( 'notargettitle', 'notargettext' );
88 }
89 if ( !$this->oldTitle->exists() ) {
90 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
91 }
92
93 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
94 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle->getNamespace() );
95 // Backwards compatibility for forms submitting here from other sources
96 // which is more common than it should be..
97 $newTitleText_bc = $request->getText( 'wpNewTitle' );
98 $this->newTitle = strlen( $newTitleText_bc ) > 0
99 ? Title::newFromText( $newTitleText_bc )
100 : Title::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
101
102 $user = $this->getUser();
103
104 # Check rights
105 $permErrors = $this->oldTitle->getUserPermissionsErrors( 'move', $user );
106 if ( count( $permErrors ) ) {
107 // Auto-block user's IP if the account was "hard" blocked
108 DeferredUpdates::addCallableUpdate( function() use ( $user ) {
109 $user->spreadAnyEditBlock();
110 } );
111 throw new PermissionsError( 'move', $permErrors );
112 }
113
114 $def = !$request->wasPosted();
115
116 $this->reason = $request->getText( 'wpReason' );
117 $this->moveTalk = $request->getBool( 'wpMovetalk', $def );
118 $this->fixRedirects = $request->getBool( 'wpFixRedirects', $def );
119 $this->leaveRedirect = $request->getBool( 'wpLeaveRedirect', $def );
120 $this->moveSubpages = $request->getBool( 'wpMovesubpages' );
121 $this->deleteAndMove = $request->getBool( 'wpDeleteAndMove' );
122 $this->moveOverShared = $request->getBool( 'wpMoveOverSharedFile' );
123 $this->watch = $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
124
125 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
126 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
127 ) {
128 $this->doSubmit();
129 } else {
130 $this->showForm( [] );
131 }
132 }
133
134 /**
135 * Show the form
136 *
137 * @param array $err Error messages. Each item is an error message.
138 * It may either be a string message name or array message name and
139 * parameters, like the second argument to OutputPage::wrapWikiMsg().
140 */
141 function showForm( $err ) {
142 $this->getSkin()->setRelevantTitle( $this->oldTitle );
143
144 $out = $this->getOutput();
145 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle->getPrefixedText() ) );
146 $out->addModules( 'mediawiki.special.movePage' );
147 $out->addModuleStyles( 'mediawiki.special.movePage.styles' );
148 $this->addHelpLink( 'Help:Moving a page' );
149
150 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
151 'movepagetext' :
152 'movepagetext-noredirectfixer'
153 );
154
155 if ( $this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) {
156 $out->wrapWikiMsg(
157 "<div class=\"warningbox mw-moveuserpage-warning\">\n$1\n</div>",
158 'moveuserpage-warning'
159 );
160 } elseif ( $this->oldTitle->getNamespace() == NS_CATEGORY ) {
161 $out->wrapWikiMsg(
162 "<div class=\"warningbox mw-movecategorypage-warning\">\n$1\n</div>",
163 'movecategorypage-warning'
164 );
165 }
166
167 $deleteAndMove = false;
168 $moveOverShared = false;
169
170 $newTitle = $this->newTitle;
171
172 if ( !$newTitle ) {
173 # Show the current title as a default
174 # when the form is first opened.
175 $newTitle = $this->oldTitle;
176 } elseif ( !count( $err ) ) {
177 # If a title was supplied, probably from the move log revert
178 # link, check for validity. We can then show some diagnostic
179 # information and save a click.
180 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
181 if ( is_array( $newerr ) ) {
182 $err = $newerr;
183 }
184 }
185
186 $user = $this->getUser();
187
188 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
189 && $newTitle->quickUserCan( 'delete', $user )
190 ) {
191 $out->wrapWikiMsg(
192 "<div class='warningbox'>\n$1\n</div>\n",
193 [ 'delete_and_move_text', $newTitle->getPrefixedText() ]
194 );
195 $deleteAndMove = true;
196 $err = [];
197 }
198
199 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
200 && $user->isAllowed( 'reupload-shared' )
201 ) {
202 $out->wrapWikiMsg(
203 "<div class='warningbox'>\n$1\n</div>\n",
204 [
205 'move-over-sharedrepo',
206 $newTitle->getPrefixedText()
207 ]
208 );
209 $moveOverShared = true;
210 $err = [];
211 }
212
213 $oldTalk = $this->oldTitle->getTalkPage();
214 $oldTitleSubpages = $this->oldTitle->hasSubpages();
215 $oldTitleTalkSubpages = $this->oldTitle->getTalkPage()->hasSubpages();
216
217 $canMoveSubpage = ( $oldTitleSubpages || $oldTitleTalkSubpages ) &&
218 !count( $this->oldTitle->getUserPermissionsErrors( 'move-subpages', $user ) );
219
220 # We also want to be able to move assoc. subpage talk-pages even if base page
221 # has no associated talk page, so || with $oldTitleTalkSubpages.
222 $considerTalk = !$this->oldTitle->isTalkPage() &&
223 ( $oldTalk->exists()
224 || ( $oldTitleTalkSubpages && $canMoveSubpage ) );
225
226 $dbr = wfGetDB( DB_SLAVE );
227 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
228 $hasRedirects = $dbr->selectField( 'redirect', '1',
229 [
230 'rd_namespace' => $this->oldTitle->getNamespace(),
231 'rd_title' => $this->oldTitle->getDBkey(),
232 ], __METHOD__ );
233 } else {
234 $hasRedirects = false;
235 }
236
237 if ( count( $err ) ) {
238 $out->addHTML( "<div class='errorbox'>\n" );
239 $action_desc = $this->msg( 'action-move' )->plain();
240 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
241
242 if ( count( $err ) == 1 ) {
243 $errMsg = $err[0];
244 $errMsgName = array_shift( $errMsg );
245
246 if ( $errMsgName == 'hookaborted' ) {
247 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
248 } else {
249 $out->addWikiMsgArray( $errMsgName, $errMsg );
250 }
251 } else {
252 $errStr = [];
253
254 foreach ( $err as $errMsg ) {
255 if ( $errMsg[0] == 'hookaborted' ) {
256 $errStr[] = $errMsg[1];
257 } else {
258 $errMsgName = array_shift( $errMsg );
259 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
260 }
261 }
262
263 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
264 }
265 $out->addHTML( "</div>\n" );
266 }
267
268 if ( $this->oldTitle->isProtected( 'move' ) ) {
269 # Is the title semi-protected?
270 if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
271 $noticeMsg = 'semiprotectedpagemovewarning';
272 $classes[] = 'mw-textarea-sprotected';
273 } else {
274 # Then it must be protected based on static groups (regular)
275 $noticeMsg = 'protectedpagemovewarning';
276 $classes[] = 'mw-textarea-protected';
277 }
278 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
279 $out->addWikiMsg( $noticeMsg );
280 LogEventsList::showLogExtract(
281 $out,
282 'protect',
283 $this->oldTitle,
284 '',
285 [ 'lim' => 1 ]
286 );
287 $out->addHTML( "</div>\n" );
288 }
289
290 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
291 // is enforced in the mediawiki.special.movePage module
292
293 $immovableNamespaces = [];
294 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
295 if ( !MWNamespace::isMovable( $nsId ) ) {
296 $immovableNamespaces[] = $nsId;
297 }
298 }
299
300 $handler = ContentHandler::getForTitle( $this->oldTitle );
301
302 $out->enableOOUI();
303 $fields = [];
304
305 $fields[] = new OOUI\FieldLayout(
306 new MediaWiki\Widget\ComplexTitleInputWidget( [
307 'id' => 'wpNewTitle',
308 'namespace' => [
309 'id' => 'wpNewTitleNs',
310 'name' => 'wpNewTitleNs',
311 'value' => $newTitle->getNamespace(),
312 'exclude' => $immovableNamespaces,
313 ],
314 'title' => [
315 'id' => 'wpNewTitleMain',
316 'name' => 'wpNewTitleMain',
317 'value' => $newTitle->getText(),
318 // Inappropriate, since we're expecting the user to input a non-existent page's title
319 'suggestions' => false,
320 ],
321 'infusable' => true,
322 ] ),
323 [
324 'label' => $this->msg( 'newtitle' )->text(),
325 'align' => 'top',
326 ]
327 );
328
329 $fields[] = new OOUI\FieldLayout(
330 new OOUI\TextInputWidget( [
331 'name' => 'wpReason',
332 'id' => 'wpReason',
333 'maxLength' => 200,
334 'infusable' => true,
335 'value' => $this->reason,
336 ] ),
337 [
338 'label' => $this->msg( 'movereason' )->text(),
339 'align' => 'top',
340 ]
341 );
342
343 if ( $considerTalk ) {
344 $fields[] = new OOUI\FieldLayout(
345 new OOUI\CheckboxInputWidget( [
346 'name' => 'wpMovetalk',
347 'id' => 'wpMovetalk',
348 'value' => '1',
349 'selected' => $this->moveTalk,
350 ] ),
351 [
352 'label' => $this->msg( 'movetalk' )->text(),
353 'help' => new OOUI\HtmlSnippet( $this->msg( 'movepagetalktext' )->parseAsBlock() ),
354 'align' => 'inline',
355 'infusable' => true,
356 ]
357 );
358 }
359
360 if ( $user->isAllowed( 'suppressredirect' ) ) {
361 if ( $handler->supportsRedirects() ) {
362 $isChecked = $this->leaveRedirect;
363 $isDisabled = false;
364 } else {
365 $isChecked = false;
366 $isDisabled = true;
367 }
368 $fields[] = new OOUI\FieldLayout(
369 new OOUI\CheckboxInputWidget( [
370 'name' => 'wpLeaveRedirect',
371 'id' => 'wpLeaveRedirect',
372 'value' => '1',
373 'selected' => $isChecked,
374 'disabled' => $isDisabled,
375 ] ),
376 [
377 'label' => $this->msg( 'move-leave-redirect' )->text(),
378 'align' => 'inline',
379 ]
380 );
381 }
382
383 if ( $hasRedirects ) {
384 $fields[] = new OOUI\FieldLayout(
385 new OOUI\CheckboxInputWidget( [
386 'name' => 'wpFixRedirects',
387 'id' => 'wpFixRedirects',
388 'value' => '1',
389 'selected' => $this->fixRedirects,
390 ] ),
391 [
392 'label' => $this->msg( 'fix-double-redirects' )->text(),
393 'align' => 'inline',
394 ]
395 );
396 }
397
398 if ( $canMoveSubpage ) {
399 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
400 $fields[] = new OOUI\FieldLayout(
401 new OOUI\CheckboxInputWidget( [
402 'name' => 'wpMovesubpages',
403 'id' => 'wpMovesubpages',
404 'value' => '1',
405 # Don't check the box if we only have talk subpages to
406 # move and we aren't moving the talk page.
407 'selected' => $this->moveSubpages && ( $this->oldTitle->hasSubpages() || $this->moveTalk ),
408 ] ),
409 [
410 'label' => new OOUI\HtmlSnippet(
411 $this->msg(
412 ( $this->oldTitle->hasSubpages()
413 ? 'move-subpages'
414 : 'move-talk-subpages' )
415 )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
416 ),
417 'align' => 'inline',
418 ]
419 );
420 }
421
422 # Don't allow watching if user is not logged in
423 if ( $user->isLoggedIn() ) {
424 $watchChecked = $user->isLoggedIn() && ( $this->watch || $user->getBoolOption( 'watchmoves' )
425 || $user->isWatched( $this->oldTitle ) );
426 $fields[] = new OOUI\FieldLayout(
427 new OOUI\CheckboxInputWidget( [
428 'name' => 'wpWatch',
429 'id' => 'watch', # ew
430 'value' => '1',
431 'selected' => $watchChecked,
432 ] ),
433 [
434 'label' => $this->msg( 'move-watch' )->text(),
435 'align' => 'inline',
436 ]
437 );
438 }
439
440 $hiddenFields = '';
441 if ( $moveOverShared ) {
442 $hiddenFields .= Html::hidden( 'wpMoveOverSharedFile', '1' );
443 }
444
445 if ( $deleteAndMove ) {
446 $fields[] = new OOUI\FieldLayout(
447 new OOUI\CheckboxInputWidget( [
448 'name' => 'wpDeleteAndMove',
449 'id' => 'wpDeleteAndMove',
450 'value' => '1',
451 ] ),
452 [
453 'label' => $this->msg( 'delete_and_move_confirm' )->text(),
454 'align' => 'inline',
455 ]
456 );
457 }
458
459 $fields[] = new OOUI\FieldLayout(
460 new OOUI\ButtonInputWidget( [
461 'name' => 'wpMove',
462 'value' => $this->msg( 'movepagebtn' )->text(),
463 'label' => $this->msg( 'movepagebtn' )->text(),
464 'flags' => [ 'constructive', 'primary' ],
465 'type' => 'submit',
466 ] ),
467 [
468 'align' => 'top',
469 ]
470 );
471
472 $fieldset = new OOUI\FieldsetLayout( [
473 'label' => $this->msg( 'move-page-legend' )->text(),
474 'id' => 'mw-movepage-table',
475 'items' => $fields,
476 ] );
477
478 $form = new OOUI\FormLayout( [
479 'method' => 'post',
480 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
481 'id' => 'movepage',
482 ] );
483 $form->appendContent(
484 $fieldset,
485 new OOUI\HtmlSnippet(
486 $hiddenFields .
487 Html::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
488 Html::hidden( 'wpEditToken', $user->getEditToken() )
489 )
490 );
491
492 $out->addHTML(
493 new OOUI\PanelLayout( [
494 'classes' => [ 'movepage-wrapper' ],
495 'expanded' => false,
496 'padded' => true,
497 'framed' => true,
498 'content' => $form,
499 ] )
500 );
501
502 $this->showLogFragment( $this->oldTitle );
503 $this->showSubpages( $this->oldTitle );
504 }
505
506 function doSubmit() {
507 $user = $this->getUser();
508
509 if ( $user->pingLimiter( 'move' ) ) {
510 throw new ThrottledError;
511 }
512
513 $ot = $this->oldTitle;
514 $nt = $this->newTitle;
515
516 # don't allow moving to pages with # in
517 if ( !$nt || $nt->hasFragment() ) {
518 $this->showForm( [ [ 'badtitletext' ] ] );
519
520 return;
521 }
522
523 # Show a warning if the target file exists on a shared repo
524 if ( $nt->getNamespace() == NS_FILE
525 && !( $this->moveOverShared && $user->isAllowed( 'reupload-shared' ) )
526 && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt )
527 && wfFindFile( $nt )
528 ) {
529 $this->showForm( [ [ 'file-exists-sharedrepo' ] ] );
530
531 return;
532 }
533
534 # Delete to make way if requested
535 if ( $this->deleteAndMove ) {
536 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
537 if ( count( $permErrors ) ) {
538 # Only show the first error
539 $this->showForm( $permErrors );
540
541 return;
542 }
543
544 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
545
546 // Delete an associated image if there is
547 if ( $nt->getNamespace() == NS_FILE ) {
548 $file = wfLocalFile( $nt );
549 $file->load( File::READ_LATEST );
550 if ( $file->exists() ) {
551 $file->delete( $reason, false, $user );
552 }
553 }
554
555 $error = ''; // passed by ref
556 $page = WikiPage::factory( $nt );
557 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
558 if ( !$deleteStatus->isGood() ) {
559 $this->showForm( $deleteStatus->getErrorsArray() );
560
561 return;
562 }
563 }
564
565 $handler = ContentHandler::getForTitle( $ot );
566
567 if ( !$handler->supportsRedirects() ) {
568 $createRedirect = false;
569 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
570 $createRedirect = $this->leaveRedirect;
571 } else {
572 $createRedirect = true;
573 }
574
575 # Do the actual move.
576 $mp = new MovePage( $ot, $nt );
577 $valid = $mp->isValidMove();
578 if ( !$valid->isOK() ) {
579 $this->showForm( $valid->getErrorsArray() );
580 return;
581 }
582
583 $permStatus = $mp->checkPermissions( $user, $this->reason );
584 if ( !$permStatus->isOK() ) {
585 $this->showForm( $permStatus->getErrorsArray() );
586 return;
587 }
588
589 $status = $mp->move( $user, $this->reason, $createRedirect );
590 if ( !$status->isOK() ) {
591 $this->showForm( $status->getErrorsArray() );
592 return;
593 }
594
595 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects ) {
596 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
597 }
598
599 $out = $this->getOutput();
600 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
601
602 $linkRenderer = $this->getLinkRenderer();
603 $oldLink = $linkRenderer->makeLink(
604 $ot,
605 null,
606 [ 'id' => 'movepage-oldlink' ],
607 [ 'redirect' => 'no' ]
608 );
609 $newLink = $linkRenderer->makeKnownLink(
610 $nt,
611 null,
612 [ 'id' => 'movepage-newlink' ]
613 );
614 $oldText = $ot->getPrefixedText();
615 $newText = $nt->getPrefixedText();
616
617 if ( $ot->exists() ) {
618 // NOTE: we assume that if the old title exists, it's because it was re-created as
619 // a redirect to the new title. This is not safe, but what we did before was
620 // even worse: we just determined whether a redirect should have been created,
621 // and reported that it was created if it should have, without any checks.
622 // Also note that isRedirect() is unreliable because of bug 37209.
623 $msgName = 'movepage-moved-redirect';
624 } else {
625 $msgName = 'movepage-moved-noredirect';
626 }
627
628 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
629 $newLink )->params( $oldText, $newText )->parseAsBlock() );
630 $out->addWikiMsg( $msgName );
631
632 Hooks::run( 'SpecialMovepageAfterMove', [ &$this, &$ot, &$nt ] );
633
634 # Now we move extra pages we've been asked to move: subpages and talk
635 # pages. First, if the old page or the new page is a talk page, we
636 # can't move any talk pages: cancel that.
637 if ( $ot->isTalkPage() || $nt->isTalkPage() ) {
638 $this->moveTalk = false;
639 }
640
641 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
642 $this->moveSubpages = false;
643 }
644
645 /**
646 * Next make a list of id's. This might be marginally less efficient
647 * than a more direct method, but this is not a highly performance-cri-
648 * tical code path and readable code is more important here.
649 *
650 * If the target namespace doesn't allow subpages, moving with subpages
651 * would mean that you couldn't move them back in one operation, which
652 * is bad.
653 * @todo FIXME: A specific error message should be given in this case.
654 */
655
656 // @todo FIXME: Use Title::moveSubpages() here
657 $dbr = wfGetDB( DB_MASTER );
658 if ( $this->moveSubpages && (
659 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
660 $this->moveTalk
661 && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
662 )
663 ) ) {
664 $conds = [
665 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
666 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
667 ];
668 $conds['page_namespace'] = [];
669 if ( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
670 $conds['page_namespace'][] = $ot->getNamespace();
671 }
672 if ( $this->moveTalk &&
673 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
674 ) {
675 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
676 }
677 } elseif ( $this->moveTalk ) {
678 $conds = [
679 'page_namespace' => $ot->getTalkPage()->getNamespace(),
680 'page_title' => $ot->getDBkey()
681 ];
682 } else {
683 # Skip the query
684 $conds = null;
685 }
686
687 $extraPages = [];
688 if ( !is_null( $conds ) ) {
689 $extraPages = TitleArray::newFromResult(
690 $dbr->select( 'page',
691 [ 'page_id', 'page_namespace', 'page_title' ],
692 $conds,
693 __METHOD__
694 )
695 );
696 }
697
698 $extraOutput = [];
699 $count = 1;
700 foreach ( $extraPages as $oldSubpage ) {
701 if ( $ot->equals( $oldSubpage ) || $nt->equals( $oldSubpage ) ) {
702 # Already did this one.
703 continue;
704 }
705
706 $newPageName = preg_replace(
707 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
708 StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
709 $oldSubpage->getDBkey()
710 );
711
712 if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) {
713 // Moving a subpage from a subject namespace to a talk namespace or vice-versa
714 $newNs = $nt->getNamespace();
715 } elseif ( $oldSubpage->isTalkPage() ) {
716 $newNs = $nt->getTalkPage()->getNamespace();
717 } else {
718 $newNs = $nt->getSubjectPage()->getNamespace();
719 }
720
721 # Bug 14385: we need makeTitleSafe because the new page names may
722 # be longer than 255 characters.
723 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
724 if ( !$newSubpage ) {
725 $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
726 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
727 ->params( Title::makeName( $newNs, $newPageName ) )->escaped();
728 continue;
729 }
730
731 # This was copy-pasted from Renameuser, bleh.
732 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
733 $link = $linkRenderer->makeKnownLink( $newSubpage );
734 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
735 } else {
736 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
737
738 if ( $success === true ) {
739 if ( $this->fixRedirects ) {
740 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
741 }
742 $oldLink = $linkRenderer->makeLink(
743 $oldSubpage,
744 null,
745 [],
746 [ 'redirect' => 'no' ]
747 );
748
749 $newLink = $linkRenderer->makeKnownLink( $newSubpage );
750 $extraOutput[] = $this->msg( 'movepage-page-moved' )
751 ->rawParams( $oldLink, $newLink )->escaped();
752 ++$count;
753
754 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
755 if ( $count >= $maximumMovedPages ) {
756 $extraOutput[] = $this->msg( 'movepage-max-pages' )
757 ->numParams( $maximumMovedPages )->escaped();
758 break;
759 }
760 } else {
761 $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
762 $newLink = $linkRenderer->makeLink( $newSubpage );
763 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
764 ->rawParams( $oldLink, $newLink )->escaped();
765 }
766 }
767 }
768
769 if ( $extraOutput !== [] ) {
770 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
771 }
772
773 # Deal with watches (we don't watch subpages)
774 WatchAction::doWatchOrUnwatch( $this->watch, $ot, $user );
775 WatchAction::doWatchOrUnwatch( $this->watch, $nt, $user );
776 }
777
778 function showLogFragment( $title ) {
779 $moveLogPage = new LogPage( 'move' );
780 $out = $this->getOutput();
781 $out->addHTML( Xml::element( 'h2', null, $moveLogPage->getName()->text() ) );
782 LogEventsList::showLogExtract( $out, 'move', $title );
783 }
784
785 function showSubpages( $title ) {
786 if ( !MWNamespace::hasSubpages( $title->getNamespace() ) ) {
787 return;
788 }
789
790 $subpages = $title->getSubpages();
791 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
792
793 $out = $this->getOutput();
794 $out->wrapWikiMsg( '== $1 ==', [ 'movesubpage', $count ] );
795
796 # No subpages.
797 if ( $count == 0 ) {
798 $out->addWikiMsg( 'movenosubpage' );
799
800 return;
801 }
802
803 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
804 $out->addHTML( "<ul>\n" );
805
806 $linkRenderer = $this->getLinkRenderer();
807 foreach ( $subpages as $subpage ) {
808 $link = $linkRenderer->makeLink( $subpage );
809 $out->addHTML( "<li>$link</li>\n" );
810 }
811 $out->addHTML( "</ul>\n" );
812 }
813
814 /**
815 * Return an array of subpages beginning with $search that this special page will accept.
816 *
817 * @param string $search Prefix to search for
818 * @param int $limit Maximum number of results to return (usually 10)
819 * @param int $offset Number of results to skip (usually 0)
820 * @return string[] Matching subpages
821 */
822 public function prefixSearchSubpages( $search, $limit, $offset ) {
823 return $this->prefixSearchString( $search, $limit, $offset );
824 }
825
826 protected function getGroupName() {
827 return 'pagetools';
828 }
829 }