Users moving a page can now have all subpages automatically moved as well. Done...
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * Constructor
9 */
10 function wfSpecialMovepage( $par = null ) {
11 global $wgUser, $wgOut, $wgRequest, $action;
12
13 # Check rights
14 if ( !$wgUser->isAllowed( 'move' ) ) {
15 $wgOut->showPermissionsErrorPage( array( $wgUser->isAnon() ? 'movenologintext' : 'movenotallowed' ) );
16 return;
17 }
18
19 # Don't allow blocked users to move pages
20 if ( $wgUser->isBlocked() ) {
21 $wgOut->blockedPage();
22 return;
23 }
24
25 # Check for database lock
26 if ( wfReadOnly() ) {
27 $wgOut->readOnlyPage();
28 return;
29 }
30
31 $f = new MovePageForm( $par );
32
33 if ( 'submit' == $action && $wgRequest->wasPosted()
34 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
35 $f->doSubmit();
36 } else {
37 $f->showForm( '' );
38 }
39 }
40
41 /**
42 * HTML form for Special:Movepage
43 * @addtogroup SpecialPage
44 */
45 class MovePageForm {
46 var $oldTitle, $newTitle, $reason; # Text input
47 var $moveTalk, $deleteAndMove, $moveSubpages;
48
49 private $watch = false;
50
51 function MovePageForm( $par ) {
52 global $wgRequest;
53 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
54 $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $target );
55 $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
56 $this->reason = $wgRequest->getText( 'wpReason' );
57 if ( $wgRequest->wasPosted() ) {
58 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
59 } else {
60 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
61 }
62 $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
63 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
64 $this->watch = $wgRequest->getCheck( 'wpWatch' );
65 }
66
67 function showForm( $err, $hookErr = '' ) {
68 global $wgOut, $wgUser, $wgNamespacesWithSubpages;
69
70 $ot = Title::newFromURL( $this->oldTitle );
71 if( is_null( $ot ) ) {
72 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
73 return;
74 }
75
76 $sk = $wgUser->getSkin();
77
78 $oldTitleLink = $sk->makeLinkObj( $ot );
79 $oldTitle = $ot->getPrefixedText();
80
81 $wgOut->setPagetitle( wfMsg( 'move-page', $oldTitle ) );
82 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
83
84 if( $this->newTitle == '' ) {
85 # Show the current title as a default
86 # when the form is first opened.
87 $newTitle = $oldTitle;
88 } else {
89 if( $err == '' ) {
90 $nt = Title::newFromURL( $this->newTitle );
91 if( $nt ) {
92 # If a title was supplied, probably from the move log revert
93 # link, check for validity. We can then show some diagnostic
94 # information and save a click.
95 $newerr = $ot->isValidMoveOperation( $nt );
96 if( is_string( $newerr ) ) {
97 $err = $newerr;
98 }
99 }
100 }
101 $newTitle = $this->newTitle;
102 }
103
104 if ( $err == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
105 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle );
106 $movepagebtn = wfMsg( 'delete_and_move' );
107 $submitVar = 'wpDeleteAndMove';
108 $confirm = "
109 <tr>
110 <td></td>
111 <td class='mw-input'>" .
112 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
113 "</td>
114 </tr>";
115 $err = '';
116 } else {
117 $wgOut->addWikiMsg( 'movepagetext' );
118 $movepagebtn = wfMsg( 'movepagebtn' );
119 $submitVar = 'wpMove';
120 $confirm = false;
121 }
122
123 $oldTalk = $ot->getTalkPage();
124 $considerTalk = ( !$ot->isTalkPage() && $oldTalk->exists() );
125
126 if ( $considerTalk ) {
127 $wgOut->addWikiMsg( 'movepagetalktext' );
128 }
129
130 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
131 $token = htmlspecialchars( $wgUser->editToken() );
132
133 if ( $err != '' ) {
134 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
135 $errMsg = "";
136 if( $err == 'hookaborted' ) {
137 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
138 } else if (is_array($err)) {
139 $errMsg = '<p><strong class="error">' . call_user_func_array( 'wfMsgWikiHtml', $err ) . "</strong></p>\n";
140 } else {
141 $errMsg = '<p><strong class="error">' . wfMsgWikiHtml( $err ) . "</strong></p>\n";
142 }
143 $wgOut->addHTML( $errMsg );
144 }
145
146 $wgOut->addHTML(
147 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
148 Xml::openElement( 'fieldset' ) .
149 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
150 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
151 "<tr>
152 <td class='mw-label'>" .
153 wfMsgHtml( 'movearticle' ) .
154 "</td>
155 <td class='mw-input'>
156 <strong>{$oldTitleLink}</strong>
157 </td>
158 </tr>
159 <tr>
160 <td class='mw-label'>" .
161 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
162 "</td>
163 <td class='mw-input'>" .
164 Xml::input( 'wpNewTitle', 40, $newTitle, array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
165 Xml::hidden( 'wpOldTitle', $oldTitle ) .
166 "</td>
167 </tr>
168 <tr>
169 <td class='mw-label'>" .
170 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
171 "</td>
172 <td class='mw-input'>" .
173 Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
174 "</td>
175 </tr>"
176 );
177
178 if( $considerTalk ) {
179 $wgOut->addHTML( "
180 <tr>
181 <td></td>
182 <td class='mw-input'>" .
183 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk',
184 'wpMovetalk', $this->moveTalk ? ' checked="checked"' : '').
185 "</td>
186 </tr>"
187 );
188 }
189
190 if( isset( $wgNamespacesWithSubpages[$ot->getNamespace()] ) ) {
191 $wgOut->addHTML( "
192 <tr>
193 <td></td>
194 <td class=\"mw-input\">" .
195 Xml::checkLabel( wfMsgHtml( 'move-subpages' ),
196 'wpMovesubpages', 'wpMovesubpages',
197 $this->moveSubpages ? ' checked="checked"' : '' ) .
198 "</td>
199 </tr>"
200 );
201 }
202
203 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' ) || $ot->userIsWatching();
204 $wgOut->addHTML( "
205 <tr>
206 <td></td>
207 <td class='mw-input'>" .
208 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
209 "</td>
210 </tr>
211 {$confirm}
212 <tr>
213 <td>&nbsp;</td>
214 <td class='mw-submit'>" .
215 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
216 "</td>
217 </tr>" .
218 Xml::closeElement( 'table' ) .
219 Xml::hidden( 'wpEditToken', $token ) .
220 Xml::closeElement( 'fieldset' ) .
221 Xml::closeElement( 'form' ) .
222 "\n"
223 );
224
225 $this->showLogFragment( $ot, $wgOut );
226
227 }
228
229 function doSubmit() {
230 global $wgOut, $wgUser, $wgRequest;
231
232 if ( $wgUser->pingLimiter( 'move' ) ) {
233 $wgOut->rateLimited();
234 return;
235 }
236
237 # Variables beginning with 'o' for old article 'n' for new article
238
239 $ot = Title::newFromText( $this->oldTitle );
240 $nt = Title::newFromText( $this->newTitle );
241
242 # Delete to make way if requested
243 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
244 $article = new Article( $nt );
245
246 # Disallow deletions of big articles
247 $bigHistory = $article->isBigDeletion();
248 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
249 global $wgLang, $wgDeleteRevisionsLimit;
250 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
251 return;
252 }
253
254 // This may output an error message and exit
255 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
256 }
257
258 # don't allow moving to pages with # in
259 if ( !$nt || $nt->getFragment() != '' ) {
260 $this->showForm( 'badtitletext' );
261 return;
262 }
263
264 $hookErr = null;
265 if( !wfRunHooks( 'AbortMove', array( $ot, $nt, $wgUser, &$hookErr ) ) ) {
266 $this->showForm( 'hookaborted', $hookErr );
267 return;
268 }
269
270 $error = $ot->moveTo( $nt, true, $this->reason );
271 if ( $error !== true ) {
272 $this->showForm( $error );
273 return;
274 }
275
276 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
277
278 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
279
280 $oldUrl = $ot->getFullUrl( 'redirect=no' );
281 $newUrl = $nt->getFullUrl();
282 $oldText = $ot->getPrefixedText();
283 $newText = $nt->getPrefixedText();
284 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
285 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
286
287 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
288
289 # Now we move extra pages we've been asked to move: subpages and talk
290 # pages. First, if the old page or the new page is a talk page, we
291 # can't move any talk pages: cancel that.
292 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
293 $this->moveTalk = false;
294 }
295
296 # Next make a list of id's. This might be marginally less efficient
297 # than a more direct method, but this is not a highly performance-cri-
298 # tical code path and readable code is more important here.
299 #
300 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
301 # 4 might get confused. If so, consider rewriting as a UNION.
302 $dbr = wfGetDB( DB_SLAVE );
303 if( $this->moveSubpages ) {
304 $conds = array(
305 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
306 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
307 );
308 if( $this->moveTalk ) {
309 $conds['page_namespace'] = array( $ot->getNamespace(),
310 MWNamespace::getTalk($ot->getNamespace()) );
311 } else {
312 $conds['page_namespace'] = $ot->getNamespace();
313 }
314 } else {
315 if( $this->moveTalk ) {
316 $conds = array(
317 'page_namespace' => MWNamespace::getTalk($ot->getNamespace()),
318 'page_title' => $ot->getDBKey()
319 );
320 } else {
321 $conds = '0 = 1';
322 }
323 }
324
325 $extrapages = $dbr->select(
326 'page',
327 array( 'page_id', 'page_namespace', 'page_title' ),
328 $conds,
329 __METHOD__
330 );
331
332 $extraOutput = '';
333 $skin =& $wgUser->getSkin();
334 foreach( $extrapages as $row ) {
335 if( $row->page_id == $ot->getArticleId() ) {
336 # Already did this one.
337 continue;
338 }
339
340 $oldPage = Title::newFromRow( $row );
341 $newPageName = preg_replace(
342 '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
343 $nt->getDBKey(),
344 $oldPage->getDBKey()
345 );
346 # The following line is an atrocious hack. Kill it.
347 $newNs = $nt->getNamespace() + ($oldPage->getNamespace() & 1);
348 $newPage = Title::makeTitle( $newNs, $newPageName );
349
350 # This was copy-pasted from Renameuser, bleh.
351 if ( $newPage->exists() && !$oldPage->isValidMoveTarget( $newPage ) ) {
352 $link = $skin->makeKnownLinkObj( $newPage );
353 $extraOutput .= '<li>' . wfMsgHtml( 'movepage-page-exists', $link ) . '</li>';
354 } else {
355 $success = $oldPage->moveTo( $newPage, true, $this->reason );
356 if( $success === true ) {
357 $oldLink = $skin->makeKnownLinkObj( $oldPage, '', 'redirect=no' );
358 $newLink = $skin->makeKnownLinkObj( $newPage );
359 $extraOutput .= '<li>' . wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink ) . '</li>';
360 } else {
361 $oldLink = $skin->makeKnownLinkObj( $oldPage );
362 $newLink = $skin->makeLinkObj( $newPage );
363 $extraOutput .= '<li>' . wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink ) . '</li>';
364 }
365 }
366 }
367
368 if( $extraOutput !== '' ) {
369 $wgOut->addHTML( "<ul>$extraOutput</ul>" );
370 }
371
372 # Deal with watches
373 if( $this->watch ) {
374 $wgUser->addWatch( $ot );
375 $wgUser->addWatch( $nt );
376 } else {
377 $wgUser->removeWatch( $ot );
378 $wgUser->removeWatch( $nt );
379 }
380 }
381
382 function showLogFragment( $title, &$out ) {
383 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
384 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
385 }
386
387 }