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