3fda4d301767415137da9e001aabc1307c9d6066
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?
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 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" . $row->ar_text );
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 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
68
69 $action = wfLocalUrlE( $wgLang->specialPage( "Undelete" ), "action=submit" );
70 $wgOut->addHTML("<p>
71 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
72 <input type=hidden name=\"target\" value=\"{$target}\">
73 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
74 </form>");
75
76 $log = wfGetSQL("cur", "cur_text", "cur_namespace=4 AND cur_title=\"".wfMsg("dellogpage")."\"" );
77 if(preg_match("/^(.*".
78 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "")
79 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
80 $wgOut->addWikiText( $m[1] );
81 }
82
83 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
84 FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\"
85 ORDER BY ar_timestamp DESC";
86 $ret = wfQuery( $sql, DB_READ );
87
88 $special = $wgLang->getNsText( Namespace::getSpecial() );
89 $wgOut->addHTML("<ul>");
90 while( $row = wfFetchObject( $ret ) ) {
91 $wgOut->addHTML( "<li>" .
92 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
93 $wgLang->timeanddate( $row->ar_timestamp, true ),
94 "target=" . urlencode($target) . "&timestamp={$row->ar_timestamp}" ) . " " .
95 ". . {$row->ar_user_text}" .
96 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
97
98 }
99 $wgOut->addHTML("</ul>");
100
101 return 0;
102 }
103
104 /* private */ function doUndeleteArticle( $namespace, $title )
105 {
106 global $wgUser, $wgOut, $wgLang, $target, $wgDeferredUpdateList;
107
108 $fname = "doUndeleteArticle";
109
110 if ( "" == $title ) {
111 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
112 return;
113 }
114 $t = addslashes($title);
115
116 # Move article and history from the "archive" table
117 $sql = "SELECT COUNT(*) AS count FROM cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
118 $res = wfQuery( $sql, DB_READ );
119 $row = wfFetchObject( $res );
120 if( $row->count == 0) {
121 # Have to create new article...
122 $now = wfTimestampNow();
123 $max = wfGetSQL( "archive", "MAX(ar_timestamp)", "ar_namespace={$namespace} AND ar_title='{$t}'" );
124 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
125 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_random,cur_touched)" .
126 "SELECT ar_namespace,ar_title,ar_text,ar_comment," .
127 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,RAND(),'{$now}' FROM archive " .
128 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
129 wfQuery( $sql, DB_WRITE, $fname );
130 $newid = wfInsertId();
131 $oldones = "AND ar_timestamp<{$max}";
132 } else {
133 # If already exists, put history entirely into old table
134 $oldones = "";
135 $newid = 0;
136 }
137
138 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
139 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
140 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
141 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
142 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
143 wfQuery( $sql, DB_WRITE, $fname );
144
145 # Finally, clean up the link tables
146 if( $newid ) {
147 # Create a dummy OutputPage to update the outgoing links
148 # This works at the moment due to good luck. It may stop working in the
149 # future. Damn globals.
150 $dummyOut = new OutputPage();
151 $to = Title::newFromDBKey( $target );
152 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " .
153 "AND cur_namespace={$namespace}", DB_READ, $fname );
154 $row = wfFetchObject( $res );
155 $text = $row->cur_text;
156 $dummyOut->addWikiText( $text );
157 wfFreeResult( $res );
158
159 $u = new LinksUpdate( $newid, $to->getPrefixedDBkey() );
160 array_push( $wgDeferredUpdateList, $u );
161
162 global $wgEnablePersistentLC;
163 if ( $wgEnablePersistentLC ) {
164 // Purge related entries in links cache on undelete, to heal broken links
165 $ptitle = wfStrencode( $to->getPrefixedDBkey() );
166 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
167 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE);
168 }
169 #TODO: SearchUpdate, etc.
170 }
171
172 # Now that it's safely stored, take it out of the archive
173 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " .
174 "ar_title='{$t}'";
175 wfQuery( $sql, DB_WRITE, $fname );
176
177
178 # Touch the log?
179 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
180 $log->addEntry( wfMsg( "undeletedarticle", $target ), "" );
181
182 $wgOut->addWikiText( wfMsg( "undeletedtext", $target ) );
183 return 0;
184 }
185 ?>