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