b4d66ea8c144e1ee245e63a51000f2a9fa1d0c2a
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?
2
3 function wfSpecialUndelete( )
4 {
5 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
6
7 if( isset($target ) ) {
8 $t = Title::newFromURL( $target );
9 $title = $t->mDbkeyform;
10 $namespace = $t->mNamespace;
11 if( isset( $timestamp ) ) {
12 return doUndeleteShowRevision( $namespace, $title, $timestamp );
13 }
14 if( isset( $action ) and isset( $restore) and $action == "submit" ) {
15 return doUndeleteArticle( $namespace, $title );
16 }
17 return doUndeleteShowHistory( $namespace, $title );
18 }
19
20 # List undeletable articles
21 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM archive GROUP BY ar_namespace,ar_title ORDER BY ar_title";
22 $res = wfQuery( $sql );
23
24 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
25 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
26
27 $special = $wgLang->getNsText( Namespace::getSpecial() );
28 $sk = $wgUser->getSkin();
29 $wgOut->addHTML( "<ul>\n" );
30 while ($row = wfFetchObject( $res )) {
31 $n = ($row->ar_namespace ?
32 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
33 $row->ar_title;
34
35 $wgOut->addHTML( "<li>" .
36 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
37 $n, "target=" . urlencode($n) ) . " " .
38 str_replace( '$1', $row->count, wfMsg( "undeleterevisions" )) );
39 }
40 $wgOut->addHTML( "</ul>\n" );
41
42 return $ret;
43 }
44
45 /* private */ function doUndeleteShowRevision( $namespace, $title, $timestamp ) {
46 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
47
48 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
49
50 $sql = "SELECT ar_text FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\" AND ar_timestamp={$timestamp}";
51 $ret = wfQuery( $sql );
52 $row = wfFetchObject( $ret );
53
54 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
55 $wgOut->addWikiText( "(" . str_replace("$1",
56 $wgLang->date($timestamp, true), wfMsg( "undeleterevision" ))
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 );
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 );
119 $row = wfFetchObject( $res );
120 if( $row->count == 0) {
121 # Have to create new article...
122 $max = wfGetSQL( "archive", "MAX(ar_timestamp)", "ar_namespace={$namespace} AND ar_title='{$t}'" );
123 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
124 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_random)" .
125 "SELECT ar_namespace,ar_title,ar_text,ar_comment," .
126 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,RAND() FROM archive " .
127 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
128 wfQuery( $sql, $fname );
129 $newid = wfInsertId();
130 $oldones = "AND ar_timestamp<{$max}";
131 } else {
132 # If already exists, put history entirely into old table
133 $oldones = "";
134 $newid = 0;
135 }
136
137 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
138 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
139 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
140 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
141 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
142 wfQuery( $sql, $fname );
143
144 # Finally, clean up the link tables
145 if( $newid ) {
146 # Create a dummy OutputPage to update the outgoing links
147 # This works at the moment due to good luck. It may stop working in the
148 # future. Damn globals.
149 $dummyOut = new OutputPage();
150 $to = Title::newFromDBKey( $target );
151 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " .
152 "AND cur_namespace={$namespace}", $fname );
153 $row = wfFetchObject( $res );
154 $text = $row->cur_text;
155 $dummyOut->addWikiText( $text );
156 wfFreeResult( $res );
157
158 $u = new LinksUpdate( $newid, $to->getPrefixedDBkey() );
159 array_push( $wgDeferredUpdateList, $u );
160
161 #TODO: SearchUpdate, etc.
162 }
163
164 # Now that it's safely stored, take it out of the archive
165 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " .
166 "ar_title='{$t}'";
167 wfQuery( $sql, $fname );
168
169
170 # Touch the log?
171
172 $wgOut->addWikiText(str_replace("$1", $target, wfMsg("undeletedtext")));
173 return 0;
174 }
175 ?>