last round of PHP5 stuff for now.
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 /**
4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
6 *
7 * @package MediaWiki
8 * @subpackage Special pages
9 */
10
11 /** */
12 require_once( 'Revision.php' );
13
14 /**
15 *
16 */
17 function wfSpecialUndelete( $par ) {
18 global $wgRequest;
19
20 $form = new UndeleteForm( $wgRequest, $par );
21 $form->execute();
22 }
23
24 /**
25 *
26 * @package MediaWiki
27 * @subpackage SpecialPage
28 */
29 class PageArchive {
30 public
31 $title;
32
33 function PageArchive( &$title ) {
34 if( is_null( $title ) ) {
35 wfDebugDieBacktrace( 'Archiver() given a null title.');
36 }
37 $this->title =& $title;
38 }
39
40 /**
41 * List all deleted pages recorded in the archive table. Returns result
42 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
43 * namespace/title. Can be called staticaly.
44 *
45 * @return ResultWrapper
46 */
47 /* static */ function listAllPages() {
48 $dbr =& wfGetDB( DB_SLAVE );
49 $archive = $dbr->tableName( 'archive' );
50
51 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
52 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
53
54 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
55 }
56
57 /**
58 * List the revisions of the given page. Returns result wrapper with
59 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
60 *
61 * @return ResultWrapper
62 */
63 function listRevisions() {
64 $dbr =& wfGetDB( DB_SLAVE );
65 $res = $dbr->select( 'archive',
66 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
67 array( 'ar_namespace' => $this->title->getNamespace(),
68 'ar_title' => $this->title->getDBkey() ),
69 'PageArchive::listRevisions',
70 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
71 $ret = $dbr->resultObject( $res );
72 return $ret;
73 }
74
75 /**
76 * Fetch (and decompress if necessary) the stored text for the deleted
77 * revision of the page with the given timestamp.
78 *
79 * @return string
80 */
81 function getRevisionText( $timestamp ) {
82 $fname = 'PageArchive::getRevisionText';
83 $dbr =& wfGetDB( DB_SLAVE );
84 $row = $dbr->selectRow( 'archive',
85 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
86 array( 'ar_namespace' => $this->title->getNamespace(),
87 'ar_title' => $this->title->getDbkey(),
88 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
89 $fname );
90 return $this->getTextFromRow( $row );
91 }
92
93 /**
94 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
95 */
96 function getTextFromRow( $row ) {
97 $fname = 'PageArchive::getTextFromRow';
98
99 if( is_null( $row->ar_text_id ) ) {
100 // An old row from MediaWiki 1.4 or previous.
101 // Text is embedded in this row in classic compression format.
102 return Revision::getRevisionText( $row, "ar_" );
103 } else {
104 // New-style: keyed to the text storage backend.
105 $dbr =& wfGetDB( DB_SLAVE );
106 $text = $dbr->selectRow( 'text',
107 array( 'old_text', 'old_flags' ),
108 array( 'old_id' => $row->ar_text_id ),
109 $fname );
110 return Revision::getRevisionText( $text );
111 }
112 }
113
114
115 /**
116 * Fetch (and decompress if necessary) the stored text of the most
117 * recently edited deleted revision of the page.
118 *
119 * If there are no archived revisions for the page, returns NULL.
120 *
121 * @return string
122 */
123 function getLastRevisionText() {
124 $dbr =& wfGetDB( DB_SLAVE );
125 $row = $dbr->selectRow( 'archive',
126 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
127 array( 'ar_namespace' => $this->title->getNamespace(),
128 'ar_title' => $this->title->getDBkey() ),
129 'PageArchive::getLastRevisionText',
130 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
131 if( $row ) {
132 return $this->getTextFromRow( $row );
133 } else {
134 return NULL;
135 }
136 }
137
138 /**
139 * Quick check if any archived revisions are present for the page.
140 * @return bool
141 */
142 function isDeleted() {
143 $dbr =& wfGetDB( DB_SLAVE );
144 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
145 array( 'ar_namespace' => $this->title->getNamespace(),
146 'ar_title' => $this->title->getDBkey() ) );
147 return ($n > 0);
148 }
149
150 /**
151 * This is the meaty bit -- restores archived revisions of the given page
152 * to the cur/old tables. If the page currently exists, all revisions will
153 * be stuffed into old, otherwise the most recent will go into cur.
154 * The deletion log will be updated with an undeletion notice.
155 *
156 * Returns true on success.
157 *
158 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
159 * @return bool
160 */
161 function undelete( $timestamps, $comment = '' ) {
162 global $wgParser, $wgDBtype;
163
164 $fname = "doUndeleteArticle";
165 $restoreAll = empty( $timestamps );
166 $restoreRevisions = count( $timestamps );
167
168 $dbw =& wfGetDB( DB_MASTER );
169 extract( $dbw->tableNames( 'page', 'archive' ) );
170
171 # Does this page already exist? We'll have to update it...
172 $article = new Article( $this->title );
173 $options = ( $wgDBtype == 'PostgreSQL' )
174 ? '' // pg doesn't support this?
175 : 'FOR UPDATE';
176 $page = $dbw->selectRow( 'page',
177 array( 'page_id', 'page_latest' ),
178 array( 'page_namespace' => $this->title->getNamespace(),
179 'page_title' => $this->title->getDBkey() ),
180 $fname,
181 $options );
182 if( $page ) {
183 # Page already exists. Import the history, and if necessary
184 # we'll update the latest revision field in the record.
185 $newid = 0;
186 $pageId = $page->page_id;
187 $previousRevId = $page->page_latest;
188 } else {
189 # Have to create a new article...
190 $newid = $article->insertOn( $dbw );
191 $pageId = $newid;
192 $previousRevId = 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 $newRevId = $previousRevId;
230
231 while( $row = $dbw->fetchObject( $result ) ) {
232 if( $row->ar_text_id ) {
233 // Revision was deleted in 1.5+; text is in
234 // the regular text table, use the reference.
235 // Specify null here so the so the text is
236 // dereferenced for page length info if needed.
237 $revText = null;
238 } else {
239 // Revision was deleted in 1.4 or earlier.
240 // Text is squashed into the archive row, and
241 // a new text table entry will be created for it.
242 $revText = Revision::getRevisionText( $row, 'ar_' );
243 }
244 $revision = new Revision( array(
245 'page' => $pageId,
246 'id' => $row->ar_rev_id,
247 'text' => $revText,
248 'comment' => $row->ar_comment,
249 'user' => $row->ar_user,
250 'user_text' => $row->ar_user_text,
251 'timestamp' => $row->ar_timestamp,
252 'minor_edit' => $row->ar_minor_edit,
253 'text_id' => $row->ar_text_id,
254 ) );
255 $newRevId = $revision->insertOn( $dbw );
256 }
257
258 if( $revision ) {
259 # FIXME: Update latest if newer as well...
260 if( $newid ) {
261 # FIXME: update article count if changed...
262 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
263
264 # Finally, clean up the link tables
265 $options = new ParserOptions;
266 $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options,
267 true, true, $newRevId );
268 $u = new LinksUpdate( $this->title, $parserOutput );
269 $u->doUpdate();
270
271 #TODO: SearchUpdate, etc.
272 }
273
274 if( $newid ) {
275 Article::onArticleCreate( $this->title );
276 } else {
277 Article::onArticleEdit( $this->title );
278 }
279 } else {
280 # Something went terribly wrong!
281 }
282
283 # Now that it's safely stored, take it out of the archive
284 $dbw->delete( 'archive',
285 /* WHERE */ array(
286 'ar_namespace' => $this->title->getNamespace(),
287 'ar_title' => $this->title->getDBkey(),
288 $oldones ),
289 $fname );
290
291 # Touch the log!
292 $log = new LogPage( 'delete' );
293 if( $restoreAll ) {
294 $reason = $comment;
295 } else {
296 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
297 if( trim( $comment ) != '' )
298 $reason .= ": {$comment}";
299 }
300 $log->addEntry( 'restore', $this->title, $reason );
301
302 return true;
303 }
304 }
305
306 /**
307 *
308 * @package MediaWiki
309 * @subpackage SpecialPage
310 */
311 class UndeleteForm {
312 private
313 $mAction,
314 $mAllowed,
315 $mComment,
316 $mRestore,
317 $mTarget,
318 $mTargetObj,
319 $mTargetTimestamp,
320 $mTimestamp ;
321
322 function UndeleteForm( &$request, $par = "" ) {
323 global $wgUser;
324 $this->mAction = $request->getText( 'action' );
325 $this->mTarget = $request->getText( 'target' );
326 $this->mTimestamp = $request->getText( 'timestamp' );
327
328 $posted = $request->wasPosted() &&
329 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
330 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
331 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
332 $this->mComment = $request->getText( 'wpComment' );
333
334 if( $par != "" ) {
335 $this->mTarget = $par;
336 }
337 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
338 $this->mAllowed = true;
339 } else {
340 $this->mAllowed = false;
341 $this->mTimestamp = '';
342 $this->mRestore = false;
343 }
344 if ( $this->mTarget !== "" ) {
345 $this->mTargetObj = Title::newFromURL( $this->mTarget );
346 } else {
347 $this->mTargetObj = NULL;
348 }
349 if( $this->mRestore ) {
350 $timestamps = array();
351 foreach( $_REQUEST as $key => $val ) {
352 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
353 array_push( $timestamps, $matches[1] );
354 }
355 }
356 rsort( $timestamps );
357 $this->mTargetTimestamp = $timestamps;
358 }
359 }
360
361 function execute() {
362
363 if( is_null( $this->mTargetObj ) ) {
364 return $this->showList();
365 }
366 if( $this->mTimestamp !== '' ) {
367 return $this->showRevision( $this->mTimestamp );
368 }
369 if( $this->mRestore && $this->mAction == "submit" ) {
370 return $this->undelete();
371 }
372 return $this->showHistory();
373 }
374
375 /* private */ function showList() {
376 global $wgLang, $wgContLang, $wgUser, $wgOut;
377 $fname = "UndeleteForm::showList";
378
379 # List undeletable articles
380 $result = PageArchive::listAllPages();
381
382 if ( $this->mAllowed ) {
383 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
384 } else {
385 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
386 }
387 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
388
389 $sk = $wgUser->getSkin();
390 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
391 $wgOut->addHTML( "<ul>\n" );
392 while( $row = $result->fetchObject() ) {
393 $n = ($row->ar_namespace ?
394 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
395 $row->ar_title;
396 $link = $sk->makeKnownLinkObj( $undelete,
397 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
398 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
399 $wgLang->formatNum( $row->count ) ) );
400 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
401 }
402 $result->free();
403 $wgOut->addHTML( "</ul>\n" );
404
405 return true;
406 }
407
408 /* private */ function showRevision( $timestamp ) {
409 global $wgLang, $wgUser, $wgOut;
410 $fname = "UndeleteForm::showRevision";
411
412 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
413
414 $archive =& new PageArchive( $this->mTargetObj );
415 $text = $archive->getRevisionText( $timestamp );
416
417 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
418 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
419 $wgLang->date( $timestamp ) ) . ")\n" );
420
421 if( $this->mPreview ) {
422 $wgOut->addHtml( "<hr />\n" );
423 $wgOut->addWikiText( $text );
424 }
425
426 $self = Title::makeTitle( NS_SPECIAL, "Undelete" );
427
428 $wgOut->addHtml(
429 wfElement( 'textarea', array(
430 'readonly' => true,
431 'cols' => intval( $wgUser->getOption( 'cols' ) ),
432 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
433 $text . "\n" ) .
434 wfOpenElement( 'div' ) .
435 wfOpenElement( 'form', array(
436 'method' => 'post',
437 'action' => $self->getLocalURL( "action=submit" ) ) ) .
438 wfElement( 'input', array(
439 'type' => 'hidden',
440 'name' => 'target',
441 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
442 wfElement( 'input', array(
443 'type' => 'hidden',
444 'name' => 'timestamp',
445 'value' => $timestamp ) ) .
446 wfElement( 'input', array(
447 'type' => 'hidden',
448 'name' => 'wpEditToken',
449 'value' => $wgUser->editToken() ) ) .
450 wfElement( 'input', array(
451 'type' => 'hidden',
452 'name' => 'preview',
453 'value' => '1' ) ) .
454 wfElement( 'input', array(
455 'type' => 'submit',
456 'value' => wfMsg( 'showpreview' ) ) ) .
457 wfCloseElement( 'form' ) .
458 wfCloseElement( 'div' ) );
459 }
460
461 /* private */ function showHistory() {
462 global $wgLang, $wgUser, $wgOut;
463
464 $sk = $wgUser->getSkin();
465 if ( $this->mAllowed ) {
466 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
467 } else {
468 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
469 }
470
471 $archive = new PageArchive( $this->mTargetObj );
472 $text = $archive->getLastRevisionText();
473 if( is_null( $text ) ) {
474 $wgOut->addWikiText( wfMsg( "nohistory" ) );
475 return;
476 }
477 if ( $this->mAllowed ) {
478 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
479 } else {
480 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
481 }
482
483 # List all stored revisions
484 $revisions = $archive->listRevisions();
485
486 if ( $this->mAllowed ) {
487 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
488 $action = $titleObj->getLocalURL( "action=submit" );
489 # Start the form here
490 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action ) );
491 $wgOut->addHtml( $top );
492 }
493
494 # Show relevant lines from the deletion log:
495 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
496 require_once( 'SpecialLog.php' );
497 $logViewer =& new LogViewer(
498 new LogReader(
499 new FauxRequest(
500 array( 'page' => $this->mTargetObj->getPrefixedText(),
501 'type' => 'delete' ) ) ) );
502 $logViewer->showList( $wgOut );
503
504 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
505
506 if( $this->mAllowed ) {
507 # Brief explanation of how it all works
508 $wgOut->addHtml( '<fieldset>' );
509 #$wgOut->addWikiText( wfMsg( 'undeleteextrahelp' ) );
510 # Format the user-visible controls (comment field, submission button)
511 # in a nice little table
512 $table = '<table><tr>';
513 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
514 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
515 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
516 $table .= '</tr><tr><td>&nbsp;</td><td>';
517 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
518 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
519 $table .= '</td></tr></table></fieldset>';
520 $wgOut->addHtml( $table );
521 }
522
523 # The page's stored (deleted) history:
524 $wgOut->addHTML("<ul>");
525 $target = urlencode( $this->mTarget );
526 while( $row = $revisions->fetchObject() ) {
527 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
528 if ( $this->mAllowed ) {
529 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
530 $pageLink = $sk->makeKnownLinkObj( $titleObj,
531 $wgLang->timeanddate( $ts, true ),
532 "target=$target&timestamp=$ts" );
533 } else {
534 $checkBox = '';
535 $pageLink = $wgLang->timeanddate( $ts, true );
536 }
537 $userLink = htmlspecialchars( $row->ar_user_text );
538 if( $row->ar_user ) {
539 $userLink = $sk->makeKnownLinkObj(
540 Title::makeTitle( NS_USER, $row->ar_user_text ),
541 $userLink );
542 } else {
543 $userLink = $sk->makeKnownLinkObj(
544 Title::makeTitle( NS_SPECIAL, 'Contributions' ),
545 $userLink, 'target=' . $row->ar_user_text );
546 }
547 $comment = $sk->commentBlock( $row->ar_comment );
548 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
549
550 }
551 $revisions->free();
552 $wgOut->addHTML("</ul>");
553
554 if ( $this->mAllowed ) {
555 # Slip in the hidden controls here
556 $misc = wfHidden( 'target', $this->mTarget );
557 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
558 $wgOut->addHtml( $misc . '</form>' );
559 }
560
561 return true;
562 }
563
564 function undelete() {
565 global $wgOut;
566 if( !is_null( $this->mTargetObj ) ) {
567 $archive = new PageArchive( $this->mTargetObj );
568 if( $archive->undelete( $this->mTargetTimestamp, $this->mComment ) ) {
569 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
570
571 if (NS_IMAGE == $this->mTargetObj->getNamespace()) {
572 /* refresh image metadata cache */
573 new Image( $this->mTargetObj );
574 }
575
576 return true;
577 }
578 }
579 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
580 return false;
581 }
582 }
583
584 ?>