Merge "[JobQueue] Use regular wfDebug() in some places."
[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 protected function getGroupName() {
56 return 'media';
57 }
58 }
59
60 /**
61 * @ingroup SpecialPage Pager
62 */
63 class ImageListPager extends TablePager {
64 var $mFieldNames = null;
65 var $mQueryConds = array();
66 var $mUserName = null;
67 var $mSearch = '';
68 var $mIncluding = false;
69
70 function __construct( IContextSource $context, $userName = null, $search = '', $including = false ) {
71 global $wgMiserMode;
72
73 $this->mIncluding = $including;
74
75 if ( $userName ) {
76 $nt = Title::newFromText( $userName, NS_USER );
77 if ( !is_null( $nt ) ) {
78 $this->mUserName = $nt->getText();
79 $this->mQueryConds['img_user_text'] = $this->mUserName;
80 }
81 }
82
83 if ( $search != '' && !$wgMiserMode ) {
84 $this->mSearch = $search;
85 $nt = Title::newFromURL( $this->mSearch );
86 if ( $nt ) {
87 $dbr = wfGetDB( DB_SLAVE );
88 $this->mQueryConds[] = 'LOWER(img_name)' .
89 $dbr->buildLike( $dbr->anyString(),
90 strtolower( $nt->getDBkey() ), $dbr->anyString() );
91 }
92 }
93
94 if ( !$including ) {
95 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
96 $this->mDefaultDirection = true;
97 } else {
98 $this->mDefaultDirection = false;
99 }
100 } else {
101 $this->mDefaultDirection = true;
102 }
103
104 parent::__construct( $context );
105 }
106
107 /**
108 * @return Array
109 */
110 function getFieldNames() {
111 if ( !$this->mFieldNames ) {
112 global $wgMiserMode;
113 $this->mFieldNames = array(
114 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
115 'img_name' => $this->msg( 'listfiles_name' )->text(),
116 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
117 'img_size' => $this->msg( 'listfiles_size' )->text(),
118 'img_user_text' => $this->msg( 'listfiles_user' )->text(),
119 'img_description' => $this->msg( 'listfiles_description' )->text(),
120 );
121 if ( !$wgMiserMode ) {
122 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
123 }
124 }
125 return $this->mFieldNames;
126 }
127
128 function isFieldSortable( $field ) {
129 if ( $this->mIncluding ) {
130 return false;
131 }
132 static $sortable = array( 'img_timestamp', 'img_name' );
133 if ( $field == 'img_size' ) {
134 # No index for both img_size and img_user_text
135 return !isset( $this->mQueryConds['img_user_text'] );
136 }
137 return in_array( $field, $sortable );
138 }
139
140 function getQueryInfo() {
141 $tables = array( 'image' );
142 $fields = array_keys( $this->getFieldNames() );
143 $fields[] = 'img_user';
144 $fields[array_search( 'thumb', $fields )] = 'img_name AS thumb';
145 $options = $join_conds = array();
146
147 # Depends on $wgMiserMode
148 if ( isset( $this->mFieldNames['count'] ) ) {
149 $tables[] = 'oldimage';
150
151 # Need to rewrite this one
152 foreach ( $fields as &$field ) {
153 if ( $field == 'count' ) {
154 $field = 'COUNT(oi_archive_name) AS count';
155 }
156 }
157 unset( $field );
158
159 $dbr = wfGetDB( DB_SLAVE );
160 if ( $dbr->implicitGroupby() ) {
161 $options = array( 'GROUP BY' => 'img_name' );
162 } else {
163 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
164 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
165 }
166 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
167 }
168 return array(
169 'tables' => $tables,
170 'fields' => $fields,
171 'conds' => $this->mQueryConds,
172 'options' => $options,
173 'join_conds' => $join_conds
174 );
175 }
176
177 function getDefaultSort() {
178 return 'img_timestamp';
179 }
180
181 function doBatchLookups() {
182 $userIds = array();
183 $this->mResult->seek( 0 );
184 foreach ( $this->mResult as $row ) {
185 $userIds[] = $row->img_user;
186 }
187 # Do a link batch query for names and userpages
188 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
189 }
190
191 function formatValue( $field, $value ) {
192 switch ( $field ) {
193 case 'thumb':
194 $file = wfLocalFile( $value );
195 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
196 return $thumb->toHtml( array( 'desc-link' => true ) );
197 case 'img_timestamp':
198 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
199 case 'img_name':
200 static $imgfile = null;
201 if ( $imgfile === null ) {
202 $imgfile = $this->msg( 'imgfile' )->text();
203 }
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 $name = User::whoIs( $this->mCurrentRow->img_user );
221 $link = Linker::link(
222 Title::makeTitle( NS_USER, $name ),
223 htmlspecialchars( $name )
224 );
225 } else {
226 $link = htmlspecialchars( $value );
227 }
228 return $link;
229 case 'img_size':
230 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
231 case 'img_description':
232 return Linker::formatComment( $value );
233 case 'count':
234 return intval( $value ) + 1;
235 }
236 }
237
238 function getForm() {
239 global $wgScript, $wgMiserMode;
240 $inputForm = array();
241 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
242 if ( !$wgMiserMode ) {
243 $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $this->mSearch, 'text',
244 array(
245 'size' => '40',
246 'maxlength' => '255',
247 'id' => 'mw-ilsearch',
248 ) );
249 }
250 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
251 'size' => '40',
252 'maxlength' => '255',
253 'id' => 'mw-listfiles-user',
254 ) );
255 return Html::openElement( 'form',
256 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
257 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
258 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
259 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
260 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title' ) ) .
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
296 function getTitle() {
297 return SpecialPage::getTitleFor( 'Listfiles' );
298 }
299 }