fixed problem with updateTimestamp() altering existing IP blocks
[mediawiki.git] / includes / SpecialUndelete.php
blobac2ff9c310c7068fab8172399ab1da6b842b927d
1 <?php
3 function wfSpecialUndelete( $par )
5 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
7 if( $par != "" ) $target = $par;
8 if( isset($target ) ) {
9 $t = Title::newFromURL( $target );
10 $title = $t->mDbkeyform;
11 $namespace = $t->mNamespace;
12 if( isset( $timestamp ) ) {
13 return doUndeleteShowRevision( $namespace, $title, $timestamp );
15 if( isset( $action ) and isset( $restore) and $action == "submit" ) {
16 return doUndeleteArticle( $namespace, $title );
18 return doUndeleteShowHistory( $namespace, $title );
21 # List undeletable articles
22 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM archive GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
23 $res = wfQuery( $sql, DB_READ );
25 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
26 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
28 $special = $wgLang->getNsText( Namespace::getSpecial() );
29 $sk = $wgUser->getSkin();
30 $wgOut->addHTML( "<ul>\n" );
31 while ($row = wfFetchObject( $res )) {
32 $n = ($row->ar_namespace ?
33 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
34 $row->ar_title;
36 $wgOut->addHTML( "<li>" .
37 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
38 $n, "target=" . urlencode($n) ) . " " .
39 wfMsg( "undeleterevisions", $row->count ) );
41 $wgOut->addHTML( "</ul>\n" );
43 return $ret;
46 /* private */ function doUndeleteShowRevision( $namespace, $title, $timestamp ) {
47 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
49 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
51 $sql = "SELECT ar_text,ar_flags FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\" AND ar_timestamp={$timestamp}";
52 $ret = wfQuery( $sql, DB_READ );
53 $row = wfFetchObject( $ret );
55 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
56 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
57 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) );
59 return 0;
62 /* private */ function doUndeleteShowHistory( $namespace, $title ) {
63 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
65 $sk = $wgUser->getSkin();
66 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
68 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
69 FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\"
70 ORDER BY ar_timestamp DESC";
71 $ret = wfQuery( $sql, DB_READ );
73 if( wfNumRows( $ret ) == 0 ) {
74 $wgOut->addWikiText( wfMsg( "nohistory" ) );
75 return 0;
78 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
80 $action = wfLocalUrlE( $wgLang->specialPage( "Undelete" ), "action=submit" );
81 $wgOut->addHTML("<p>
82 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
83 <input type=hidden name=\"target\" value=\"{$target}\">
84 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
85 </form>");
87 $log = wfGetSQL("cur", "cur_text", "cur_namespace=4 AND cur_title=\"".wfMsg("dellogpage")."\"" );
88 if(preg_match("/^(.*".
89 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "")
90 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
91 $wgOut->addWikiText( $m[1] );
94 $special = $wgLang->getNsText( Namespace::getSpecial() );
95 $wgOut->addHTML("<ul>");
96 while( $row = wfFetchObject( $ret ) ) {
97 $wgOut->addHTML( "<li>" .
98 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
99 $wgLang->timeanddate( $row->ar_timestamp, true ),
100 "target=" . urlencode($target) . "&timestamp={$row->ar_timestamp}" ) . " " .
101 ". . {$row->ar_user_text}" .
102 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
105 $wgOut->addHTML("</ul>");
107 return 0;
110 /* private */ function doUndeleteArticle( $namespace, $title )
112 global $wgUser, $wgOut, $wgLang, $target, $wgDeferredUpdateList;
113 global $wgUseSquid, $wgInternalServer;
115 $fname = "doUndeleteArticle";
117 if ( "" == $title ) {
118 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
119 return;
121 $t = addslashes($title);
123 # Move article and history from the "archive" table
124 $sql = "SELECT COUNT(*) AS count FROM cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
125 $res = wfQuery( $sql, DB_READ );
126 $row = wfFetchObject( $res );
127 $now = wfTimestampNow();
129 if( $row->count == 0) {
130 # Have to create new article...
131 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' ORDER BY ar_timestamp DESC LIMIT 1";
132 $res = wfQuery( $sql, DB_READ, $fname );
133 $s = wfFetchObject( $res );
134 $max = $s->ar_timestamp;
135 $redirect = MagicWord::get( MAG_REDIRECT );
136 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
138 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
139 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
140 "SELECT ar_namespace,ar_title,ar_text,ar_comment," .
141 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM archive " .
142 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
143 wfQuery( $sql, DB_WRITE, $fname );
144 $newid = wfInsertId();
145 $oldones = "AND ar_timestamp<{$max}";
146 } else {
147 # If already exists, put history entirely into old table
148 $oldones = "";
149 $newid = 0;
151 # But to make the history list show up right, we need to touch it.
152 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
153 wfQuery( $sql, DB_WRITE, $fname );
155 # FIXME: Sometimes restored entries will be _newer_ than the current version.
156 # We should merge.
159 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
160 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
161 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
162 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
163 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
164 wfQuery( $sql, DB_WRITE, $fname );
166 # Finally, clean up the link tables
167 if( $newid ) {
168 # Create a dummy OutputPage to update the outgoing links
169 # This works at the moment due to good luck. It may stop working in the
170 # future. Damn globals.
171 $dummyOut = new OutputPage();
172 $to = Title::newFromDBKey( $target );
173 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " .
174 "AND cur_namespace={$namespace}", DB_READ, $fname );
175 $row = wfFetchObject( $res );
176 $text = $row->cur_text;
177 $dummyOut->addWikiText( $text );
178 wfFreeResult( $res );
180 $u = new LinksUpdate( $newid, $to->getPrefixedDBkey() );
181 array_push( $wgDeferredUpdateList, $u );
183 Article::onArticleCreate( $to );
185 # Squid purging
186 if ( $wgUseSquid ) {
187 /* this needs to be done after LinksUpdate */
188 $u = new SquidUpdate($to);
189 array_push( $wgDeferredUpdateList, $u );
192 #TODO: SearchUpdate, etc.
195 # Now that it's safely stored, take it out of the archive
196 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " .
197 "ar_title='{$t}'";
198 wfQuery( $sql, DB_WRITE, $fname );
201 # Touch the log?
202 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
203 $log->addEntry( wfMsg( "undeletedarticle", $target ), "" );
205 $wgOut->addWikiText( wfMsg( "undeletedtext", $target ) );
206 return 0;