3 function wfSpecialMovepage()
5 global $wgUser, $wgOut, $action, $target;
7 if ( 0 == $wgUser->getID() or $wgUser->isBlocked() ) {
8 $wgOut->errorpage( "movenologin", "movenologintext" );
12 $wgOut->readOnlyPage();
15 $fields = array( "wpNewTitle", "wpOldTitle" );
16 wfCleanFormFields( $fields );
18 $f = new MovePageForm();
20 if ( "success" == $action ) { $f->showSuccess(); }
21 else if ( "submit" == $action ) { $f->doSubmit(); }
22 else { $f->showForm( "" ); }
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")
35 function showForm( $err )
37 global $wgOut, $wgUser, $wgLang;
38 global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target;
40 $wgOut->setPagetitle( wfMsg( "movepage" ) );
42 if ( ! $wpOldTitle ) {
43 $target = wfCleanQueryVar( $target );
44 if ( "" == $target ) {
45 $wgOut->errorpage( "notargettitle", "notargettext" );
48 $wpOldTitle = $target;
50 $ot = Title
::newFromURL( $wpOldTitle );
51 $ott = $ot->getPrefixedText();
53 $wgOut->addWikiText( wfMsg( "movepagetext" ) );
54 if ( ! Namespace::isTalk( $ot->getNamespace() ) )
55 $wgOut->addWikiText( "\n\n" . wfMsg( "movepagetalktext" ) );
57 $ma = wfMsg( "movearticle" );
58 $newt = wfMsg( "newtitle" );
59 $mpb = wfMsg( "movepagebtn" );
60 $movetalk = wfMsg( "movetalk" );
62 $action = wfLocalUrlE( $wgLang->specialPage( "Movepage" ),
66 $wgOut->setSubtitle( wfMsg( "formerror" ) );
67 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
70 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
72 <td align=right>{$ma}:</td>
73 <td align=left><strong>{$ott}</strong></td>
75 <td align=right>{$newt}:</td>
77 <input type=text size=40 name=\"wpNewTitle\" value=\"{$wpNewTitle}\">
78 <input type=hidden name=\"wpOldTitle\" value=\"{$wpOldTitle}\">
82 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
86 <input type=checkbox name=\"wpMovetalk\" checked value=\"1\">
87 </td><td>{$movetalk}</td>
92 <td> </td><td align=left>
93 <input type=submit name=\"wpMove\" value=\"{$mpb}\">
101 global $wgOut, $wgUser, $wgLang;
102 global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target;
103 global $wgDeferredUpdateList;
104 $fname = "MovePageForm::doSubmit";
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();
117 $this->oldid
= $this->ot
->getArticleID();
118 $this->newid
= $this->nt
->getArticleID();
120 if ( strlen( trim( $this->ndt
) ) < 2 ) {
121 $this->showForm( wfMsg( "articleexists" ) );
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" ) );
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).
137 if ( 0 != $this->newid
) { # Target exists; check for validity
138 if ( ! $this->isValidTarget() ) {
139 $this->showForm( wfMsg( "articleexists" ) );
142 $this->moveOverExistingRedirect();
143 } else { # Target didn't exist, do normal move.
144 $this->moveToNewTitle();
147 $this->updateWatchlists();
149 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
151 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
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.
158 if ( ( 1 == $wpMovetalk ) &&
159 ( ! Namespace::isTalk( $this->ons
) ) &&
160 ( $this->ons
== $this->nns
) ) {
162 $this->ons
= $this->nns
= Namespace::getTalk( $this->ons
);
164 $this->ot
= Title
::newFromText( Title
::makeName(
165 $this->ons
, $wpOldTitle ) );
166 $this->nt
= Title
::newFromText( Title
::makeName(
167 $this->nns
, $wpNewTitle ) );
169 # odt, ndt, ofx, nfx remain the same
171 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
172 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
174 $this->oldid
= $this->ot
->getArticleID();
175 $this->newid
= $this->nt
->getArticleID();
177 if ( 0 != $this->oldid
) {
178 if ( 0 != $this->newid
) {
179 if ( $this->isValidTarget() ) {
180 $this->moveOverExistingRedirect();
181 $this->talkmoved
= 1;
183 $this->talkmoved
= 'invalid';
186 $this->moveToNewTitle();
187 $this->talkmoved
= 1;
189 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
191 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
195 $success = wfLocalUrl( $wgLang->specialPage( "Movepage" ),
196 "action=success&oldtitle=" . wfUrlencode( $this->ofx
) .
197 "&newtitle=" . wfUrlencode( $this->nfx
) .
198 "&talkmoved={$this->talkmoved}" );
200 $wgOut->redirect( $success );
203 function showSuccess()
205 global $wgOut, $wgUser;
206 global $newtitle, $oldtitle, $talkmoved;
208 $wgOut->setPagetitle( wfMsg( "movepage" ) );
209 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
211 $fields = array( "oldtitle", "newtitle" );
212 wfCleanFormFields( $fields );
214 $text = str_replace( "$1", $oldtitle, wfMsg( "pagemovedtext" ) );
215 $text = str_replace( "$2", $newtitle, $text );
216 $wgOut->addWikiText( $text );
218 if ( 1 == $talkmoved ) {
219 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
220 } elseif( 'invalid' == $talkmoved ) {
221 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
223 $ot = Title
::newFromURL( $oldtitle );
224 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
225 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
230 # Is the the existing target title valid?
232 function isValidTarget()
234 $fname = "MovePageForm::isValidTarget";
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 );
241 if ( 0 == $obj->cur_is_redirect
) { return false; }
243 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text
, $m ) ) {
244 $rt = Title
::newFromText( $m[1] );
245 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
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; }
258 # Move page to title which is presently a redirect to the source
259 # page. Handling link tables here is tricky.
261 function moveOverExistingRedirect()
264 $fname = "MovePageForm::moveOverExistingRedirect";
265 $mt = wfMsg( "movedto" );
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 );
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 );
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 );
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 );
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 );
301 # The only link from here should be the old redirect
303 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
304 wfQuery( $sql, $fname );
306 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
307 wfQuery( $sql, $fname );
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
312 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
313 wfQuery( $sql, $fname );
315 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
316 wfQuery( $sql, $fname );
318 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
319 wfQuery( $sql, $fname );
321 # Note: the insert below must be after the updates above!
323 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
324 wfQuery( $sql, $fname );
326 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
327 wfQuery( $sql, $fname );
330 # Move page to non-existing title.
332 function moveToNewTitle()
335 $fname = "MovePageForm::moveToNewTitle";
336 $mt = wfMsg( "movedto" );
338 $sql = "UPDATE cur SET cur_touched='{$now}'," .
339 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
340 "WHERE cur_id={$this->oldid}";
341 wfQuery( $sql, $fname );
343 $now = wfTimestampNow();
344 $won = wfInvertTimestamp( $now );
345 $common = "{$this->ons},'{$this->odt}'," .
346 "'{$mt} \\\"{$this->nft}\\\"','" .
347 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
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();
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 );
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 );
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 );
372 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
373 wfQuery( $sql, $fname );
375 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
376 wfQuery( $sql, $fname );
378 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
379 wfQuery( $sql, $fname );
381 # Non-existent target may have had broken links to it; these must
382 # now be removed and made into good links.
384 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->nft}'";
385 $res = wfQuery( $sql, $fname );
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 );
393 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->nft}'";
394 wfQuery( $sql, $fname );
396 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
397 wfQuery( $sql, $fname );
400 function updateWatchlists()
402 $oldnamespace = $this->ons
& ~
1;
403 $newnamespace = $this->nns
& ~
1;
404 $oldtitle = $this->odt
;
405 $newtitle = $this->ndt
;
407 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
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}')";
419 wfQuery( $sql, $fname );