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