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 ){
31 global $wgOut, $wgRequest;
33 $this->outputHeader();
35 if ( $this->including() ) {
39 $userName = $wgRequest->getText( 'user', $par );
40 $search = $wgRequest->getText( 'ilsearch', '' );
43 $pager = new ImageListPager( $userName, $search, $this->including() );
45 if ( $this->including() ) {
46 $html = $pager->getBody();
48 $form = $pager->getForm();
49 $body = $pager->getBody();
50 $nav = $pager->getNavigationBar();
51 $html = "$form<br />\n$body<br />\n$nav";
53 $wgOut->addHTML( $html );
58 * @ingroup SpecialPage Pager
60 class ImageListPager
extends TablePager
{
61 var $mFieldNames = null;
62 var $mQueryConds = array();
63 var $mUserName = null;
65 var $mIncluding = false;
67 function __construct( $userName = null, $search = '', $including = false ) {
68 global $wgRequest, $wgMiserMode;
70 $this->mIncluding
= $including;
73 $nt = Title
::newFromText( $userName, NS_USER
);
74 if ( !is_null( $nt ) ) {
75 $this->mUserName
= $nt->getText();
76 $this->mQueryConds
['img_user_text'] = $this->mUserName
;
80 if ( $search != '' && !$wgMiserMode ) {
81 $this->mSearch
= $search;
82 $nt = Title
::newFromURL( $this->mSearch
);
84 $dbr = wfGetDB( DB_SLAVE
);
85 $this->mQueryConds
[] = 'LOWER(img_name)' .
86 $dbr->buildLike( $dbr->anyString(),
87 strtolower( $nt->getDBkey() ), $dbr->anyString() );
92 if ( $wgRequest->getText( 'sort', 'img_date' ) == 'img_date' ) {
93 $this->mDefaultDirection
= true;
95 $this->mDefaultDirection
= false;
98 $this->mDefaultDirection
= true;
101 parent
::__construct();
104 function getTitle() {
105 return SpecialPage
::getTitleFor( 'Listfiles' );
111 function getFieldNames() {
112 if ( !$this->mFieldNames
) {
114 $this->mFieldNames
= array(
115 'img_timestamp' => wfMsg( 'listfiles_date' ),
116 'img_name' => wfMsg( 'listfiles_name' ),
117 'thumb' => wfMsg( 'listfiles_thumb' ),
118 'img_size' => wfMsg( 'listfiles_size' ),
119 'img_user_text' => wfMsg( 'listfiles_user' ),
120 'img_description' => wfMsg( 'listfiles_description' ),
122 if( !$wgMiserMode ) {
123 $this->mFieldNames
['count'] = wfMsg( 'listfiles_count' );
126 return $this->mFieldNames
;
129 function isFieldSortable( $field ) {
130 if ( $this->mIncluding
) {
133 static $sortable = array( 'img_timestamp', 'img_name' );
134 if ( $field == 'img_size' ) {
135 # No index for both img_size and img_user_text
136 return !isset( $this->mQueryConds
['img_user_text'] );
138 return in_array( $field, $sortable );
141 function getQueryInfo() {
142 $tables = array( 'image' );
143 $fields = array_keys( $this->getFieldNames() );
144 $fields[] = 'img_user';
145 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
146 $options = $join_conds = array();
148 # Depends on $wgMiserMode
149 if( isset( $this->mFieldNames
['count'] ) ) {
150 $tables[] = 'oldimage';
152 # Need to rewrite this one
153 foreach ( $fields as &$field ) {
154 if ( $field == 'count' ) {
155 $field = 'COUNT(oi_archive_name) AS count';
160 $dbr = wfGetDB( DB_SLAVE
);
161 if( $dbr->implicitGroupby() ) {
162 $options = array( 'GROUP BY' => 'img_name' );
164 $columnlist = implode( ',',
165 preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
166 $options = array( 'GROUP BY' => "img_user, $columnlist" );
168 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
173 'conds' => $this->mQueryConds
,
174 'options' => $options,
175 'join_conds' => $join_conds
179 function getDefaultSort() {
180 return 'img_timestamp';
183 function getStartBody() {
184 # Do a link batch query for user pages
185 if ( $this->mResult
->numRows() ) {
187 $this->mResult
->seek( 0 );
188 foreach ( $this->mResult
as $row ) {
189 if ( $row->img_user
) {
190 $lb->add( NS_USER
, str_replace( ' ', '_', $row->img_user_text
) );
196 return parent
::getStartBody();
199 function formatValue( $field, $value ) {
203 $file = wfLocalFile( $value );
204 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
205 return $thumb->toHtml( array( 'desc-link' => true ) );
206 case 'img_timestamp':
207 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
209 static $imgfile = null;
210 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
212 // Weird files can maybe exist? Bug 22227
213 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
215 $link = $this->getSkin()->linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
216 $download = Xml
::element( 'a',
217 array( 'href' => wfLocalFile( $filePage )->getURL() ),
220 return "$link ($download)";
222 return htmlspecialchars( $value );
224 case 'img_user_text':
225 if ( $this->mCurrentRow
->img_user
) {
226 $link = $this->getSkin()->link(
227 Title
::makeTitle( NS_USER
, $value ),
228 htmlspecialchars( $value )
231 $link = htmlspecialchars( $value );
235 return $this->getSkin()->formatSize( $value );
236 case 'img_description':
237 return $this->getSkin()->commentBlock( $value );
239 return intval( $value ) +
1;
244 global $wgScript, $wgMiserMode;
245 $inputForm = array();
246 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
247 if ( !$wgMiserMode ) {
248 $inputForm['listfiles_search_for'] = Html
::input( 'ilsearch', $this->mSearch
, 'text',
251 'maxlength' => '255',
252 'id' => 'mw-ilsearch',
255 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
257 'maxlength' => '255',
258 'id' => 'mw-listfiles-user',
260 return Html
::openElement( 'form',
261 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
262 Xml
::fieldset( wfMsg( 'listfiles' ) ) .
263 Xml
::buildForm( $inputForm, 'table_pager_limit_submit' ) .
264 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
265 Html
::closeElement( 'fieldset' ) .
266 Html
::closeElement( 'form' ) . "\n";
269 function getTableClass() {
270 return 'listfiles ' . parent
::getTableClass();
273 function getNavClass() {
274 return 'listfiles_nav ' . parent
::getNavClass();
277 function getSortHeaderClass() {
278 return 'listfiles_sort ' . parent
::getSortHeaderClass();
281 function getPagingQueries() {
282 $queries = parent
::getPagingQueries();
283 if ( !is_null( $this->mUserName
) ) {
284 # Append the username to the query string
285 foreach ( $queries as &$query ) {
286 $query['user'] = $this->mUserName
;
292 function getDefaultQuery() {
293 $queries = parent
::getDefaultQuery();
294 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
295 $queries['user'] = $this->mUserName
;