2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
20 function wfSpecialLog( $par = '' ) {
22 $logReader =& new LogReader( $wgRequest );
23 if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
24 $logReader->limitType( $par );
26 $logViewer =& new LogViewer( $logReader );
31 var $db, $joinClauses, $whereClauses;
32 var $type = '', $user = '', $title = null;
34 function LogReader( $request ) {
35 $this->db
=& wfGetDB( DB_SLAVE
);
36 $this->setupQuery( $request );
39 function setupQuery( $request ) {
40 $cur = $this->db
->tableName( 'cur' );
41 $user = $this->db
->tableName( 'user' );
42 $this->joinClauses
= array( "LEFT OUTER JOIN $cur ON log_namespace=cur_namespace AND log_title=cur_title" );
43 $this->whereClauses
= array( 'user_id=log_user' );
45 $this->limitType( $request->getVal( 'type' ) );
46 $this->limitUser( $request->getText( 'user' ) );
47 $this->limitTitle( $request->getText( 'page' ) );
48 $this->limitTime( $request->getVal( 'from' ), '>=' );
49 $this->limitTime( $request->getVal( 'until' ), '<=' );
51 list( $this->limit
, $this->offset
) = $request->getLimitOffset();
54 function limitType( $type ) {
55 if( empty( $type ) ) {
59 $safetype = $this->db
->strencode( $type );
60 $this->whereClauses
[] = "log_type='$safetype'";
63 function limitUser( $name ) {
64 $title = Title
::makeTitleSafe( NS_USER
, $name );
65 if( empty( $name ) ||
is_null( $title ) ) {
68 $this->user
= str_replace( '_', ' ', $title->getDBkey() );
69 $safename = $this->db
->strencode( $this->user
);
70 $user = $this->db
->tableName( 'user' );
71 $this->whereClauses
[] = "user_name='$safename'";
74 function limitTitle( $page ) {
75 $title = Title
::newFromText( $page );
76 if( empty( $page ) ||
is_null( $title ) ) {
79 $this->title
=& $title;
80 $safetitle = $this->db
->strencode( $title->getDBkey() );
81 $ns = $title->getNamespace();
82 $this->whereClauses
[] = "log_namespace=$ns AND log_title='$safetitle'";
85 function limitTime( $time, $direction ) {
86 # Direction should be a comparison operator
87 if( empty( $time ) ) {
90 $safetime = $this->db
->strencode( wfTimestamp( TS_MW
, $time ) );
91 $this->whereClauses
[] = "log_timestamp $direction '$safetime'";
95 $logging = $this->db
->tableName( "logging" );
96 $user = $this->db
->tableName( 'user' );
97 $sql = "SELECT log_type, log_action, log_timestamp,
99 log_namespace, log_title, cur_id,
100 log_comment FROM $logging, $user ";
101 if( !empty( $this->joinClauses
) ) {
102 $sql .= implode( ',', $this->joinClauses
);
104 if( !empty( $this->whereClauses
) ) {
105 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses
);
107 $sql .= " ORDER BY log_timestamp DESC ";
108 $sql .= $this->db
->limitResult( $this->limit
, $this->offset
);
113 return $this->db
->resultObject( $this->db
->query( $this->getQuery() ) );
116 function queryType() {
120 function queryUser() {
124 function queryTitle() {
125 if( is_null( $this->title
) ) {
128 return $this->title
->getPrefixedText();
136 function LogViewer( &$reader ) {
138 $this->skin
=& $wgUser->getSkin();
139 $this->reader
=& $reader;
144 $this->showHeader( $wgOut );
145 $this->showOptions( $wgOut );
146 $this->showPrevNext( $wgOut );
147 $this->showList( $wgOut );
148 $this->showPrevNext( $wgOut );
151 function showList( &$out ) {
153 $result = $this->reader
->getRows();
154 while( $s = $result->fetchObject() ) {
155 $html .= $this->logLine( $s );
158 $out->addHTML( $html );
161 # wfMsg( unprotectedarticle, $text )
162 # wfMsg( 'protectedarticle', $text )
163 # wfMsg( 'deletedarticle', $text )
164 # wfMsg( 'uploadedimage', $text )
165 # wfMsg( "blocklogentry", $this->BlockAddress, $this->BlockExpiry );
166 # wfMsg( "bureaucratlogentry", $this->mUser, implode( " ", $rightsNotation ) );
167 # wfMsg( "undeletedarticle", $this->mTarget ),
168 function logLine( $s ) {
170 $title = Title
::makeTitle( $s->log_namespace
, $s->log_title
);
171 $user = Title
::makeTitleSafe( NS_USER
, $s->user_name
);
172 $time = $wgLang->timeanddate( $s->log_timestamp
);
174 $titleLink = $this->skin
->makeKnownLinkObj( $title );
176 $titleLink = $this->skin
->makeBrokenLinkObj( $title );
178 $userLink = $this->skin
->makeLinkObj( $user, htmlspecialchars( $s->user_name
) );
179 if( '' === $s->log_comment
) {
182 $comment = '(<em>' . $this->skin
->formatComment( $s->log_comment
) . '</em>)';
185 $action = LogPage
::actionText( $s->log_type
, $s->log_action
, $titleLink );
186 $out = "<li>$time $userLink $action $comment</li>\n";
190 function showHeader( &$out ) {
191 $type = $this->reader
->queryType();
192 if( LogPage
::isLogType( $type ) ) {
193 $out->setPageTitle( LogPage
::logName( $type ) );
194 $out->addWikiText( LogPage
::logHeader( $type ) );
198 function showOptions( &$out ) {
200 $action = htmlspecialchars( $wgScript );
201 $title = Title
::makeTitle( NS_SPECIAL
, 'Log' );
202 $special = htmlspecialchars( $title->getPrefixedDBkey() );
203 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
204 "<input type='hidden' name='title' value=\"$special\" />\n" .
205 $this->getTypeMenu() .
206 $this->getUserInput() .
207 $this->getTitleInput() .
208 "<input type='submit' />" .
212 function getTypeMenu() {
213 $out = "<select name='type'>\n";
214 foreach( LogPage
::validTypes() as $type ) {
215 $text = htmlspecialchars( LogPage
::logName( $type ) );
216 $selected = ($type == $this->reader
->queryType()) ?
' selected="selected"' : '';
217 $out .= "<option value=\"$type\"$selected>$text</option>\n";
219 $out .= "</select>\n";
223 function getUserInput() {
224 $user = htmlspecialchars( $this->reader
->queryUser() );
225 return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
228 function getTitleInput() {
229 $title = htmlspecialchars( $this->reader
->queryTitle() );
230 return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
233 function showPrevNext( &$out ) {
236 $pieces[] = 'type=' . htmlspecialchars( $this->reader
->queryType() );
237 $pieces[] = 'user=' . htmlspecialchars( $this->reader
->queryUser() );
238 $pieces[] = 'page=' . htmlspecialchars( $this->reader
->queryTitle() );
239 $bits = implode( '&', $pieces );
240 $offset = 0; $limit = 50;
242 # TODO: use timestamps instead of offsets to make it more natural
243 # to go huge distances in time
244 $html = wfViewPrevNext( $offset, $limit,
245 $wgLang->specialpage( 'Log' ),
248 $out->addHTML( '<p>' . $html . '</p>' );