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