(bug 16762) Special:Movepage now shows a list of subpages when possible
[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->makeLinkObj( $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 $wgOut->addHTML(
170 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
171 Xml::openElement( 'fieldset' ) .
172 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
173 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
174 "<tr>
175 <td class='mw-label'>" .
176 wfMsgHtml( 'movearticle' ) .
177 "</td>
178 <td class='mw-input'>
179 <strong>{$oldTitleLink}</strong>
180 </td>
181 </tr>
182 <tr>
183 <td class='mw-label'>" .
184 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
185 "</td>
186 <td class='mw-input'>" .
187 Xml::input( 'wpNewTitle', 40, $newTitle->getPrefixedText(), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
188 Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
189 "</td>
190 </tr>
191 <tr>
192 <td class='mw-label'>" .
193 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
194 "</td>
195 <td class='mw-input'>" .
196 Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
197 "</td>
198 </tr>"
199 );
200
201 if( $considerTalk ) {
202 $wgOut->addHTML( "
203 <tr>
204 <td></td>
205 <td class='mw-input'>" .
206 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
207 "</td>
208 </tr>"
209 );
210 }
211
212 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
213 $wgOut->addHTML( "
214 <tr>
215 <td></td>
216 <td class='mw-input' >" .
217 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
218 'wpLeaveRedirect', $this->leaveRedirect ) .
219 "</td>
220 </tr>"
221 );
222 }
223
224 if ( $hasRedirects ) {
225 $wgOut->addHTML( "
226 <tr>
227 <td></td>
228 <td class='mw-input' >" .
229 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
230 'wpFixRedirects', $this->fixRedirects ) .
231 "</td>
232 </tr>"
233 );
234 }
235
236 if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
237 && $this->oldTitle->userCan( 'move-subpages' ) ) {
238 global $wgMaximumMovedPages, $wgLang;
239
240 $wgOut->addHTML( "
241 <tr>
242 <td></td>
243 <td class=\"mw-input\">" .
244 Xml::checkLabel( wfMsgExt(
245 ( $this->oldTitle->hasSubpages()
246 ? 'move-subpages'
247 : 'move-talk-subpages' ),
248 array( 'parsemag' ),
249 $wgLang->formatNum( $wgMaximumMovedPages ),
250 # $2 to allow use of PLURAL in message.
251 $wgMaximumMovedPages
252 ),
253 'wpMovesubpages', 'wpMovesubpages',
254 # Don't check the box if we only have talk subpages to
255 # move and we aren't moving the talk page.
256 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk)
257 ) .
258 "</td>
259 </tr>"
260 );
261 }
262
263 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' )
264 || $this->oldTitle->userIsWatching();
265 $wgOut->addHTML( "
266 <tr>
267 <td></td>
268 <td class='mw-input'>" .
269 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
270 "</td>
271 </tr>
272 {$confirm}
273 <tr>
274 <td>&nbsp;</td>
275 <td class='mw-submit'>" .
276 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
277 "</td>
278 </tr>" .
279 Xml::closeElement( 'table' ) .
280 Xml::hidden( 'wpEditToken', $token ) .
281 Xml::closeElement( 'fieldset' ) .
282 Xml::closeElement( 'form' ) .
283 "\n"
284 );
285
286 $this->showLogFragment( $this->oldTitle, $wgOut );
287 $this->showSubpages( $this->oldTitle, $wgOut );
288
289 }
290
291 function doSubmit() {
292 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
293 global $wgFixDoubleRedirects;
294
295 if ( $wgUser->pingLimiter( 'move' ) ) {
296 $wgOut->rateLimited();
297 return;
298 }
299
300 $ot = $this->oldTitle;
301 $nt = $this->newTitle;
302
303 # Delete to make way if requested
304 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
305 $article = new Article( $nt );
306
307 # Disallow deletions of big articles
308 $bigHistory = $article->isBigDeletion();
309 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
310 global $wgLang, $wgDeleteRevisionsLimit;
311 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
312 return;
313 }
314
315 // Delete an associated image if there is
316 $file = wfLocalFile( $nt );
317 if( $file->exists() ) {
318 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
319 }
320
321 // This may output an error message and exit
322 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
323 }
324
325 # don't allow moving to pages with # in
326 if ( !$nt || $nt->getFragment() != '' ) {
327 $this->showForm( 'badtitletext' );
328 return;
329 }
330
331 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
332 $createRedirect = $this->leaveRedirect;
333 } else {
334 $createRedirect = true;
335 }
336
337 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
338 if ( $error !== true ) {
339 # FIXME: show all the errors in a list, not just the first one
340 $this->showForm( reset( $error ) );
341 return;
342 }
343
344 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
345 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
346 }
347
348 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
349
350 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
351
352 $oldUrl = $ot->getFullUrl( 'redirect=no' );
353 $newUrl = $nt->getFullUrl();
354 $oldText = $ot->getPrefixedText();
355 $newText = $nt->getPrefixedText();
356 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
357 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
358
359 $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect';
360 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
361 $wgOut->addWikiMsg( $msgName );
362
363 # Now we move extra pages we've been asked to move: subpages and talk
364 # pages. First, if the old page or the new page is a talk page, we
365 # can't move any talk pages: cancel that.
366 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
367 $this->moveTalk = false;
368 }
369
370 if( !$ot->userCan( 'move-subpages' ) ) {
371 $this->moveSubpages = false;
372 }
373
374 # Next make a list of id's. This might be marginally less efficient
375 # than a more direct method, but this is not a highly performance-cri-
376 # tical code path and readable code is more important here.
377 #
378 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
379 # 4 might get confused. If so, consider rewriting as a UNION.
380 #
381 # If the target namespace doesn't allow subpages, moving with subpages
382 # would mean that you couldn't move them back in one operation, which
383 # is bad. FIXME: A specific error message should be given in this
384 # case.
385
386 // FIXME: Use Title::moveSubpages() here
387 $dbr = wfGetDB( DB_MASTER );
388 if( $this->moveSubpages && (
389 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
390 $this->moveTalk &&
391 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
392 )
393 ) ) {
394 $conds = array(
395 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
396 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
397 );
398 $conds['page_namespace'] = array();
399 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
400 $conds['page_namespace'] []= $ot->getNamespace();
401 }
402 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
403 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
404 }
405 } elseif( $this->moveTalk ) {
406 $conds = array(
407 'page_namespace' => $ot->getTalkPage()->getNamespace(),
408 'page_title' => $ot->getDBKey()
409 );
410 } else {
411 # Skip the query
412 $conds = null;
413 }
414
415 $extraPages = array();
416 if( !is_null( $conds ) ) {
417 $extraPages = TitleArray::newFromResult(
418 $dbr->select( 'page',
419 array( 'page_id', 'page_namespace', 'page_title' ),
420 $conds,
421 __METHOD__
422 )
423 );
424 }
425
426 $extraOutput = array();
427 $skin = $wgUser->getSkin();
428 $count = 1;
429 foreach( $extraPages as $oldSubpage ) {
430 if( $oldSubpage->getArticleId() == $ot->getArticleId() ) {
431 # Already did this one.
432 continue;
433 }
434
435 $newPageName = preg_replace(
436 '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
437 $nt->getDBKey(),
438 $oldSubpage->getDBKey()
439 );
440 if( $oldSubpage->isTalkPage() ) {
441 $newNs = $nt->getTalkPage()->getNamespace();
442 } else {
443 $newNs = $nt->getSubjectPage()->getNamespace();
444 }
445 # Bug 14385: we need makeTitleSafe because the new page names may
446 # be longer than 255 characters.
447 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
448 if( !$newSubpage ) {
449 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
450 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
451 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
452 continue;
453 }
454
455 # This was copy-pasted from Renameuser, bleh.
456 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
457 $link = $skin->makeKnownLinkObj( $newSubpage );
458 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
459 } else {
460 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
461 if( $success === true ) {
462 if ( $this->fixRedirects ) {
463 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
464 }
465 $oldLink = $skin->makeKnownLinkObj( $oldSubpage, '', 'redirect=no' );
466 $newLink = $skin->makeKnownLinkObj( $newSubpage );
467 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
468 } else {
469 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
470 $newLink = $skin->makeLinkObj( $newSubpage );
471 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
472 }
473 }
474
475 ++$count;
476 if( $count >= $wgMaximumMovedPages ) {
477 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
478 break;
479 }
480 }
481
482 if( $extraOutput !== array() ) {
483 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
484 }
485
486 # Deal with watches (we don't watch subpages)
487 if( $this->watch ) {
488 $wgUser->addWatch( $ot );
489 $wgUser->addWatch( $nt );
490 } else {
491 $wgUser->removeWatch( $ot );
492 $wgUser->removeWatch( $nt );
493 }
494 }
495
496 function showLogFragment( $title, &$out ) {
497 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
498 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
499 }
500
501 function showSubpages( $title, $out ) {
502 global $wgUser;
503
504 if( !MWNamespace::hasSubpages( $title->getNamespace() ) )
505 return;
506
507 $out->wrapWikiMsg( '== $1 ==', 'movesubpage' );
508 $subpages = $title->getSubpages();
509
510 # No subpages.
511 if ( !( $subpages instanceof TitleArray ) || $subpages->count() == 0 ) {
512 $out->addWikiMsg( 'movenosubpage' );
513 return;
514 }
515
516 $skin = $wgUser->getSkin();
517 $out->addHTML( "<ul>\n" );
518
519 foreach( $subpages as $subpage ) {
520 $link = $skin->link( $subpage );
521 if ( $subpage->isRedirect() )
522 $link = '<div class="allpagesredirect">' . $link . '</div>' ;
523
524 $out->addHTML( "<li>$link</li>\n" );
525 }
526 $out->addHTML( "</ul>\n" );
527 }
528 }
529