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