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