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
21 * @ingroup SpecialPage
24 class SpecialListFiles
extends IncludableSpecialPage
{
25 public function __construct() {
26 parent
::__construct( 'Listfiles' );
29 public function execute( $par ) {
31 $this->outputHeader();
33 if ( $this->including() ) {
37 $userName = $this->getRequest()->getText( 'user', $par );
38 $search = $this->getRequest()->getText( 'ilsearch', '' );
41 $pager = new ImageListPager(
48 if ( $this->including() ) {
49 $html = $pager->getBody();
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() {
65 * @ingroup SpecialPage Pager
67 class ImageListPager
extends TablePager
{
68 var $mFieldNames = null;
69 var $mQueryConds = array();
70 var $mUserName = null;
72 var $mIncluding = false;
74 function __construct( IContextSource
$context, $userName = null, $search = '',
79 $this->mIncluding
= $including;
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
);
94 $dbr = wfGetDB( DB_SLAVE
);
95 $this->mQueryConds
[] = 'LOWER(img_name)' .
96 $dbr->buildLike( $dbr->anyString(),
97 strtolower( $nt->getDBkey() ), $dbr->anyString() );
102 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
103 $this->mDefaultDirection
= true;
105 $this->mDefaultDirection
= false;
108 $this->mDefaultDirection
= true;
111 parent
::__construct( $context );
117 function getFieldNames() {
118 if ( !$this->mFieldNames
) {
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
) {
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';
168 $dbr = wfGetDB( DB_SLAVE
);
169 if ( $dbr->implicitGroupby() ) {
170 $options = array( 'GROUP BY' => 'img_name' );
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' ) );
181 'conds' => $this->mQueryConds
,
182 'options' => $options,
183 'join_conds' => $join_conds
187 function getDefaultSort() {
188 return 'img_timestamp';
191 function doBatchLookups() {
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 ) {
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 );
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 );
220 $link = Linker
::linkKnown(
222 htmlspecialchars( $filePage->getText() )
224 $download = Xml
::element( 'a',
225 array( 'href' => wfLocalFile( $filePage )->getURL() ),
228 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
230 return "$link $download";
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 )
242 $link = htmlspecialchars( $value );
247 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
248 case 'img_description':
249 return Linker
::formatComment( $value );
251 return intval( $value ) +
1;
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(
266 'maxlength' => '255',
267 'id' => 'mw-ilsearch',
271 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
273 'maxlength' => '255',
274 'id' => 'mw-listfiles-user',
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
;
312 function getDefaultQuery() {
313 $queries = parent
::getDefaultQuery();
314 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
315 $queries['user'] = $this->mUserName
;
321 function getTitle() {
322 return SpecialPage
::getTitleFor( 'Listfiles' );