Changing comments layout preparing for generated documentation with Phpdocumentor
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2 /**
3 *
4 */
5
6 /**
7 *
8 */
9 function wfSpecialUndelete( $par ) {
10 global $wgRequest;
11
12 $form = new UndeleteForm( $wgRequest, $par );
13 $form->execute();
14 }
15
16 /**
17 *
18 */
19 class PageArchive {
20 var $title;
21
22 function PageArchive( &$title ) {
23 if( is_null( $title ) ) {
24 wfDebugDieBacktrace( 'Archiver() given a null title.');
25 }
26 $this->title =& $title;
27 }
28
29 /* static */ function &listAllPages() {
30 $dbr =& wfGetDB( DB_SLAVE );
31 $archive = $dbr->tableName( 'archive' );
32
33 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
34 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
35
36 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
37 }
38
39 function &listRevisions() {
40 $dbr =& wfGetDB( DB_SLAVE );
41 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
42 FROM archive WHERE ar_namespace=" . $this->title->getNamespace() .
43 " AND ar_title='" . $dbr->strencode( $this->title->getDBkey() ) .
44 "' ORDER BY ar_timestamp DESC";
45 return $dbr->resultObject( $dbr->query( $sql ) );
46 }
47
48 function getRevisionText( $timestamp ) {
49 $dbr =& wfGetDB( DB_SLAVE );
50 $row = $dbr->selectRow( 'archive',
51 array( 'ar_text', 'ar_flags' ),
52 array( 'ar_namespace' => $this->title->getNamespace(),
53 'ar_title' => $this->title->getDbkey(),
54 'ar_timestamp' => $timestamp ) );
55 return Article::getRevisionText( $row, "ar_" );
56 }
57
58 function getLastRevisionText() {
59 $dbr =& wfGetDB( DB_SLAVE );
60 $archive = $dbr->tableName( 'archive' );
61
62 # Get text of first revision
63 $sql = "SELECT ar_text,ar_flags FROM $archive WHERE ar_namespace=" . $this->title->getNamespace() . " AND ar_title='" .
64 $dbr->strencode( $this->title->getDBkey() ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
65 $ret = $dbr->query( $sql );
66 $row = $dbr->fetchObject( $ret );
67 $dbr->freeResult( $ret );
68 if( $row ) {
69 return Article::getRevisionText( $row, "ar_" );
70 } else {
71 return NULL;
72 }
73 }
74
75 function isDeleted() {
76 $dbr =& wfGetDB( DB_SLAVE );
77 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
78 array( 'ar_namespace' => $this->title->getNamespace(),
79 'ar_title' => $this->title->getDBkey() ) );
80 return ($n > 0);
81 }
82
83 function undelete() {
84 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
85 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
86
87 $fname = "doUndeleteArticle";
88
89 $dbw =& wfGetDB( DB_MASTER );
90 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
91 $namespace = $this->title->getNamespace();
92 $t = $dbw->strencode( $this->title->getDBkey() );
93
94 # Move article and history from the "archive" table
95 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
96 $res = $dbw->query( $sql, $fname );
97 $row = $dbw->fetchObject( $res );
98 $now = wfTimestampNow();
99
100 if( $row->count == 0) {
101 # Have to create new article...
102 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
103 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
104 $res = $dbw->query( $sql, $fname );
105 $s = $dbw->fetchObject( $res );
106 $max = $s->ar_timestamp;
107 $redirect = MagicWord::get( MAG_REDIRECT );
108 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
109
110 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
111
112 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
113 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
114 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
115 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
116 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
117 $dbw->query( $sql, $fname );
118 $newid = $dbw->insertId();
119 $oldones = "AND ar_timestamp<{$max}";
120 } else {
121 # If already exists, put history entirely into old table
122 $oldones = "";
123 $newid = 0;
124
125 # But to make the history list show up right, we need to touch it.
126 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
127 $dbw->query( $sql, $fname );
128
129 # FIXME: Sometimes restored entries will be _newer_ than the current version.
130 # We should merge.
131 }
132
133 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
134 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
135 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
136 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
137 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
138 $dbw->query( $sql, $fname );
139
140 # Finally, clean up the link tables
141 if( $newid ) {
142 $wgLinkCache = new LinkCache();
143 # Select for update
144 $wgLinkCache->forUpdate( true );
145 # Create a dummy OutputPage to update the outgoing links
146 $dummyOut = new OutputPage();
147 # Get the text
148 $text = $dbw->selectField( 'cur', 'cur_text',
149 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
150 $fname, 'FOR UPDATE'
151 );
152 $dummyOut->addWikiText( $text );
153
154 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
155 array_push( $wgDeferredUpdateList, $u );
156
157 Article::onArticleCreate( $this->title );
158
159 #TODO: SearchUpdate, etc.
160 }
161
162 # Now that it's safely stored, take it out of the archive
163 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
164 "ar_title='{$t}'";
165 $dbw->query( $sql, $fname );
166
167
168 # Touch the log?
169 $log = new LogPage( 'delete' );
170 $log->addEntry( 'restore', $this->title, "" );
171
172 return true;
173 }
174 }
175
176 /**
177 *
178 */
179 class UndeleteForm {
180 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
181
182 function UndeleteForm( &$request, $par = "" ) {
183 $this->mAction = $request->getText( 'action' );
184 $this->mTarget = $request->getText( 'target' );
185 $this->mTimestamp = $request->getText( 'timestamp' );
186 $this->mRestore = $request->getCheck( 'restore' ) && $request->wasPosted();
187 if( $par != "" ) {
188 $this->mTarget = $par;
189 }
190 if ( $this->mTarget !== "" ) {
191 $this->mTargetObj = Title::newFromURL( $this->mTarget );
192 } else {
193 $this->mTargetObj = NULL;
194 }
195 }
196
197 function execute() {
198 if( is_null( $this->mTargetObj ) ) {
199 return $this->showList();
200 }
201 if( $this->mTimestamp !== "" ) {
202 return $this->showRevision( $this->mTimestamp );
203 }
204 if( $this->mRestore && $this->mAction == "submit" ) {
205 return $this->undelete();
206 }
207 return $this->showHistory();
208 }
209
210 /* private */ function showList() {
211 global $wgLang, $wgUser, $wgOut;
212 $fname = "UndeleteForm::showList";
213
214 # List undeletable articles
215 $result = PageArchive::listAllPages();
216
217 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
218 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
219
220 $sk = $wgUser->getSkin();
221 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
222 $wgOut->addHTML( "<ul>\n" );
223 while( $row = $result->fetchObject() ) {
224 $n = ($row->ar_namespace ?
225 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
226 $row->ar_title;
227 $link = $sk->makeKnownLinkObj( $undelete,
228 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
229 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
230 $wgLang->formatNum( $row->count ) ) );
231 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
232 }
233 $result->free();
234 $wgOut->addHTML( "</ul>\n" );
235
236 return true;
237 }
238
239 /* private */ function showRevision( $timestamp ) {
240 global $wgLang, $wgUser, $wgOut;
241 $fname = "UndeleteForm::showRevision";
242
243 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
244
245 $archive =& new PageArchive( $this->mTargetObj );
246 $text = $archive->getRevisionText( $timestamp );
247
248 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
249 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
250 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
251 }
252
253 /* private */ function showHistory() {
254 global $wgLang, $wgUser, $wgOut;
255
256 $sk = $wgUser->getSkin();
257 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
258
259 $archive = new PageArchive( $this->mTargetObj );
260 $text = $archive->getLastRevisionText();
261 if( is_null( $text ) ) {
262 $wgOut->addWikiText( wfMsg( "nohistory" ) );
263 return;
264 }
265 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
266
267 # List all stored revisions
268 $revisions = $archive->listRevisions();
269
270 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
271 $action = $titleObj->escapeLocalURL( "action=submit" );
272 $encTarget = htmlspecialchars( $this->mTarget );
273 $button = htmlspecialchars( wfMsg("undeletebtn") );
274
275 $wgOut->addHTML("
276 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
277 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
278 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
279 </form>");
280
281 # Show relevant lines from the deletion log:
282 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
283 require_once( 'SpecialLog.php' );
284 $logViewer =& new LogViewer(
285 new LogReader(
286 new FauxRequest(
287 array( 'page', $this->mTargetObj->getPrefixedText() ) ) ) );
288 $logViewer->showList( $wgOut );
289
290 # The page's stored (deleted) history:
291 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
292 $wgOut->addHTML("<ul>");
293 $target = urlencode( $this->mTarget );
294 while( $row = $revisions->fetchObject() ) {
295 $pageLink = $sk->makeKnownLinkObj( $titleObj,
296 $wgLang->timeanddate( $row->ar_timestamp, true ),
297 "target=$target&timestamp={$row->ar_timestamp}" );
298 $userLink = htmlspecialchars( $row->ar_user_text );
299 if( $row->ar_user ) {
300 $userLink = $sk->makeKnownLinkObj(
301 Title::makeTitle( NS_USER, $row->ar_user_text ),
302 $userLink );
303 }
304 $comment = $sk->formatComment( $row->ar_comment );
305 $wgOut->addHTML( "<li>$pageLink . . $userLink (<i>$comment</i>)</li>\n" );
306
307 }
308 $revisions->free();
309 $wgOut->addHTML("</ul>");
310
311 return true;
312 }
313
314 function undelete() {
315 global $wgOut;
316 if( !is_null( $this->mTargetObj ) ) {
317 $archive = new PageArchive( $this->mTargetObj );
318 if( $archive->undelete() ) {
319 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
320 return true;
321 }
322 }
323 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
324 return false;
325 }
326 }
327
328 ?>