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