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