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 );
55 protected function getGroupName() {
61 * @ingroup SpecialPage Pager
63 class ImageListPager
extends TablePager
{
64 var $mFieldNames = null;
65 var $mQueryConds = array();
66 var $mUserName = null;
68 var $mIncluding = false;
70 function __construct( IContextSource
$context, $userName = null, $search = '', $including = false ) {
73 $this->mIncluding
= $including;
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
;
83 if ( $search != '' && !$wgMiserMode ) {
84 $this->mSearch
= $search;
85 $nt = Title
::newFromURL( $this->mSearch
);
87 $dbr = wfGetDB( DB_SLAVE
);
88 $this->mQueryConds
[] = 'LOWER(img_name)' .
89 $dbr->buildLike( $dbr->anyString(),
90 strtolower( $nt->getDBkey() ), $dbr->anyString() );
95 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
96 $this->mDefaultDirection
= true;
98 $this->mDefaultDirection
= false;
101 $this->mDefaultDirection
= true;
104 parent
::__construct( $context );
110 function getFieldNames() {
111 if ( !$this->mFieldNames
) {
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(),
121 if ( !$wgMiserMode ) {
122 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
125 return $this->mFieldNames
;
128 function isFieldSortable( $field ) {
129 if ( $this->mIncluding
) {
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'] );
137 return in_array( $field, $sortable );
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();
147 # Depends on $wgMiserMode
148 if ( isset( $this->mFieldNames
['count'] ) ) {
149 $tables[] = 'oldimage';
151 # Need to rewrite this one
152 foreach ( $fields as &$field ) {
153 if ( $field == 'count' ) {
154 $field = 'COUNT(oi_archive_name) AS count';
159 $dbr = wfGetDB( DB_SLAVE
);
160 if ( $dbr->implicitGroupby() ) {
161 $options = array( 'GROUP BY' => 'img_name' );
163 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
164 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
166 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
171 'conds' => $this->mQueryConds
,
172 'options' => $options,
173 'join_conds' => $join_conds
177 function getDefaultSort() {
178 return 'img_timestamp';
181 function doBatchLookups() {
183 $this->mResult
->seek( 0 );
184 foreach ( $this->mResult
as $row ) {
185 $userIds[] = $row->img_user
;
187 # Do a link batch query for names and userpages
188 UserCache
::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__
);
191 function formatValue( $field, $value ) {
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() ) );
200 static $imgfile = null;
201 if ( $imgfile === null ) {
202 $imgfile = $this->msg( 'imgfile' )->text();
205 // Weird files can maybe exist? Bug 22227
206 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
208 $link = Linker
::linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
209 $download = Xml
::element( 'a',
210 array( 'href' => wfLocalFile( $filePage )->getURL() ),
213 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
214 return "$link $download";
216 return htmlspecialchars( $value );
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 )
226 $link = htmlspecialchars( $value );
230 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
231 case 'img_description':
232 return Linker
::formatComment( $value );
234 return intval( $value ) +
1;
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',
246 'maxlength' => '255',
247 'id' => 'mw-ilsearch',
250 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
252 'maxlength' => '255',
253 'id' => 'mw-listfiles-user',
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";
265 function getTableClass() {
266 return 'listfiles ' . parent
::getTableClass();
269 function getNavClass() {
270 return 'listfiles_nav ' . parent
::getNavClass();
273 function getSortHeaderClass() {
274 return 'listfiles_sort ' . parent
::getSortHeaderClass();
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
;
288 function getDefaultQuery() {
289 $queries = parent
::getDefaultQuery();
290 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
291 $queries['user'] = $this->mUserName
;
296 function getTitle() {
297 return SpecialPage
::getTitleFor( 'Listfiles' );