Disable double redirect fixer by default, pending resolution of bug 15622, causes...
[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 function showForm( $err ) {
84 global $wgOut, $wgUser, $wgFixDoubleRedirects;
85
86 $skin = $wgUser->getSkin();
87
88 $oldTitleLink = $skin->makeLinkObj( $this->oldTitle );
89
90 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) );
91 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
92
93 $newTitle = $this->newTitle;
94
95 if( !$newTitle ) {
96 # Show the current title as a default
97 # when the form is first opened.
98 $newTitle = $this->oldTitle;
99 }
100 // WTF is this doing, passing title *object* to newFromUrl()??
101 /*else {
102 if( empty($err) ) {
103 $nt = Title::newFromURL( $this->newTitle );
104 if( $nt ) {
105 # If a title was supplied, probably from the move log revert
106 # link, check for validity. We can then show some diagnostic
107 # information and save a click.
108 $newerr = $this->oldTitle->isValidMoveOperation( $nt );
109 if( is_string( $newerr ) ) {
110 $err = $newerr;
111 }
112 }
113 }
114 }*/
115
116 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
117 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
118 $movepagebtn = wfMsg( 'delete_and_move' );
119 $submitVar = 'wpDeleteAndMove';
120 $confirm = "
121 <tr>
122 <td></td>
123 <td class='mw-input'>" .
124 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
125 "</td>
126 </tr>";
127 $err = '';
128 } else {
129 $wgOut->addWikiMsg( 'movepagetext' );
130 $movepagebtn = wfMsg( 'movepagebtn' );
131 $submitVar = 'wpMove';
132 $confirm = false;
133 }
134
135 $oldTalk = $this->oldTitle->getTalkPage();
136 $considerTalk = ( !$this->oldTitle->isTalkPage() && $oldTalk->exists() );
137
138 $dbr = wfGetDB( DB_SLAVE );
139 if ( $wgFixDoubleRedirects ) {
140 $hasRedirects = $dbr->selectField( 'redirect', '1',
141 array(
142 'rd_namespace' => $this->oldTitle->getNamespace(),
143 'rd_title' => $this->oldTitle->getDBkey(),
144 ) , __METHOD__ );
145 } else {
146 $hasRedirects = false;
147 }
148
149 if ( $considerTalk ) {
150 $wgOut->addWikiMsg( 'movepagetalktext' );
151 }
152
153 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
154 $token = htmlspecialchars( $wgUser->editToken() );
155
156 if ( !empty($err) ) {
157 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
158 if( $err[0] == 'hookaborted' ) {
159 $hookErr = $err[1];
160 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
161 $wgOut->addHTML( $errMsg );
162 } else {
163 $wgOut->wrapWikiMsg( '<p><strong class="error">$1</strong></p>', $err );
164 }
165 }
166
167 $wgOut->addHTML(
168 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
169 Xml::openElement( 'fieldset' ) .
170 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
171 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
172 "<tr>
173 <td class='mw-label'>" .
174 wfMsgHtml( 'movearticle' ) .
175 "</td>
176 <td class='mw-input'>
177 <strong>{$oldTitleLink}</strong>
178 </td>
179 </tr>
180 <tr>
181 <td class='mw-label'>" .
182 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
183 "</td>
184 <td class='mw-input'>" .
185 Xml::input( 'wpNewTitle', 40, $newTitle->getPrefixedText(), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
186 Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
187 "</td>
188 </tr>
189 <tr>
190 <td class='mw-label'>" .
191 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
192 "</td>
193 <td class='mw-input'>" .
194 Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
195 "</td>
196 </tr>"
197 );
198
199 if( $considerTalk ) {
200 $wgOut->addHTML( "
201 <tr>
202 <td></td>
203 <td class='mw-input'>" .
204 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
205 "</td>
206 </tr>"
207 );
208 }
209
210 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
211 $wgOut->addHTML( "
212 <tr>
213 <td></td>
214 <td class='mw-input' >" .
215 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
216 'wpLeaveRedirect', $this->leaveRedirect ) .
217 "</td>
218 </tr>"
219 );
220 }
221
222 if ( $hasRedirects ) {
223 $wgOut->addHTML( "
224 <tr>
225 <td></td>
226 <td class='mw-input' >" .
227 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
228 'wpFixRedirects', $this->fixRedirects ) .
229 "</td>
230 </tr>"
231 );
232 }
233
234 if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
235 && $this->oldTitle->userCan( 'move-subpages' ) ) {
236 $wgOut->addHTML( "
237 <tr>
238 <td></td>
239 <td class=\"mw-input\">" .
240 Xml::checkLabel( wfMsg(
241 $this->oldTitle->hasSubpages()
242 ? 'move-subpages'
243 : 'move-talk-subpages'
244 ),
245 'wpMovesubpages', 'wpMovesubpages',
246 # Don't check the box if we only have talk subpages to
247 # move and we aren't moving the talk page.
248 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk)
249 ) .
250 "</td>
251 </tr>"
252 );
253 }
254
255 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' )
256 || $this->oldTitle->userIsWatching();
257 $wgOut->addHTML( "
258 <tr>
259 <td></td>
260 <td class='mw-input'>" .
261 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
262 "</td>
263 </tr>
264 {$confirm}
265 <tr>
266 <td>&nbsp;</td>
267 <td class='mw-submit'>" .
268 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
269 "</td>
270 </tr>" .
271 Xml::closeElement( 'table' ) .
272 Xml::hidden( 'wpEditToken', $token ) .
273 Xml::closeElement( 'fieldset' ) .
274 Xml::closeElement( 'form' ) .
275 "\n"
276 );
277
278 $this->showLogFragment( $this->oldTitle, $wgOut );
279
280 }
281
282 function doSubmit() {
283 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
284 global $wgFixDoubleRedirects;
285
286 if ( $wgUser->pingLimiter( 'move' ) ) {
287 $wgOut->rateLimited();
288 return;
289 }
290
291 $ot = $this->oldTitle;
292 $nt = $this->newTitle;
293
294 # Delete to make way if requested
295 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
296 $article = new Article( $nt );
297
298 # Disallow deletions of big articles
299 $bigHistory = $article->isBigDeletion();
300 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
301 global $wgLang, $wgDeleteRevisionsLimit;
302 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
303 return;
304 }
305
306 // This may output an error message and exit
307 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
308 }
309
310 # don't allow moving to pages with # in
311 if ( !$nt || $nt->getFragment() != '' ) {
312 $this->showForm( 'badtitletext' );
313 return;
314 }
315
316 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
317 $createRedirect = $this->leaveRedirect;
318 } else {
319 $createRedirect = true;
320 }
321
322 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
323 if ( $error !== true ) {
324 call_user_func_array( array($this, 'showForm'), $error );
325 return;
326 }
327
328 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
329 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
330 }
331
332 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
333
334 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
335
336 $oldUrl = $ot->getFullUrl( 'redirect=no' );
337 $newUrl = $nt->getFullUrl();
338 $oldText = $ot->getPrefixedText();
339 $newText = $nt->getPrefixedText();
340 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
341 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
342
343 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
344
345 # Now we move extra pages we've been asked to move: subpages and talk
346 # pages. First, if the old page or the new page is a talk page, we
347 # can't move any talk pages: cancel that.
348 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
349 $this->moveTalk = false;
350 }
351
352 if( !$ot->userCan( 'move-subpages' ) ) {
353 $this->moveSubpages = false;
354 }
355
356 # Next make a list of id's. This might be marginally less efficient
357 # than a more direct method, but this is not a highly performance-cri-
358 # tical code path and readable code is more important here.
359 #
360 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
361 # 4 might get confused. If so, consider rewriting as a UNION.
362 #
363 # If the target namespace doesn't allow subpages, moving with subpages
364 # would mean that you couldn't move them back in one operation, which
365 # is bad. FIXME: A specific error message should be given in this
366 # case.
367 $dbr = wfGetDB( DB_MASTER );
368 if( $this->moveSubpages && (
369 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
370 $this->moveTalk &&
371 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
372 )
373 ) ) {
374 $conds = array(
375 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
376 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
377 );
378 $conds['page_namespace'] = array();
379 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
380 $conds['page_namespace'] []= $ot->getNamespace();
381 }
382 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
383 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
384 }
385 } elseif( $this->moveTalk ) {
386 $conds = array(
387 'page_namespace' => $ot->getTalkPage()->getNamespace(),
388 'page_title' => $ot->getDBKey()
389 );
390 } else {
391 # Skip the query
392 $conds = null;
393 }
394
395 $extraPages = array();
396 if( !is_null( $conds ) ) {
397 $extraPages = TitleArray::newFromResult(
398 $dbr->select( 'page',
399 array( 'page_id', 'page_namespace', 'page_title' ),
400 $conds,
401 __METHOD__
402 )
403 );
404 }
405
406 $extraOutput = array();
407 $skin = $wgUser->getSkin();
408 $count = 1;
409 foreach( $extraPages as $oldSubpage ) {
410 if( $oldSubpage->getArticleId() == $ot->getArticleId() ) {
411 # Already did this one.
412 continue;
413 }
414
415 $newPageName = preg_replace(
416 '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
417 $nt->getDBKey(),
418 $oldSubpage->getDBKey()
419 );
420 if( $oldSubpage->isTalkPage() ) {
421 $newNs = $nt->getTalkPage()->getNamespace();
422 } else {
423 $newNs = $nt->getSubjectPage()->getNamespace();
424 }
425 # Bug 14385: we need makeTitleSafe because the new page names may
426 # be longer than 255 characters.
427 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
428 if( !$newSubpage ) {
429 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
430 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
431 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
432 continue;
433 }
434
435 # This was copy-pasted from Renameuser, bleh.
436 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
437 $link = $skin->makeKnownLinkObj( $newSubpage );
438 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
439 } else {
440 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason );
441 if( $success === true ) {
442 if ( $this->fixRedirects ) {
443 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
444 }
445 $oldLink = $skin->makeKnownLinkObj( $oldSubpage, '', 'redirect=no' );
446 $newLink = $skin->makeKnownLinkObj( $newSubpage );
447 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
448 } else {
449 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
450 $newLink = $skin->makeLinkObj( $newSubpage );
451 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
452 }
453 }
454
455 ++$count;
456 if( $count >= $wgMaximumMovedPages ) {
457 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
458 break;
459 }
460 }
461
462 if( $extraOutput !== array() ) {
463 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
464 }
465
466 # Deal with watches (we don't watch subpages)
467 if( $this->watch ) {
468 $wgUser->addWatch( $ot );
469 $wgUser->addWatch( $nt );
470 } else {
471 $wgUser->removeWatch( $ot );
472 $wgUser->removeWatch( $nt );
473 }
474 }
475
476 function showLogFragment( $title, &$out ) {
477 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
478 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
479 }
480
481 }