Changing comments layout preparing for generated documentation with Phpdocumentor
[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 /**
21 *
22 */
23
24 /**
25 * constructor
26 */
27 function wfSpecialLog( $par = '' ) {
28 global $wgRequest;
29 $logReader =& new LogReader( $wgRequest );
30 if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
31 $logReader->limitType( $par );
32 }
33 $logViewer =& new LogViewer( $logReader );
34 $logViewer->show();
35 }
36
37 /**
38 *
39 */
40 class LogReader {
41 var $db, $joinClauses, $whereClauses;
42 var $type = '', $user = '', $title = null;
43
44 function LogReader( $request ) {
45 $this->db =& wfGetDB( DB_SLAVE );
46 $this->setupQuery( $request );
47 }
48
49 function setupQuery( $request ) {
50 $cur = $this->db->tableName( 'cur' );
51 $user = $this->db->tableName( 'user' );
52 $this->joinClauses = array( "LEFT OUTER JOIN $cur ON log_namespace=cur_namespace AND log_title=cur_title" );
53 $this->whereClauses = array( 'user_id=log_user' );
54
55 $this->limitType( $request->getVal( 'type' ) );
56 $this->limitUser( $request->getText( 'user' ) );
57 $this->limitTitle( $request->getText( 'page' ) );
58 $this->limitTime( $request->getVal( 'from' ), '>=' );
59 $this->limitTime( $request->getVal( 'until' ), '<=' );
60
61 list( $this->limit, $this->offset ) = $request->getLimitOffset();
62 }
63
64 function limitType( $type ) {
65 if( empty( $type ) ) {
66 return false;
67 }
68 $this->type = $type;
69 $safetype = $this->db->strencode( $type );
70 $this->whereClauses[] = "log_type='$safetype'";
71 }
72
73 function limitUser( $name ) {
74 $title = Title::makeTitleSafe( NS_USER, $name );
75 if( empty( $name ) || is_null( $title ) ) {
76 return false;
77 }
78 $this->user = str_replace( '_', ' ', $title->getDBkey() );
79 $safename = $this->db->strencode( $this->user );
80 $user = $this->db->tableName( 'user' );
81 $this->whereClauses[] = "user_name='$safename'";
82 }
83
84 function limitTitle( $page ) {
85 $title = Title::newFromText( $page );
86 if( empty( $page ) || is_null( $title ) ) {
87 return false;
88 }
89 $this->title =& $title;
90 $safetitle = $this->db->strencode( $title->getDBkey() );
91 $ns = $title->getNamespace();
92 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
93 }
94
95 function limitTime( $time, $direction ) {
96 # Direction should be a comparison operator
97 if( empty( $time ) ) {
98 return false;
99 }
100 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
101 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
102 }
103
104 function getQuery() {
105 $logging = $this->db->tableName( "logging" );
106 $user = $this->db->tableName( 'user' );
107 $sql = "SELECT log_type, log_action, log_timestamp,
108 log_user, user_name,
109 log_namespace, log_title, cur_id,
110 log_comment FROM $logging, $user ";
111 if( !empty( $this->joinClauses ) ) {
112 $sql .= implode( ',', $this->joinClauses );
113 }
114 if( !empty( $this->whereClauses ) ) {
115 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
116 }
117 $sql .= " ORDER BY log_timestamp DESC ";
118 $sql .= $this->db->limitResult( $this->limit, $this->offset );
119 return $sql;
120 }
121
122 function getRows() {
123 return $this->db->resultObject( $this->db->query( $this->getQuery() ) );
124 }
125
126 function queryType() {
127 return $this->type;
128 }
129
130 function queryUser() {
131 return $this->user;
132 }
133
134 function queryTitle() {
135 if( is_null( $this->title ) ) {
136 return '';
137 } else {
138 return $this->title->getPrefixedText();
139 }
140 }
141 }
142
143 /**
144 *
145 */
146 class LogViewer {
147 var $reader, $skin;
148
149 function LogViewer( &$reader ) {
150 global $wgUser;
151 $this->skin =& $wgUser->getSkin();
152 $this->reader =& $reader;
153 }
154
155 function show() {
156 global $wgOut;
157 $this->showHeader( $wgOut );
158 $this->showOptions( $wgOut );
159 $this->showPrevNext( $wgOut );
160 $this->showList( $wgOut );
161 $this->showPrevNext( $wgOut );
162 }
163
164 function showList( &$out ) {
165 $html = "";
166 $result = $this->reader->getRows();
167 while( $s = $result->fetchObject() ) {
168 $html .= $this->logLine( $s );
169 }
170 $result->free();
171 $out->addHTML( $html );
172 }
173
174 # wfMsg( unprotectedarticle, $text )
175 # wfMsg( 'protectedarticle', $text )
176 # wfMsg( 'deletedarticle', $text )
177 # wfMsg( 'uploadedimage', $text )
178 # wfMsg( "blocklogentry", $this->BlockAddress, $this->BlockExpiry );
179 # wfMsg( "bureaucratlogentry", $this->mUser, implode( " ", $rightsNotation ) );
180 # wfMsg( "undeletedarticle", $this->mTarget ),
181 function logLine( $s ) {
182 global $wgLang;
183 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
184 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
185 $time = $wgLang->timeanddate( $s->log_timestamp );
186 if( $s->cur_id ) {
187 $titleLink = $this->skin->makeKnownLinkObj( $title );
188 } else {
189 $titleLink = $this->skin->makeBrokenLinkObj( $title );
190 }
191 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
192 if( '' === $s->log_comment ) {
193 $comment = '';
194 } else {
195 $comment = '(<em>' . $this->skin->formatComment( $s->log_comment ) . '</em>)';
196 }
197
198 $action = LogPage::actionText( $s->log_type, $s->log_action, $titleLink );
199 $out = "<li>$time $userLink $action $comment</li>\n";
200 return $out;
201 }
202
203 function showHeader( &$out ) {
204 $type = $this->reader->queryType();
205 if( LogPage::isLogType( $type ) ) {
206 $out->setPageTitle( LogPage::logName( $type ) );
207 $out->addWikiText( LogPage::logHeader( $type ) );
208 }
209 }
210
211 function showOptions( &$out ) {
212 global $wgScript;
213 $action = htmlspecialchars( $wgScript );
214 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
215 $special = htmlspecialchars( $title->getPrefixedDBkey() );
216 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
217 "<input type='hidden' name='title' value=\"$special\" />\n" .
218 $this->getTypeMenu() .
219 $this->getUserInput() .
220 $this->getTitleInput() .
221 "<input type='submit' />" .
222 "</form>" );
223 }
224
225 function getTypeMenu() {
226 $out = "<select name='type'>\n";
227 foreach( LogPage::validTypes() as $type ) {
228 $text = htmlspecialchars( LogPage::logName( $type ) );
229 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
230 $out .= "<option value=\"$type\"$selected>$text</option>\n";
231 }
232 $out .= "</select>\n";
233 return $out;
234 }
235
236 function getUserInput() {
237 $user = htmlspecialchars( $this->reader->queryUser() );
238 return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
239 }
240
241 function getTitleInput() {
242 $title = htmlspecialchars( $this->reader->queryTitle() );
243 return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
244 }
245
246 function showPrevNext( &$out ) {
247 global $wgLang;
248 $pieces = array();
249 $pieces[] = 'type=' . htmlspecialchars( $this->reader->queryType() );
250 $pieces[] = 'user=' . htmlspecialchars( $this->reader->queryUser() );
251 $pieces[] = 'page=' . htmlspecialchars( $this->reader->queryTitle() );
252 $bits = implode( '&', $pieces );
253 $offset = 0; $limit = 50;
254
255 # TODO: use timestamps instead of offsets to make it more natural
256 # to go huge distances in time
257 $html = wfViewPrevNext( $offset, $limit,
258 $wgLang->specialpage( 'Log' ),
259 $bits,
260 false);
261 $out->addHTML( '<p>' . $html . '</p>' );
262 }
263 }
264
265
266 ?>