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 = implode( ',',
160 preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
161 $options = array( 'GROUP BY' => "img_user, $columnlist" );
163 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
168 'conds' => $this->mQueryConds
,
169 'options' => $options,
170 'join_conds' => $join_conds
174 function getDefaultSort() {
175 return 'img_timestamp';
178 function getStartBody() {
179 # Do a link batch query for user pages
180 if ( $this->mResult
->numRows() ) {
182 $this->mResult
->seek( 0 );
183 foreach ( $this->mResult
as $row ) {
184 if ( $row->img_user
) {
185 $lb->add( NS_USER
, str_replace( ' ', '_', $row->img_user_text
) );
191 return parent
::getStartBody();
194 function formatValue( $field, $value ) {
197 $file = wfLocalFile( $value );
198 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
199 return $thumb->toHtml( array( 'desc-link' => true ) );
200 case 'img_timestamp':
201 return htmlspecialchars( $this->getLanguage()->timeanddate( $value, true ) );
203 static $imgfile = null;
204 if ( $imgfile === null ) $imgfile = $this->msg( 'imgfile' )->text();
206 // Weird files can maybe exist? Bug 22227
207 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
209 $link = Linker
::linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
210 $download = Xml
::element( 'a',
211 array( 'href' => wfLocalFile( $filePage )->getURL() ),
214 return "$link ($download)";
216 return htmlspecialchars( $value );
218 case 'img_user_text':
219 if ( $this->mCurrentRow
->img_user
) {
220 $link = Linker
::link(
221 Title
::makeTitle( NS_USER
, $value ),
222 htmlspecialchars( $value )
225 $link = htmlspecialchars( $value );
229 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
230 case 'img_description':
231 return Linker
::commentBlock( $value );
233 return intval( $value ) +
1;
238 global $wgScript, $wgMiserMode;
239 $inputForm = array();
240 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
241 if ( !$wgMiserMode ) {
242 $inputForm['listfiles_search_for'] = Html
::input( 'ilsearch', $this->mSearch
, 'text',
245 'maxlength' => '255',
246 'id' => 'mw-ilsearch',
249 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
251 'maxlength' => '255',
252 'id' => 'mw-listfiles-user',
254 return Html
::openElement( 'form',
255 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
256 Xml
::fieldset( $this->msg( 'listfiles' )->text() ) .
257 Xml
::buildForm( $inputForm, 'table_pager_limit_submit' ) .
258 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
259 Html
::closeElement( 'fieldset' ) .
260 Html
::closeElement( 'form' ) . "\n";
263 function getTableClass() {
264 return 'listfiles ' . parent
::getTableClass();
267 function getNavClass() {
268 return 'listfiles_nav ' . parent
::getNavClass();
271 function getSortHeaderClass() {
272 return 'listfiles_sort ' . parent
::getSortHeaderClass();
275 function getPagingQueries() {
276 $queries = parent
::getPagingQueries();
277 if ( !is_null( $this->mUserName
) ) {
278 # Append the username to the query string
279 foreach ( $queries as &$query ) {
280 $query['user'] = $this->mUserName
;
286 function getDefaultQuery() {
287 $queries = parent
::getDefaultQuery();
288 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
289 $queries['user'] = $this->mUserName
;