Update timestamp of redirect when renaming over a redirect; add some comments
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
1 <?
2 include_once( "LinksUpdate.php" );
3
4 function wfSpecialMovepage()
5 {
6 global $wgUser, $wgOut, $action, $target;
7
8 if ( 0 == $wgUser->getID() or $wgUser->isBlocked() ) {
9 $wgOut->errorpage( "movenologin", "movenologintext" );
10 return;
11 }
12 if ( wfReadOnly() ) {
13 $wgOut->readOnlyPage();
14 return;
15 }
16 $fields = array( "wpNewTitle", "wpOldTitle" );
17 wfCleanFormFields( $fields );
18
19 $f = new MovePageForm();
20
21 if ( "success" == $action ) { $f->showSuccess(); }
22 else if ( "submit" == $action ) { $f->doSubmit(); }
23 else { $f->showForm( "" ); }
24 }
25
26 class MovePageForm {
27
28 var $ot, $nt; # Old, new Title objects
29 var $ons, $nns; # Namespaces
30 var $odt, $ndt; # Pagenames (dbkey form)
31 var $oft, $nft; # Full page titles (DBkey form)
32 var $ofx, $nfx; # Full page titles (Text form)
33 var $oldid, $newid; # "cur_id" field (yes, both from "cur")
34 var $talkmoved = 0;
35
36 function showForm( $err )
37 {
38 global $wgOut, $wgUser, $wgLang;
39 global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target;
40
41 $wgOut->setPagetitle( wfMsg( "movepage" ) );
42
43 if ( ! $wpOldTitle ) {
44 $target = wfCleanQueryVar( $target );
45 if ( "" == $target ) {
46 $wgOut->errorpage( "notargettitle", "notargettext" );
47 return;
48 }
49 $wpOldTitle = $target;
50 }
51 $ot = Title::newFromURL( $wpOldTitle );
52 $ott = $ot->getPrefixedText();
53
54 $wgOut->addWikiText( wfMsg( "movepagetext" ) );
55 if ( ! Namespace::isTalk( $ot->getNamespace() ) )
56 $wgOut->addWikiText( "\n\n" . wfMsg( "movepagetalktext" ) );
57
58 $ma = wfMsg( "movearticle" );
59 $newt = wfMsg( "newtitle" );
60 $mpb = wfMsg( "movepagebtn" );
61 $movetalk = wfMsg( "movetalk" );
62
63 $action = wfLocalUrlE( $wgLang->specialPage( "Movepage" ),
64 "action=submit" );
65
66 if ( "" != $err ) {
67 $wgOut->setSubtitle( wfMsg( "formerror" ) );
68 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
69 }
70 $wgOut->addHTML( "<p>
71 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
72 <table border=0><tr>
73 <td align=right>{$ma}:</td>
74 <td align=left><strong>{$ott}</strong></td>
75 </tr><tr>
76 <td align=right>{$newt}:</td>
77 <td align=left>
78 <input type=text size=40 name=\"wpNewTitle\" value=\"{$wpNewTitle}\">
79 <input type=hidden name=\"wpOldTitle\" value=\"{$wpOldTitle}\">
80 </td>
81 </tr>" );
82
83 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
84 $wgOut->addHTML(
85 "<tr>
86 <td align=right>
87 <input type=checkbox name=\"wpMovetalk\" checked value=\"1\">
88 </td><td>{$movetalk}</td>
89 </tr>" );
90 }
91 $wgOut->addHTML(
92 "<tr>
93 <td>&nbsp;</td><td align=left>
94 <input type=submit name=\"wpMove\" value=\"{$mpb}\">
95 </td></tr></table>
96 </form>\n" );
97
98 }
99
100 function doSubmit()
101 {
102 global $wgOut, $wgUser, $wgLang;
103 global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target;
104 global $wgDeferredUpdateList;
105 $fname = "MovePageForm::doSubmit";
106
107 $this->ot = Title::newFromText( $wpOldTitle );
108 $this->nt = Title::newFromText( $wpNewTitle );
109 if( !$this->ot or !$this->nt ) {
110 $this->showForm( wfMsg( "badtitletext" ) );
111 return;
112 }
113 $this->ons = $this->ot->getNamespace();
114 $this->nns = $this->nt->getNamespace();
115 $this->odt = wfStrencode( $this->ot->getDBkey() );
116 $this->ndt = wfStrencode( $this->nt->getDBkey() );
117 $this->oft = wfStrencode( $this->ot->getPrefixedDBkey() );
118 $this->nft = wfStrencode( $this->nt->getPrefixedDBkey() );
119 $this->ofx = $this->ot->getPrefixedText();
120 $this->nfx = $this->nt->getPrefixedText();
121
122 $this->oldid = $this->ot->getArticleID();
123 $this->newid = $this->nt->getArticleID();
124
125 if ( strlen( trim( $this->ndt ) ) < 1 ) {
126 $this->showForm( wfMsg( "articleexists" ) );
127 return;
128 }
129 if ( ( ! Namespace::isMovable( $this->ons ) ) ||
130 ( "" == $this->odt ) ||
131 ( "" != $this->ot->getInterwiki() ) ||
132 ( !$this->ot->userCanEdit() ) ||
133 ( !$this->oldid ) ||
134 ( ! Namespace::isMovable( $nns ) ) ||
135 ( "" == $this->ndt ) ||
136 ( "" != $this->nt->getInterwiki() ) ||
137 ( !$this->nt->userCanEdit() ) ) {
138 $this->showForm( wfMsg( "badarticleerror" ) );
139 return;
140 }
141 # The move is allowed only if (1) the target doesn't exist, or
142 # (2) the target is a redirect to the source, and has no history
143 # (so we can undo bad moves right after they're done).
144
145 if ( 0 != $this->newid ) { # Target exists; check for validity
146 if ( ! $this->isValidTarget() ) {
147 $this->showForm( wfMsg( "articleexists" ) );
148 return;
149 }
150 $this->moveOverExistingRedirect();
151 } else { # Target didn't exist, do normal move.
152 $this->moveToNewTitle();
153 }
154
155 $this->updateWatchlists();
156
157 $u = new SearchUpdate( $this->oldid, $this->nt->getPrefixedDBkey() );
158 $u->doUpdate();
159 $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
160 $u->doUpdate();
161
162 # Move talk page if (1) the checkbox says to, (2) the source
163 # and target namespaces are identical, (3) the namespaces are not
164 # themselves talk namespaces, and of course (4) it exists.
165
166 if ( ( 1 == $wpMovetalk ) &&
167 ( ! Namespace::isTalk( $this->ons ) ) &&
168 ( $this->ons == $this->nns ) ) {
169
170 $this->ons = $this->nns = Namespace::getTalk( $this->ons );
171
172 $this->ot = Title::newFromText( Title::makeName(
173 $this->ons, $wpOldTitle ) );
174 $this->nt = Title::newFromText( Title::makeName(
175 $this->nns, $wpNewTitle ) );
176
177 # odt, ndt, ofx, nfx remain the same
178
179 $this->oft = wfStrencode( $this->ot->getPrefixedDBkey() );
180 $this->nft = wfStrencode( $this->nt->getPrefixedDBkey() );
181
182 $this->oldid = $this->ot->getArticleID();
183 $this->newid = $this->nt->getArticleID();
184
185 if ( 0 != $this->oldid ) {
186 if ( 0 != $this->newid ) {
187 if ( $this->isValidTarget() ) {
188 $this->moveOverExistingRedirect();
189 $this->talkmoved = 1;
190 } else {
191 $this->talkmoved = 'invalid';
192 }
193 } else {
194 $this->moveToNewTitle();
195 $this->talkmoved = 1;
196 }
197 $u = new SearchUpdate( $this->oldid, $this->nt->getPrefixedDBkey() );
198 $u->doUpdate();
199 $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
200 $u->doUpdate();
201 }
202 }
203 $success = wfLocalUrl( $wgLang->specialPage( "Movepage" ),
204 "action=success&oldtitle=" . wfUrlencode( $this->ofx ) .
205 "&newtitle=" . wfUrlencode( $this->nfx ) .
206 "&talkmoved={$this->talkmoved}" );
207
208 $wgOut->redirect( $success );
209 }
210
211 function showSuccess()
212 {
213 global $wgOut, $wgUser;
214 global $newtitle, $oldtitle, $talkmoved;
215
216 $wgOut->setPagetitle( wfMsg( "movepage" ) );
217 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
218
219 $fields = array( "oldtitle", "newtitle" );
220 wfCleanFormFields( $fields );
221
222 $text = wfMsg( "pagemovedtext", $oldtitle, $newtitle );
223 $wgOut->addWikiText( $text );
224
225 if ( 1 == $talkmoved ) {
226 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
227 } elseif( 'invalid' == $talkmoved ) {
228 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
229 } else {
230 $ot = Title::newFromURL( $oldtitle );
231 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
232 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
233 }
234 }
235 }
236
237 # Is the the existing target title valid?
238
239 function isValidTarget()
240 {
241 $fname = "MovePageForm::isValidTarget";
242
243 $sql = "SELECT cur_is_redirect,cur_text FROM cur " .
244 "WHERE cur_id={$this->newid}";
245 $res = wfQuery( $sql, DB_READ, $fname );
246 $obj = wfFetchObject( $res );
247
248 if ( 0 == $obj->cur_is_redirect ) { return false; }
249
250 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text, $m ) ) {
251 $rt = Title::newFromText( $m[1] );
252 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
253 $this->oft ) ) {
254 return false;
255 }
256 }
257 $sql = "SELECT old_id FROM old WHERE old_namespace={$this->nns} " .
258 "AND old_title='{$this->ndt}'";
259 $res = wfQuery( $sql, DB_READ, $fname );
260 if ( 0 != wfNumRows( $res ) ) { return false; }
261
262 return true;
263 }
264
265 # Move page to title which is presently a redirect to the source
266 # page. Handling link tables here is tricky.
267
268 function moveOverExistingRedirect()
269 {
270 global $wgUser, $wgLinkCache;
271 $fname = "MovePageForm::moveOverExistingRedirect";
272 $mt = wfMsg( "movedto" );
273
274 # Change the name of the target page:
275 $now = wfTimestampNow();
276 $sql = "UPDATE cur SET cur_touched='{$now}'," .
277 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
278 "WHERE cur_id={$this->oldid}";
279 wfQuery( $sql, DB_WRITE, $fname );
280 $wgLinkCache->clearLink( $this->nft );
281
282 # Repurpose the old redirect. We don't save it to history since
283 # by definition if we've got here it's rather uninteresting.
284 $sql = "UPDATE cur SET cur_touched='{$now}',cur_timestamp='{$now}'," .
285 "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
286 "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
287 "{$mt} \\\"{$this->nft}\\\"',cur_user='" . $wgUser->getID() .
288 "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
289 "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
290 "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
291 wfQuery( $sql, DB_WRITE, $fname );
292 $wgLinkCache->clearLink( $this->oft );
293
294 # Fix the redundant names for the past revisions of the target page.
295 # The redirect should have no old revisions.
296 $sql = "UPDATE old SET " .
297 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
298 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
299 wfQuery( $sql, DB_WRITE, $fname );
300
301 $sql = "UPDATE recentchanges SET ".
302 "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
303 "rc_cur_id={$this->oldid}";
304 wfQuery( $sql, DB_WRITE, $fname );
305
306 # FIXME: Here we mark the redirect as 'new' in recentchanges,
307 # but as old in cur. Is there a reason for this?
308 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
309 rc_comment,rc_user,rc_user_text,rc_timestamp,
310 rc_cur_time,rc_cur_id,rc_new)
311 VALUES ({$this->ons},'{$this->odt}'," .
312 "'{$mt} \\\"{$this->nft}\\\"','" .
313 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
314 "','{$now}','{$now}',{$this->newid},1)";
315 wfQuery( $sql, DB_WRITE, $fname );
316
317 # The only link from here should be the old redirect
318
319 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
320 wfQuery( $sql, DB_WRITE, $fname );
321
322 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
323 wfQuery( $sql, DB_WRITE, $fname );
324
325 # Swap links. Using MAXINT as a temp; if there's ever an article
326 # with id 4294967295, this will fail, but I think that's pretty safe
327
328 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
329 wfQuery( $sql, DB_WRITE, $fname );
330
331 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
332 wfQuery( $sql, DB_WRITE, $fname );
333
334 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
335 wfQuery( $sql, DB_WRITE, $fname );
336
337 # Note: the insert below must be after the updates above!
338
339 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
340 wfQuery( $sql, DB_WRITE, $fname );
341
342 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
343 wfQuery( $sql, DB_WRITE, $fname );
344 }
345
346 # Move page to non-existing title.
347
348 function moveToNewTitle()
349 {
350 global $wgUser, $wgLinkCache;
351 $fname = "MovePageForm::moveToNewTitle";
352 $mt = wfMsg( "movedto" );
353
354 $now = wfTimestampNow();
355 $won = wfInvertTimestamp( $now );
356 $sql = "UPDATE cur SET cur_touched='{$now}'," .
357 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
358 "WHERE cur_id={$this->oldid}";
359 wfQuery( $sql, DB_WRITE, $fname );
360 $wgLinkCache->clearLink( $this->nft );
361
362 $common = "{$this->ons},'{$this->odt}'," .
363 "'{$mt} \\\"{$this->nft}\\\"','" .
364 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
365 "','{$now}'";
366 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
367 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
368 "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
369 "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
370 wfQuery( $sql, DB_WRITE, $fname );
371 $this->newid = wfInsertId();
372 $wgLinkCache->clearLink( $this->oft );
373
374 $sql = "UPDATE old SET " .
375 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
376 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
377 wfQuery( $sql, DB_WRITE, $fname );
378
379 $sql = "UPDATE recentchanges SET ".
380 "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
381 "rc_namespace={$this->ons} AND rc_title='{$this->odt}'";
382 wfQuery( $sql, DB_WRITE, $fname );
383
384 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
385 rc_comment,rc_user,rc_user_text,rc_timestamp,
386 rc_cur_time,rc_cur_id,rc_new)
387 VALUES ({$common},'{$now}',{$this->newid},1)";
388 wfQuery( $sql, DB_WRITE, $fname );
389
390 global $wgEnablePersistentLC;
391 if ( $wgEnablePersistentLC ) {
392 // Purge related entries in links cache on new page, to heal broken links
393 $ptitle = wfStrencode( $this->nft );
394 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
395 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE);
396 }
397
398 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
399 wfQuery( $sql, DB_WRITE, $fname );
400
401 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
402 wfQuery( $sql, DB_WRITE, $fname );
403
404 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
405 wfQuery( $sql, DB_WRITE, $fname );
406
407 # Non-existent target may have had broken links to it; these must
408 # now be removed and made into good links.
409 $update = new LinksUpdate( $this->oldid, $this->nft );
410 $update->fixBrokenLinks();
411
412 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
413 wfQuery( $sql, DB_WRITE, $fname );
414 }
415
416 function updateWatchlists()
417 {
418 $oldnamespace = $this->ons & ~1;
419 $newnamespace = $this->nns & ~1;
420 $oldtitle = $this->odt;
421 $newtitle = $this->ndt;
422
423 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
424 return;
425
426 WatchedItem::duplicateEntries( $this->ot, $this->nt );
427 }
428
429 }
430 ?>