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