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