Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[lhc/web/wiklou.git] / includes / specials / SpecialListfiles.php
1 <?php
2 /**
3 * Implements Special:Listfiles
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 * @file
21 * @ingroup SpecialPage
22 */
23
24 class SpecialListFiles extends IncludableSpecialPage {
25
26 public function __construct(){
27 parent::__construct( 'Listfiles' );
28 }
29
30 public function execute( $par ){
31 $this->setHeaders();
32 $this->outputHeader();
33
34 if ( $this->including() ) {
35 $userName = $par;
36 $search = '';
37 } else {
38 $userName = $this->getRequest()->getText( 'user', $par );
39 $search = $this->getRequest()->getText( 'ilsearch', '' );
40 }
41
42 $pager = new ImageListPager( $this->getContext(), $userName, $search, $this->including() );
43
44 if ( $this->including() ) {
45 $html = $pager->getBody();
46 } else {
47 $form = $pager->getForm();
48 $body = $pager->getBody();
49 $nav = $pager->getNavigationBar();
50 $html = "$form<br />\n$body<br />\n$nav";
51 }
52 $this->getOutput()->addHTML( $html );
53 }
54 }
55
56 /**
57 * @ingroup SpecialPage Pager
58 */
59 class ImageListPager extends TablePager {
60 var $mFieldNames = null;
61 var $mQueryConds = array();
62 var $mUserName = null;
63 var $mSearch = '';
64 var $mIncluding = false;
65
66 function __construct( IContextSource $context, $userName = null, $search = '', $including = false ) {
67 global $wgMiserMode;
68
69 $this->mIncluding = $including;
70
71 if ( $userName ) {
72 $nt = Title::newFromText( $userName, NS_USER );
73 if ( !is_null( $nt ) ) {
74 $this->mUserName = $nt->getText();
75 $this->mQueryConds['img_user_text'] = $this->mUserName;
76 }
77 }
78
79 if ( $search != '' && !$wgMiserMode ) {
80 $this->mSearch = $search;
81 $nt = Title::newFromURL( $this->mSearch );
82 if ( $nt ) {
83 $dbr = wfGetDB( DB_SLAVE );
84 $this->mQueryConds[] = 'LOWER(img_name)' .
85 $dbr->buildLike( $dbr->anyString(),
86 strtolower( $nt->getDBkey() ), $dbr->anyString() );
87 }
88 }
89
90 if ( !$including ) {
91 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
92 $this->mDefaultDirection = true;
93 } else {
94 $this->mDefaultDirection = false;
95 }
96 } else {
97 $this->mDefaultDirection = true;
98 }
99
100 parent::__construct( $context );
101 }
102
103 /**
104 * @return Array
105 */
106 function getFieldNames() {
107 if ( !$this->mFieldNames ) {
108 global $wgMiserMode;
109 $this->mFieldNames = array(
110 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
111 'img_name' => $this->msg( 'listfiles_name' )->text(),
112 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
113 'img_size' => $this->msg( 'listfiles_size' )->text(),
114 'img_user_text' => $this->msg( 'listfiles_user' )->text(),
115 'img_description' => $this->msg( 'listfiles_description' )->text(),
116 );
117 if( !$wgMiserMode ) {
118 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
119 }
120 }
121 return $this->mFieldNames;
122 }
123
124 function isFieldSortable( $field ) {
125 if ( $this->mIncluding ) {
126 return false;
127 }
128 static $sortable = array( 'img_timestamp', 'img_name' );
129 if ( $field == 'img_size' ) {
130 # No index for both img_size and img_user_text
131 return !isset( $this->mQueryConds['img_user_text'] );
132 }
133 return in_array( $field, $sortable );
134 }
135
136 function getQueryInfo() {
137 $tables = array( 'image' );
138 $fields = array_keys( $this->getFieldNames() );
139 $fields[] = 'img_user';
140 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
141 $options = $join_conds = array();
142
143 # Depends on $wgMiserMode
144 if( isset( $this->mFieldNames['count'] ) ) {
145 $tables[] = 'oldimage';
146
147 # Need to rewrite this one
148 foreach ( $fields as &$field ) {
149 if ( $field == 'count' ) {
150 $field = 'COUNT(oi_archive_name) AS count';
151 }
152 }
153 unset( $field );
154
155 $dbr = wfGetDB( DB_SLAVE );
156 if( $dbr->implicitGroupby() ) {
157 $options = array( 'GROUP BY' => 'img_name' );
158 } else {
159 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
160 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
161 }
162 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
163 }
164 return array(
165 'tables' => $tables,
166 'fields' => $fields,
167 'conds' => $this->mQueryConds,
168 'options' => $options,
169 'join_conds' => $join_conds
170 );
171 }
172
173 function getDefaultSort() {
174 return 'img_timestamp';
175 }
176
177 function getStartBody() {
178 # Do a link batch query for user pages
179 if ( $this->mResult->numRows() ) {
180 $lb = new LinkBatch;
181 $this->mResult->seek( 0 );
182 foreach ( $this->mResult as $row ) {
183 if ( $row->img_user ) {
184 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
185 }
186 }
187 $lb->execute();
188 }
189
190 return parent::getStartBody();
191 }
192
193 function formatValue( $field, $value ) {
194 switch ( $field ) {
195 case 'thumb':
196 $file = wfLocalFile( $value );
197 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
198 return $thumb->toHtml( array( 'desc-link' => true ) );
199 case 'img_timestamp':
200 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
201 case 'img_name':
202 static $imgfile = null;
203 if ( $imgfile === null ) $imgfile = $this->msg( 'imgfile' )->text();
204
205 // Weird files can maybe exist? Bug 22227
206 $filePage = Title::makeTitleSafe( NS_FILE, $value );
207 if( $filePage ) {
208 $link = Linker::linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
209 $download = Xml::element( 'a',
210 array( 'href' => wfLocalFile( $filePage )->getURL() ),
211 $imgfile
212 );
213 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
214 return "$link $download";
215 } else {
216 return htmlspecialchars( $value );
217 }
218 case 'img_user_text':
219 if ( $this->mCurrentRow->img_user ) {
220 $link = Linker::link(
221 Title::makeTitle( NS_USER, $value ),
222 htmlspecialchars( $value )
223 );
224 } else {
225 $link = htmlspecialchars( $value );
226 }
227 return $link;
228 case 'img_size':
229 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
230 case 'img_description':
231 return Linker::commentBlock( $value );
232 case 'count':
233 return intval( $value ) + 1;
234 }
235 }
236
237 function getForm() {
238 global $wgScript, $wgMiserMode;
239 $inputForm = array();
240 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
241 if ( !$wgMiserMode ) {
242 $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $this->mSearch, 'text',
243 array(
244 'size' => '40',
245 'maxlength' => '255',
246 'id' => 'mw-ilsearch',
247 ) );
248 }
249 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
250 'size' => '40',
251 'maxlength' => '255',
252 'id' => 'mw-listfiles-user',
253 ) );
254 return Html::openElement( 'form',
255 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
256 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
257 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
258 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
259 Html::closeElement( 'fieldset' ) .
260 Html::closeElement( 'form' ) . "\n";
261 }
262
263 function getTableClass() {
264 return 'listfiles ' . parent::getTableClass();
265 }
266
267 function getNavClass() {
268 return 'listfiles_nav ' . parent::getNavClass();
269 }
270
271 function getSortHeaderClass() {
272 return 'listfiles_sort ' . parent::getSortHeaderClass();
273 }
274
275 function getPagingQueries() {
276 $queries = parent::getPagingQueries();
277 if ( !is_null( $this->mUserName ) ) {
278 # Append the username to the query string
279 foreach ( $queries as &$query ) {
280 $query['user'] = $this->mUserName;
281 }
282 }
283 return $queries;
284 }
285
286 function getDefaultQuery() {
287 $queries = parent::getDefaultQuery();
288 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
289 $queries['user'] = $this->mUserName;
290 }
291 return $queries;
292 }
293 }