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;
108 global $wgUseSquid, $wgInternalServer;
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 $wgInternalServer.$this->nt
->getLocalURL(),
174 $wgInternalServer.$this->ot
->getLocalURL(),
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 );
187 # Move talk page if (1) the checkbox says to, (2) the source
188 # and target namespaces are identical, (3) the namespaces are not
189 # themselves talk namespaces, and of course (4) it exists.
191 if ( ( 1 == $_REQUEST['wpMovetalk'] ) &&
192 ( ! Namespace::isTalk( $this->ons
) ) &&
193 ( $this->ons
== $this->nns
) ) {
195 $this->ons
= $this->nns
= Namespace::getTalk( $this->ons
);
196 $this->ot
= Title
::makeTitle( $this->ons
, $this->ot
->getDBkey() );
197 $this->nt
= Title
::makeTitle( $this->nns
, $this->nt
->getDBkey() );
199 # odt, ndt, ofx, nfx remain the same
201 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
202 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
204 $this->oldid
= $this->ot
->getArticleID();
205 $this->newid
= $this->nt
->getArticleID();
207 if ( 0 != $this->oldid
) {
208 if ( 0 != $this->newid
) {
209 if ( $this->isValidTarget() ) {
210 $this->moveOverExistingRedirect();
211 $this->talkmoved
= 1;
213 $this->talkmoved
= 'invalid';
216 $this->moveToNewTitle();
217 $this->talkmoved
= 1;
219 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
221 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
226 /* this needs to be done after LinksUpdate */
229 $nt->getInternalURL(),
231 $ot->getInternalURL(),
233 wfPurgeSquidServers($urlArr);
234 # purge pages linking to new title
235 $u = new SquidUpdate($this->nt
);
236 array_push( $wgDeferredUpdateList, $u );
237 # purge pages linking to old title
238 $u = new SquidUpdate($this->ot
);
239 array_push( $wgDeferredUpdateList, $u );
245 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Movepage" );
246 $success = $titleObj->getFullURL(
247 "action=success&oldtitle=" . wfUrlencode( $this->ofx
) .
248 "&newtitle=" . wfUrlencode( $this->nfx
) .
249 "&talkmoved={$this->talkmoved}" );
251 $wgOut->redirect( $success );
254 function showSuccess()
256 global $wgOut, $wgUser;
258 $wgOut->setPagetitle( wfMsg( "movepage" ) );
259 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
261 $text = wfMsg( "pagemovedtext", $_REQUEST['oldtitle'], $_REQUEST['newtitle'] );
262 $wgOut->addWikiText( $text );
264 if ( 1 == $_REQUEST['talkmoved'] ) {
265 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
266 } elseif( 'invalid' == $_REQUEST['talkmoved'] ) {
267 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
269 $ot = Title
::newFromURL( $_REQUEST['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 $won = wfInvertTimestamp( $now );
316 $sql = "UPDATE cur SET cur_touched='{$now}'," .
317 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
318 "WHERE cur_id={$this->oldid}";
319 wfQuery( $sql, DB_WRITE
, $fname );
320 $wgLinkCache->clearLink( $this->nft
);
322 # Repurpose the old redirect. We don't save it to history since
323 # by definition if we've got here it's rather uninteresting.
324 $sql = "UPDATE cur SET cur_touched='{$now}',cur_timestamp='{$now}',inverse_timestamp='${won}'," .
325 "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
326 "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
327 "{$mt} \\\"{$this->nft}\\\"',cur_user='" . $wgUser->getID() .
328 "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
329 "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
330 "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
331 wfQuery( $sql, DB_WRITE
, $fname );
332 $wgLinkCache->clearLink( $this->oft
);
334 # Fix the redundant names for the past revisions of the target page.
335 # The redirect should have no old revisions.
336 $sql = "UPDATE old SET " .
337 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
338 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
339 wfQuery( $sql, DB_WRITE
, $fname );
341 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $mt );
343 # The only link from here should be the old redirect
345 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
346 wfQuery( $sql, DB_WRITE
, $fname );
348 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
349 wfQuery( $sql, DB_WRITE
, $fname );
351 # Swap links. Using MAXINT as a temp; if there's ever an article
352 # with id 4294967295, this will fail, but I think that's pretty safe
354 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
355 wfQuery( $sql, DB_WRITE
, $fname );
357 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
358 wfQuery( $sql, DB_WRITE
, $fname );
360 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
361 wfQuery( $sql, DB_WRITE
, $fname );
363 # Note: the insert below must be after the updates above!
365 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
366 wfQuery( $sql, DB_WRITE
, $fname );
368 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
369 wfQuery( $sql, DB_WRITE
, $fname );
372 # Move page to non-existing title.
374 function moveToNewTitle()
376 global $wgUser, $wgLinkCache;
377 $fname = "MovePageForm::moveToNewTitle";
378 $mt = wfMsg( "movedto" );
380 $now = wfTimestampNow();
381 $won = wfInvertTimestamp( $now );
382 $sql = "UPDATE cur SET cur_touched='{$now}'," .
383 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
384 "WHERE cur_id={$this->oldid}";
385 wfQuery( $sql, DB_WRITE
, $fname );
386 $wgLinkCache->clearLink( $this->nft
);
388 $comment = "{$mt} \"{$this->nft}\"";
389 $encComment = wfStrencode( $comment );
390 $common = "{$this->ons},'{$this->odt}'," .
391 "'$encComment','" .$wgUser->getID() . "','" .
392 wfStrencode( $wgUser->getName() ) ."','{$now}'";
393 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
394 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
395 "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
396 "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
397 wfQuery( $sql, DB_WRITE
, $fname );
398 $this->newid
= wfInsertId();
399 $wgLinkCache->clearLink( $this->oft
);
401 $sql = "UPDATE old SET " .
402 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
403 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
404 wfQuery( $sql, DB_WRITE
, $fname );
406 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $comment );
407 Article
::onArticleCreate( $this->nt
);
409 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
410 wfQuery( $sql, DB_WRITE
, $fname );
412 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
413 wfQuery( $sql, DB_WRITE
, $fname );
415 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
416 wfQuery( $sql, DB_WRITE
, $fname );
418 # Non-existent target may have had broken links to it; these must
419 # now be removed and made into good links.
420 $update = new LinksUpdate( $this->oldid
, $this->nft
);
421 $update->fixBrokenLinks();
423 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
424 wfQuery( $sql, DB_WRITE
, $fname );
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
);