Printable mode cleanup. Now done through stylesheets, <link>ed so that the
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 function wfSpecialUndelete( $par )
4 {
5 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
6
7 if( $par != "" ) $target = $par;
8 if( isset($target ) ) {
9 $t = Title::newFromURL( $target );
10 $title = $t->mDbkeyform;
11 $namespace = $t->mNamespace;
12 if( isset( $timestamp ) ) {
13 return doUndeleteShowRevision( $namespace, $title, $timestamp );
14 }
15 if( isset( $action ) and isset( $restore) and $action == "submit" ) {
16 return doUndeleteArticle( $namespace, $title );
17 }
18 return doUndeleteShowHistory( $namespace, $title );
19 }
20
21 # List undeletable articles
22 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM archive GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
23 $res = wfQuery( $sql, DB_READ );
24
25 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
26 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
27
28 $special = $wgLang->getNsText( Namespace::getSpecial() );
29 $sk = $wgUser->getSkin();
30 $wgOut->addHTML( "<ul>\n" );
31 while ($row = wfFetchObject( $res )) {
32 $n = ($row->ar_namespace ?
33 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
34 $row->ar_title;
35
36 $wgOut->addHTML( "<li>" .
37 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
38 $n, "target=" . urlencode($n) ) . " " .
39 wfMsg( "undeleterevisions", $row->count ) );
40 }
41 $wgOut->addHTML( "</ul>\n" );
42
43 return $ret;
44 }
45
46 /* private */ function doUndeleteShowRevision( $namespace, $title, $timestamp ) {
47 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
48
49 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
50
51 $sql = "SELECT ar_text,ar_flags FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\" AND ar_timestamp={$timestamp}";
52 $ret = wfQuery( $sql, DB_READ );
53 $row = wfFetchObject( $ret );
54
55 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
56 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
57 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) );
58
59 return 0;
60 }
61
62 /* private */ function doUndeleteShowHistory( $namespace, $title ) {
63 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
64
65 $sk = $wgUser->getSkin();
66 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
67
68 # Get text of first revision
69 $sql = "SELECT ar_text FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\"
70 ORDER BY ar_timestamp DESC LIMIT 1";
71 $ret = wfQuery( $sql, DB_READ );
72
73 if( wfNumRows( $ret ) == 0 ) {
74 $wgOut->addWikiText( wfMsg( "nohistory" ) );
75 return 0;
76 }
77 $row = wfFetchObject( $ret );
78 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
79
80 # Get remaining revisions
81 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
82 FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\"
83 ORDER BY ar_timestamp DESC";
84 $ret = wfQuery( $sql, DB_READ );
85 # Ditch first row
86 $row = wfFetchObject( $ret );
87
88 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
89 $action = $titleObj->escapeLocalURL( "action=submit" );
90 $wgOut->addHTML("<p>
91 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
92 <input type=hidden name=\"target\" value=\"{$target}\">
93 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
94 </form>");
95
96 $log = wfGetSQL("cur", "cur_text", "cur_namespace=4 AND cur_title=\"".wfMsg("dellogpage")."\"" );
97 if(preg_match("/^(.*".
98 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "")
99 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
100 $wgOut->addWikiText( $m[1] );
101 }
102
103 $special = $wgLang->getNsText( Namespace::getSpecial() );
104 $wgOut->addHTML("<ul>");
105 while( $row = wfFetchObject( $ret ) ) {
106 $wgOut->addHTML( "<li>" .
107 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
108 $wgLang->timeanddate( $row->ar_timestamp, true ),
109 "target=" . urlencode($target) . "&timestamp={$row->ar_timestamp}" ) . " " .
110 ". . {$row->ar_user_text}" .
111 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
112
113 }
114 $wgOut->addHTML("</ul>");
115
116 return 0;
117 }
118
119 /* private */ function doUndeleteArticle( $namespace, $title )
120 {
121 global $wgUser, $wgOut, $wgLang, $target, $wgDeferredUpdateList;
122 global $wgUseSquid, $wgInternalServer;
123
124 $fname = "doUndeleteArticle";
125
126 if ( "" == $title ) {
127 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
128 return;
129 }
130 $t = addslashes($title);
131
132 # Move article and history from the "archive" table
133 $sql = "SELECT COUNT(*) AS count FROM cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
134 $res = wfQuery( $sql, DB_READ );
135 $row = wfFetchObject( $res );
136 $now = wfTimestampNow();
137
138 if( $row->count == 0) {
139 # Have to create new article...
140 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' ORDER BY ar_timestamp DESC LIMIT 1";
141 $res = wfQuery( $sql, DB_READ, $fname );
142 $s = wfFetchObject( $res );
143 $max = $s->ar_timestamp;
144 $redirect = MagicWord::get( MAG_REDIRECT );
145 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
146
147 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
148 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
149 "SELECT ar_namespace,ar_title,ar_text,ar_comment," .
150 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM archive " .
151 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
152 wfQuery( $sql, DB_WRITE, $fname );
153 $newid = wfInsertId();
154 $oldones = "AND ar_timestamp<{$max}";
155 } else {
156 # If already exists, put history entirely into old table
157 $oldones = "";
158 $newid = 0;
159
160 # But to make the history list show up right, we need to touch it.
161 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
162 wfQuery( $sql, DB_WRITE, $fname );
163
164 # FIXME: Sometimes restored entries will be _newer_ than the current version.
165 # We should merge.
166 }
167
168 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
169 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
170 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
171 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
172 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
173 wfQuery( $sql, DB_WRITE, $fname );
174
175 # Finally, clean up the link tables
176 if( $newid ) {
177 # Create a dummy OutputPage to update the outgoing links
178 # This works at the moment due to good luck. It may stop working in the
179 # future. Damn globals.
180 $dummyOut = new OutputPage();
181 $to = Title::newFromDBKey( $target );
182 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " .
183 "AND cur_namespace={$namespace}", DB_READ, $fname );
184 $row = wfFetchObject( $res );
185 $text = $row->cur_text;
186 $dummyOut->addWikiText( $text );
187 wfFreeResult( $res );
188
189 $u = new LinksUpdate( $newid, $to->getPrefixedDBkey() );
190 array_push( $wgDeferredUpdateList, $u );
191
192 Article::onArticleCreate( $to );
193
194 #TODO: SearchUpdate, etc.
195 }
196
197 # Now that it's safely stored, take it out of the archive
198 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " .
199 "ar_title='{$t}'";
200 wfQuery( $sql, DB_WRITE, $fname );
201
202
203 # Touch the log?
204 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
205 $log->addEntry( wfMsg( "undeletedarticle", $target ), "" );
206
207 $wgOut->addWikiText( wfMsg( "undeletedtext", $target ) );
208 return 0;
209 }
210 ?>