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