API: More cleanup:
[mediawiki.git] / includes / specials / SpecialMovepage.php
blobcaba8c4df0d107b3301e9d5d7341de06d84b044c
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
7 /**
8 * Constructor
9 */
10 function wfSpecialMovepage( $par = null ) {
11 global $wgUser, $wgOut, $wgRequest, $action;
13 # Check for database lock
14 if ( wfReadOnly() ) {
15 $wgOut->readOnlyPage();
16 return;
19 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
20 $oldTitle = $wgRequest->getText( 'wpOldTitle', $target );
21 $newTitle = $wgRequest->getText( 'wpNewTitle' );
23 # Variables beginning with 'o' for old article 'n' for new article
24 $ot = Title::newFromText( $oldTitle );
25 $nt = Title::newFromText( $newTitle );
27 if( is_null( $ot ) ) {
28 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
29 return;
31 if( !$ot->exists() ) {
32 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
33 return;
36 # Check rights
37 $permErrors = $ot->getUserPermissionsErrors( 'move', $wgUser );
38 if( !empty( $permErrors ) ) {
39 $wgOut->showPermissionsErrorPage( $permErrors );
40 return;
43 $f = new MovePageForm( $ot, $nt );
45 if ( 'submit' == $action && $wgRequest->wasPosted()
46 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
47 $f->doSubmit();
48 } else {
49 $f->showForm( '' );
53 /**
54 * HTML form for Special:Movepage
55 * @ingroup SpecialPage
57 class MovePageForm {
58 var $oldTitle, $newTitle, $reason; # Text input
59 var $moveTalk, $deleteAndMove, $moveSubpages;
61 private $watch = false;
63 function MovePageForm( $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 } else {
72 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
74 $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
75 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
76 $this->watch = $wgRequest->getCheck( 'wpWatch' );
79 function showForm( $err, $hookErr = '' ) {
80 global $wgOut, $wgUser;
82 $ot = $this->oldTitle;
83 $sk = $wgUser->getSkin();
85 $oldTitleLink = $sk->makeLinkObj( $ot );
86 $oldTitle = $ot->getPrefixedText();
88 $wgOut->setPagetitle( wfMsg( 'move-page', $oldTitle ) );
89 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
91 if( $this->newTitle == '' ) {
92 # Show the current title as a default
93 # when the form is first opened.
94 $newTitle = $oldTitle;
95 } else {
96 if( $err == '' ) {
97 $nt = Title::newFromURL( $this->newTitle );
98 if( $nt ) {
99 # If a title was supplied, probably from the move log revert
100 # link, check for validity. We can then show some diagnostic
101 # information and save a click.
102 $newerr = $ot->isValidMoveOperation( $nt );
103 if( is_string( $newerr ) ) {
104 $err = $newerr;
108 $newTitle = $this->newTitle;
111 if ( $err == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
112 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle );
113 $movepagebtn = wfMsg( 'delete_and_move' );
114 $submitVar = 'wpDeleteAndMove';
115 $confirm = "
116 <tr>
117 <td></td>
118 <td class='mw-input'>" .
119 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
120 "</td>
121 </tr>";
122 $err = '';
123 } else {
124 $wgOut->addWikiMsg( 'movepagetext' );
125 $movepagebtn = wfMsg( 'movepagebtn' );
126 $submitVar = 'wpMove';
127 $confirm = false;
130 $oldTalk = $ot->getTalkPage();
131 $considerTalk = ( !$ot->isTalkPage() && $oldTalk->exists() );
133 if ( $considerTalk ) {
134 $wgOut->addWikiMsg( 'movepagetalktext' );
137 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
138 $token = htmlspecialchars( $wgUser->editToken() );
140 if ( $err != '' ) {
141 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
142 if( $err == 'hookaborted' ) {
143 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
144 $wgOut->addHTML( $errMsg );
145 } else {
146 $wgOut->wrapWikiMsg( '<p><strong class="error">$1</strong></p>', $err );
150 $wgOut->addHTML(
151 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
152 Xml::openElement( 'fieldset' ) .
153 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
154 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
155 "<tr>
156 <td class='mw-label'>" .
157 wfMsgHtml( 'movearticle' ) .
158 "</td>
159 <td class='mw-input'>
160 <strong>{$oldTitleLink}</strong>
161 </td>
162 </tr>
163 <tr>
164 <td class='mw-label'>" .
165 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
166 "</td>
167 <td class='mw-input'>" .
168 Xml::input( 'wpNewTitle', 40, $newTitle, array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
169 Xml::hidden( 'wpOldTitle', $oldTitle ) .
170 "</td>
171 </tr>
172 <tr>
173 <td class='mw-label'>" .
174 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
175 "</td>
176 <td class='mw-input'>" .
177 Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
178 "</td>
179 </tr>"
182 if( $considerTalk ) {
183 $wgOut->addHTML( "
184 <tr>
185 <td></td>
186 <td class='mw-input'>" .
187 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
188 "</td>
189 </tr>"
193 if( ($ot->hasSubpages() || $ot->getTalkPage()->hasSubpages())
194 && $ot->userCan( 'move-subpages' ) ) {
195 $wgOut->addHTML( "
196 <tr>
197 <td></td>
198 <td class=\"mw-input\">" .
199 Xml::checkLabel( wfMsgHtml(
200 $ot->hasSubpages()
201 ? 'move-subpages'
202 : 'move-talk-subpages'
204 'wpMovesubpages', 'wpMovesubpages',
205 # Don't check the box if we only have talk subpages to
206 # move and we aren't moving the talk page.
207 $this->moveSubpages && ($ot->hasSubpages() || $this->moveTalk)
209 "</td>
210 </tr>"
214 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' ) || $ot->userIsWatching();
215 $wgOut->addHTML( "
216 <tr>
217 <td></td>
218 <td class='mw-input'>" .
219 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
220 "</td>
221 </tr>
222 {$confirm}
223 <tr>
224 <td>&nbsp;</td>
225 <td class='mw-submit'>" .
226 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
227 "</td>
228 </tr>" .
229 Xml::closeElement( 'table' ) .
230 Xml::hidden( 'wpEditToken', $token ) .
231 Xml::closeElement( 'fieldset' ) .
232 Xml::closeElement( 'form' ) .
233 "\n"
236 $this->showLogFragment( $ot, $wgOut );
240 function doSubmit() {
241 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
243 if ( $wgUser->pingLimiter( 'move' ) ) {
244 $wgOut->rateLimited();
245 return;
248 $ot = $this->oldTitle;
249 $nt = $this->newTitle;
251 # Delete to make way if requested
252 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
253 $article = new Article( $nt );
255 # Disallow deletions of big articles
256 $bigHistory = $article->isBigDeletion();
257 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
258 global $wgLang, $wgDeleteRevisionsLimit;
259 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
260 return;
263 // This may output an error message and exit
264 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
267 # don't allow moving to pages with # in
268 if ( !$nt || $nt->getFragment() != '' ) {
269 $this->showForm( 'badtitletext' );
270 return;
273 $error = $ot->moveTo( $nt, true, $this->reason );
274 if ( $error !== true ) {
275 # FIXME: showForm() should handle multiple errors
276 call_user_func_array(array($this, 'showForm'), $error[0]);
277 return;
280 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
282 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
284 $oldUrl = $ot->getFullUrl( 'redirect=no' );
285 $newUrl = $nt->getFullUrl();
286 $oldText = $ot->getPrefixedText();
287 $newText = $nt->getPrefixedText();
288 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
289 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
291 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
293 # Now we move extra pages we've been asked to move: subpages and talk
294 # pages. First, if the old page or the new page is a talk page, we
295 # can't move any talk pages: cancel that.
296 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
297 $this->moveTalk = false;
300 if( !$ot->userCan( 'move-subpages' ) ) {
301 $this->moveSubpages = false;
304 # Next make a list of id's. This might be marginally less efficient
305 # than a more direct method, but this is not a highly performance-cri-
306 # tical code path and readable code is more important here.
308 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
309 # 4 might get confused. If so, consider rewriting as a UNION.
311 # If the target namespace doesn't allow subpages, moving with subpages
312 # would mean that you couldn't move them back in one operation, which
313 # is bad. FIXME: A specific error message should be given in this
314 # case.
315 $dbr = wfGetDB( DB_MASTER );
316 if( $this->moveSubpages && (
317 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
318 $this->moveTalk &&
319 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
321 ) ) {
322 $conds = array(
323 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
324 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
326 $conds['page_namespace'] = array();
327 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
328 $conds['page_namespace'] []= $ot->getNamespace();
330 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
331 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
333 } elseif( $this->moveTalk ) {
334 $conds = array(
335 'page_namespace' => $ot->getTalkPage()->getNamespace(),
336 'page_title' => $ot->getDBKey()
338 } else {
339 # Skip the query
340 $conds = null;
343 $extrapages = array();
344 if( !is_null( $conds ) ) {
345 $extrapages = $dbr->select( 'page',
346 array( 'page_id', 'page_namespace', 'page_title' ),
347 $conds,
348 __METHOD__
352 $extraOutput = array();
353 $skin = $wgUser->getSkin();
354 $count = 1;
355 foreach( $extrapages as $row ) {
356 if( $row->page_id == $ot->getArticleId() ) {
357 # Already did this one.
358 continue;
361 $oldPage = Title::newFromRow( $row );
362 $newPageName = preg_replace(
363 '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
364 $nt->getDBKey(),
365 $oldPage->getDBKey()
367 if( $oldPage->isTalkPage() ) {
368 $newNs = $nt->getTalkPage()->getNamespace();
369 } else {
370 $newNs = $nt->getSubjectPage()->getNamespace();
372 # Bug 14385: we need makeTitleSafe because the new page names may
373 # be longer than 255 characters.
374 $newPage = Title::makeTitleSafe( $newNs, $newPageName );
375 if( !$newPage ) {
376 $oldLink = $skin->makeKnownLinkObj( $oldPage );
377 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
378 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
379 continue;
382 # This was copy-pasted from Renameuser, bleh.
383 if ( $newPage->exists() && !$oldPage->isValidMoveTarget( $newPage ) ) {
384 $link = $skin->makeKnownLinkObj( $newPage );
385 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
386 } else {
387 $success = $oldPage->moveTo( $newPage, true, $this->reason );
388 if( $success === true ) {
389 $oldLink = $skin->makeKnownLinkObj( $oldPage, '', 'redirect=no' );
390 $newLink = $skin->makeKnownLinkObj( $newPage );
391 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
392 } else {
393 $oldLink = $skin->makeKnownLinkObj( $oldPage );
394 $newLink = $skin->makeLinkObj( $newPage );
395 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
399 ++$count;
400 if( $count >= $wgMaximumMovedPages ) {
401 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
402 break;
406 if( $extraOutput !== array() ) {
407 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
410 # Deal with watches (we don't watch subpages)
411 if( $this->watch ) {
412 $wgUser->addWatch( $ot );
413 $wgUser->addWatch( $nt );
414 } else {
415 $wgUser->removeWatch( $ot );
416 $wgUser->removeWatch( $nt );
420 function showLogFragment( $title, &$out ) {
421 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
422 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );