2 include_once( "LinksUpdate.php" );
4 function wfSpecialMovepage()
6 global $wgUser, $wgOut, $wgRequest, $action;
8 if ( 0 == $wgUser->getID() or $wgUser->isBlocked() ) {
9 $wgOut->errorpage( "movenologin", "movenologintext" );
13 $wgOut->readOnlyPage();
17 $f = new MovePageForm();
19 if ( "success" == $action ) { $f->showSuccess(); }
20 else if ( "submit" == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
21 else { $f->showForm( "" ); }
25 var $oldTitle, $newTitle; # Text input
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 MovePageForm() {
37 $this->oldTitle
= $wgRequest->getText( 'wpOldTitle', $wgRequest->getVal( 'target' ) );
38 $this->newTitle
= $wgRequest->getText( 'wpNewTitle' );
41 function showForm( $err )
43 global $wgOut, $wgUser, $wgLang;
45 $wgOut->setPagetitle( wfMsg( "movepage" ) );
47 if ( empty( $this->oldTitle
) ) {
48 $wgOut->errorpage( "notargettitle", "notargettext" );
52 $encOldTitle = htmlspecialchars( $this->oldTitle
);
53 $encNewTitle = htmlspecialchars( $this->newTitle
);
54 $ot = Title
::newFromURL( $this->oldTitle
);
55 $ott = $ot->getPrefixedText();
57 $wgOut->addWikiText( wfMsg( "movepagetext" ) );
58 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
59 $wgOut->addWikiText( "\n\n" . wfMsg( "movepagetalktext" ) );
62 $ma = wfMsg( "movearticle" );
63 $newt = wfMsg( "newtitle" );
64 $mpb = wfMsg( "movepagebtn" );
65 $movetalk = wfMsg( "movetalk" );
67 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Movepage" );
68 $action = $titleObj->escapeLocalURL( "action=submit" );
71 $wgOut->setSubtitle( wfMsg( "formerror" ) );
72 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
75 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
77 <td align=right>{$ma}:</td>
78 <td align=left><strong>{$ott}</strong></td>
80 <td align=right>{$newt}:</td>
82 <input type=text size=40 name=\"wpNewTitle\" value=\"{$encNewTitle}\">
83 <input type=hidden name=\"wpOldTitle\" value=\"{$encOldTitle}\">
87 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
91 <input type=checkbox name=\"wpMovetalk\" checked value=\"1\">
92 </td><td>{$movetalk}</td>
97 <td> </td><td align=left>
98 <input type=submit name=\"wpMove\" value=\"{$mpb}\">
106 global $wgOut, $wgUser, $wgLang;
107 global $wgDeferredUpdateList, $wgMessageCache;
109 $fname = "MovePageForm::doSubmit";
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" ) );
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();
126 $this->oldid
= $this->ot
->getArticleID();
127 $this->newid
= $this->nt
->getArticleID();
129 if ( strlen( trim( $this->ndt
) ) < 1 ) {
130 $this->showForm( wfMsg( "articleexists" ) );
133 if ( ( ! Namespace::isMovable( $this->ons
) ) ||
134 ( "" == $this->odt
) ||
135 ( "" != $this->ot
->getInterwiki() ) ||
136 ( !$this->ot
->userCanEdit() ) ||
138 ( ! Namespace::isMovable( $this->nns
) ) ||
139 ( "" == $this->ndt
) ||
140 ( "" != $this->nt
->getInterwiki() ) ||
141 ( !$this->nt
->userCanEdit() ) ||
142 ( $this->ons
== NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->odt
) ) ) {
143 $this->showForm( wfMsg( "badarticleerror" ) );
146 # The move is allowed only if (1) the target doesn't exist, or
147 # (2) the target is a redirect to the source, and has no history
148 # (so we can undo bad moves right after they're done).
150 if ( 0 != $this->newid
) { # Target exists; check for validity
151 if ( ! $this->isValidTarget() ) {
152 $this->showForm( wfMsg( "articleexists" ) );
155 $this->moveOverExistingRedirect();
156 } else { # Target didn't exist, do normal move.
157 $this->moveToNewTitle();
160 $this->updateWatchlists();
162 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
164 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
169 /* this needs to be done after LinksUpdate */
172 $this->nt
->getInternalURL(),
174 $this->ot
->getInternalURL(),
176 wfPurgeSquidServers($urlArr);
177 # purge pages linking to new title
178 $u = new SquidUpdate($this->nt
);
179 array_push( $wgDeferredUpdateList, $u );
180 # purge pages linking to old title
181 $u = new SquidUpdate($this->ot
);
182 array_push( $wgDeferredUpdateList, $u );
188 # (1) the checkbox says to,
189 # (2) the namespaces are not themselves talk namespaces, and of course
192 if ( ( 1 == $_REQUEST['wpMovetalk'] ) &&
193 ( ! Namespace::isTalk( $this->ons
) ) &&
194 ( ! Namespace::isTalk( $this->nns
) ) ) {
196 # get old talk page namespace
197 $this->ons
= Namespace::getTalk( $this->ons
);
198 # get new talk page namespace
199 $this->nns
= Namespace::getTalk( $this->nns
);
202 # grab the newer title objects
203 $this->ot
= Title
::makeTitle( $this->ons
, $this->ot
->getDBkey() );
204 $this->nt
= Title
::makeTitle( $this->nns
, $this->nt
->getDBkey() );
206 # odt, ndt, ofx, nfx remain the same
208 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
209 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
211 $this->oldid
= $this->ot
->getArticleID();
212 $this->newid
= $this->nt
->getArticleID();
214 if ( 0 != $this->oldid
) {
215 if ( 0 != $this->newid
) {
216 if ( $this->isValidTarget() ) {
217 $this->moveOverExistingRedirect();
218 $this->talkmoved
= 1;
220 $this->talkmoved
= 'invalid';
223 $this->moveToNewTitle();
224 $this->talkmoved
= 1;
226 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
228 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
233 /* this needs to be done after LinksUpdate */
236 $nt->getInternalURL(),
238 $ot->getInternalURL(),
240 wfPurgeSquidServers($urlArr);
241 # purge pages linking to new title
242 $u = new SquidUpdate($this->nt
);
243 array_push( $wgDeferredUpdateList, $u );
244 # purge pages linking to old title
245 $u = new SquidUpdate($this->ot
);
246 array_push( $wgDeferredUpdateList, $u );
252 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Movepage" );
253 $success = $titleObj->getFullURL(
254 "action=success&oldtitle=" . wfUrlencode( $this->ofx
) .
255 "&newtitle=" . wfUrlencode( $this->nfx
) .
256 "&talkmoved={$this->talkmoved}" );
258 $wgOut->redirect( $success );
261 function showSuccess()
263 global $wgOut, $wgUser;
265 $wgOut->setPagetitle( wfMsg( "movepage" ) );
266 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
268 $text = wfMsg( "pagemovedtext", $_REQUEST['oldtitle'], $_REQUEST['newtitle'] );
269 $wgOut->addWikiText( $text );
271 if ( 1 == $_REQUEST['talkmoved'] ) {
272 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
273 } elseif( 'invalid' == $_REQUEST['talkmoved'] ) {
274 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
276 $ot = Title
::newFromURL( $_REQUEST['oldtitle'] );
277 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
278 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
283 # Is the the existing target title valid?
285 function isValidTarget()
287 $fname = "MovePageForm::isValidTarget";
289 $sql = "SELECT cur_is_redirect,cur_text FROM cur " .
290 "WHERE cur_id={$this->newid}";
291 $res = wfQuery( $sql, DB_READ
, $fname );
292 $obj = wfFetchObject( $res );
294 if ( 0 == $obj->cur_is_redirect
) { return false; }
296 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text
, $m ) ) {
297 $rt = Title
::newFromText( $m[1] );
298 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
303 $sql = "SELECT old_id FROM old WHERE old_namespace={$this->nns} " .
304 "AND old_title='{$this->ndt}'";
305 $res = wfQuery( $sql, DB_READ
, $fname );
306 if ( 0 != wfNumRows( $res ) ) { return false; }
311 # Move page to title which is presently a redirect to the source
312 # page. Handling link tables here is tricky.
314 function moveOverExistingRedirect()
316 global $wgUser, $wgLinkCache;
317 $fname = "MovePageForm::moveOverExistingRedirect";
318 $mt = wfMsg( "movedto" );
320 # Change the name of the target page:
321 $now = wfTimestampNow();
322 $won = wfInvertTimestamp( $now );
323 $sql = "UPDATE cur SET cur_touched='{$now}'," .
324 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
325 "WHERE cur_id={$this->oldid}";
326 wfQuery( $sql, DB_WRITE
, $fname );
327 $wgLinkCache->clearLink( $this->nft
);
329 # Repurpose the old redirect. We don't save it to history since
330 # by definition if we've got here it's rather uninteresting.
331 $sql = "UPDATE cur SET cur_touched='{$now}',cur_timestamp='{$now}',inverse_timestamp='${won}'," .
332 "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
333 "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
334 "{$mt} \\\"{$this->nft}\\\"',cur_user='" . $wgUser->getID() .
335 "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
336 "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
337 "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
338 wfQuery( $sql, DB_WRITE
, $fname );
339 $wgLinkCache->clearLink( $this->oft
);
341 # Fix the redundant names for the past revisions of the target page.
342 # The redirect should have no old revisions.
343 $sql = "UPDATE old SET " .
344 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
345 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
346 wfQuery( $sql, DB_WRITE
, $fname );
348 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $mt );
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
354 # Reassign links to the old title to LIMBO
355 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
356 wfQuery( $sql, DB_WRITE
, $fname );
358 # Reassign links to the new title to the old title
359 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
360 wfQuery( $sql, DB_WRITE
, $fname );
362 # Reassign links from LIMBO to the new title. Ah, clear as mud!
363 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
364 wfQuery( $sql, DB_WRITE
, $fname );
366 # Note: the insert below must be after the updates above!
368 # Now, we record the link from the redirect to the new title.
369 # It should have no other outgoing links...
370 $sql = "DELETE FROM links WHERE l_from={$this->newid}";
371 wfQuery( $sql, DB_WRITE
, $fname );
372 $sql = "INSERT INTO links (l_from,l_to) VALUES ({$this->newid},{$this->oldid})";
373 wfQuery( $sql, DB_WRITE
, $fname );
376 # Move page to non-existing title.
378 function moveToNewTitle()
380 global $wgUser, $wgLinkCache;
381 $fname = "MovePageForm::moveToNewTitle";
382 $mt = wfMsg( "movedto" );
384 $now = wfTimestampNow();
385 $won = wfInvertTimestamp( $now );
386 $sql = "UPDATE cur SET cur_touched='{$now}'," .
387 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
388 "WHERE cur_id={$this->oldid}";
389 wfQuery( $sql, DB_WRITE
, $fname );
390 $wgLinkCache->clearLink( $this->nft
);
392 $comment = "{$mt} \"{$this->nft}\"";
393 $encComment = wfStrencode( $comment );
394 $common = "{$this->ons},'{$this->odt}'," .
395 "'$encComment','" .$wgUser->getID() . "','" .
396 wfStrencode( $wgUser->getName() ) ."','{$now}'";
397 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
398 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
399 "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
400 "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
401 wfQuery( $sql, DB_WRITE
, $fname );
402 $this->newid
= wfInsertId();
403 $wgLinkCache->clearLink( $this->oft
);
405 $sql = "UPDATE old SET " .
406 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
407 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
408 wfQuery( $sql, DB_WRITE
, $fname );
410 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $comment );
411 Article
::onArticleCreate( $this->nt
);
413 # Any text links to the old title must be reassigned to the redirect
414 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
415 wfQuery( $sql, DB_WRITE
, $fname );
417 # Record the just-created redirect's linking to the page
418 $sql = "INSERT INTO links (l_from,l_to) VALUES ({$this->newid},{$this->oldid})";
419 wfQuery( $sql, DB_WRITE
, $fname );
421 # Non-existent target may have had broken links to it; these must
422 # now be removed and made into good links.
423 $update = new LinksUpdate( $this->oldid
, $this->nft
);
424 $update->fixBrokenLinks();
427 function updateWatchlists()
429 $oldnamespace = $this->ons
& ~
1;
430 $newnamespace = $this->nns
& ~
1;
431 $oldtitle = $this->odt
;
432 $newtitle = $this->ndt
;
434 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
437 WatchedItem
::duplicateEntries( $this->ot
, $this->nt
);