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