Moved image history retrieval function to Image.php.
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 function wfSpecialUndelete( $par )
4 {
5 global $wgRequest;
6
7 $form = new UndeleteForm( $wgRequest, $par );
8 $form->execute();
9 }
10
11 class UndeleteForm {
12 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
13
14 function UndeleteForm( &$request, $par = "" ) {
15 $this->mAction = $request->getText( 'action' );
16 $this->mTarget = $request->getText( 'target' );
17 $this->mTimestamp = $request->getText( 'timestamp' );
18 $this->mRestore = $request->getCheck( 'restore' );
19 if( $par != "" ) {
20 $this->mTarget = $par;
21 }
22 if ( $this->mTarget !== "" ) {
23 $this->mTargetObj = Title::newFromURL( $this->mTarget );
24 } else {
25 $this->mTargetObj = NULL;
26 }
27 }
28
29 function execute() {
30 if( !is_null( $this->mTargetObj ) ) {
31 $title = $this->mTargetObj->mDbkeyform;
32 $namespace = $this->mTargetObj->mNamespace;
33 if( $this->mTimestamp !== "" ) {
34 return $this->showRevision( $namespace, $title, $this->mTimestamp );
35 }
36 if( $this->mRestore and $this->mAction == "submit" ) {
37 return $this->undelete( $namespace, $title );
38 }
39 return $this->showHistory( $namespace, $title );
40 } else {
41 return $this->showList();
42 }
43 }
44
45 /* private */ function showList() {
46 global $wgLang, $wgUser, $wgOut;
47 $fname = "UndeleteForm::showList";
48
49 # List undeletable articles
50 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM archive " .
51 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
52 $res = wfQuery( $sql, DB_READ, $fname );
53
54 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
55 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
56
57 $special = $wgLang->getNsText( Namespace::getSpecial() );
58 $sk = $wgUser->getSkin();
59 $wgOut->addHTML( "<ul>\n" );
60 while ($row = wfFetchObject( $res )) {
61 $n = ($row->ar_namespace ?
62 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
63 $row->ar_title;
64
65 $wgOut->addHTML( "<li>" .
66 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
67 $n, "target=" . urlencode($n) ) . " " .
68 wfMsg( "undeleterevisions", $row->count ) );
69 }
70 $wgOut->addHTML( "</ul>\n" );
71
72 return 0;
73 }
74
75 /* private */ function showRevision( $namespace, $title, $timestamp ) {
76 global $wgLang, $wgUser, $wgOut;
77 $fname = "UndeleteForm::showRevision";
78
79 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
80
81 $sql = "SELECT ar_text,ar_flags FROM archive ".
82 "WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\" AND ar_timestamp={$timestamp}";
83 $ret = wfQuery( $sql, DB_READ, $fname );
84 $row = wfFetchObject( $ret );
85
86 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
87 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
88 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) );
89
90 return 0;
91 }
92
93 /* private */ function showHistory( $namespace, $title ) {
94 global $wgLang, $wgUser, $wgOut;
95
96 $sk = $wgUser->getSkin();
97 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
98
99 # Get text of first revision
100 $sql = "SELECT ar_text FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\"
101 ORDER BY ar_timestamp DESC LIMIT 1";
102 $ret = wfQuery( $sql, DB_READ );
103
104 if( wfNumRows( $ret ) == 0 ) {
105 $wgOut->addWikiText( wfMsg( "nohistory" ) );
106 return 0;
107 }
108 $row = wfFetchObject( $ret );
109 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
110
111 # Get remaining revisions
112 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
113 FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\"
114 ORDER BY ar_timestamp DESC";
115 $ret = wfQuery( $sql, DB_READ );
116 # Ditch first row
117 $row = wfFetchObject( $ret );
118
119 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
120 $action = $titleObj->escapeLocalURL( "action=submit" );
121 $encTarget = htmlspecialchars( $this->mTarget );
122
123 $wgOut->addHTML("<p>
124 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
125 <input type=hidden name=\"target\" value=\"{$encTarget}\">
126 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
127 </form>");
128
129 $log = wfGetSQL("cur", "cur_text", "cur_namespace=4 AND cur_title=\"".wfMsg("dellogpage")."\"" );
130 if(preg_match("/^(.*".
131 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "")
132 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
133 $wgOut->addWikiText( $m[1] );
134 }
135
136 $special = $wgLang->getNsText( Namespace::getSpecial() );
137 $wgOut->addHTML("<ul>");
138 while( $row = wfFetchObject( $ret ) ) {
139 $wgOut->addHTML( "<li>" .
140 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
141 $wgLang->timeanddate( $row->ar_timestamp, true ),
142 "target=" . urlencode($this->mTarget) . "&timestamp={$row->ar_timestamp}" ) . " " .
143 ". . {$row->ar_user_text}" .
144 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
145
146 }
147 $wgOut->addHTML("</ul>");
148
149 return 0;
150 }
151
152 /* private */ function undelete( $namespace, $title )
153 {
154 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
155 global $wgUseSquid, $wgInternalServer;
156
157 $fname = "doUndeleteArticle";
158
159 if ( "" == $title ) {
160 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
161 return;
162 }
163 $t = addslashes($title);
164
165 # Move article and history from the "archive" table
166 $sql = "SELECT COUNT(*) AS count FROM cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
167 $res = wfQuery( $sql, DB_READ );
168 $row = wfFetchObject( $res );
169 $now = wfTimestampNow();
170
171 if( $row->count == 0) {
172 # Have to create new article...
173 $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";
174 $res = wfQuery( $sql, DB_READ, $fname );
175 $s = wfFetchObject( $res );
176 $max = $s->ar_timestamp;
177 $redirect = MagicWord::get( MAG_REDIRECT );
178 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
179
180 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
181 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
182 "SELECT ar_namespace,ar_title,ar_text,ar_comment," .
183 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM archive " .
184 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
185 wfQuery( $sql, DB_WRITE, $fname );
186 $newid = wfInsertId();
187 $oldones = "AND ar_timestamp<{$max}";
188 } else {
189 # If already exists, put history entirely into old table
190 $oldones = "";
191 $newid = 0;
192
193 # But to make the history list show up right, we need to touch it.
194 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
195 wfQuery( $sql, DB_WRITE, $fname );
196
197 # FIXME: Sometimes restored entries will be _newer_ than the current version.
198 # We should merge.
199 }
200
201 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
202 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
203 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
204 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
205 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
206 wfQuery( $sql, DB_WRITE, $fname );
207
208 # Finally, clean up the link tables
209 if( $newid ) {
210 # Create a dummy OutputPage to update the outgoing links
211 # This works at the moment due to good luck. It may stop working in the
212 # future. Damn globals.
213 $dummyOut = new OutputPage();
214 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " .
215 "AND cur_namespace={$namespace}", DB_READ, $fname );
216 $row = wfFetchObject( $res );
217 $text = $row->cur_text;
218 $dummyOut->addWikiText( $text );
219 wfFreeResult( $res );
220
221 $u = new LinksUpdate( $newid, $this->mTargetObj->getPrefixedDBkey() );
222 array_push( $wgDeferredUpdateList, $u );
223
224 Article::onArticleCreate( $this->mTargetObj );
225
226 #TODO: SearchUpdate, etc.
227 }
228
229 # Now that it's safely stored, take it out of the archive
230 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " .
231 "ar_title='{$t}'";
232 wfQuery( $sql, DB_WRITE, $fname );
233
234
235 # Touch the log?
236 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
237 $log->addEntry( wfMsg( "undeletedarticle", $this->mTarget ), "" );
238
239 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
240 return 0;
241 }
242 }
243 ?>