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 getStartBody() {
178 # Do a link batch query for user pages
179 if ( $this->mResult
->numRows() ) {
181 $this->mResult
->seek( 0 );
182 foreach ( $this->mResult
as $row ) {
183 if ( $row->img_user
) {
184 $lb->add( NS_USER
, str_replace( ' ', '_', $row->img_user_text
) );
190 return parent
::getStartBody();
193 function formatValue( $field, $value ) {
196 $file = wfLocalFile( $value );
197 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
198 return $thumb->toHtml( array( 'desc-link' => true ) );
199 case 'img_timestamp':
200 return htmlspecialchars( $this->getLanguage()->timeanddate( $value, true ) );
202 static $imgfile = null;
203 if ( $imgfile === null ) $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 return "$link ($download)";
215 return htmlspecialchars( $value );
217 case 'img_user_text':
218 if ( $this->mCurrentRow
->img_user
) {
219 $link = Linker
::link(
220 Title
::makeTitle( NS_USER
, $value ),
221 htmlspecialchars( $value )
224 $link = htmlspecialchars( $value );
228 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
229 case 'img_description':
230 return Linker
::commentBlock( $value );
232 return intval( $value ) +
1;
237 global $wgScript, $wgMiserMode;
238 $inputForm = array();
239 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
240 if ( !$wgMiserMode ) {
241 $inputForm['listfiles_search_for'] = Html
::input( 'ilsearch', $this->mSearch
, 'text',
244 'maxlength' => '255',
245 'id' => 'mw-ilsearch',
248 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
250 'maxlength' => '255',
251 'id' => 'mw-listfiles-user',
253 return Html
::openElement( 'form',
254 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
255 Xml
::fieldset( $this->msg( 'listfiles' )->text() ) .
256 Xml
::buildForm( $inputForm, 'table_pager_limit_submit' ) .
257 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
258 Html
::closeElement( 'fieldset' ) .
259 Html
::closeElement( 'form' ) . "\n";
262 function getTableClass() {
263 return 'listfiles ' . parent
::getTableClass();
266 function getNavClass() {
267 return 'listfiles_nav ' . parent
::getNavClass();
270 function getSortHeaderClass() {
271 return 'listfiles_sort ' . parent
::getSortHeaderClass();
274 function getPagingQueries() {
275 $queries = parent
::getPagingQueries();
276 if ( !is_null( $this->mUserName
) ) {
277 # Append the username to the query string
278 foreach ( $queries as &$query ) {
279 $query['user'] = $this->mUserName
;
285 function getDefaultQuery() {
286 $queries = parent
::getDefaultQuery();
287 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
288 $queries['user'] = $this->mUserName
;