One more time... :P shouldn't commit this late at night.
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 function wfSpecialUndelete( $par ) {
12 global $wgRequest;
13
14 $form = new UndeleteForm( $wgRequest, $par );
15 $form->execute();
16 }
17
18 /**
19 *
20 * @package MediaWiki
21 * @subpackage SpecialPage
22 */
23 class PageArchive {
24 var $title;
25
26 function PageArchive( &$title ) {
27 if( is_null( $title ) ) {
28 wfDebugDieBacktrace( 'Archiver() given a null title.');
29 }
30 $this->title =& $title;
31 }
32
33 /* static */ function &listAllPages() {
34 $dbr =& wfGetDB( DB_SLAVE );
35 $archive = $dbr->tableName( 'archive' );
36
37 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
38 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
39
40 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
41 }
42
43 function &listRevisions() {
44 $dbr =& wfGetDB( DB_SLAVE );
45 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
46 FROM archive WHERE ar_namespace=" . $this->title->getNamespace() .
47 " AND ar_title='" . $dbr->strencode( $this->title->getDBkey() ) .
48 "' ORDER BY ar_timestamp DESC";
49 return $dbr->resultObject( $dbr->query( $sql ) );
50 }
51
52 function getRevisionText( $timestamp ) {
53 $dbr =& wfGetDB( DB_SLAVE );
54 $row = $dbr->selectRow( 'archive',
55 array( 'ar_text', 'ar_flags' ),
56 array( 'ar_namespace' => $this->title->getNamespace(),
57 'ar_title' => $this->title->getDbkey(),
58 'ar_timestamp' => $timestamp ) );
59 return Article::getRevisionText( $row, "ar_" );
60 }
61
62 function getLastRevisionText() {
63 $dbr =& wfGetDB( DB_SLAVE );
64 $archive = $dbr->tableName( 'archive' );
65
66 # Get text of first revision
67 $sql = "SELECT ar_text,ar_flags FROM $archive WHERE ar_namespace=" . $this->title->getNamespace() . " AND ar_title='" .
68 $dbr->strencode( $this->title->getDBkey() ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
69 $ret = $dbr->query( $sql );
70 $row = $dbr->fetchObject( $ret );
71 $dbr->freeResult( $ret );
72 if( $row ) {
73 return Article::getRevisionText( $row, "ar_" );
74 } else {
75 return NULL;
76 }
77 }
78
79 function isDeleted() {
80 $dbr =& wfGetDB( DB_SLAVE );
81 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
82 array( 'ar_namespace' => $this->title->getNamespace(),
83 'ar_title' => $this->title->getDBkey() ) );
84 return ($n > 0);
85 }
86
87 function undelete() {
88 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
89 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
90
91 $fname = "doUndeleteArticle";
92
93 $dbw =& wfGetDB( DB_MASTER );
94 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
95 $namespace = $this->title->getNamespace();
96 $t = $dbw->strencode( $this->title->getDBkey() );
97
98 # Move article and history from the "archive" table
99 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
100 $res = $dbw->query( $sql, $fname );
101 $row = $dbw->fetchObject( $res );
102 $now = wfTimestampNow();
103
104 if( $row->count == 0) {
105 # Have to create new article...
106 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
107 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
108 $res = $dbw->query( $sql, $fname );
109 $s = $dbw->fetchObject( $res );
110 $max = $s->ar_timestamp;
111 $redirect = MagicWord::get( MAG_REDIRECT );
112 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
113
114 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
115
116 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
117 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
118 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
119 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
120 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
121 $dbw->query( $sql, $fname );
122 $newid = $dbw->insertId();
123 $oldones = "AND ar_timestamp<{$max}";
124 } else {
125 # If already exists, put history entirely into old table
126 $oldones = "";
127 $newid = 0;
128
129 # But to make the history list show up right, we need to touch it.
130 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
131 $dbw->query( $sql, $fname );
132
133 # FIXME: Sometimes restored entries will be _newer_ than the current version.
134 # We should merge.
135 }
136
137 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
138 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
139 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
140 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
141 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
142 $dbw->query( $sql, $fname );
143
144 # Finally, clean up the link tables
145 if( $newid ) {
146 $wgLinkCache = new LinkCache();
147 # Select for update
148 $wgLinkCache->forUpdate( true );
149 # Create a dummy OutputPage to update the outgoing links
150 $dummyOut = new OutputPage();
151 # Get the text
152 $text = $dbw->selectField( 'cur', 'cur_text',
153 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
154 $fname, 'FOR UPDATE'
155 );
156 $dummyOut->addWikiText( $text );
157
158 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
159 array_push( $wgDeferredUpdateList, $u );
160
161 Article::onArticleCreate( $this->title );
162
163 #TODO: SearchUpdate, etc.
164 }
165
166 # Now that it's safely stored, take it out of the archive
167 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
168 "ar_title='{$t}'";
169 $dbw->query( $sql, $fname );
170
171
172 # Touch the log?
173 $log = new LogPage( 'delete' );
174 $log->addEntry( 'restore', $this->title, "" );
175
176 return true;
177 }
178 }
179
180 /**
181 *
182 * @package MediaWiki
183 * @subpackage SpecialPage
184 */
185 class UndeleteForm {
186 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
187
188 function UndeleteForm( &$request, $par = "" ) {
189 $this->mAction = $request->getText( 'action' );
190 $this->mTarget = $request->getText( 'target' );
191 $this->mTimestamp = $request->getText( 'timestamp' );
192 $this->mRestore = $request->getCheck( 'restore' ) && $request->wasPosted();
193 if( $par != "" ) {
194 $this->mTarget = $par;
195 }
196 if ( $this->mTarget !== "" ) {
197 $this->mTargetObj = Title::newFromURL( $this->mTarget );
198 } else {
199 $this->mTargetObj = NULL;
200 }
201 }
202
203 function execute() {
204 if( is_null( $this->mTargetObj ) ) {
205 return $this->showList();
206 }
207 if( $this->mTimestamp !== "" ) {
208 return $this->showRevision( $this->mTimestamp );
209 }
210 if( $this->mRestore && $this->mAction == "submit" ) {
211 return $this->undelete();
212 }
213 return $this->showHistory();
214 }
215
216 /* private */ function showList() {
217 global $wgLang, $wgUser, $wgOut;
218 $fname = "UndeleteForm::showList";
219
220 # List undeletable articles
221 $result = PageArchive::listAllPages();
222
223 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
224 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
225
226 $sk = $wgUser->getSkin();
227 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
228 $wgOut->addHTML( "<ul>\n" );
229 while( $row = $result->fetchObject() ) {
230 $n = ($row->ar_namespace ?
231 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
232 $row->ar_title;
233 $link = $sk->makeKnownLinkObj( $undelete,
234 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
235 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
236 $wgLang->formatNum( $row->count ) ) );
237 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
238 }
239 $result->free();
240 $wgOut->addHTML( "</ul>\n" );
241
242 return true;
243 }
244
245 /* private */ function showRevision( $timestamp ) {
246 global $wgLang, $wgUser, $wgOut;
247 $fname = "UndeleteForm::showRevision";
248
249 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
250
251 $archive =& new PageArchive( $this->mTargetObj );
252 $text = $archive->getRevisionText( $timestamp );
253
254 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
255 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
256 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
257 }
258
259 /* private */ function showHistory() {
260 global $wgLang, $wgUser, $wgOut;
261
262 $sk = $wgUser->getSkin();
263 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
264
265 $archive = new PageArchive( $this->mTargetObj );
266 $text = $archive->getLastRevisionText();
267 if( is_null( $text ) ) {
268 $wgOut->addWikiText( wfMsg( "nohistory" ) );
269 return;
270 }
271 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
272
273 # List all stored revisions
274 $revisions = $archive->listRevisions();
275
276 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
277 $action = $titleObj->escapeLocalURL( "action=submit" );
278 $encTarget = htmlspecialchars( $this->mTarget );
279 $button = htmlspecialchars( wfMsg("undeletebtn") );
280
281 $wgOut->addHTML("
282 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
283 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
284 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
285 </form>");
286
287 # Show relevant lines from the deletion log:
288 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
289 require_once( 'SpecialLog.php' );
290 $logViewer =& new LogViewer(
291 new LogReader(
292 new FauxRequest(
293 array( 'page', $this->mTargetObj->getPrefixedText() ) ) ) );
294 $logViewer->showList( $wgOut );
295
296 # The page's stored (deleted) history:
297 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
298 $wgOut->addHTML("<ul>");
299 $target = urlencode( $this->mTarget );
300 while( $row = $revisions->fetchObject() ) {
301 $pageLink = $sk->makeKnownLinkObj( $titleObj,
302 $wgLang->timeanddate( $row->ar_timestamp, true ),
303 "target=$target&timestamp={$row->ar_timestamp}" );
304 $userLink = htmlspecialchars( $row->ar_user_text );
305 if( $row->ar_user ) {
306 $userLink = $sk->makeKnownLinkObj(
307 Title::makeTitle( NS_USER, $row->ar_user_text ),
308 $userLink );
309 }
310 $comment = $sk->formatComment( $row->ar_comment );
311 $wgOut->addHTML( "<li>$pageLink . . $userLink (<i>$comment</i>)</li>\n" );
312
313 }
314 $revisions->free();
315 $wgOut->addHTML("</ul>");
316
317 return true;
318 }
319
320 function undelete() {
321 global $wgOut;
322 if( !is_null( $this->mTargetObj ) ) {
323 $archive = new PageArchive( $this->mTargetObj );
324 if( $archive->undelete() ) {
325 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
326 return true;
327 }
328 }
329 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
330 return false;
331 }
332 }
333
334 ?>