Update formatting
[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 if ( $this->mIncluding ) {
138 return false;
139 }
140 static $sortable = array( 'img_timestamp', 'img_name' );
141 if ( $field == 'img_size' ) {
142 # No index for both img_size and img_user_text
143 return !isset( $this->mQueryConds['img_user_text'] );
144 }
145
146 return in_array( $field, $sortable );
147 }
148
149 function getQueryInfo() {
150 $tables = array( 'image' );
151 $fields = array_keys( $this->getFieldNames() );
152 $fields[] = 'img_user';
153 $fields[array_search( 'thumb', $fields )] = 'img_name AS thumb';
154 $options = $join_conds = array();
155
156 # Depends on $wgMiserMode
157 if ( isset( $this->mFieldNames['count'] ) ) {
158 $tables[] = 'oldimage';
159
160 # Need to rewrite this one
161 foreach ( $fields as &$field ) {
162 if ( $field == 'count' ) {
163 $field = 'COUNT(oi_archive_name) AS count';
164 }
165 }
166 unset( $field );
167
168 $dbr = wfGetDB( DB_SLAVE );
169 if ( $dbr->implicitGroupby() ) {
170 $options = array( 'GROUP BY' => 'img_name' );
171 } else {
172 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
173 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
174 }
175 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
176 }
177
178 return array(
179 'tables' => $tables,
180 'fields' => $fields,
181 'conds' => $this->mQueryConds,
182 'options' => $options,
183 'join_conds' => $join_conds
184 );
185 }
186
187 function getDefaultSort() {
188 return 'img_timestamp';
189 }
190
191 function doBatchLookups() {
192 $userIds = array();
193 $this->mResult->seek( 0 );
194 foreach ( $this->mResult as $row ) {
195 $userIds[] = $row->img_user;
196 }
197 # Do a link batch query for names and userpages
198 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
199 }
200
201 function formatValue( $field, $value ) {
202 switch ( $field ) {
203 case 'thumb':
204 $file = wfLocalFile( $value );
205 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
206
207 return $thumb->toHtml( array( 'desc-link' => true ) );
208 case 'img_timestamp':
209 $timeAndDate = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
210 return htmlspecialchars( $timeAndDate );
211 case 'img_name':
212 static $imgfile = null;
213 if ( $imgfile === null ) {
214 $imgfile = $this->msg( 'imgfile' )->text();
215 }
216
217 // Weird files can maybe exist? Bug 22227
218 $filePage = Title::makeTitleSafe( NS_FILE, $value );
219 if ( $filePage ) {
220 $link = Linker::linkKnown(
221 $filePage,
222 htmlspecialchars( $filePage->getText() )
223 );
224 $download = Xml::element( 'a',
225 array( 'href' => wfLocalFile( $filePage )->getURL() ),
226 $imgfile
227 );
228 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
229
230 return "$link $download";
231 } else {
232 return htmlspecialchars( $value );
233 }
234 case 'img_user_text':
235 if ( $this->mCurrentRow->img_user ) {
236 $name = User::whoIs( $this->mCurrentRow->img_user );
237 $link = Linker::link(
238 Title::makeTitle( NS_USER, $name ),
239 htmlspecialchars( $name )
240 );
241 } else {
242 $link = htmlspecialchars( $value );
243 }
244
245 return $link;
246 case 'img_size':
247 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
248 case 'img_description':
249 return Linker::formatComment( $value );
250 case 'count':
251 return intval( $value ) + 1;
252 }
253 }
254
255 function getForm() {
256 global $wgScript, $wgMiserMode;
257 $inputForm = array();
258 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
259 if ( !$wgMiserMode ) {
260 $inputForm['listfiles_search_for'] = Html::input(
261 'ilsearch',
262 $this->mSearch,
263 'text',
264 array(
265 'size' => '40',
266 'maxlength' => '255',
267 'id' => 'mw-ilsearch',
268 )
269 );
270 }
271 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
272 'size' => '40',
273 'maxlength' => '255',
274 'id' => 'mw-listfiles-user',
275 ) );
276
277 return Html::openElement( 'form',
278 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' )
279 ) .
280 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
281 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
282 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
283 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title' ) ) .
284 Html::closeElement( 'fieldset' ) .
285 Html::closeElement( 'form' ) . "\n";
286 }
287
288 function getTableClass() {
289 return 'listfiles ' . parent::getTableClass();
290 }
291
292 function getNavClass() {
293 return 'listfiles_nav ' . parent::getNavClass();
294 }
295
296 function getSortHeaderClass() {
297 return 'listfiles_sort ' . parent::getSortHeaderClass();
298 }
299
300 function getPagingQueries() {
301 $queries = parent::getPagingQueries();
302 if ( !is_null( $this->mUserName ) ) {
303 # Append the username to the query string
304 foreach ( $queries as &$query ) {
305 $query['user'] = $this->mUserName;
306 }
307 }
308
309 return $queries;
310 }
311
312 function getDefaultQuery() {
313 $queries = parent::getDefaultQuery();
314 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
315 $queries['user'] = $this->mUserName;
316 }
317
318 return $queries;
319 }
320
321 function getTitle() {
322 return SpecialPage::getTitleFor( 'Listfiles' );
323 }
324 }