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