fbefb3b7d53b07149d3b3ae8389939404969b26c
[lhc/web/wiklou.git] / includes / specials / SpecialMovepage.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * Constructor
9 */
10 function wfSpecialMovepage( $par = null ) {
11 global $wgUser, $wgOut, $wgRequest, $action;
12
13 # Check for database lock
14 if ( wfReadOnly() ) {
15 $wgOut->readOnlyPage();
16 return;
17 }
18
19 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
20
21 // Yes, the use of getVal() and getText() is wanted, see bug 20365
22 $oldTitleText = $wgRequest->getVal( 'wpOldTitle', $target );
23 $newTitleText = $wgRequest->getText( 'wpNewTitle' );
24
25 $oldTitle = Title::newFromText( $oldTitleText );
26 $newTitle = Title::newFromText( $newTitleText );
27
28 if( is_null( $oldTitle ) ) {
29 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
30 return;
31 }
32 if( !$oldTitle->exists() ) {
33 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
34 return;
35 }
36
37 # Check rights
38 $permErrors = $oldTitle->getUserPermissionsErrors( 'move', $wgUser );
39 if( !empty( $permErrors ) ) {
40 $wgOut->showPermissionsErrorPage( $permErrors );
41 return;
42 }
43
44 $form = new MovePageForm( $oldTitle, $newTitle );
45
46 if ( 'submit' == $action && $wgRequest->wasPosted()
47 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
48 $form->doSubmit();
49 } else {
50 $form->showForm( '' );
51 }
52 }
53
54 /**
55 * HTML form for Special:Movepage
56 * @ingroup SpecialPage
57 */
58 class MovePageForm {
59 var $oldTitle, $newTitle; # Objects
60 var $reason; # Text input
61 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared; # Checks
62
63 private $watch = false;
64
65 function __construct( $oldTitle, $newTitle ) {
66 global $wgRequest, $wgUser;
67 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
68 $this->oldTitle = $oldTitle;
69 $this->newTitle = $newTitle;
70 $this->reason = $wgRequest->getText( 'wpReason' );
71 if ( $wgRequest->wasPosted() ) {
72 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
73 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', false );
74 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', false );
75 } else {
76 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
77 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', true );
78 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', true );
79 }
80 $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
81 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
82 $this->moveOverShared = $wgRequest->getBool( 'wpMoveOverSharedFile', false );
83 $this->watch = $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn();
84 }
85
86 /**
87 * Show the form
88 * @param mixed $err Error message. May either be a string message name or
89 * array message name and parameters, like the second argument to
90 * OutputPage::wrapWikiMsg().
91 */
92 function showForm( $err ) {
93 global $wgOut, $wgUser, $wgContLang, $wgFixDoubleRedirects;
94
95 $skin = $wgUser->getSkin();
96
97 $oldTitleLink = $skin->link( $this->oldTitle );
98
99 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) );
100 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
101
102 $newTitle = $this->newTitle;
103
104 if( !$newTitle ) {
105 # Show the current title as a default
106 # when the form is first opened.
107 $newTitle = $this->oldTitle;
108 }
109 else {
110 if( empty($err) ) {
111 # If a title was supplied, probably from the move log revert
112 # link, check for validity. We can then show some diagnostic
113 # information and save a click.
114 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
115 if( $newerr ) {
116 $err = $newerr[0];
117 }
118 }
119 }
120
121 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
122 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
123 $movepagebtn = wfMsg( 'delete_and_move' );
124 $submitVar = 'wpDeleteAndMove';
125 $confirm = "
126 <tr>
127 <td></td>
128 <td class='mw-input'>" .
129 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
130 "</td>
131 </tr>";
132 $err = '';
133 } else {
134 $wgOut->addWikiMsg( 'movepagetext' );
135 $movepagebtn = wfMsg( 'movepagebtn' );
136 $submitVar = 'wpMove';
137 $confirm = false;
138 }
139
140 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) {
141 $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
142 $submitVar = 'wpMoveOverSharedFile';
143 $err = '';
144 }
145
146 $oldTalk = $this->oldTitle->getTalkPage();
147 $considerTalk = ( !$this->oldTitle->isTalkPage() && $oldTalk->exists() );
148
149 $dbr = wfGetDB( DB_SLAVE );
150 if ( $wgFixDoubleRedirects ) {
151 $hasRedirects = $dbr->selectField( 'redirect', '1',
152 array(
153 'rd_namespace' => $this->oldTitle->getNamespace(),
154 'rd_title' => $this->oldTitle->getDBkey(),
155 ) , __METHOD__ );
156 } else {
157 $hasRedirects = false;
158 }
159
160 if ( $considerTalk ) {
161 $wgOut->addWikiMsg( 'movepagetalktext' );
162 }
163
164 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
165 $token = htmlspecialchars( $wgUser->editToken() );
166
167 if ( !empty($err) ) {
168 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
169 if( $err[0] == 'hookaborted' ) {
170 $hookErr = $err[1];
171 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
172 $wgOut->addHTML( $errMsg );
173 } else {
174 $wgOut->wrapWikiMsg( '<p><strong class="error">$1</strong></p>', $err );
175 }
176 }
177
178 if ( $this->oldTitle->isProtected( 'move' ) ) {
179 # Is the title semi-protected?
180 if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
181 $noticeMsg = 'semiprotectedpagemovewarning';
182 $classes[] = 'mw-textarea-sprotected';
183 } else {
184 # Then it must be protected based on static groups (regular)
185 $noticeMsg = 'protectedpagemovewarning';
186 $classes[] = 'mw-textarea-protected';
187 }
188 $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
189 $wgOut->addWikiMsg( $noticeMsg );
190 LogEventsList::showLogExtract( $wgOut, 'protect', $this->oldTitle->getPrefixedText(), '', array( 'lim' => 1 ) );
191 $wgOut->addHTML( "</div>\n" );
192 }
193
194 $wgOut->addHTML(
195 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
196 Xml::openElement( 'fieldset' ) .
197 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
198 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
199 "<tr>
200 <td class='mw-label'>" .
201 wfMsgHtml( 'movearticle' ) .
202 "</td>
203 <td class='mw-input'>
204 <strong>{$oldTitleLink}</strong>
205 </td>
206 </tr>
207 <tr>
208 <td class='mw-label'>" .
209 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
210 "</td>
211 <td class='mw-input'>" .
212 Xml::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
213 Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
214 "</td>
215 </tr>
216 <tr>
217 <td class='mw-label'>" .
218 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
219 "</td>
220 <td class='mw-input'>" .
221 Html::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
222 'maxlength' => 200 ), $this->reason ) .
223 "</td>
224 </tr>"
225 );
226
227 if( $considerTalk ) {
228 $wgOut->addHTML( "
229 <tr>
230 <td></td>
231 <td class='mw-input'>" .
232 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
233 "</td>
234 </tr>"
235 );
236 }
237
238 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
239 $wgOut->addHTML( "
240 <tr>
241 <td></td>
242 <td class='mw-input' >" .
243 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
244 'wpLeaveRedirect', $this->leaveRedirect ) .
245 "</td>
246 </tr>"
247 );
248 }
249
250 if ( $hasRedirects ) {
251 $wgOut->addHTML( "
252 <tr>
253 <td></td>
254 <td class='mw-input' >" .
255 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
256 'wpFixRedirects', $this->fixRedirects ) .
257 "</td>
258 </tr>"
259 );
260 }
261
262 if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
263 && $this->oldTitle->userCan( 'move-subpages' ) )
264 {
265 global $wgMaximumMovedPages, $wgLang;
266
267 $wgOut->addHTML( "
268 <tr>
269 <td></td>
270 <td class=\"mw-input\">" .
271 Xml::check(
272 'wpMovesubpages',
273 # Don't check the box if we only have talk subpages to
274 # move and we aren't moving the talk page.
275 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk),
276 array( 'id' => 'wpMovesubpages' )
277 ) . '&nbsp;' .
278 Xml::tags( 'label', array( 'for' => 'wpMovesubpages' ),
279 wfMsgExt(
280 ( $this->oldTitle->hasSubpages()
281 ? 'move-subpages'
282 : 'move-talk-subpages' ),
283 array( 'parseinline' ),
284 $wgLang->formatNum( $wgMaximumMovedPages ),
285 # $2 to allow use of PLURAL in message.
286 $wgMaximumMovedPages
287 )
288 ) .
289 "</td>
290 </tr>"
291 );
292 }
293
294 $watchChecked = $wgUser->isLoggedIn() && ($this->watch || $wgUser->getBoolOption( 'watchmoves' )
295 || $this->oldTitle->userIsWatching());
296 # Don't allow watching if user is not logged in
297 if( $wgUser->isLoggedIn() ) {
298 $wgOut->addHTML( "
299 <tr>
300 <td></td>
301 <td class='mw-input'>" .
302 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
303 "</td>
304 </tr>");
305 }
306
307 $wgOut->addHTML( "
308 {$confirm}
309 <tr>
310 <td>&nbsp;</td>
311 <td class='mw-submit'>" .
312 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
313 "</td>
314 </tr>" .
315 Xml::closeElement( 'table' ) .
316 Xml::hidden( 'wpEditToken', $token ) .
317 Xml::closeElement( 'fieldset' ) .
318 Xml::closeElement( 'form' ) .
319 "\n"
320 );
321
322 $this->showLogFragment( $this->oldTitle, $wgOut );
323 $this->showSubpages( $this->oldTitle, $wgOut );
324
325 }
326
327 function doSubmit() {
328 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
329 global $wgFixDoubleRedirects;
330
331 if ( $wgUser->pingLimiter( 'move' ) ) {
332 $wgOut->rateLimited();
333 return;
334 }
335
336 $ot = $this->oldTitle;
337 $nt = $this->newTitle;
338
339 # Delete to make way if requested
340 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
341 $article = new Article( $nt );
342
343 # Disallow deletions of big articles
344 $bigHistory = $article->isBigDeletion();
345 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
346 global $wgLang, $wgDeleteRevisionsLimit;
347 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
348 return;
349 }
350
351 // Delete an associated image if there is
352 $file = wfLocalFile( $nt );
353 if( $file->exists() ) {
354 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
355 }
356
357 // This may output an error message and exit
358 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
359 }
360
361 # don't allow moving to pages with # in
362 if ( !$nt || $nt->getFragment() != '' ) {
363 $this->showForm( 'badtitletext' );
364 return;
365 }
366
367 # Show a warning if the target file exists on a shared repo
368 if ( $nt->getNamespace() == NS_FILE
369 && !( $this->moveOverShared && $wgUser->isAllowed( 'reupload-shared' ) )
370 && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt )
371 && wfFindFile( $nt ) )
372 {
373 $this->showForm( array('file-exists-sharedrepo') );
374 return;
375
376 }
377
378 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
379 $createRedirect = $this->leaveRedirect;
380 } else {
381 $createRedirect = true;
382 }
383
384 # Do the actual move.
385 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
386 if ( $error !== true ) {
387 # FIXME: show all the errors in a list, not just the first one
388 $this->showForm( reset( $error ) );
389 return;
390 }
391
392 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
393 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
394 }
395
396 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
397
398 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
399
400 $oldUrl = $ot->getFullUrl( 'redirect=no' );
401 $newUrl = $nt->getFullUrl();
402 $oldText = $ot->getPrefixedText();
403 $newText = $nt->getPrefixedText();
404 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
405 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
406
407 $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect';
408 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
409 $wgOut->addWikiMsg( $msgName );
410
411 # Now we move extra pages we've been asked to move: subpages and talk
412 # pages. First, if the old page or the new page is a talk page, we
413 # can't move any talk pages: cancel that.
414 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
415 $this->moveTalk = false;
416 }
417
418 if( !$ot->userCan( 'move-subpages' ) ) {
419 $this->moveSubpages = false;
420 }
421
422 # Next make a list of id's. This might be marginally less efficient
423 # than a more direct method, but this is not a highly performance-cri-
424 # tical code path and readable code is more important here.
425 #
426 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
427 # 4 might get confused. If so, consider rewriting as a UNION.
428 #
429 # If the target namespace doesn't allow subpages, moving with subpages
430 # would mean that you couldn't move them back in one operation, which
431 # is bad. FIXME: A specific error message should be given in this
432 # case.
433
434 // FIXME: Use Title::moveSubpages() here
435 $dbr = wfGetDB( DB_MASTER );
436 if( $this->moveSubpages && (
437 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
438 $this->moveTalk &&
439 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
440 )
441 ) ) {
442 $conds = array(
443 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
444 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
445 );
446 $conds['page_namespace'] = array();
447 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
448 $conds['page_namespace'] []= $ot->getNamespace();
449 }
450 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
451 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
452 }
453 } elseif( $this->moveTalk ) {
454 $conds = array(
455 'page_namespace' => $ot->getTalkPage()->getNamespace(),
456 'page_title' => $ot->getDBkey()
457 );
458 } else {
459 # Skip the query
460 $conds = null;
461 }
462
463 $extraPages = array();
464 if( !is_null( $conds ) ) {
465 $extraPages = TitleArray::newFromResult(
466 $dbr->select( 'page',
467 array( 'page_id', 'page_namespace', 'page_title' ),
468 $conds,
469 __METHOD__
470 )
471 );
472 }
473
474 $extraOutput = array();
475 $skin = $wgUser->getSkin();
476 $count = 1;
477 foreach( $extraPages as $oldSubpage ) {
478 if( $ot->equals( $oldSubpage ) ) {
479 # Already did this one.
480 continue;
481 }
482
483 $newPageName = preg_replace(
484 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
485 str_replace( '\\', '\\\\', $nt->getDBkey() ), # bug 21234
486 $oldSubpage->getDBkey()
487 );
488 if( $oldSubpage->isTalkPage() ) {
489 $newNs = $nt->getTalkPage()->getNamespace();
490 } else {
491 $newNs = $nt->getSubjectPage()->getNamespace();
492 }
493 # Bug 14385: we need makeTitleSafe because the new page names may
494 # be longer than 255 characters.
495 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
496 if( !$newSubpage ) {
497 $oldLink = $skin->linkKnown( $oldSubpage );
498 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
499 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
500 continue;
501 }
502
503 # This was copy-pasted from Renameuser, bleh.
504 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
505 $link = $skin->linkKnown( $newSubpage );
506 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
507 } else {
508 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
509 if( $success === true ) {
510 if ( $this->fixRedirects ) {
511 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
512 }
513 $oldLink = $skin->linkKnown(
514 $oldSubpage,
515 null,
516 array(),
517 array( 'redirect' => 'no' )
518 );
519 $newLink = $skin->linkKnown( $newSubpage );
520 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
521 ++$count;
522 if( $count >= $wgMaximumMovedPages ) {
523 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
524 break;
525 }
526 } else {
527 $oldLink = $skin->linkKnown( $oldSubpage );
528 $newLink = $skin->link( $newSubpage );
529 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
530 }
531 }
532
533 }
534
535 if( $extraOutput !== array() ) {
536 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
537 }
538
539 # Deal with watches (we don't watch subpages)
540 if( $this->watch && $wgUser->isLoggedIn() ) {
541 $wgUser->addWatch( $ot );
542 $wgUser->addWatch( $nt );
543 } else {
544 $wgUser->removeWatch( $ot );
545 $wgUser->removeWatch( $nt );
546 }
547
548 # Re-clear the file redirect cache, which may have been polluted by
549 # parsing in messages above. See CR r56745.
550 # FIXME: needs a more robust solution inside FileRepo.
551 if( $ot->getNamespace() == NS_FILE ) {
552 RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
553 }
554 }
555
556 function showLogFragment( $title, &$out ) {
557 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'move' ) ) );
558 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
559 }
560
561 function showSubpages( $title, $out ) {
562 global $wgUser, $wgLang;
563
564 if( !MWNamespace::hasSubpages( $title->getNamespace() ) )
565 return;
566
567 $subpages = $title->getSubpages();
568 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
569
570 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
571
572 # No subpages.
573 if ( $count == 0 ) {
574 $out->addWikiMsg( 'movenosubpage' );
575 return;
576 }
577
578 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
579 $skin = $wgUser->getSkin();
580 $out->addHTML( "<ul>\n" );
581
582 foreach( $subpages as $subpage ) {
583 $link = $skin->link( $subpage );
584 $out->addHTML( "<li>$link</li>\n" );
585 }
586 $out->addHTML( "</ul>\n" );
587 }
588 }
589