Fixed display of most recent revision
[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 $res = $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 $ret = $dbr->resultObject( $res );
68 return $ret;
69 }
70
71 /**
72 * Fetch (and decompress if necessary) the stored text for the deleted
73 * revision of the page with the given timestamp.
74 *
75 * @return string
76 */
77 function getRevisionText( $timestamp ) {
78 $fname = 'PageArchive::getRevisionText';
79 $dbr =& wfGetDB( DB_SLAVE );
80 $row = $dbr->selectRow( 'archive',
81 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
82 array( 'ar_namespace' => $this->title->getNamespace(),
83 'ar_title' => $this->title->getDbkey(),
84 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
85 $fname );
86 return $this->getTextFromRow( $row );
87 }
88
89 /**
90 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
91 */
92 function getTextFromRow( $row ) {
93 if( is_null( $row->ar_text_id ) ) {
94 // An old row from MediaWiki 1.4 or previous.
95 // Text is embedded in this row in classic compression format.
96 return Revision::getRevisionText( $row, "ar_" );
97 } else {
98 // New-style: keyed to the text storage backend.
99 $dbr =& wfGetDB( DB_SLAVE );
100 $text = $dbr->selectRow( 'text',
101 array( 'old_text', 'old_flags' ),
102 array( 'old_id' => $row->ar_text_id ),
103 $fname );
104 return Revision::getRevisionText( $text );
105 }
106 }
107
108
109 /**
110 * Fetch (and decompress if necessary) the stored text of the most
111 * recently edited deleted revision of the page.
112 *
113 * If there are no archived revisions for the page, returns NULL.
114 *
115 * @return string
116 */
117 function getLastRevisionText() {
118 $dbr =& wfGetDB( DB_SLAVE );
119 $row = $dbr->selectRow( 'archive',
120 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
121 array( 'ar_namespace' => $this->title->getNamespace(),
122 'ar_title' => $this->title->getDBkey() ),
123 'PageArchive::getLastRevisionText',
124 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
125 if( $row ) {
126 return $this->getTextFromRow( $row );
127 } else {
128 return NULL;
129 }
130 }
131
132 /**
133 * Quick check if any archived revisions are present for the page.
134 * @return bool
135 */
136 function isDeleted() {
137 $dbr =& wfGetDB( DB_SLAVE );
138 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
139 array( 'ar_namespace' => $this->title->getNamespace(),
140 'ar_title' => $this->title->getDBkey() ) );
141 return ($n > 0);
142 }
143
144 /**
145 * This is the meaty bit -- restores archived revisions of the given page
146 * to the cur/old tables. If the page currently exists, all revisions will
147 * be stuffed into old, otherwise the most recent will go into cur.
148 * The deletion log will be updated with an undeletion notice.
149 *
150 * Returns true on success.
151 *
152 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
153 * @return bool
154 */
155 function undelete( $timestamps ) {
156 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
157 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
158 global $wgDBtype;
159
160 $fname = "doUndeleteArticle";
161 $restoreAll = empty( $timestamps );
162 $restoreRevisions = count( $timestamps );
163
164 $dbw =& wfGetDB( DB_MASTER );
165 extract( $dbw->tableNames( 'page', 'archive' ) );
166
167 # Does this page already exist? We'll have to update it...
168 $article = new Article( $this->title );
169 $options = ( $wgDBtype == 'PostgreSQL' )
170 ? '' // pg doesn't support this?
171 : 'FOR UPDATE';
172 $page = $dbw->selectRow( 'page',
173 array( 'page_id', 'page_latest' ),
174 array( 'page_namespace' => $this->title->getNamespace(),
175 'page_title' => $this->title->getDBkey() ),
176 $fname,
177 $options );
178 if( $page ) {
179 # Page already exists. Import the history, and if necessary
180 # we'll update the latest revision field in the record.
181 $newid = 0;
182 $pageId = $page->page_id;
183 $previousRevId = $page->page_latest;
184 $previousTimestamp = $page->rev_timestamp;
185 } else {
186 # Have to create a new article...
187 $newid = $article->insertOn( $dbw );
188 $pageId = $newid;
189 $previousRevId = 0;
190 $previousTimestamp = 0;
191 }
192
193 if( $restoreAll ) {
194 $oldones = '1 = 1'; # All revisions...
195 } else {
196 $oldts = implode( ',',
197 array_map( array( &$dbw, 'addQuotes' ),
198 array_map( array( &$dbw, 'timestamp' ),
199 $timestamps ) ) );
200
201 $oldones = "ar_timestamp IN ( {$oldts} )";
202 }
203
204 /**
205 * Restore each revision...
206 */
207 $result = $dbw->select( 'archive',
208 /* fields */ array(
209 'ar_rev_id',
210 'ar_text',
211 'ar_comment',
212 'ar_user',
213 'ar_user_text',
214 'ar_timestamp',
215 'ar_minor_edit',
216 'ar_flags',
217 'ar_text_id' ),
218 /* WHERE */ array(
219 'ar_namespace' => $this->title->getNamespace(),
220 'ar_title' => $this->title->getDBkey(),
221 $oldones ),
222 $fname,
223 /* options */ array(
224 'ORDER BY' => 'ar_timestamp' )
225 );
226 $revision = null;
227 while( $row = $dbw->fetchObject( $result ) ) {
228 $revision = new Revision( array(
229 'page' => $pageId,
230 'id' => $row->ar_rev_id,
231 'text' => Revision::getRevisionText( $row, 'ar_' ),
232 'comment' => $row->ar_comment,
233 'user' => $row->ar_user,
234 'user_text' => $row->ar_user_text,
235 'timestamp' => $row->ar_timestamp,
236 'minor_edit' => $row->ar_minor_edit,
237 'text_id' => $row->ar_text_id,
238 ) );
239 $revision->insertOn( $dbw );
240 }
241
242 if( $revision ) {
243 # FIXME: Update latest if newer as well...
244 if( $newid ) {
245 # FIXME: update article count if changed...
246 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
247
248 # Finally, clean up the link tables
249 $wgLinkCache = new LinkCache();
250 # Select for update
251 $wgLinkCache->forUpdate( true );
252
253 # Create a dummy OutputPage to update the outgoing links
254 $dummyOut = new OutputPage();
255 $dummyOut->addWikiText( $revision->getText() );
256
257 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
258 array_push( $wgDeferredUpdateList, $u );
259
260 #TODO: SearchUpdate, etc.
261 }
262
263 if( $newid ) {
264 Article::onArticleCreate( $this->title );
265 } else {
266 Article::onArticleEdit( $this->title );
267 }
268 } else {
269 # Something went terribly worong!
270 }
271
272 # Now that it's safely stored, take it out of the archive
273 $dbw->delete( 'archive',
274 /* WHERE */ array(
275 'ar_namespace' => $this->title->getNamespace(),
276 'ar_title' => $this->title->getDBkey(),
277 $oldones ),
278 $fname );
279
280 # Touch the log!
281 $log = new LogPage( 'delete' );
282 if( $restoreAll ) {
283 $reason = '';
284 } else {
285 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
286 }
287 $log->addEntry( 'restore', $this->title, $reason );
288
289 return true;
290 }
291 }
292
293 /**
294 *
295 * @package MediaWiki
296 * @subpackage SpecialPage
297 */
298 class UndeleteForm {
299 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
300 var $mTargetTimestamp;
301
302 function UndeleteForm( &$request, $par = "" ) {
303 global $wgUser;
304 $this->mAction = $request->getText( 'action' );
305 $this->mTarget = $request->getText( 'target' );
306 $this->mTimestamp = $request->getText( 'timestamp' );
307 $this->mRestore = $request->getCheck( 'restore' ) &&
308 $request->wasPosted() &&
309 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
310 if( $par != "" ) {
311 $this->mTarget = $par;
312 }
313 if ( $this->mTarget !== "" ) {
314 $this->mTargetObj = Title::newFromURL( $this->mTarget );
315 } else {
316 $this->mTargetObj = NULL;
317 }
318 if( $this->mRestore ) {
319 $timestamps = array();
320 foreach( $_REQUEST as $key => $val ) {
321 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
322 array_push( $timestamps, $matches[1] );
323 }
324 }
325 rsort( $timestamps );
326 $this->mTargetTimestamp = $timestamps;
327 }
328 }
329
330 function execute() {
331 if( is_null( $this->mTargetObj ) ) {
332 return $this->showList();
333 }
334 if( $this->mTimestamp !== "" ) {
335 return $this->showRevision( $this->mTimestamp );
336 }
337 if( $this->mRestore && $this->mAction == "submit" ) {
338 return $this->undelete();
339 }
340 return $this->showHistory();
341 }
342
343 /* private */ function showList() {
344 global $wgLang, $wgContLang, $wgUser, $wgOut;
345 $fname = "UndeleteForm::showList";
346
347 # List undeletable articles
348 $result = PageArchive::listAllPages();
349
350 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
351 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
352
353 $sk = $wgUser->getSkin();
354 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
355 $wgOut->addHTML( "<ul>\n" );
356 while( $row = $result->fetchObject() ) {
357 $n = ($row->ar_namespace ?
358 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
359 $row->ar_title;
360 $link = $sk->makeKnownLinkObj( $undelete,
361 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
362 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
363 $wgLang->formatNum( $row->count ) ) );
364 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
365 }
366 $result->free();
367 $wgOut->addHTML( "</ul>\n" );
368
369 return true;
370 }
371
372 /* private */ function showRevision( $timestamp ) {
373 global $wgLang, $wgUser, $wgOut;
374 $fname = "UndeleteForm::showRevision";
375
376 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
377
378 $archive =& new PageArchive( $this->mTargetObj );
379 $text = $archive->getRevisionText( $timestamp );
380
381 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
382 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
383 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
384 }
385
386 /* private */ function showHistory() {
387 global $wgLang, $wgUser, $wgOut;
388
389 $sk = $wgUser->getSkin();
390 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
391
392 $archive = new PageArchive( $this->mTargetObj );
393 $text = $archive->getLastRevisionText();
394 if( is_null( $text ) ) {
395 $wgOut->addWikiText( wfMsg( "nohistory" ) );
396 return;
397 }
398 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
399
400 # List all stored revisions
401 $revisions = $archive->listRevisions();
402
403 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
404 $action = $titleObj->escapeLocalURL( "action=submit" );
405 $encTarget = htmlspecialchars( $this->mTarget );
406 $button = htmlspecialchars( wfMsg("undeletebtn") );
407 $token = htmlspecialchars( $wgUser->editToken() );
408
409 $wgOut->addHTML("
410 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
411 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
412 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
413 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
414 ");
415
416 # Show relevant lines from the deletion log:
417 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
418 require_once( 'SpecialLog.php' );
419 $logViewer =& new LogViewer(
420 new LogReader(
421 new FauxRequest(
422 array( 'page' => $this->mTargetObj->getPrefixedText(),
423 'type' => 'delete' ) ) ) );
424 $logViewer->showList( $wgOut );
425
426 # The page's stored (deleted) history:
427 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
428 $wgOut->addHTML("<ul>");
429 $target = urlencode( $this->mTarget );
430 while( $row = $revisions->fetchObject() ) {
431 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
432 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
433 $pageLink = $sk->makeKnownLinkObj( $titleObj,
434 $wgLang->timeanddate( $ts, true ),
435 "target=$target&timestamp=$ts" );
436 $userLink = htmlspecialchars( $row->ar_user_text );
437 if( $row->ar_user ) {
438 $userLink = $sk->makeKnownLinkObj(
439 Title::makeTitle( NS_USER, $row->ar_user_text ),
440 $userLink );
441 } else {
442 $userLink = $sk->makeKnownLinkObj(
443 Title::makeTitle( NS_SPECIAL, 'Contributions' ),
444 $userLink, 'target=' . $row->ar_user_text );
445 }
446 $comment = $sk->commentBlock( $row->ar_comment );
447 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
448
449 }
450 $revisions->free();
451 $wgOut->addHTML("</ul>\n</form>");
452
453 return true;
454 }
455
456 function undelete() {
457 global $wgOut;
458 if( !is_null( $this->mTargetObj ) ) {
459 $archive = new PageArchive( $this->mTargetObj );
460 if( $archive->undelete( $this->mTargetTimestamp ) ) {
461 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
462
463 if (NS_IMAGE == $this->mTargetObj->getNamespace()) {
464 /* refresh image metadata cache */
465 new Image( $this->mTargetObj );
466 }
467
468 return true;
469 }
470 }
471 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
472 return false;
473 }
474 }
475
476 ?>