2 include_once( "LinksUpdate.php" );
4 function wfSpecialMovepage()
6 global $wgUser, $wgOut, $action, $target;
8 if ( 0 == $wgUser->getID() or $wgUser->isBlocked() ) {
9 $wgOut->errorpage( "movenologin", "movenologintext" );
13 $wgOut->readOnlyPage();
16 $fields = array( "wpNewTitle", "wpOldTitle" );
17 wfCleanFormFields( $fields );
19 $f = new MovePageForm();
21 if ( "success" == $action ) { $f->showSuccess(); }
22 else if ( "submit" == $action ) { $f->doSubmit(); }
23 else { $f->showForm( "" ); }
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")
36 function showForm( $err )
38 global $wgOut, $wgUser, $wgLang;
39 global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target;
41 $wgOut->setPagetitle( wfMsg( "movepage" ) );
43 if ( ! $wpOldTitle ) {
44 $target = wfCleanQueryVar( $target );
45 if ( "" == $target ) {
46 $wgOut->errorpage( "notargettitle", "notargettext" );
49 $wpOldTitle = $target;
51 $ot = Title
::newFromURL( $wpOldTitle );
52 $ott = $ot->getPrefixedText();
54 $wgOut->addWikiText( wfMsg( "movepagetext" ) );
55 if ( ! Namespace::isTalk( $ot->getNamespace() ) )
56 $wgOut->addWikiText( "\n\n" . wfMsg( "movepagetalktext" ) );
58 $ma = wfMsg( "movearticle" );
59 $newt = wfMsg( "newtitle" );
60 $mpb = wfMsg( "movepagebtn" );
61 $movetalk = wfMsg( "movetalk" );
63 $action = wfLocalUrlE( $wgLang->specialPage( "Movepage" ),
67 $wgOut->setSubtitle( wfMsg( "formerror" ) );
68 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
71 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
73 <td align=right>{$ma}:</td>
74 <td align=left><strong>{$ott}</strong></td>
76 <td align=right>{$newt}:</td>
78 <input type=text size=40 name=\"wpNewTitle\" value=\"{$wpNewTitle}\">
79 <input type=hidden name=\"wpOldTitle\" value=\"{$wpOldTitle}\">
83 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
87 <input type=checkbox name=\"wpMovetalk\" checked value=\"1\">
88 </td><td>{$movetalk}</td>
93 <td> </td><td align=left>
94 <input type=submit name=\"wpMove\" value=\"{$mpb}\">
102 global $wgOut, $wgUser, $wgLang;
103 global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target;
104 global $wgDeferredUpdateList, $wgMessageCache;
105 global $wgUseSquid, $wgInternalServer;
106 $fname = "MovePageForm::doSubmit";
108 $this->ot
= Title
::newFromText( $wpOldTitle );
109 $this->nt
= Title
::newFromText( $wpNewTitle );
110 if( !$this->ot
or !$this->nt
) {
111 $this->showForm( wfMsg( "badtitletext" ) );
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();
123 $this->oldid
= $this->ot
->getArticleID();
124 $this->newid
= $this->nt
->getArticleID();
126 if ( strlen( trim( $this->ndt
) ) < 1 ) {
127 $this->showForm( wfMsg( "articleexists" ) );
130 if ( ( ! Namespace::isMovable( $this->ons
) ) ||
131 ( "" == $this->odt
) ||
132 ( "" != $this->ot
->getInterwiki() ) ||
133 ( !$this->ot
->userCanEdit() ) ||
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" ) );
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).
147 if ( 0 != $this->newid
) { # Target exists; check for validity
148 if ( ! $this->isValidTarget() ) {
149 $this->showForm( wfMsg( "articleexists" ) );
152 $this->moveOverExistingRedirect();
153 } else { # Target didn't exist, do normal move.
154 $this->moveToNewTitle();
157 $this->updateWatchlists();
159 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
161 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
166 /* this needs to be done after LinksUpdate */
169 $wgInternalServer.wfLocalUrl( $this->nt
->getPrefixedURL()),
171 $wgInternalServer.wfLocalUrl( $this->ot
->getPrefixedURL())
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 );
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.
188 if ( ( 1 == $wpMovetalk ) &&
189 ( ! Namespace::isTalk( $this->ons
) ) &&
190 ( $this->ons
== $this->nns
) ) {
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() );
196 # odt, ndt, ofx, nfx remain the same
198 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
199 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
201 $this->oldid
= $this->ot
->getArticleID();
202 $this->newid
= $this->nt
->getArticleID();
204 if ( 0 != $this->oldid
) {
205 if ( 0 != $this->newid
) {
206 if ( $this->isValidTarget() ) {
207 $this->moveOverExistingRedirect();
208 $this->talkmoved
= 1;
210 $this->talkmoved
= 'invalid';
213 $this->moveToNewTitle();
214 $this->talkmoved
= 1;
216 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
218 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
223 /* this needs to be done after LinksUpdate */
226 $wgInternalServer.wfLocalUrl( $this->nt
->getPrefixedURL()),
228 $wgInternalServer.wfLocalUrl( $this->ot
->getPrefixedURL())
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 );
242 $success = wfLocalUrl( $wgLang->specialPage( "Movepage" ),
243 "action=success&oldtitle=" . wfUrlencode( $this->ofx
) .
244 "&newtitle=" . wfUrlencode( $this->nfx
) .
245 "&talkmoved={$this->talkmoved}" );
247 $wgOut->redirect( $success );
250 function showSuccess()
252 global $wgOut, $wgUser;
253 global $newtitle, $oldtitle, $talkmoved;
255 $wgOut->setPagetitle( wfMsg( "movepage" ) );
256 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
258 $fields = array( "oldtitle", "newtitle" );
259 wfCleanFormFields( $fields );
261 $text = wfMsg( "pagemovedtext", $oldtitle, $newtitle );
262 $wgOut->addWikiText( $text );
264 if ( 1 == $talkmoved ) {
265 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
266 } elseif( 'invalid' == $talkmoved ) {
267 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
269 $ot = Title
::newFromURL( $oldtitle );
270 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
271 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
276 # Is the the existing target title valid?
278 function isValidTarget()
280 $fname = "MovePageForm::isValidTarget";
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 );
287 if ( 0 == $obj->cur_is_redirect
) { return false; }
289 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text
, $m ) ) {
290 $rt = Title
::newFromText( $m[1] );
291 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
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; }
304 # Move page to title which is presently a redirect to the source
305 # page. Handling link tables here is tricky.
307 function moveOverExistingRedirect()
309 global $wgUser, $wgLinkCache;
310 $fname = "MovePageForm::moveOverExistingRedirect";
311 $mt = wfMsg( "movedto" );
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
);
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
);
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 );
340 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $mt );
342 # The only link from here should be the old redirect
344 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
345 wfQuery( $sql, DB_WRITE
, $fname );
347 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
348 wfQuery( $sql, DB_WRITE
, $fname );
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
353 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
354 wfQuery( $sql, DB_WRITE
, $fname );
356 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
357 wfQuery( $sql, DB_WRITE
, $fname );
359 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
360 wfQuery( $sql, DB_WRITE
, $fname );
362 # Note: the insert below must be after the updates above!
364 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
365 wfQuery( $sql, DB_WRITE
, $fname );
367 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
368 wfQuery( $sql, DB_WRITE
, $fname );
371 # Move page to non-existing title.
373 function moveToNewTitle()
375 global $wgUser, $wgLinkCache;
376 $fname = "MovePageForm::moveToNewTitle";
377 $mt = wfMsg( "movedto" );
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
);
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
);
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 );
405 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $comment );
406 Article
::onArticleCreate( $this->nt
);
408 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
409 wfQuery( $sql, DB_WRITE
, $fname );
411 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
412 wfQuery( $sql, DB_WRITE
, $fname );
414 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
415 wfQuery( $sql, DB_WRITE
, $fname );
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();
422 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
423 wfQuery( $sql, DB_WRITE
, $fname );
426 function updateWatchlists()
428 $oldnamespace = $this->ons
& ~
1;
429 $newnamespace = $this->nns
& ~
1;
430 $oldtitle = $this->odt
;
431 $newtitle = $this->ndt
;
433 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
436 WatchedItem
::duplicateEntries( $this->ot
, $this->nt
);