* User Pager for special:log
[lhc/web/wiklou.git] / includes / SpecialLog.php
1 <?php
2 # Copyright (C) 2008 Aaron Schulz
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 *
22 * @addtogroup SpecialPage
23 */
24
25 /**
26 * constructor
27 */
28 function wfSpecialLog( $par = '' ) {
29 global $wgRequest, $wgOut, $wgUser;
30 # Get parameters
31 $type = $wgRequest->getVal( 'type', $par );
32 $user = $wgRequest->getText( 'user' );
33 $title = $wgRequest->getText( 'page' );
34 $pattern = $wgRequest->getBool( 'pattern' );
35 # Create a LogPager item to get the results and a LogEventList
36 # item to format them...
37 $loglist = new LogEventList( $wgUser->getSkin() );
38 $pager = new LogPager( $loglist, $type, $user, $title, $pattern );
39 # Set title and add header
40 $loglist->showHeader( $wgOut, $pager->getType() );
41 # Show form options
42 $loglist->showOptions( $wgOut, $pager->getType(), $pager->getUser(), $pager->getPage(), $pager->getPattern() );
43 # Insert list
44 $wgOut->addHTML(
45 $pager->getNavigationBar() .
46 '<ul id="logevents">' . "\n" .
47 $pager->getBody() .
48 '</ul>' . "\n" .
49 $pager->getNavigationBar()
50 );
51 }
52
53 /**
54 * @addtogroup Pager
55 */
56 class LogPager extends ReverseChronologicalPager {
57 var $type = '', $user = '', $title = '', $pattern = '';
58 /**
59 * constructor
60 * @param LogEventList $loglist,
61 * @param string $type,
62 * @param string $user,
63 * @param string $page,
64 * @param string $pattern
65 * @param array $conds
66 */
67 function __construct( $loglist, $type, $user, $title, $pattern, $conds = array() ) {
68 parent::__construct();
69 $this->mConds = $conds;
70
71 $this->mLogList = $loglist;
72
73 $this->limitType( $type );
74 $this->limitUser( $user );
75 $this->limitTitle( $title, $pattern );
76 }
77
78 /**
79 * Set the log reader to return only entries of the given type.
80 * Type restrictions enforced here
81 * @param string $type A log type ('upload', 'delete', etc)
82 * @private
83 */
84 private function limitType( $type ) {
85 global $wgLogRestrictions, $wgUser;
86 // Reset the array, clears extra "where" clauses when $par is used
87 $hiddenLogs = array();
88 // Nothing to show the user requested a log they can't see
89 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
90 $this->mConds[] = "NULL";
91 return false;
92 }
93 // Don't show private logs to unpriviledged users
94 foreach( $wgLogRestrictions as $logtype => $right ) {
95 if( !$wgUser->isAllowed($right) || empty($type) ) {
96 $safetype = $this->mDb->strencode( $logtype );
97 $hiddenLogs[] = "'$safetype'";
98 }
99 }
100 if( !empty($hiddenLogs) ) {
101 $this->mConds[] = 'log_type NOT IN('.implode(',',$hiddenLogs).')';
102 }
103
104 if( empty($type) ) {
105 return false;
106 }
107 $this->type = $type;
108 $this->mConds['log_type'] = $type;
109 }
110
111 /**
112 * Set the log reader to return only entries by the given user.
113 * @param string $name (In)valid user name
114 * @private
115 */
116 function limitUser( $name ) {
117 if( $name == '' ) {
118 return false;
119 }
120 $usertitle = Title::makeTitleSafe( NS_USER, $name );
121 if( is_null($usertitle) ) {
122 return false;
123 }
124 $this->user = $usertitle->getText();
125 /* Fetch userid at first, if known, provides awesome query plan afterwards */
126 $userid = User::idFromName( $this->user );
127 if( !$userid ) {
128 /* It should be nicer to abort query at all,
129 but for now it won't pass anywhere behind the optimizer */
130 $this->mConds[] = "NULL";
131 } else {
132 $this->mConds['log_user'] = $userid;
133 }
134 }
135
136 /**
137 * Set the log reader to return only entries affecting the given page.
138 * (For the block and rights logs, this is a user page.)
139 * @param string $page Title name as text
140 * @private
141 */
142 function limitTitle( $page, $pattern ) {
143 global $wgMiserMode;
144
145 $title = Title::newFromText( $page );
146 if( strlen($page) == 0 || !$title instanceof Title )
147 return false;
148
149 $this->title = $title->getPrefixedText();
150 $this->pattern = $pattern;
151 $ns = $title->getNamespace();
152 if( $pattern && !$wgMiserMode ) {
153 # use escapeLike to avoid expensive search patterns like 't%st%'
154 $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
155 $this->mConds['log_namespace'] = $ns;
156 $this->mConds[] = "log_title LIKE '$safetitle%'";
157 } else {
158 $this->mConds['log_namespace'] = $ns;
159 $this->mConds['log_title'] = $title->getDBkey();
160 }
161 }
162
163 function getQueryInfo() {
164 $this->mConds[] = 'user_id = log_user';
165 return array(
166 'tables' => array( 'logging', 'user' ),
167 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
168 'log_params', 'log_comment', 'log_id', 'log_deleted', 'log_timestamp', 'user_name' ),
169 'conds' => $this->mConds,
170 'options' => array()
171 );
172 }
173
174 function getIndexField() {
175 return 'log_timestamp';
176 }
177
178 function formatRow( $row ) {
179 return $this->mLogList->logLine( $row );
180 }
181
182 public function getType() {
183 return $this->type;
184 }
185
186 public function getUser() {
187 return $this->user;
188 }
189
190 public function getPage() {
191 return $this->title;
192 }
193
194 public function getPattern() {
195 return $this->pattern;
196 }
197 }