changing wfQuery to allow replication
[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 $this->ons = $this->ot->getNamespace();
110 $this->nns = $this->nt->getNamespace();
111 $this->odt = wfStrencode( $this->ot->getDBkey() );
112 $this->ndt = wfStrencode( $this->nt->getDBkey() );
113 $this->oft = wfStrencode( $this->ot->getPrefixedDBkey() );
114 $this->nft = wfStrencode( $this->nt->getPrefixedDBkey() );
115 $this->ofx = $this->ot->getPrefixedText();
116 $this->nfx = $this->nt->getPrefixedText();
117
118 $this->oldid = $this->ot->getArticleID();
119 $this->newid = $this->nt->getArticleID();
120
121 if ( strlen( trim( $this->ndt ) ) < 1 ) {
122 $this->showForm( wfMsg( "articleexists" ) );
123 return;
124 }
125 if ( ( ! Namespace::isMovable( $this->ons ) ) ||
126 ( "" == $this->odt ) ||
127 ( "" != $this->ot->getInterwiki() ) ||
128 ( ! Namespace::isMovable( $nns ) ) ||
129 ( "" == $this->ndt ) ||
130 ( "" != $this->nt->getInterwiki() ) ) {
131 $this->showForm( wfMsg( "badarticleerror" ) );
132 return;
133 }
134 # The move is allowed only if (1) the target doesn't exist, or
135 # (2) the target is a redirect to the source, and has no history
136 # (so we can undo bad moves right after they're done).
137
138 if ( 0 != $this->newid ) { # Target exists; check for validity
139 if ( ! $this->isValidTarget() ) {
140 $this->showForm( wfMsg( "articleexists" ) );
141 return;
142 }
143 $this->moveOverExistingRedirect();
144 } else { # Target didn't exist, do normal move.
145 $this->moveToNewTitle();
146 }
147
148 $this->updateWatchlists();
149
150 $u = new SearchUpdate( $this->oldid, $this->nt->getPrefixedDBkey() );
151 $u->doUpdate();
152 $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
153 $u->doUpdate();
154
155 # Move talk page if (1) the checkbox says to, (2) the source
156 # and target namespaces are identical, (3) the namespaces are not
157 # themselves talk namespaces, and of course (4) it exists.
158
159 if ( ( 1 == $wpMovetalk ) &&
160 ( ! Namespace::isTalk( $this->ons ) ) &&
161 ( $this->ons == $this->nns ) ) {
162
163 $this->ons = $this->nns = Namespace::getTalk( $this->ons );
164
165 $this->ot = Title::newFromText( Title::makeName(
166 $this->ons, $wpOldTitle ) );
167 $this->nt = Title::newFromText( Title::makeName(
168 $this->nns, $wpNewTitle ) );
169
170 # odt, ndt, ofx, nfx remain the same
171
172 $this->oft = wfStrencode( $this->ot->getPrefixedDBkey() );
173 $this->nft = wfStrencode( $this->nt->getPrefixedDBkey() );
174
175 $this->oldid = $this->ot->getArticleID();
176 $this->newid = $this->nt->getArticleID();
177
178 if ( 0 != $this->oldid ) {
179 if ( 0 != $this->newid ) {
180 if ( $this->isValidTarget() ) {
181 $this->moveOverExistingRedirect();
182 $this->talkmoved = 1;
183 } else {
184 $this->talkmoved = 'invalid';
185 }
186 } else {
187 $this->moveToNewTitle();
188 $this->talkmoved = 1;
189 }
190 $u = new SearchUpdate( $this->oldid, $this->nt->getPrefixedDBkey() );
191 $u->doUpdate();
192 $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
193 $u->doUpdate();
194 }
195 }
196 $success = wfLocalUrl( $wgLang->specialPage( "Movepage" ),
197 "action=success&oldtitle=" . wfUrlencode( $this->ofx ) .
198 "&newtitle=" . wfUrlencode( $this->nfx ) .
199 "&talkmoved={$this->talkmoved}" );
200
201 $wgOut->redirect( $success );
202 }
203
204 function showSuccess()
205 {
206 global $wgOut, $wgUser;
207 global $newtitle, $oldtitle, $talkmoved;
208
209 $wgOut->setPagetitle( wfMsg( "movepage" ) );
210 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
211
212 $fields = array( "oldtitle", "newtitle" );
213 wfCleanFormFields( $fields );
214
215 $text = str_replace( "$1", $oldtitle, wfMsg( "pagemovedtext" ) );
216 $text = str_replace( "$2", $newtitle, $text );
217 $wgOut->addWikiText( $text );
218
219 if ( 1 == $talkmoved ) {
220 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
221 } elseif( 'invalid' == $talkmoved ) {
222 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
223 } else {
224 $ot = Title::newFromURL( $oldtitle );
225 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
226 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
227 }
228 }
229 }
230
231 # Is the the existing target title valid?
232
233 function isValidTarget()
234 {
235 $fname = "MovePageForm::isValidTarget";
236
237 $sql = "SELECT cur_is_redirect,cur_text FROM cur " .
238 "WHERE cur_id={$this->newid}";
239 $res = wfQuery( $sql, DB_READ, $fname );
240 $obj = wfFetchObject( $res );
241
242 if ( 0 == $obj->cur_is_redirect ) { return false; }
243
244 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text, $m ) ) {
245 $rt = Title::newFromText( $m[1] );
246 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
247 $this->oft ) ) {
248 return false;
249 }
250 }
251 $sql = "SELECT old_id FROM old WHERE old_namespace={$this->nns} " .
252 "AND old_title='{$this->ndt}'";
253 $res = wfQuery( $sql, DB_READ, $fname );
254 if ( 0 != wfNumRows( $res ) ) { return false; }
255
256 return true;
257 }
258
259 # Move page to title which is presently a redirect to the source
260 # page. Handling link tables here is tricky.
261
262 function moveOverExistingRedirect()
263 {
264 global $wgUser;
265 $fname = "MovePageForm::moveOverExistingRedirect";
266 $mt = wfMsg( "movedto" );
267
268 $now = wfTimestampNow();
269 $sql = "UPDATE cur SET cur_touched='{$now}'," .
270 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
271 "WHERE cur_id={$this->oldid}";
272 wfQuery( $sql, DB_WRITE, $fname );
273
274 $sql = "UPDATE cur SET cur_touched='{$now}'," .
275 "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
276 "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
277 "{$mt} \\\"{$this->nft}\\\"',cur_user='" . $wgUser->getID() .
278 "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
279 "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
280 "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
281 wfQuery( $sql, DB_WRITE, $fname );
282
283 $sql = "UPDATE old SET " .
284 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
285 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
286 wfQuery( $sql, DB_WRITE, $fname );
287
288 $sql = "UPDATE recentchanges SET ".
289 "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
290 "rc_cur_id={$this->oldid}";
291 wfQuery( $sql, DB_WRITE, $fname );
292
293 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
294 rc_comment,rc_user,rc_user_text,rc_timestamp,
295 rc_cur_time,rc_cur_id,rc_new)
296 VALUES ({$this->ons},'{$this->odt}'," .
297 "'{$mt} \\\"{$this->nft}\\\"','" .
298 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
299 "','{$now}','{$now}',{$this->newid},1)";
300 wfQuery( $sql, DB_WRITE, $fname );
301
302 # The only link from here should be the old redirect
303
304 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
305 wfQuery( $sql, DB_WRITE, $fname );
306
307 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
308 wfQuery( $sql, DB_WRITE, $fname );
309
310 # Swap links. Using MAXINT as a temp; if there's ever an article
311 # with id 4294967295, this will fail, but I think that's pretty safe
312
313 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
314 wfQuery( $sql, DB_WRITE, $fname );
315
316 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
317 wfQuery( $sql, DB_WRITE, $fname );
318
319 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
320 wfQuery( $sql, DB_WRITE, $fname );
321
322 # Note: the insert below must be after the updates above!
323
324 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
325 wfQuery( $sql, DB_WRITE, $fname );
326
327 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
328 wfQuery( $sql, DB_WRITE, $fname );
329 }
330
331 # Move page to non-existing title.
332
333 function moveToNewTitle()
334 {
335 global $wgUser;
336 $fname = "MovePageForm::moveToNewTitle";
337 $mt = wfMsg( "movedto" );
338
339 $now = wfTimestampNow();
340 $won = wfInvertTimestamp( $now );
341 $sql = "UPDATE cur SET cur_touched='{$now}'," .
342 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
343 "WHERE cur_id={$this->oldid}";
344 wfQuery( $sql, DB_WRITE, $fname );
345
346 $common = "{$this->ons},'{$this->odt}'," .
347 "'{$mt} \\\"{$this->nft}\\\"','" .
348 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
349 "','{$now}'";
350 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
351 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
352 "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
353 "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
354 wfQuery( $sql, DB_WRITE, $fname );
355 $this->newid = wfInsertId();
356
357 $sql = "UPDATE old SET " .
358 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
359 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
360 wfQuery( $sql, DB_WRITE, $fname );
361
362 $sql = "UPDATE recentchanges SET ".
363 "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
364 "rc_namespace={$this->ons} AND rc_title='{$this->odt}'";
365 wfQuery( $sql, DB_WRITE, $fname );
366
367 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
368 rc_comment,rc_user,rc_user_text,rc_timestamp,
369 rc_cur_time,rc_cur_id,rc_new)
370 VALUES ({$common},'{$now}',{$this->newid},1)";
371 wfQuery( $sql, DB_WRITE, $fname );
372
373 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
374 wfQuery( $sql, DB_WRITE, $fname );
375
376 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
377 wfQuery( $sql, DB_WRITE, $fname );
378
379 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
380 wfQuery( $sql, DB_WRITE, $fname );
381
382 # Non-existent target may have had broken links to it; these must
383 # now be removed and made into good links.
384 $update = new LinksUpdate( $this->oldid, $this->nft );
385 $update->fixBrokenLinks();
386
387 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
388 wfQuery( $sql, DB_WRITE, $fname );
389 }
390
391 function updateWatchlists()
392 {
393 $oldnamespace = $this->ons & ~1;
394 $newnamespace = $this->nns & ~1;
395 $oldtitle = $this->odt;
396 $newtitle = $this->ndt;
397
398 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
399 return;
400
401 $sql = "SELECT wl_user FROM watchlist
402 WHERE wl_namespace={$oldnamespace} AND wl_title='{$oldtitle}'";
403 $res = wfQuery( $sql, DB_READ, $fname );
404 if( $s = wfFetchObject( $res ) ) {
405 $sql = "REPLACE INTO watchlist (wl_user,wl_namespace,wl_title)
406 VALUES ({$s->wl_user},{$newnamespace},'{$newtitle}')";
407 while( $s = wfFetchObject( $res ) ) {
408 $sql .= ",({$s->wl_user},{$newnamespace},'{$newtitle}')";
409 }
410 wfQuery( $sql, DB_WRITE, $fname );
411 }
412 }
413
414 }
415 ?>