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