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