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