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