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->showForm( wfMsg( "badarticleerror" ) );
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).
149 if ( 0 != $this->newid
) { # Target exists; check for validity
150 if ( ! $this->isValidTarget() ) {
151 $this->showForm( wfMsg( "articleexists" ) );
154 $this->moveOverExistingRedirect();
155 } else { # Target didn't exist, do normal move.
156 $this->moveToNewTitle();
159 $this->updateWatchlists();
161 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
163 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
167 # (1) the checkbox says to,
168 # (2) the namespaces are not themselves talk namespaces, and of course
171 if ( ( 1 == $_REQUEST['wpMovetalk'] ) &&
172 ( ! Namespace::isTalk( $this->ons
) ) &&
173 ( ! Namespace::isTalk( $this->nns
) ) ) {
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
);
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() );
185 # odt, ndt, ofx, nfx remain the same
187 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
188 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
190 $this->oldid
= $this->ot
->getArticleID();
191 $this->newid
= $this->nt
->getArticleID();
193 if ( 0 != $this->oldid
) {
194 if ( 0 != $this->newid
) {
195 if ( $this->isValidTarget() ) {
196 $this->moveOverExistingRedirect();
197 $this->talkmoved
= 1;
199 $this->talkmoved
= 'invalid';
202 $this->moveToNewTitle();
203 $this->talkmoved
= 1;
205 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
207 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
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}" );
217 $wgOut->redirect( $success );
220 function showSuccess()
222 global $wgOut, $wgUser;
224 $wgOut->setPagetitle( wfMsg( "movepage" ) );
225 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
227 $text = wfMsg( "pagemovedtext", $_REQUEST['oldtitle'], $_REQUEST['newtitle'] );
228 $wgOut->addWikiText( $text );
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>" );
235 $ot = Title
::newFromURL( $_REQUEST['oldtitle'] );
236 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
237 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
242 # Is the the existing target title valid?
244 function isValidTarget()
246 $fname = "MovePageForm::isValidTarget";
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 );
253 if ( 0 == $obj->cur_is_redirect
) { return false; }
255 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text
, $m ) ) {
256 $rt = Title
::newFromText( $m[1] );
257 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
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; }
270 # Move page to title which is presently a redirect to the source
271 # page. Handling link tables here is tricky.
273 function moveOverExistingRedirect()
275 global $wgUser, $wgLinkCache, $wgUseSquid;
276 $fname = "MovePageForm::moveOverExistingRedirect";
277 $mt = wfMsg( "movedto" );
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
);
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
);
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 );
307 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $mt );
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
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 );
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 );
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 );
325 # Note: the insert below must be after the updates above!
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 );
336 $urls = array_merge( $this->nt
->getSquidURLs(), $this->ot
->getSquidURLs() );
337 $u = new SquidUpdate( $urls );
342 # Move page to non-existing title.
344 function moveToNewTitle()
346 global $wgUser, $wgLinkCache, $wgUseSquid;
347 $fname = "MovePageForm::moveToNewTitle";
348 $mt = wfMsg( "movedto" );
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
);
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
);
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 );
376 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $comment );
377 Article
::onArticleCreate( $this->nt
);
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 );
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 );
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();
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();
396 $urls = $this->ot
->getSquidURLs();
397 foreach ( $titles as $linkTitle ) {
398 $urls[] = $linkTitle->getInternalURL();
400 $u = new SquidUpdate( $urls );
405 function updateWatchlists()
407 $oldnamespace = $this->ons
& ~
1;
408 $newnamespace = $this->nns
& ~
1;
409 $oldtitle = $this->odt
;
410 $newtitle = $this->ndt
;
412 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
415 WatchedItem
::duplicateEntries( $this->ot
, $this->nt
);