* Added templatelinks table. The table currently represents a literal list of templat...
[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 $wgDeferredUpdateList, $wgParser, $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 $newRevId = $previousRevId;
228
229 while( $row = $dbw->fetchObject( $result ) ) {
230 if( $row->ar_text_id ) {
231 // Revision was deleted in 1.5+; text is in
232 // the regular text table, use the reference.
233 // Specify null here so the so the text is
234 // dereferenced for page length info if needed.
235 $revText = null;
236 } else {
237 // Revision was deleted in 1.4 or earlier.
238 // Text is squashed into the archive row, and
239 // a new text table entry will be created for it.
240 $revText = Revision::getRevisionText( $row, 'ar_' );
241 }
242 $revision = new Revision( array(
243 'page' => $pageId,
244 'id' => $row->ar_rev_id,
245 'text' => $revText,
246 'comment' => $row->ar_comment,
247 'user' => $row->ar_user,
248 'user_text' => $row->ar_user_text,
249 'timestamp' => $row->ar_timestamp,
250 'minor_edit' => $row->ar_minor_edit,
251 'text_id' => $row->ar_text_id,
252 ) );
253 $newRevId = $revision->insertOn( $dbw );
254 }
255
256 if( $revision ) {
257 # FIXME: Update latest if newer as well...
258 if( $newid ) {
259 # FIXME: update article count if changed...
260 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
261
262 # Finally, clean up the link tables
263 $parserOptions = new ParserOptions;
264 $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options,
265 true, true, $newRevId );
266 $u = new LinksUpdate( $this->title, $parserOutput );
267 $u->doUpdate();
268
269 #TODO: SearchUpdate, etc.
270 }
271
272 if( $newid ) {
273 Article::onArticleCreate( $this->title );
274 } else {
275 Article::onArticleEdit( $this->title );
276 }
277 } else {
278 # Something went terribly wrong!
279 }
280
281 # Now that it's safely stored, take it out of the archive
282 $dbw->delete( 'archive',
283 /* WHERE */ array(
284 'ar_namespace' => $this->title->getNamespace(),
285 'ar_title' => $this->title->getDBkey(),
286 $oldones ),
287 $fname );
288
289 # Touch the log!
290 $log = new LogPage( 'delete' );
291 if( $restoreAll ) {
292 $reason = '';
293 } else {
294 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
295 }
296 $log->addEntry( 'restore', $this->title, $reason );
297
298 return true;
299 }
300 }
301
302 /**
303 *
304 * @package MediaWiki
305 * @subpackage SpecialPage
306 */
307 class UndeleteForm {
308 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
309 var $mTargetTimestamp, $mAllowed;
310
311 function UndeleteForm( &$request, $par = "" ) {
312 global $wgUser;
313 $this->mAction = $request->getText( 'action' );
314 $this->mTarget = $request->getText( 'target' );
315 $this->mTimestamp = $request->getText( 'timestamp' );
316 $this->mRestore = $request->getCheck( 'restore' ) &&
317 $request->wasPosted() &&
318 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
319 if( $par != "" ) {
320 $this->mTarget = $par;
321 }
322 if ( $wgUser->isAllowed( 'delete' ) ) {
323 $this->mAllowed = true;
324 } else {
325 $this->mAllowed = false;
326 $this->mTimestamp = '';
327 $this->mRestore = false;
328 }
329 if ( $this->mTarget !== "" ) {
330 $this->mTargetObj = Title::newFromURL( $this->mTarget );
331 } else {
332 $this->mTargetObj = NULL;
333 }
334 if( $this->mRestore ) {
335 $timestamps = array();
336 foreach( $_REQUEST as $key => $val ) {
337 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
338 array_push( $timestamps, $matches[1] );
339 }
340 }
341 rsort( $timestamps );
342 $this->mTargetTimestamp = $timestamps;
343 }
344 }
345
346 function execute() {
347
348 if( is_null( $this->mTargetObj ) ) {
349 return $this->showList();
350 }
351 if( $this->mTimestamp !== '' ) {
352 return $this->showRevision( $this->mTimestamp );
353 }
354 if( $this->mRestore && $this->mAction == "submit" ) {
355 return $this->undelete();
356 }
357 return $this->showHistory();
358 }
359
360 /* private */ function showList() {
361 global $wgLang, $wgContLang, $wgUser, $wgOut;
362 $fname = "UndeleteForm::showList";
363
364 # List undeletable articles
365 $result = PageArchive::listAllPages();
366
367 if ( $this->mAllowed ) {
368 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
369 } else {
370 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
371 }
372 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
373
374 $sk = $wgUser->getSkin();
375 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
376 $wgOut->addHTML( "<ul>\n" );
377 while( $row = $result->fetchObject() ) {
378 $n = ($row->ar_namespace ?
379 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
380 $row->ar_title;
381 $link = $sk->makeKnownLinkObj( $undelete,
382 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
383 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
384 $wgLang->formatNum( $row->count ) ) );
385 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
386 }
387 $result->free();
388 $wgOut->addHTML( "</ul>\n" );
389
390 return true;
391 }
392
393 /* private */ function showRevision( $timestamp ) {
394 global $wgLang, $wgUser, $wgOut;
395 $fname = "UndeleteForm::showRevision";
396
397 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
398
399 $archive =& new PageArchive( $this->mTargetObj );
400 $text = $archive->getRevisionText( $timestamp );
401
402 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
403 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
404 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
405 }
406
407 /* private */ function showHistory() {
408 global $wgLang, $wgUser, $wgOut;
409
410 $sk = $wgUser->getSkin();
411 if ( $this->mAllowed ) {
412 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
413 } else {
414 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
415 }
416
417 $archive = new PageArchive( $this->mTargetObj );
418 $text = $archive->getLastRevisionText();
419 if( is_null( $text ) ) {
420 $wgOut->addWikiText( wfMsg( "nohistory" ) );
421 return;
422 }
423 if ( $this->mAllowed ) {
424 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
425 } else {
426 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
427 }
428
429 # List all stored revisions
430 $revisions = $archive->listRevisions();
431
432 if ( $this->mAllowed ) {
433 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
434 $action = $titleObj->escapeLocalURL( "action=submit" );
435 $encTarget = htmlspecialchars( $this->mTarget );
436 $button = htmlspecialchars( wfMsg("undeletebtn") );
437 $token = htmlspecialchars( $wgUser->editToken() );
438
439 $wgOut->addHTML("
440 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
441 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
442 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
443 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
444 ");
445 }
446
447 # Show relevant lines from the deletion log:
448 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
449 require_once( 'SpecialLog.php' );
450 $logViewer =& new LogViewer(
451 new LogReader(
452 new FauxRequest(
453 array( 'page' => $this->mTargetObj->getPrefixedText(),
454 'type' => 'delete' ) ) ) );
455 $logViewer->showList( $wgOut );
456
457 # The page's stored (deleted) history:
458 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
459 $wgOut->addHTML("<ul>");
460 $target = urlencode( $this->mTarget );
461 while( $row = $revisions->fetchObject() ) {
462 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
463 if ( $this->mAllowed ) {
464 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
465 $pageLink = $sk->makeKnownLinkObj( $titleObj,
466 $wgLang->timeanddate( $ts, true ),
467 "target=$target&timestamp=$ts" );
468 } else {
469 $checkBox = '';
470 $pageLink = $wgLang->timeanddate( $ts, true );
471 }
472 $userLink = htmlspecialchars( $row->ar_user_text );
473 if( $row->ar_user ) {
474 $userLink = $sk->makeKnownLinkObj(
475 Title::makeTitle( NS_USER, $row->ar_user_text ),
476 $userLink );
477 } else {
478 $userLink = $sk->makeKnownLinkObj(
479 Title::makeTitle( NS_SPECIAL, 'Contributions' ),
480 $userLink, 'target=' . $row->ar_user_text );
481 }
482 $comment = $sk->commentBlock( $row->ar_comment );
483 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
484
485 }
486 $revisions->free();
487 $wgOut->addHTML("</ul>");
488 if ( $this->mAllowed ) {
489 $wgOut->addHTML( "\n</form>" );
490 }
491
492 return true;
493 }
494
495 function undelete() {
496 global $wgOut;
497 if( !is_null( $this->mTargetObj ) ) {
498 $archive = new PageArchive( $this->mTargetObj );
499 if( $archive->undelete( $this->mTargetTimestamp ) ) {
500 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
501
502 if (NS_IMAGE == $this->mTargetObj->getNamespace()) {
503 /* refresh image metadata cache */
504 new Image( $this->mTargetObj );
505 }
506
507 return true;
508 }
509 }
510 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
511 return false;
512 }
513 }
514
515 ?>