testPngNativetZtxt requires zlib extension
[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 doBatchLookups() {
178 $userIds = array();
179 $this->mResult->seek( 0 );
180 foreach ( $this->mResult as $row ) {
181 $userIds[] = $row->img_user;
182 }
183 # Do a link batch query for names and userpages
184 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
185 }
186
187 function formatValue( $field, $value ) {
188 switch ( $field ) {
189 case 'thumb':
190 $file = wfLocalFile( $value );
191 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
192 return $thumb->toHtml( array( 'desc-link' => true ) );
193 case 'img_timestamp':
194 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
195 case 'img_name':
196 static $imgfile = null;
197 if ( $imgfile === null ) $imgfile = $this->msg( 'imgfile' )->text();
198
199 // Weird files can maybe exist? Bug 22227
200 $filePage = Title::makeTitleSafe( NS_FILE, $value );
201 if( $filePage ) {
202 $link = Linker::linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
203 $download = Xml::element( 'a',
204 array( 'href' => wfLocalFile( $filePage )->getURL() ),
205 $imgfile
206 );
207 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
208 return "$link $download";
209 } else {
210 return htmlspecialchars( $value );
211 }
212 case 'img_user_text':
213 if ( $this->mCurrentRow->img_user ) {
214 $name = User::whoIs( $this->mCurrentRow->img_user );
215 $link = Linker::link(
216 Title::makeTitle( NS_USER, $name ),
217 htmlspecialchars( $name )
218 );
219 } else {
220 $link = htmlspecialchars( $value );
221 }
222 return $link;
223 case 'img_size':
224 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
225 case 'img_description':
226 return Linker::formatComment( $value );
227 case 'count':
228 return intval( $value ) + 1;
229 }
230 }
231
232 function getForm() {
233 global $wgScript, $wgMiserMode;
234 $inputForm = array();
235 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
236 if ( !$wgMiserMode ) {
237 $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $this->mSearch, 'text',
238 array(
239 'size' => '40',
240 'maxlength' => '255',
241 'id' => 'mw-ilsearch',
242 ) );
243 }
244 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
245 'size' => '40',
246 'maxlength' => '255',
247 'id' => 'mw-listfiles-user',
248 ) );
249 return Html::openElement( 'form',
250 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
251 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
252 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
253 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
254 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title' ) ) .
255 Html::closeElement( 'fieldset' ) .
256 Html::closeElement( 'form' ) . "\n";
257 }
258
259 function getTableClass() {
260 return 'listfiles ' . parent::getTableClass();
261 }
262
263 function getNavClass() {
264 return 'listfiles_nav ' . parent::getNavClass();
265 }
266
267 function getSortHeaderClass() {
268 return 'listfiles_sort ' . parent::getSortHeaderClass();
269 }
270
271 function getPagingQueries() {
272 $queries = parent::getPagingQueries();
273 if ( !is_null( $this->mUserName ) ) {
274 # Append the username to the query string
275 foreach ( $queries as &$query ) {
276 $query['user'] = $this->mUserName;
277 }
278 }
279 return $queries;
280 }
281
282 function getDefaultQuery() {
283 $queries = parent::getDefaultQuery();
284 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
285 $queries['user'] = $this->mUserName;
286 }
287 return $queries;
288 }
289
290 function getTitle() {
291 return SpecialPage::getTitleFor( 'Listfiles' );
292 }
293 }