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