Show pretty localized log names in Recentchanges instead of Special:Log/upload etc
[lhc/web/wiklou.git] / includes / SpecialLog.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 function wfSpecialLog( $par = '' ) {
21 global $wgRequest;
22 $logReader =& new LogReader( $wgRequest );
23 if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
24 $logReader->limitType( $par );
25 }
26 $logViewer =& new LogViewer( $logReader );
27 $logViewer->show();
28 $logReader->cleanUp();
29 }
30
31 class LogReader {
32 var $db, $joinClauses, $whereClauses;
33 var $type = '', $user = '', $title = null;
34
35 function LogReader( $request ) {
36 $this->db =& wfGetDB( DB_SLAVE );
37 $this->setupQuery( $request );
38 }
39
40 function setupQuery( $request ) {
41 $cur = $this->db->tableName( 'cur' );
42 $user = $this->db->tableName( 'user' );
43 $this->joinClauses = array( "LEFT OUTER JOIN $cur ON log_namespace=cur_namespace AND log_title=cur_title" );
44 $this->whereClauses = array( 'user_id=log_user' );
45
46 $this->limitType( $request->getVal( 'type' ) );
47 $this->limitUser( $request->getText( 'user' ) );
48 $this->limitTitle( $request->getText( 'page' ) );
49 $this->limitTime( $request->getVal( 'from' ), '>=' );
50 $this->limitTime( $request->getVal( 'until' ), '<=' );
51
52 list( $this->limit, $this->offset ) = $request->getLimitOffset();
53 }
54
55 function limitType( $type ) {
56 if( empty( $type ) ) {
57 return false;
58 }
59 $this->type = $type;
60 $safetype = $this->db->strencode( $type );
61 $this->whereClauses[] = "log_type='$safetype'";
62 }
63
64 function limitUser( $name ) {
65 $title = Title::makeTitleSafe( NS_USER, $name );
66 if( empty( $name ) || is_null( $title ) ) {
67 return false;
68 }
69 $this->user = str_replace( '_', ' ', $title->getDBkey() );
70 $safename = $this->db->strencode( $this->user );
71 $user = $this->db->tableName( 'user' );
72 $this->whereClauses[] = "user_name='$safename'";
73 }
74
75 function limitTitle( $page ) {
76 $title = Title::newFromText( $page );
77 if( empty( $page ) || is_null( $title ) ) {
78 return false;
79 }
80 $this->title =& $title;
81 $safetitle = $this->db->strencode( $title->getDBkey() );
82 $ns = $title->getNamespace();
83 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
84 }
85
86 function limitTime( $time, $direction ) {
87 # Direction should be a comparison operator
88 if( empty( $time ) ) {
89 return false;
90 }
91 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
92 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
93 }
94
95 function getQuery() {
96 $logging = $this->db->tableName( "logging" );
97 $user = $this->db->tableName( 'user' );
98 $sql = "SELECT log_type, log_action, log_timestamp,
99 log_user, user_name,
100 log_namespace, log_title, cur_id,
101 log_comment FROM $logging, $user ";
102 if( !empty( $this->joinClauses ) ) {
103 $sql .= implode( ',', $this->joinClauses );
104 }
105 if( !empty( $this->whereClauses ) ) {
106 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
107 }
108 $sql .= " ORDER BY log_timestamp DESC ";
109 $sql .= $this->db->limitResult( $this->limit, $this->offset );
110 return $sql;
111 }
112
113 function initQuery() {
114 $this->result = $this->db->query( $this->getQuery() );
115 }
116
117 function fetchObject() {
118 return $this->db->fetchObject( $this->result );
119 }
120
121 function cleanUp() {
122 $this->db->freeResult( $this->result );
123 }
124
125 function queryType() {
126 return $this->type;
127 }
128
129 function queryUser() {
130 return $this->user;
131 }
132
133 function queryTitle() {
134 if( is_null( $this->title ) ) {
135 return '';
136 } else {
137 return $this->title->getPrefixedText();
138 }
139 }
140 }
141
142 class LogViewer {
143 var $reader, $skin;
144
145 function LogViewer( &$reader ) {
146 global $wgUser;
147 $this->skin =& $wgUser->getSkin();
148 $this->reader =& $reader;
149 }
150
151 function show() {
152 global $wgOut;
153 $this->showHeader( $wgOut );
154 $this->showOptions( $wgOut );
155 $this->showPrevNext( $wgOut );
156 $out = "";
157 $this->reader->initQuery();
158 while( $s = $this->reader->fetchObject() ) {
159 $out .= $this->logLine( $s );
160 }
161 $wgOut->addHTML( $out );
162 $this->showPrevNext( $wgOut );
163 }
164
165 # wfMsg( unprotectedarticle, $text )
166 # wfMsg( 'protectedarticle', $text )
167 # wfMsg( 'deletedarticle', $text )
168 # wfMsg( 'uploadedimage', $text )
169 # wfMsg( "blocklogentry", $this->BlockAddress, $this->BlockExpiry );
170 # wfMsg( "bureaucratlogentry", $this->mUser, implode( " ", $rightsNotation ) );
171 # wfMsg( "undeletedarticle", $this->mTarget ),
172 function logLine( $s ) {
173 global $wgLang;
174 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
175 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
176 $time = $wgLang->timeanddate( $s->log_timestamp );
177 if( $s->cur_id ) {
178 $titleLink = $this->skin->makeKnownLinkObj( $title );
179 } else {
180 $titleLink = $this->skin->makeBrokenLinkObj( $title );
181 }
182 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
183 if( '' === $s->log_comment ) {
184 $comment = '';
185 } else {
186 $comment = '(<em>' . $this->skin->formatComment( $s->log_comment ) . '</em>)';
187 }
188
189 $action = LogPage::actionText( $s->log_type, $s->log_action, $titleLink );
190 $out = "<li>$time $userLink $action $comment</li>\n";
191 return $out;
192 }
193
194 function showHeader( &$out ) {
195 $type = $this->reader->queryType();
196 if( LogPage::isLogType( $type ) ) {
197 $out->setPageTitle( LogPage::logName( $type ) );
198 $out->addWikiText( LogPage::logHeader( $type ) );
199 }
200 }
201
202 function showOptions( &$out ) {
203 global $wgScript;
204 $action = htmlspecialchars( $wgScript );
205 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
206 $special = htmlspecialchars( $title->getPrefixedDBkey() );
207 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
208 "<input type='hidden' name='title' value=\"$special\" />\n" .
209 $this->getTypeMenu() .
210 $this->getUserInput() .
211 $this->getTitleInput() .
212 "<input type='submit' />" .
213 "</form>" );
214 }
215
216 function getTypeMenu() {
217 $out = "<select name='type'>\n";
218 foreach( LogPage::validTypes() as $type ) {
219 $text = htmlspecialchars( LogPage::logName( $type ) );
220 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
221 $out .= "<option value=\"$type\"$selected>$text</option>\n";
222 }
223 $out .= "</select>\n";
224 return $out;
225 }
226
227 function getUserInput() {
228 $user = htmlspecialchars( $this->reader->queryUser() );
229 return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
230 }
231
232 function getTitleInput() {
233 $title = htmlspecialchars( $this->reader->queryTitle() );
234 return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
235 }
236
237 function showPrevNext( &$out ) {
238 global $wgLang;
239 $pieces = array();
240 $pieces[] = 'type=' . htmlspecialchars( $this->reader->queryType() );
241 $pieces[] = 'user=' . htmlspecialchars( $this->reader->queryUser() );
242 $pieces[] = 'page=' . htmlspecialchars( $this->reader->queryTitle() );
243 $bits = implode( '&', $pieces );
244 $offset = 0; $limit = 50;
245
246 # TODO: use timestamps instead of offsets to make it more natural
247 # to go huge distances in time
248 $html = wfViewPrevNext( $offset, $limit,
249 $wgLang->specialpage( 'Log' ),
250 $bits,
251 false);
252 $out->addHTML( '<p>' . $html . '</p>' );
253 }
254 }
255
256
257 ?>