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
{
26 public function __construct(){
27 parent
::__construct( 'Listfiles' );
30 public function execute( $par ){
32 $this->outputHeader();
34 if ( $this->including() ) {
38 $userName = $this->getRequest()->getText( 'user', $par );
39 $search = $this->getRequest()->getText( 'ilsearch', '' );
42 $pager = new ImageListPager( $this->getContext(), $userName, $search, $this->including() );
44 if ( $this->including() ) {
45 $html = $pager->getBody();
47 $form = $pager->getForm();
48 $body = $pager->getBody();
49 $nav = $pager->getNavigationBar();
50 $html = "$form<br />\n$body<br />\n$nav";
52 $this->getOutput()->addHTML( $html );
57 * @ingroup SpecialPage Pager
59 class ImageListPager
extends TablePager
{
60 var $mFieldNames = null;
61 var $mQueryConds = array();
62 var $mUserName = null;
64 var $mIncluding = false;
66 function __construct( IContextSource
$context, $userName = null, $search = '', $including = false ) {
69 $this->mIncluding
= $including;
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
;
79 if ( $search != '' && !$wgMiserMode ) {
80 $this->mSearch
= $search;
81 $nt = Title
::newFromURL( $this->mSearch
);
83 $dbr = wfGetDB( DB_SLAVE
);
84 $this->mQueryConds
[] = 'LOWER(img_name)' .
85 $dbr->buildLike( $dbr->anyString(),
86 strtolower( $nt->getDBkey() ), $dbr->anyString() );
91 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
92 $this->mDefaultDirection
= true;
94 $this->mDefaultDirection
= false;
97 $this->mDefaultDirection
= true;
100 parent
::__construct( $context );
106 function getFieldNames() {
107 if ( !$this->mFieldNames
) {
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(),
117 if( !$wgMiserMode ) {
118 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
121 return $this->mFieldNames
;
124 function isFieldSortable( $field ) {
125 if ( $this->mIncluding
) {
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'] );
133 return in_array( $field, $sortable );
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();
143 # Depends on $wgMiserMode
144 if( isset( $this->mFieldNames
['count'] ) ) {
145 $tables[] = 'oldimage';
147 # Need to rewrite this one
148 foreach ( $fields as &$field ) {
149 if ( $field == 'count' ) {
150 $field = 'COUNT(oi_archive_name) AS count';
155 $dbr = wfGetDB( DB_SLAVE
);
156 if( $dbr->implicitGroupby() ) {
157 $options = array( 'GROUP BY' => 'img_name' );
159 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
160 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
162 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
167 'conds' => $this->mQueryConds
,
168 'options' => $options,
169 'join_conds' => $join_conds
173 function getDefaultSort() {
174 return 'img_timestamp';
177 function doBatchLookups() {
179 $this->mResult
->seek( 0 );
180 foreach ( $this->mResult
as $row ) {
181 $userIds[] = $row->img_user
;
183 # Do a link batch query for names and userpages
184 UserCache
::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__
);
187 function formatValue( $field, $value ) {
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() ) );
196 static $imgfile = null;
197 if ( $imgfile === null ) $imgfile = $this->msg( 'imgfile' )->text();
199 // Weird files can maybe exist? Bug 22227
200 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
202 $link = Linker
::linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
203 $download = Xml
::element( 'a',
204 array( 'href' => wfLocalFile( $filePage )->getURL() ),
207 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
208 return "$link $download";
210 return htmlspecialchars( $value );
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 )
220 $link = htmlspecialchars( $value );
224 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
225 case 'img_description':
226 return Linker
::commentBlock( $value );
228 return intval( $value ) +
1;
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',
240 'maxlength' => '255',
241 'id' => 'mw-ilsearch',
244 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
246 'maxlength' => '255',
247 'id' => 'mw-listfiles-user',
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";
259 function getTableClass() {
260 return 'listfiles ' . parent
::getTableClass();
263 function getNavClass() {
264 return 'listfiles_nav ' . parent
::getNavClass();
267 function getSortHeaderClass() {
268 return 'listfiles_sort ' . parent
::getSortHeaderClass();
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
;
282 function getDefaultQuery() {
283 $queries = parent
::getDefaultQuery();
284 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
285 $queries['user'] = $this->mUserName
;
290 function getTitle() {
291 return SpecialPage
::getTitleFor( 'Listfiles' );