Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / specials / SpecialListfiles.php
blobf508b7c57dc5925155276109f98a544cf52bbdce
1 <?php
2 /**
3 * Implements Special:Listfiles
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 class SpecialListFiles extends IncludableSpecialPage {
25 public function __construct() {
26 parent::__construct( 'Listfiles' );
29 public function execute( $par ) {
30 $this->setHeaders();
31 $this->outputHeader();
33 if ( $this->including() ) {
34 $userName = $par;
35 $search = '';
36 } else {
37 $userName = $this->getRequest()->getText( 'user', $par );
38 $search = $this->getRequest()->getText( 'ilsearch', '' );
41 $pager = new ImageListPager(
42 $this->getContext(),
43 $userName,
44 $search,
45 $this->including()
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";
56 $this->getOutput()->addHTML( $html );
59 protected function getGroupName() {
60 return 'media';
64 /**
65 * @ingroup SpecialPage Pager
67 class ImageListPager extends TablePager {
68 var $mFieldNames = null;
69 var $mQueryConds = array();
70 var $mUserName = null;
71 var $mSearch = '';
72 var $mIncluding = false;
74 function __construct( IContextSource $context, $userName = null, $search = '',
75 $including = false
76 ) {
77 global $wgMiserMode;
79 $this->mIncluding = $including;
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;
89 if ( $search != '' && !$wgMiserMode ) {
90 $this->mSearch = $search;
91 $nt = Title::newFromURL( $this->mSearch );
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() );
101 if ( !$including ) {
102 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
103 $this->mDefaultDirection = true;
104 } else {
105 $this->mDefaultDirection = false;
107 } else {
108 $this->mDefaultDirection = true;
111 parent::__construct( $context );
115 * @return Array
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(),
128 if ( !$wgMiserMode ) {
129 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
133 return $this->mFieldNames;
136 function isFieldSortable( $field ) {
137 if ( $this->mIncluding ) {
138 return false;
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'] );
146 return in_array( $field, $sortable );
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();
156 # Depends on $wgMiserMode
157 if ( isset( $this->mFieldNames['count'] ) ) {
158 $tables[] = 'oldimage';
160 # Need to rewrite this one
161 foreach ( $fields as &$field ) {
162 if ( $field == 'count' ) {
163 $field = 'COUNT(oi_archive_name) AS count';
166 unset( $field );
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 ) );
175 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
178 return array(
179 'tables' => $tables,
180 'fields' => $fields,
181 'conds' => $this->mQueryConds,
182 'options' => $options,
183 'join_conds' => $join_conds
187 function getDefaultSort() {
188 return 'img_timestamp';
191 function doBatchLookups() {
192 $userIds = array();
193 $this->mResult->seek( 0 );
194 foreach ( $this->mResult as $row ) {
195 $userIds[] = $row->img_user;
197 # Do a link batch query for names and userpages
198 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
201 function formatValue( $field, $value ) {
202 switch ( $field ) {
203 case 'thumb':
204 $file = wfLocalFile( $value );
205 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
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();
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() )
224 $download = Xml::element( 'a',
225 array( 'href' => wfLocalFile( $filePage )->getURL() ),
226 $imgfile
228 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
230 return "$link $download";
231 } else {
232 return htmlspecialchars( $value );
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 )
241 } else {
242 $link = htmlspecialchars( $value );
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;
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',
271 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
272 'size' => '40',
273 'maxlength' => '255',
274 'id' => 'mw-listfiles-user',
275 ) );
277 return Html::openElement( 'form',
278 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' )
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";
288 function getTableClass() {
289 return 'listfiles ' . parent::getTableClass();
292 function getNavClass() {
293 return 'listfiles_nav ' . parent::getNavClass();
296 function getSortHeaderClass() {
297 return 'listfiles_sort ' . parent::getSortHeaderClass();
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;
309 return $queries;
312 function getDefaultQuery() {
313 $queries = parent::getDefaultQuery();
314 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
315 $queries['user'] = $this->mUserName;
318 return $queries;
321 function getTitle() {
322 return SpecialPage::getTitleFor( 'Listfiles' );