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