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