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 );
28 $logReader->cleanUp();
32 var $db, $joinClauses, $whereClauses;
33 var $type = '', $user = '', $title = null;
35 function LogReader( $request ) {
36 $this->db
=& wfGetDB( DB_SLAVE
);
37 $this->setupQuery( $request );
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' );
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' ), '<=' );
52 list( $this->limit
, $this->offset
) = $request->getLimitOffset();
55 function limitType( $type ) {
56 if( empty( $type ) ) {
60 $safetype = $this->db
->strencode( $type );
61 $this->whereClauses
[] = "log_type='$safetype'";
64 function limitUser( $name ) {
65 $title = Title
::makeTitleSafe( NS_USER
, $name );
66 if( empty( $name ) ||
is_null( $title ) ) {
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'";
75 function limitTitle( $page ) {
76 $title = Title
::newFromText( $page );
77 if( empty( $page ) ||
is_null( $title ) ) {
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'";
86 function limitTime( $time, $direction ) {
87 # Direction should be a comparison operator
88 if( empty( $time ) ) {
91 $safetime = $this->db
->strencode( wfTimestamp( TS_MW
, $time ) );
92 $this->whereClauses
[] = "log_timestamp $direction '$safetime'";
96 $logging = $this->db
->tableName( "logging" );
97 $user = $this->db
->tableName( 'user' );
98 $sql = "SELECT log_type, log_action, log_timestamp,
100 log_namespace, log_title, cur_id,
101 log_comment FROM $logging, $user ";
102 if( !empty( $this->joinClauses
) ) {
103 $sql .= implode( ',', $this->joinClauses
);
105 if( !empty( $this->whereClauses
) ) {
106 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses
);
108 $sql .= " ORDER BY log_timestamp DESC ";
109 $sql .= $this->db
->limitResult( $this->limit
, $this->offset
);
113 function initQuery() {
114 $this->result
= $this->db
->query( $this->getQuery() );
117 function fetchObject() {
118 return $this->db
->fetchObject( $this->result
);
122 $this->db
->freeResult( $this->result
);
125 function queryType() {
129 function queryUser() {
133 function queryTitle() {
134 if( is_null( $this->title
) ) {
137 return $this->title
->getPrefixedText();
145 function LogViewer( &$reader ) {
147 $this->skin
=& $wgUser->getSkin();
148 $this->reader
=& $reader;
153 $this->showHeader( $wgOut );
154 $this->showOptions( $wgOut );
155 $this->showPrevNext( $wgOut );
156 $this->showList( $wgOut );
157 $this->showPrevNext( $wgOut );
160 function showList( &$out ) {
162 $this->reader
->initQuery();
163 while( $s = $this->reader
->fetchObject() ) {
164 $html .= $this->logLine( $s );
166 $out->addHTML( $html );
169 # wfMsg( unprotectedarticle, $text )
170 # wfMsg( 'protectedarticle', $text )
171 # wfMsg( 'deletedarticle', $text )
172 # wfMsg( 'uploadedimage', $text )
173 # wfMsg( "blocklogentry", $this->BlockAddress, $this->BlockExpiry );
174 # wfMsg( "bureaucratlogentry", $this->mUser, implode( " ", $rightsNotation ) );
175 # wfMsg( "undeletedarticle", $this->mTarget ),
176 function logLine( $s ) {
178 $title = Title
::makeTitle( $s->log_namespace
, $s->log_title
);
179 $user = Title
::makeTitleSafe( NS_USER
, $s->user_name
);
180 $time = $wgLang->timeanddate( $s->log_timestamp
);
182 $titleLink = $this->skin
->makeKnownLinkObj( $title );
184 $titleLink = $this->skin
->makeBrokenLinkObj( $title );
186 $userLink = $this->skin
->makeLinkObj( $user, htmlspecialchars( $s->user_name
) );
187 if( '' === $s->log_comment
) {
190 $comment = '(<em>' . $this->skin
->formatComment( $s->log_comment
) . '</em>)';
193 $action = LogPage
::actionText( $s->log_type
, $s->log_action
, $titleLink );
194 $out = "<li>$time $userLink $action $comment</li>\n";
198 function showHeader( &$out ) {
199 $type = $this->reader
->queryType();
200 if( LogPage
::isLogType( $type ) ) {
201 $out->setPageTitle( LogPage
::logName( $type ) );
202 $out->addWikiText( LogPage
::logHeader( $type ) );
206 function showOptions( &$out ) {
208 $action = htmlspecialchars( $wgScript );
209 $title = Title
::makeTitle( NS_SPECIAL
, 'Log' );
210 $special = htmlspecialchars( $title->getPrefixedDBkey() );
211 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
212 "<input type='hidden' name='title' value=\"$special\" />\n" .
213 $this->getTypeMenu() .
214 $this->getUserInput() .
215 $this->getTitleInput() .
216 "<input type='submit' />" .
220 function getTypeMenu() {
221 $out = "<select name='type'>\n";
222 foreach( LogPage
::validTypes() as $type ) {
223 $text = htmlspecialchars( LogPage
::logName( $type ) );
224 $selected = ($type == $this->reader
->queryType()) ?
' selected="selected"' : '';
225 $out .= "<option value=\"$type\"$selected>$text</option>\n";
227 $out .= "</select>\n";
231 function getUserInput() {
232 $user = htmlspecialchars( $this->reader
->queryUser() );
233 return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
236 function getTitleInput() {
237 $title = htmlspecialchars( $this->reader
->queryTitle() );
238 return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
241 function showPrevNext( &$out ) {
244 $pieces[] = 'type=' . htmlspecialchars( $this->reader
->queryType() );
245 $pieces[] = 'user=' . htmlspecialchars( $this->reader
->queryUser() );
246 $pieces[] = 'page=' . htmlspecialchars( $this->reader
->queryTitle() );
247 $bits = implode( '&', $pieces );
248 $offset = 0; $limit = 50;
250 # TODO: use timestamps instead of offsets to make it more natural
251 # to go huge distances in time
252 $html = wfViewPrevNext( $offset, $limit,
253 $wgLang->specialpage( 'Log' ),
256 $out->addHTML( '<p>' . $html . '</p>' );