Work through the NFC substeps with the actual data to make the substep times more...
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 function wfSpecialUndelete( $par ) {
12 global $wgRequest;
13
14 $form = new UndeleteForm( $wgRequest, $par );
15 $form->execute();
16 }
17
18 /**
19 *
20 * @package MediaWiki
21 * @subpackage SpecialPage
22 */
23 class PageArchive {
24 var $title;
25
26 function PageArchive( &$title ) {
27 if( is_null( $title ) ) {
28 wfDebugDieBacktrace( 'Archiver() given a null title.');
29 }
30 $this->title =& $title;
31 }
32
33 /**
34 * List all deleted pages recorded in the archive table. Returns result
35 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
36 * namespace/title. Can be called staticaly.
37 *
38 * @return ResultWrapper
39 */
40 /* static */ function &listAllPages() {
41 $dbr =& wfGetDB( DB_SLAVE );
42 $archive = $dbr->tableName( 'archive' );
43
44 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
45 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
46
47 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
48 }
49
50 /**
51 * List the revisions of the given page. Returns result wrapper with
52 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
53 *
54 * @return ResultWrapper
55 */
56 function &listRevisions() {
57 $dbr =& wfGetDB( DB_SLAVE );
58 return $dbr->resultObject( $dbr->select( 'archive',
59 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
60 array( 'ar_namespace' => $this->title->getNamespace(),
61 'ar_title' => $this->title->getDBkey() ),
62 'PageArchive::listRevisions',
63 array( 'ORDER BY' => 'ar_timestamp DESC' ) ) );
64 }
65
66 /**
67 * Fetch (and decompress if necessary) the stored text for the deleted
68 * revision of the page with the given timestamp.
69 *
70 * @return string
71 */
72 function getRevisionText( $timestamp ) {
73 $dbr =& wfGetDB( DB_SLAVE );
74 $row = $dbr->selectRow( 'archive',
75 array( 'ar_text', 'ar_flags' ),
76 array( 'ar_namespace' => $this->title->getNamespace(),
77 'ar_title' => $this->title->getDbkey(),
78 'ar_timestamp' => $dbr->timestamp( $timestamp ) ) );
79 return Article::getRevisionText( $row, "ar_" );
80 }
81
82 /**
83 * Fetch (and decompress if necessary) the stored text of the most
84 * recently edited deleted revision of the page.
85 *
86 * If there are no archived revisions for the page, returns NULL.
87 *
88 * @return string
89 */
90 function getLastRevisionText() {
91 $dbr =& wfGetDB( DB_SLAVE );
92 $row = $dbr->selectRow( 'archive',
93 array( 'ar_text', 'ar_flags' ),
94 array( 'ar_namespace' => $this->title->getNamespace(),
95 'ar_title' => $this->title->getDBkey() ),
96 'PageArchive::getLastRevisionText',
97 'ORDER BY ar_timestamp DESC LIMIT 1' );
98 if( $row ) {
99 return Article::getRevisionText( $row, "ar_" );
100 } else {
101 return NULL;
102 }
103 }
104
105 /**
106 * Quick check if any archived revisions are present for the page.
107 * @return bool
108 */
109 function isDeleted() {
110 $dbr =& wfGetDB( DB_SLAVE );
111 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
112 array( 'ar_namespace' => $this->title->getNamespace(),
113 'ar_title' => $this->title->getDBkey() ) );
114 return ($n > 0);
115 }
116
117 /**
118 * This is the meaty bit -- restores archived revisions of the given page
119 * to the cur/old tables. If the page currently exists, all revisions will
120 * be stuffed into old, otherwise the most recent will go into cur.
121 * The deletion log will be updated with an undeletion notice.
122 *
123 * Returns true on success.
124 *
125 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
126 * @return bool
127 */
128 function undelete( $timestamps ) {
129 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
130 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
131
132 $fname = "doUndeleteArticle";
133 $restoreAll = empty( $timestamps );
134 $restoreRevisions = count( $timestamps );
135
136 $dbw =& wfGetDB( DB_MASTER );
137 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
138 $namespace = $this->title->getNamespace();
139 $ttl = $this->title->getDBkey();
140 $t = $dbw->strencode( $ttl );
141
142 # Move article and history from the "archive" table
143 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
144 global $wgDBtype;
145 if( $wgDBtype != 'PostgreSQL' ) { # HACKHACKHACKHACK
146 $sql .= ' FOR UPDATE';
147 }
148 $res = $dbw->query( $sql, $fname );
149 $row = $dbw->fetchObject( $res );
150 $now = wfTimestampNow();
151
152 if( $row->count == 0) {
153 # Have to create new article...
154 $sql = "SELECT ar_text,ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' ";
155 if( !$restoreAll ) {
156 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
157 $sql .= "AND ar_timestamp={$max} ";
158 }
159 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
160 $res = $dbw->query( $sql, $fname );
161 $s = $dbw->fetchObject( $res );
162 if( $restoreAll ) {
163 $max = $s->ar_timestamp;
164 }
165 $text = Article::getRevisionText( $s, "ar_" );
166
167 $redirect = MagicWord::get( MAG_REDIRECT );
168 $redir = $redirect->matchStart( $text ) ? 1 : 0;
169
170 $rand = wfRandom();
171 $dbw->insert( 'cur', array(
172 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
173 'cur_namespace' => $namespace,
174 'cur_title' => $ttl,
175 'cur_text' => $text,
176 'cur_comment' => $s->ar_comment,
177 'cur_user' => $s->ar_user,
178 'cur_timestamp' => $s->ar_timestamp,
179 'cur_minor_edit' => $s->ar_minor_edit,
180 'cur_user_text' => $s->ar_user_text,
181 'cur_is_redirect' => $redir,
182 'cur_random' => $rand,
183 'cur_touched' => $dbw->timestamp( $now ),
184 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW, $s->ar_timestamp ) ),
185 ), $fname );
186
187 $newid = $dbw->insertId();
188 if( $restoreAll ) {
189 $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) );
190 }
191 } else {
192 # If already exists, put history entirely into old table
193 $oldones = "";
194 $newid = 0;
195
196 # But to make the history list show up right, we need to touch it.
197 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
198 $dbw->query( $sql, $fname );
199
200 # FIXME: Sometimes restored entries will be _newer_ than the current version.
201 # We should merge.
202 }
203
204 if( !$restoreAll ) {
205 $oldts = array();
206 foreach( $timestamps as $ts ) {
207 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
208 }
209 $oldts = join( ",", $oldts );
210 $oldones = "AND ar_timestamp IN ( {$oldts} )";
211 }
212 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
213 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
214 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
215 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
216 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
217 if( $restoreAll || !empty( $oldts ) ) {
218 $dbw->query( $sql, $fname );
219 }
220
221 # Finally, clean up the link tables
222 if( $newid ) {
223 $wgLinkCache = new LinkCache();
224 # Select for update
225 $wgLinkCache->forUpdate( true );
226 # Create a dummy OutputPage to update the outgoing links
227 $dummyOut = new OutputPage();
228 $dummyOut->addWikiText( $text );
229
230 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
231 array_push( $wgDeferredUpdateList, $u );
232
233 Article::onArticleCreate( $this->title );
234
235 #TODO: SearchUpdate, etc.
236 }
237
238 # Now that it's safely stored, take it out of the archive
239 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
240 "ar_title='{$t}'";
241 if( !$restoreAll ) {
242 $sql .= " AND ar_timestamp IN ( {$oldts}";
243 if( $newid ) {
244 if( !empty( $oldts ) ) $sql .= ",";
245 $sql .= $max;
246 }
247 $sql .= ")";
248 }
249 $dbw->query( $sql, $fname );
250
251
252 # Touch the log?
253 $log = new LogPage( 'delete' );
254 if( $restoreAll ) {
255 $reason = "";
256 } else {
257 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
258 }
259 $log->addEntry( 'restore', $this->title, $reason );
260
261 return true;
262 }
263 }
264
265 /**
266 *
267 * @package MediaWiki
268 * @subpackage SpecialPage
269 */
270 class UndeleteForm {
271 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
272 var $mTargetTimestamp;
273
274 function UndeleteForm( &$request, $par = "" ) {
275 $this->mAction = $request->getText( 'action' );
276 $this->mTarget = $request->getText( 'target' );
277 $this->mTimestamp = $request->getText( 'timestamp' );
278 $this->mRestore = $request->getCheck( 'restore' ) && $request->wasPosted();
279 if( $par != "" ) {
280 $this->mTarget = $par;
281 }
282 if ( $this->mTarget !== "" ) {
283 $this->mTargetObj = Title::newFromURL( $this->mTarget );
284 } else {
285 $this->mTargetObj = NULL;
286 }
287 if( $this->mRestore ) {
288 $timestamps = array();
289 foreach( $_REQUEST as $key => $val ) {
290 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
291 array_push( $timestamps, $matches[1] );
292 }
293 }
294 rsort( $timestamps );
295 $this->mTargetTimestamp = $timestamps;
296 }
297 }
298
299 function execute() {
300 if( is_null( $this->mTargetObj ) ) {
301 return $this->showList();
302 }
303 if( $this->mTimestamp !== "" ) {
304 return $this->showRevision( $this->mTimestamp );
305 }
306 if( $this->mRestore && $this->mAction == "submit" ) {
307 return $this->undelete();
308 }
309 return $this->showHistory();
310 }
311
312 /* private */ function showList() {
313 global $wgLang, $wgContLang, $wgUser, $wgOut;
314 $fname = "UndeleteForm::showList";
315
316 # List undeletable articles
317 $result = PageArchive::listAllPages();
318
319 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
320 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
321
322 $sk = $wgUser->getSkin();
323 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
324 $wgOut->addHTML( "<ul>\n" );
325 while( $row = $result->fetchObject() ) {
326 $n = ($row->ar_namespace ?
327 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
328 $row->ar_title;
329 $link = $sk->makeKnownLinkObj( $undelete,
330 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
331 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
332 $wgLang->formatNum( $row->count ) ) );
333 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
334 }
335 $result->free();
336 $wgOut->addHTML( "</ul>\n" );
337
338 return true;
339 }
340
341 /* private */ function showRevision( $timestamp ) {
342 global $wgLang, $wgUser, $wgOut;
343 $fname = "UndeleteForm::showRevision";
344
345 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
346
347 $archive =& new PageArchive( $this->mTargetObj );
348 $text = $archive->getRevisionText( $timestamp );
349
350 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
351 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
352 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
353 }
354
355 /* private */ function showHistory() {
356 global $wgLang, $wgUser, $wgOut;
357
358 $sk = $wgUser->getSkin();
359 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
360
361 $archive = new PageArchive( $this->mTargetObj );
362 $text = $archive->getLastRevisionText();
363 if( is_null( $text ) ) {
364 $wgOut->addWikiText( wfMsg( "nohistory" ) );
365 return;
366 }
367 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
368
369 # List all stored revisions
370 $revisions = $archive->listRevisions();
371
372 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
373 $action = $titleObj->escapeLocalURL( "action=submit" );
374 $encTarget = htmlspecialchars( $this->mTarget );
375 $button = htmlspecialchars( wfMsg("undeletebtn") );
376
377 $wgOut->addHTML("
378 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
379 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
380 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
381 ");
382
383 # Show relevant lines from the deletion log:
384 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
385 require_once( 'SpecialLog.php' );
386 $logViewer =& new LogViewer(
387 new LogReader(
388 new FauxRequest(
389 array( 'page' => $this->mTargetObj->getPrefixedText(),
390 'type' => 'delete' ) ) ) );
391 $logViewer->showList( $wgOut );
392
393 # The page's stored (deleted) history:
394 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
395 $wgOut->addHTML("<ul>");
396 $target = urlencode( $this->mTarget );
397 while( $row = $revisions->fetchObject() ) {
398 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
399 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
400 $pageLink = $sk->makeKnownLinkObj( $titleObj,
401 $wgLang->timeanddate( $row->ar_timestamp, true ),
402 "target=$target&timestamp=$ts" );
403 $userLink = htmlspecialchars( $row->ar_user_text );
404 if( $row->ar_user ) {
405 $userLink = $sk->makeKnownLinkObj(
406 Title::makeTitle( NS_USER, $row->ar_user_text ),
407 $userLink );
408 }
409 $comment = $sk->formatComment( $row->ar_comment );
410 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink (<i>$comment</i>)</li>\n" );
411
412 }
413 $revisions->free();
414 $wgOut->addHTML("</ul>\n</form>");
415
416 return true;
417 }
418
419 function undelete() {
420 global $wgOut;
421 if( !is_null( $this->mTargetObj ) ) {
422 $archive = new PageArchive( $this->mTargetObj );
423 if( $archive->undelete( $this->mTargetTimestamp ) ) {
424 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
425 return true;
426 }
427 }
428 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
429 return false;
430 }
431 }
432
433 ?>