Snarf the deletion log entries on the undelete page. Make the log items
[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 $dbr =& wfGetDB( DB_SLAVE );
51 $archive = $dbr->tableName( 'archive' );
52
53 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
54 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
55
56 $res = $dbr->query( $sql, $fname );
57
58 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
59 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
60
61 $special = $wgLang->getNsText( Namespace::getSpecial() );
62 $sk = $wgUser->getSkin();
63 $wgOut->addHTML( "<ul>\n" );
64 while ($row = $dbr->fetchObject( $res )) {
65 $n = ($row->ar_namespace ?
66 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
67 $row->ar_title;
68
69 $wgOut->addHTML( "<li>" .
70 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
71 $n, "target=" . urlencode($n) ) . " " .
72 wfMsg( "undeleterevisions", $row->count ) );
73 }
74 $wgOut->addHTML( "</ul>\n" );
75
76 return 0;
77 }
78
79 /* private */ function showRevision( $namespace, $title, $timestamp ) {
80 global $wgLang, $wgUser, $wgOut;
81 $fname = "UndeleteForm::showRevision";
82
83 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
84
85 $dbr =& wfGetDB( DB_SLAVE );
86 $archive = $dbr->tableName( 'archive' );
87 $sql = "SELECT ar_text,ar_flags FROM $archive ".
88 "WHERE ar_namespace={$namespace} AND ar_title='" .
89 $dbr->strencode( $title ) . "' AND ar_timestamp='" . $dbr->strencode( $timestamp ) ."'";
90 $ret = $dbr->query( $sql, $fname );
91 $row = $dbr->fetchObject( $ret );
92
93 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
94 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
95 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) );
96
97 return 0;
98 }
99
100 /* private */ function showHistory( $namespace, $title ) {
101 global $wgLang, $wgUser, $wgOut;
102
103 $sk = $wgUser->getSkin();
104 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
105 $dbr =& wfGetDB( DB_SLAVE );
106 $archive = $dbr->tableName( 'archive' );
107
108 # Get text of first revision
109 $sql = "SELECT ar_text FROM $archive WHERE ar_namespace={$namespace} AND ar_title='" .
110 $dbr->strencode( $title ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
111 $ret = $dbr->query( $sql );
112
113 if( $dbr->numRows( $ret ) == 0 ) {
114 $wgOut->addWikiText( wfMsg( "nohistory" ) );
115 return 0;
116 }
117 $row = $dbr->fetchObject( $ret );
118 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
119
120 # List all stored revisions
121 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
122 FROM archive WHERE ar_namespace={$namespace} AND ar_title='" . $dbr->strencode( $title ) .
123 "' ORDER BY ar_timestamp DESC";
124 $ret = $dbr->query( $sql );
125
126 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
127 $action = $titleObj->escapeLocalURL( "action=submit" );
128 $encTarget = htmlspecialchars( $this->mTarget );
129
130 $wgOut->addHTML("<p>
131 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
132 <input type=hidden name=\"target\" value=\"{$encTarget}\">
133 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
134 </form>");
135
136 # Show relevant lines from the deletion log:
137 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
138 require_once( 'SpecialLog.php' );
139 $t = Title::MakeTitle( $namespace, $title );
140 # TODO: make a pure, faux request for this.
141 global $wgRequest;
142 $logReader = new LogReader( $wgRequest );
143 $logReader->limitTitle( $t->getPrefixedText() );
144 $logViewer =& new LogViewer( $logReader );
145 $logViewer->showList( $wgOut );
146
147 # The page's stored (deleted) history:
148 $special = $wgLang->getNsText( Namespace::getSpecial() );
149 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
150 $wgOut->addHTML("<ul>");
151 while( $row = $dbr->fetchObject( $ret ) ) {
152 $wgOut->addHTML( "<li>" .
153 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
154 $wgLang->timeanddate( $row->ar_timestamp, true ),
155 "target=" . urlencode($this->mTarget) . "&timestamp={$row->ar_timestamp}" ) . " " .
156 ". . " . htmlspecialchars( $row->ar_user_text ) .
157 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
158
159 }
160 $wgOut->addHTML("</ul>");
161
162 return 0;
163 }
164
165 /* private */ function undelete( $namespace, $title )
166 {
167 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
168 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
169
170 $fname = "doUndeleteArticle";
171
172 if ( "" == $title ) {
173 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
174 return;
175 }
176 $dbw =& wfGetDB( DB_MASTER );
177 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
178 $t = $dbw->strencode($title);
179
180 # Move article and history from the "archive" table
181 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
182 $res = $dbw->query( $sql, $fname );
183 $row = $dbw->fetchObject( $res );
184 $now = wfTimestampNow();
185
186 if( $row->count == 0) {
187 # Have to create new article...
188 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
189 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
190 $res = $dbw->query( $sql, $fname );
191 $s = $dbw->fetchObject( $res );
192 $max = $s->ar_timestamp;
193 $redirect = MagicWord::get( MAG_REDIRECT );
194 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
195
196 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
197
198 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
199 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
200 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
201 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
202 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
203 $dbw->query( $sql, $fname );
204 $newid = $dbw->insertId();
205 $oldones = "AND ar_timestamp<{$max}";
206 } else {
207 # If already exists, put history entirely into old table
208 $oldones = "";
209 $newid = 0;
210
211 # But to make the history list show up right, we need to touch it.
212 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
213 $dbw->query( $sql, $fname );
214
215 # FIXME: Sometimes restored entries will be _newer_ than the current version.
216 # We should merge.
217 }
218
219 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
220 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
221 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
222 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
223 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
224 $dbw->query( $sql, $fname );
225
226 # Finally, clean up the link tables
227 if( $newid ) {
228 $wgLinkCache = new LinkCache();
229 # Select for update
230 $wgLinkCache->forUpdate( true );
231 # Create a dummy OutputPage to update the outgoing links
232 $dummyOut = new OutputPage();
233 # Get the text
234 $text = $dbw->selectField( 'cur', 'cur_text',
235 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
236 $fname, 'FOR UPDATE'
237 );
238 $dummyOut->addWikiText( $text );
239
240 $u = new LinksUpdate( $newid, $this->mTargetObj->getPrefixedDBkey() );
241 array_push( $wgDeferredUpdateList, $u );
242
243 Article::onArticleCreate( $this->mTargetObj );
244
245 #TODO: SearchUpdate, etc.
246 }
247
248 # Now that it's safely stored, take it out of the archive
249 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
250 "ar_title='{$t}'";
251 $dbw->query( $sql, $fname );
252
253
254 # Touch the log?
255 $log = new LogPage( 'delete' );
256 $log->addEntry( 'restore', Title::makeTitle( $namespace, $title ), "" );
257
258 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
259 return 0;
260 }
261 }
262 ?>