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