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