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