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 $fname = "MovePageForm::doSubmit";
107 $this->ot
= Title
::newFromText( $wpOldTitle );
108 $this->nt
= Title
::newFromText( $wpNewTitle );
109 if( !$this->ot
or !$this->nt
) {
110 $this->showForm( wfMsg( "badtitletext" ) );
113 $this->ons
= $this->ot
->getNamespace();
114 $this->nns
= $this->nt
->getNamespace();
115 $this->odt
= wfStrencode( $this->ot
->getDBkey() );
116 $this->ndt
= wfStrencode( $this->nt
->getDBkey() );
117 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
118 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
119 $this->ofx
= $this->ot
->getPrefixedText();
120 $this->nfx
= $this->nt
->getPrefixedText();
122 $this->oldid
= $this->ot
->getArticleID();
123 $this->newid
= $this->nt
->getArticleID();
125 if ( strlen( trim( $this->ndt
) ) < 1 ) {
126 $this->showForm( wfMsg( "articleexists" ) );
129 if ( ( ! Namespace::isMovable( $this->ons
) ) ||
130 ( "" == $this->odt
) ||
131 ( "" != $this->ot
->getInterwiki() ) ||
132 ( !$this->ot
->userCanEdit() ) ||
134 ( ! Namespace::isMovable( $nns ) ) ||
135 ( "" == $this->ndt
) ||
136 ( "" != $this->nt
->getInterwiki() ) ||
137 ( !$this->nt
->userCanEdit() ) ||
138 ( $this->ons
== NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->odt
) ) ) {
139 $this->showForm( wfMsg( "badarticleerror" ) );
142 # The move is allowed only if (1) the target doesn't exist, or
143 # (2) the target is a redirect to the source, and has no history
144 # (so we can undo bad moves right after they're done).
146 if ( 0 != $this->newid
) { # Target exists; check for validity
147 if ( ! $this->isValidTarget() ) {
148 $this->showForm( wfMsg( "articleexists" ) );
151 $this->moveOverExistingRedirect();
152 } else { # Target didn't exist, do normal move.
153 $this->moveToNewTitle();
156 $this->updateWatchlists();
158 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
160 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
163 # Move talk page if (1) the checkbox says to, (2) the source
164 # and target namespaces are identical, (3) the namespaces are not
165 # themselves talk namespaces, and of course (4) it exists.
167 if ( ( 1 == $wpMovetalk ) &&
168 ( ! Namespace::isTalk( $this->ons
) ) &&
169 ( $this->ons
== $this->nns
) ) {
171 $this->ons
= $this->nns
= Namespace::getTalk( $this->ons
);
172 $this->ot
= Title
::makeTitle( $this->ons
, $this->ot
->getDBkey() );
173 $this->nt
= Title
::makeTitle( $this->nns
, $this->nt
->getDBkey() );
175 # odt, ndt, ofx, nfx remain the same
177 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
178 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
180 $this->oldid
= $this->ot
->getArticleID();
181 $this->newid
= $this->nt
->getArticleID();
183 if ( 0 != $this->oldid
) {
184 if ( 0 != $this->newid
) {
185 if ( $this->isValidTarget() ) {
186 $this->moveOverExistingRedirect();
187 $this->talkmoved
= 1;
189 $this->talkmoved
= 'invalid';
192 $this->moveToNewTitle();
193 $this->talkmoved
= 1;
195 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
197 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
201 $success = wfLocalUrl( $wgLang->specialPage( "Movepage" ),
202 "action=success&oldtitle=" . wfUrlencode( $this->ofx
) .
203 "&newtitle=" . wfUrlencode( $this->nfx
) .
204 "&talkmoved={$this->talkmoved}" );
206 $wgOut->redirect( $success );
209 function showSuccess()
211 global $wgOut, $wgUser;
212 global $newtitle, $oldtitle, $talkmoved;
214 $wgOut->setPagetitle( wfMsg( "movepage" ) );
215 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
217 $fields = array( "oldtitle", "newtitle" );
218 wfCleanFormFields( $fields );
220 $text = wfMsg( "pagemovedtext", $oldtitle, $newtitle );
221 $wgOut->addWikiText( $text );
223 if ( 1 == $talkmoved ) {
224 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
225 } elseif( 'invalid' == $talkmoved ) {
226 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
228 $ot = Title
::newFromURL( $oldtitle );
229 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
230 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
235 # Is the the existing target title valid?
237 function isValidTarget()
239 $fname = "MovePageForm::isValidTarget";
241 $sql = "SELECT cur_is_redirect,cur_text FROM cur " .
242 "WHERE cur_id={$this->newid}";
243 $res = wfQuery( $sql, DB_READ
, $fname );
244 $obj = wfFetchObject( $res );
246 if ( 0 == $obj->cur_is_redirect
) { return false; }
248 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text
, $m ) ) {
249 $rt = Title
::newFromText( $m[1] );
250 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
255 $sql = "SELECT old_id FROM old WHERE old_namespace={$this->nns} " .
256 "AND old_title='{$this->ndt}'";
257 $res = wfQuery( $sql, DB_READ
, $fname );
258 if ( 0 != wfNumRows( $res ) ) { return false; }
263 # Move page to title which is presently a redirect to the source
264 # page. Handling link tables here is tricky.
266 function moveOverExistingRedirect()
268 global $wgUser, $wgLinkCache;
269 $fname = "MovePageForm::moveOverExistingRedirect";
270 $mt = wfMsg( "movedto" );
272 # Change the name of the target page:
273 $now = wfTimestampNow();
274 $sql = "UPDATE cur SET cur_touched='{$now}'," .
275 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
276 "WHERE cur_id={$this->oldid}";
277 wfQuery( $sql, DB_WRITE
, $fname );
278 $wgLinkCache->clearLink( $this->nft
);
280 # Repurpose the old redirect. We don't save it to history since
281 # by definition if we've got here it's rather uninteresting.
282 $sql = "UPDATE cur SET cur_touched='{$now}',cur_timestamp='{$now}'," .
283 "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
284 "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
285 "{$mt} \\\"{$this->nft}\\\"',cur_user='" . $wgUser->getID() .
286 "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
287 "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
288 "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
289 wfQuery( $sql, DB_WRITE
, $fname );
290 $wgLinkCache->clearLink( $this->oft
);
292 # Fix the redundant names for the past revisions of the target page.
293 # The redirect should have no old revisions.
294 $sql = "UPDATE old SET " .
295 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
296 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
297 wfQuery( $sql, DB_WRITE
, $fname );
299 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $mt );
301 # The only link from here should be the old redirect
303 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
304 wfQuery( $sql, DB_WRITE
, $fname );
306 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
307 wfQuery( $sql, DB_WRITE
, $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, DB_WRITE
, $fname );
315 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
316 wfQuery( $sql, DB_WRITE
, $fname );
318 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
319 wfQuery( $sql, DB_WRITE
, $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, DB_WRITE
, $fname );
326 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
327 wfQuery( $sql, DB_WRITE
, $fname );
330 # Move page to non-existing title.
332 function moveToNewTitle()
334 global $wgUser, $wgLinkCache;
335 $fname = "MovePageForm::moveToNewTitle";
336 $mt = wfMsg( "movedto" );
338 $now = wfTimestampNow();
339 $won = wfInvertTimestamp( $now );
340 $sql = "UPDATE cur SET cur_touched='{$now}'," .
341 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
342 "WHERE cur_id={$this->oldid}";
343 wfQuery( $sql, DB_WRITE
, $fname );
344 $wgLinkCache->clearLink( $this->nft
);
346 $comment = "{$mt} \"{$this->nft}\"";
347 $encComment = wfStrencode( $comment );
348 $common = "{$this->ons},'{$this->odt}'," .
349 "'$encComment','" .$wgUser->getID() . "','" .
350 wfStrencode( $wgUser->getName() ) ."','{$now}'";
351 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
352 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
353 "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
354 "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
355 wfQuery( $sql, DB_WRITE
, $fname );
356 $this->newid
= wfInsertId();
357 $wgLinkCache->clearLink( $this->oft
);
359 $sql = "UPDATE old SET " .
360 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
361 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
362 wfQuery( $sql, DB_WRITE
, $fname );
364 RecentChange
::notifyMove( $now, $this->ot
, $this->nt
, $wgUser, $comment );
365 Article
::onArticleCreate( $this->nt
);
367 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
368 wfQuery( $sql, DB_WRITE
, $fname );
370 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
371 wfQuery( $sql, DB_WRITE
, $fname );
373 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
374 wfQuery( $sql, DB_WRITE
, $fname );
376 # Non-existent target may have had broken links to it; these must
377 # now be removed and made into good links.
378 $update = new LinksUpdate( $this->oldid
, $this->nft
);
379 $update->fixBrokenLinks();
381 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
382 wfQuery( $sql, DB_WRITE
, $fname );
385 function updateWatchlists()
387 $oldnamespace = $this->ons
& ~
1;
388 $newnamespace = $this->nns
& ~
1;
389 $oldtitle = $this->odt
;
390 $newtitle = $this->ndt
;
392 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
395 WatchedItem
::duplicateEntries( $this->ot
, $this->nt
);