War on cruft: commentBlock() usage, use a styled <span> for comments instead of mix...
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /** */
9 require_once( 'Revision.php' );
10
11 /**
12 *
13 */
14 function wfSpecialUndelete( $par ) {
15 global $wgRequest;
16
17 $form = new UndeleteForm( $wgRequest, $par );
18 $form->execute();
19 }
20
21 /**
22 *
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26 class PageArchive {
27 var $title;
28
29 function PageArchive( &$title ) {
30 if( is_null( $title ) ) {
31 wfDebugDieBacktrace( 'Archiver() given a null title.');
32 }
33 $this->title =& $title;
34 }
35
36 /**
37 * List all deleted pages recorded in the archive table. Returns result
38 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
39 * namespace/title. Can be called staticaly.
40 *
41 * @return ResultWrapper
42 */
43 /* static */ function &listAllPages() {
44 $dbr =& wfGetDB( DB_SLAVE );
45 $archive = $dbr->tableName( 'archive' );
46
47 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
48 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
49
50 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
51 }
52
53 /**
54 * List the revisions of the given page. Returns result wrapper with
55 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
56 *
57 * @return ResultWrapper
58 */
59 function &listRevisions() {
60 $dbr =& wfGetDB( DB_SLAVE );
61 return $dbr->resultObject( $dbr->select( 'archive',
62 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
63 array( 'ar_namespace' => $this->title->getNamespace(),
64 'ar_title' => $this->title->getDBkey() ),
65 'PageArchive::listRevisions',
66 array( 'ORDER BY' => 'ar_timestamp DESC' ) ) );
67 }
68
69 /**
70 * Fetch (and decompress if necessary) the stored text for the deleted
71 * revision of the page with the given timestamp.
72 *
73 * @return string
74 */
75 function getRevisionText( $timestamp ) {
76 $dbr =& wfGetDB( DB_SLAVE );
77 $row = $dbr->selectRow( 'archive',
78 array( 'ar_text', 'ar_flags' ),
79 array( 'ar_namespace' => $this->title->getNamespace(),
80 'ar_title' => $this->title->getDbkey(),
81 'ar_timestamp' => $dbr->timestamp( $timestamp ) ) );
82 return Revision::getRevisionText( $row, "ar_" );
83 }
84
85 /**
86 * Fetch (and decompress if necessary) the stored text of the most
87 * recently edited deleted revision of the page.
88 *
89 * If there are no archived revisions for the page, returns NULL.
90 *
91 * @return string
92 */
93 function getLastRevisionText() {
94 $dbr =& wfGetDB( DB_SLAVE );
95 $row = $dbr->selectRow( 'archive',
96 array( 'ar_text', 'ar_flags' ),
97 array( 'ar_namespace' => $this->title->getNamespace(),
98 'ar_title' => $this->title->getDBkey() ),
99 'PageArchive::getLastRevisionText',
100 'ORDER BY ar_timestamp DESC LIMIT 1' );
101 if( $row ) {
102 return Revision::getRevisionText( $row, "ar_" );
103 } else {
104 return NULL;
105 }
106 }
107
108 /**
109 * Quick check if any archived revisions are present for the page.
110 * @return bool
111 */
112 function isDeleted() {
113 $dbr =& wfGetDB( DB_SLAVE );
114 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
115 array( 'ar_namespace' => $this->title->getNamespace(),
116 'ar_title' => $this->title->getDBkey() ) );
117 return ($n > 0);
118 }
119
120 /**
121 * This is the meaty bit -- restores archived revisions of the given page
122 * to the cur/old tables. If the page currently exists, all revisions will
123 * be stuffed into old, otherwise the most recent will go into cur.
124 * The deletion log will be updated with an undeletion notice.
125 *
126 * Returns true on success.
127 *
128 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
129 * @return bool
130 */
131 function undelete( $timestamps ) {
132 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
133 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
134
135 $fname = "doUndeleteArticle";
136 $restoreAll = empty( $timestamps );
137 $restoreRevisions = count( $timestamps );
138
139 $dbw =& wfGetDB( DB_MASTER );
140 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
141 $namespace = $this->title->getNamespace();
142 $ttl = $this->title->getDBkey();
143 $t = $dbw->strencode( $ttl );
144
145 # Move article and history from the "archive" table
146 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
147 global $wgDBtype;
148 if( $wgDBtype != 'PostgreSQL' ) { # HACKHACKHACKHACK
149 $sql .= ' FOR UPDATE';
150 }
151 $res = $dbw->query( $sql, $fname );
152 $row = $dbw->fetchObject( $res );
153 $now = wfTimestampNow();
154
155 if( $row->count == 0) {
156 # Have to create new article...
157 $sql = "SELECT ar_text,ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' ";
158 if( !$restoreAll ) {
159 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
160 $sql .= "AND ar_timestamp={$max} ";
161 }
162 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
163 $res = $dbw->query( $sql, $fname );
164 $s = $dbw->fetchObject( $res );
165 if( $restoreAll ) {
166 $max = $s->ar_timestamp;
167 }
168 $text = Revision::getRevisionText( $s, "ar_" );
169
170 $redirect = MagicWord::get( MAG_REDIRECT );
171 $redir = $redirect->matchStart( $text ) ? 1 : 0;
172
173 $rand = wfRandom();
174 $dbw->insert( 'cur', array(
175 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
176 'cur_namespace' => $namespace,
177 'cur_title' => $ttl,
178 'cur_text' => $text,
179 'cur_comment' => $s->ar_comment,
180 'cur_user' => $s->ar_user,
181 'cur_timestamp' => $s->ar_timestamp,
182 'cur_minor_edit' => $s->ar_minor_edit,
183 'cur_user_text' => $s->ar_user_text,
184 'cur_is_redirect' => $redir,
185 'cur_random' => $rand,
186 'cur_touched' => $dbw->timestamp( $now ),
187 ), $fname );
188
189 $newid = $dbw->insertId();
190 if( $restoreAll ) {
191 $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) );
192 }
193 } else {
194 # If already exists, put history entirely into old table
195 $oldones = "";
196 $newid = 0;
197
198 # But to make the history list show up right, we need to touch it.
199 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
200 $dbw->query( $sql, $fname );
201
202 # FIXME: Sometimes restored entries will be _newer_ than the current version.
203 # We should merge.
204 }
205
206 if( !$restoreAll ) {
207 $oldts = array();
208 foreach( $timestamps as $ts ) {
209 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
210 }
211 $oldts = join( ",", $oldts );
212 $oldones = "AND ar_timestamp IN ( {$oldts} )";
213 }
214 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
215 "old_comment,old_user,old_user_text,old_timestamp,old_minor_edit," .
216 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
217 "ar_user,ar_user_text,ar_timestamp,ar_minor_edit,ar_flags " .
218 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
219 if( $restoreAll || !empty( $oldts ) ) {
220 $dbw->query( $sql, $fname );
221 }
222
223 # Finally, clean up the link tables
224 if( $newid ) {
225 $wgLinkCache = new LinkCache();
226 # Select for update
227 $wgLinkCache->forUpdate( true );
228 # Create a dummy OutputPage to update the outgoing links
229 $dummyOut = new OutputPage();
230 $dummyOut->addWikiText( $text );
231
232 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
233 array_push( $wgDeferredUpdateList, $u );
234
235 Article::onArticleCreate( $this->title );
236
237 #TODO: SearchUpdate, etc.
238 }
239
240 # Now that it's safely stored, take it out of the archive
241 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
242 "ar_title='{$t}'";
243 if( !$restoreAll ) {
244 $sql .= " AND ar_timestamp IN ( {$oldts}";
245 if( $newid ) {
246 if( !empty( $oldts ) ) $sql .= ",";
247 $sql .= $max;
248 }
249 $sql .= ")";
250 }
251 $dbw->query( $sql, $fname );
252
253
254 # Touch the log?
255 $log = new LogPage( 'delete' );
256 if( $restoreAll ) {
257 $reason = "";
258 } else {
259 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
260 }
261 $log->addEntry( 'restore', $this->title, $reason );
262
263 return true;
264 }
265 }
266
267 /**
268 *
269 * @package MediaWiki
270 * @subpackage SpecialPage
271 */
272 class UndeleteForm {
273 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
274 var $mTargetTimestamp;
275
276 function UndeleteForm( &$request, $par = "" ) {
277 global $wgUser;
278 $this->mAction = $request->getText( 'action' );
279 $this->mTarget = $request->getText( 'target' );
280 $this->mTimestamp = $request->getText( 'timestamp' );
281 $this->mRestore = $request->getCheck( 'restore' ) &&
282 $request->wasPosted() &&
283 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
284 if( $par != "" ) {
285 $this->mTarget = $par;
286 }
287 if ( $this->mTarget !== "" ) {
288 $this->mTargetObj = Title::newFromURL( $this->mTarget );
289 } else {
290 $this->mTargetObj = NULL;
291 }
292 if( $this->mRestore ) {
293 $timestamps = array();
294 foreach( $_REQUEST as $key => $val ) {
295 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
296 array_push( $timestamps, $matches[1] );
297 }
298 }
299 rsort( $timestamps );
300 $this->mTargetTimestamp = $timestamps;
301 }
302 }
303
304 function execute() {
305 if( is_null( $this->mTargetObj ) ) {
306 return $this->showList();
307 }
308 if( $this->mTimestamp !== "" ) {
309 return $this->showRevision( $this->mTimestamp );
310 }
311 if( $this->mRestore && $this->mAction == "submit" ) {
312 return $this->undelete();
313 }
314 return $this->showHistory();
315 }
316
317 /* private */ function showList() {
318 global $wgLang, $wgContLang, $wgUser, $wgOut;
319 $fname = "UndeleteForm::showList";
320
321 # List undeletable articles
322 $result = PageArchive::listAllPages();
323
324 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
325 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
326
327 $sk = $wgUser->getSkin();
328 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
329 $wgOut->addHTML( "<ul>\n" );
330 while( $row = $result->fetchObject() ) {
331 $n = ($row->ar_namespace ?
332 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
333 $row->ar_title;
334 $link = $sk->makeKnownLinkObj( $undelete,
335 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
336 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
337 $wgLang->formatNum( $row->count ) ) );
338 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
339 }
340 $result->free();
341 $wgOut->addHTML( "</ul>\n" );
342
343 return true;
344 }
345
346 /* private */ function showRevision( $timestamp ) {
347 global $wgLang, $wgUser, $wgOut;
348 $fname = "UndeleteForm::showRevision";
349
350 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
351
352 $archive =& new PageArchive( $this->mTargetObj );
353 $text = $archive->getRevisionText( $timestamp );
354
355 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
356 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
357 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
358 }
359
360 /* private */ function showHistory() {
361 global $wgLang, $wgUser, $wgOut;
362
363 $sk = $wgUser->getSkin();
364 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
365
366 $archive = new PageArchive( $this->mTargetObj );
367 $text = $archive->getLastRevisionText();
368 if( is_null( $text ) ) {
369 $wgOut->addWikiText( wfMsg( "nohistory" ) );
370 return;
371 }
372 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
373
374 # List all stored revisions
375 $revisions = $archive->listRevisions();
376
377 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
378 $action = $titleObj->escapeLocalURL( "action=submit" );
379 $encTarget = htmlspecialchars( $this->mTarget );
380 $button = htmlspecialchars( wfMsg("undeletebtn") );
381 $token = htmlspecialchars( $wgUser->editToken() );
382
383 $wgOut->addHTML("
384 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
385 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
386 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
387 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
388 ");
389
390 # Show relevant lines from the deletion log:
391 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
392 require_once( 'SpecialLog.php' );
393 $logViewer =& new LogViewer(
394 new LogReader(
395 new FauxRequest(
396 array( 'page' => $this->mTargetObj->getPrefixedText(),
397 'type' => 'delete' ) ) ) );
398 $logViewer->showList( $wgOut );
399
400 # The page's stored (deleted) history:
401 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
402 $wgOut->addHTML("<ul>");
403 $target = urlencode( $this->mTarget );
404 while( $row = $revisions->fetchObject() ) {
405 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
406 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
407 $pageLink = $sk->makeKnownLinkObj( $titleObj,
408 $wgLang->timeanddate( $row->ar_timestamp, true ),
409 "target=$target&timestamp=$ts" );
410 $userLink = htmlspecialchars( $row->ar_user_text );
411 if( $row->ar_user ) {
412 $userLink = $sk->makeKnownLinkObj(
413 Title::makeTitle( NS_USER, $row->ar_user_text ),
414 $userLink );
415 }
416 $comment = $sk->commentBlock( $row->ar_comment );
417 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
418
419 }
420 $revisions->free();
421 $wgOut->addHTML("</ul>\n</form>");
422
423 return true;
424 }
425
426 function undelete() {
427 global $wgOut;
428 if( !is_null( $this->mTargetObj ) ) {
429 $archive = new PageArchive( $this->mTargetObj );
430 if( $archive->undelete( $this->mTargetTimestamp ) ) {
431 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
432 return true;
433 }
434 }
435 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
436 return false;
437 }
438 }
439
440 ?>