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