DB error log
[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='" .
83 wfStrencode( $title ) . "' AND ar_timestamp='" . wfStrencode( $timestamp ) ."'";
84 $ret = wfQuery( $sql, DB_READ, $fname );
85 $row = wfFetchObject( $ret );
86
87 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
88 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
89 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) );
90
91 return 0;
92 }
93
94 /* private */ function showHistory( $namespace, $title ) {
95 global $wgLang, $wgUser, $wgOut;
96
97 $sk = $wgUser->getSkin();
98 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
99
100 # Get text of first revision
101 $sql = "SELECT ar_text FROM archive WHERE ar_namespace={$namespace} AND ar_title='" .
102 wfStrencode( $title ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
103 $ret = wfQuery( $sql, DB_READ );
104
105 if( wfNumRows( $ret ) == 0 ) {
106 $wgOut->addWikiText( wfMsg( "nohistory" ) );
107 return 0;
108 }
109 $row = wfFetchObject( $ret );
110 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
111
112 # Get remaining revisions
113 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
114 FROM archive WHERE ar_namespace={$namespace} AND ar_title='" . wfStrencode( $title ) .
115 "' ORDER BY ar_timestamp DESC";
116 $ret = wfQuery( $sql, DB_READ );
117 # Ditch first row
118 $row = wfFetchObject( $ret );
119
120 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
121 $action = $titleObj->escapeLocalURL( "action=submit" );
122 $encTarget = htmlspecialchars( $this->mTarget );
123
124 $wgOut->addHTML("<p>
125 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
126 <input type=hidden name=\"target\" value=\"{$encTarget}\">
127 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
128 </form>");
129
130 $log = wfGetSQL("cur", "cur_text", "cur_namespace=4 AND cur_title='".
131 wfStrencode( wfMsg("dellogpage") ) . "'" );
132 if(preg_match("/^(.*".
133 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "")
134 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
135 $wgOut->addWikiText( $m[1] );
136 }
137
138 $special = $wgLang->getNsText( Namespace::getSpecial() );
139 $wgOut->addHTML("<ul>");
140 while( $row = wfFetchObject( $ret ) ) {
141 $wgOut->addHTML( "<li>" .
142 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
143 $wgLang->timeanddate( $row->ar_timestamp, true ),
144 "target=" . urlencode($this->mTarget) . "&timestamp={$row->ar_timestamp}" ) . " " .
145 ". . " . htmlspecialchars( $row->ar_user_text ) .
146 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
147
148 }
149 $wgOut->addHTML("</ul>");
150
151 return 0;
152 }
153
154 /* private */ function undelete( $namespace, $title )
155 {
156 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
157 global $wgUseSquid, $wgInternalServer;
158
159 $fname = "doUndeleteArticle";
160
161 if ( "" == $title ) {
162 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
163 return;
164 }
165 $t = wfStrencode($title);
166
167 # Move article and history from the "archive" table
168 $sql = "SELECT COUNT(*) AS count FROM cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
169 $res = wfQuery( $sql, DB_READ );
170 $row = wfFetchObject( $res );
171 $now = wfTimestampNow();
172
173 if( $row->count == 0) {
174 # Have to create new article...
175 $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";
176 $res = wfQuery( $sql, DB_READ, $fname );
177 $s = wfFetchObject( $res );
178 $max = $s->ar_timestamp;
179 $redirect = MagicWord::get( MAG_REDIRECT );
180 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
181
182 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
183 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
184 "SELECT ar_namespace,ar_title,ar_text,ar_comment," .
185 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM archive " .
186 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
187 wfQuery( $sql, DB_WRITE, $fname );
188 $newid = wfInsertId();
189 $oldones = "AND ar_timestamp<{$max}";
190 } else {
191 # If already exists, put history entirely into old table
192 $oldones = "";
193 $newid = 0;
194
195 # But to make the history list show up right, we need to touch it.
196 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
197 wfQuery( $sql, DB_WRITE, $fname );
198
199 # FIXME: Sometimes restored entries will be _newer_ than the current version.
200 # We should merge.
201 }
202
203 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
204 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
205 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
206 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
207 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
208 wfQuery( $sql, DB_WRITE, $fname );
209
210 # Finally, clean up the link tables
211 if( $newid ) {
212 # Create a dummy OutputPage to update the outgoing links
213 # This works at the moment due to good luck. It may stop working in the
214 # future. Damn globals.
215 $dummyOut = new OutputPage();
216 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " .
217 "AND cur_namespace={$namespace}", DB_READ, $fname );
218 $row = wfFetchObject( $res );
219 $text = $row->cur_text;
220 $dummyOut->addWikiText( $text );
221 wfFreeResult( $res );
222
223 $u = new LinksUpdate( $newid, $this->mTargetObj->getPrefixedDBkey() );
224 array_push( $wgDeferredUpdateList, $u );
225
226 Article::onArticleCreate( $this->mTargetObj );
227
228 #TODO: SearchUpdate, etc.
229 }
230
231 # Now that it's safely stored, take it out of the archive
232 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " .
233 "ar_title='{$t}'";
234 wfQuery( $sql, DB_WRITE, $fname );
235
236
237 # Touch the log?
238 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
239 $log->addEntry( wfMsg( "undeletedarticle", $this->mTarget ), "" );
240
241 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
242 return 0;
243 }
244 }
245 ?>