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