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;
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->showForm( wfMsg( "badarticleerror" ) );
141 # The move is allowed only if (1) the target doesn't exist, or
142 # (2) the target is a redirect to the source, and has no history
143 # (so we can undo bad moves right after they're done).
145 if ( 0 != $this->newid
) { # Target exists; check for validity
146 if ( ! $this->isValidTarget() ) {
147 $this->showForm( wfMsg( "articleexists" ) );
150 $this->moveOverExistingRedirect();
151 } else { # Target didn't exist, do normal move.
152 $this->moveToNewTitle();
155 $this->updateWatchlists();
157 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
159 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
162 # Move talk page if (1) the checkbox says to, (2) the source
163 # and target namespaces are identical, (3) the namespaces are not
164 # themselves talk namespaces, and of course (4) it exists.
166 if ( ( 1 == $wpMovetalk ) &&
167 ( ! Namespace::isTalk( $this->ons
) ) &&
168 ( $this->ons
== $this->nns
) ) {
170 $this->ons
= $this->nns
= Namespace::getTalk( $this->ons
);
172 $this->ot
= Title
::newFromText( Title
::makeName(
173 $this->ons
, $wpOldTitle ) );
174 $this->nt
= Title
::newFromText( Title
::makeName(
175 $this->nns
, $wpNewTitle ) );
177 # odt, ndt, ofx, nfx remain the same
179 $this->oft
= wfStrencode( $this->ot
->getPrefixedDBkey() );
180 $this->nft
= wfStrencode( $this->nt
->getPrefixedDBkey() );
182 $this->oldid
= $this->ot
->getArticleID();
183 $this->newid
= $this->nt
->getArticleID();
185 if ( 0 != $this->oldid
) {
186 if ( 0 != $this->newid
) {
187 if ( $this->isValidTarget() ) {
188 $this->moveOverExistingRedirect();
189 $this->talkmoved
= 1;
191 $this->talkmoved
= 'invalid';
194 $this->moveToNewTitle();
195 $this->talkmoved
= 1;
197 $u = new SearchUpdate( $this->oldid
, $this->nt
->getPrefixedDBkey() );
199 $u = new SearchUpdate( $this->newid
, $this->ot
->getPrefixedDBkey(), "" );
203 $success = wfLocalUrl( $wgLang->specialPage( "Movepage" ),
204 "action=success&oldtitle=" . wfUrlencode( $this->ofx
) .
205 "&newtitle=" . wfUrlencode( $this->nfx
) .
206 "&talkmoved={$this->talkmoved}" );
208 $wgOut->redirect( $success );
211 function showSuccess()
213 global $wgOut, $wgUser;
214 global $newtitle, $oldtitle, $talkmoved;
216 $wgOut->setPagetitle( wfMsg( "movepage" ) );
217 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
219 $fields = array( "oldtitle", "newtitle" );
220 wfCleanFormFields( $fields );
222 $text = wfMsg( "pagemovedtext", $oldtitle, $newtitle );
223 $wgOut->addWikiText( $text );
225 if ( 1 == $talkmoved ) {
226 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
227 } elseif( 'invalid' == $talkmoved ) {
228 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
230 $ot = Title
::newFromURL( $oldtitle );
231 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
232 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
237 # Is the the existing target title valid?
239 function isValidTarget()
241 $fname = "MovePageForm::isValidTarget";
243 $sql = "SELECT cur_is_redirect,cur_text FROM cur " .
244 "WHERE cur_id={$this->newid}";
245 $res = wfQuery( $sql, DB_READ
, $fname );
246 $obj = wfFetchObject( $res );
248 if ( 0 == $obj->cur_is_redirect
) { return false; }
250 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text
, $m ) ) {
251 $rt = Title
::newFromText( $m[1] );
252 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
257 $sql = "SELECT old_id FROM old WHERE old_namespace={$this->nns} " .
258 "AND old_title='{$this->ndt}'";
259 $res = wfQuery( $sql, DB_READ
, $fname );
260 if ( 0 != wfNumRows( $res ) ) { return false; }
265 # Move page to title which is presently a redirect to the source
266 # page. Handling link tables here is tricky.
268 function moveOverExistingRedirect()
270 global $wgUser, $wgLinkCache;
271 $fname = "MovePageForm::moveOverExistingRedirect";
272 $mt = wfMsg( "movedto" );
274 $now = wfTimestampNow();
275 $sql = "UPDATE cur SET cur_touched='{$now}'," .
276 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
277 "WHERE cur_id={$this->oldid}";
278 wfQuery( $sql, DB_WRITE
, $fname );
279 $wgLinkCache->clearLink( $this->nft
);
281 $sql = "UPDATE cur SET cur_touched='{$now}'," .
282 "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
283 "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
284 "{$mt} \\\"{$this->nft}\\\"',cur_user='" . $wgUser->getID() .
285 "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
286 "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
287 "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
288 wfQuery( $sql, DB_WRITE
, $fname );
289 $wgLinkCache->clearLink( $this->oft
);
291 $sql = "UPDATE old SET " .
292 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
293 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
294 wfQuery( $sql, DB_WRITE
, $fname );
296 $sql = "UPDATE recentchanges SET ".
297 "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
298 "rc_cur_id={$this->oldid}";
299 wfQuery( $sql, DB_WRITE
, $fname );
301 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
302 rc_comment,rc_user,rc_user_text,rc_timestamp,
303 rc_cur_time,rc_cur_id,rc_new)
304 VALUES ({$this->ons},'{$this->odt}'," .
305 "'{$mt} \\\"{$this->nft}\\\"','" .
306 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
307 "','{$now}','{$now}',{$this->newid},1)";
308 wfQuery( $sql, DB_WRITE
, $fname );
310 # The only link from here should be the old redirect
312 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
313 wfQuery( $sql, DB_WRITE
, $fname );
315 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
316 wfQuery( $sql, DB_WRITE
, $fname );
318 # Swap links. Using MAXINT as a temp; if there's ever an article
319 # with id 4294967295, this will fail, but I think that's pretty safe
321 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
322 wfQuery( $sql, DB_WRITE
, $fname );
324 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
325 wfQuery( $sql, DB_WRITE
, $fname );
327 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
328 wfQuery( $sql, DB_WRITE
, $fname );
330 # Note: the insert below must be after the updates above!
332 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
333 wfQuery( $sql, DB_WRITE
, $fname );
335 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
336 wfQuery( $sql, DB_WRITE
, $fname );
339 # Move page to non-existing title.
341 function moveToNewTitle()
343 global $wgUser, $wgLinkCache;
344 $fname = "MovePageForm::moveToNewTitle";
345 $mt = wfMsg( "movedto" );
347 $now = wfTimestampNow();
348 $won = wfInvertTimestamp( $now );
349 $sql = "UPDATE cur SET cur_touched='{$now}'," .
350 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
351 "WHERE cur_id={$this->oldid}";
352 wfQuery( $sql, DB_WRITE
, $fname );
353 $wgLinkCache->clearLink( $this->nft
);
355 $common = "{$this->ons},'{$this->odt}'," .
356 "'{$mt} \\\"{$this->nft}\\\"','" .
357 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
359 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
360 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
361 "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
362 "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
363 wfQuery( $sql, DB_WRITE
, $fname );
364 $this->newid
= wfInsertId();
365 $wgLinkCache->clearLink( $this->oft
);
367 $sql = "UPDATE old SET " .
368 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
369 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
370 wfQuery( $sql, DB_WRITE
, $fname );
372 $sql = "UPDATE recentchanges SET ".
373 "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
374 "rc_namespace={$this->ons} AND rc_title='{$this->odt}'";
375 wfQuery( $sql, DB_WRITE
, $fname );
377 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
378 rc_comment,rc_user,rc_user_text,rc_timestamp,
379 rc_cur_time,rc_cur_id,rc_new)
380 VALUES ({$common},'{$now}',{$this->newid},1)";
381 wfQuery( $sql, DB_WRITE
, $fname );
383 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
384 wfQuery( $sql, DB_WRITE
, $fname );
386 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
387 wfQuery( $sql, DB_WRITE
, $fname );
389 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
390 wfQuery( $sql, DB_WRITE
, $fname );
392 # Non-existent target may have had broken links to it; these must
393 # now be removed and made into good links.
394 $update = new LinksUpdate( $this->oldid
, $this->nft
);
395 $update->fixBrokenLinks();
397 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
398 wfQuery( $sql, DB_WRITE
, $fname );
401 function updateWatchlists()
403 $oldnamespace = $this->ons
& ~
1;
404 $newnamespace = $this->nns
& ~
1;
405 $oldtitle = $this->odt
;
406 $newtitle = $this->ndt
;
408 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
411 WatchedItem
::duplicateEntries( $this->ot
, $this->nt
);