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();
107 function getFieldNames() {
108 if ( !$this->mFieldNames
) {
110 $this->mFieldNames
= array(
111 'img_timestamp' => wfMsg( 'listfiles_date' ),
112 'img_name' => wfMsg( 'listfiles_name' ),
113 'thumb' => wfMsg( 'listfiles_thumb' ),
114 'img_size' => wfMsg( 'listfiles_size' ),
115 'img_user_text' => wfMsg( 'listfiles_user' ),
116 'img_description' => wfMsg( 'listfiles_description' ),
118 if( !$wgMiserMode ) {
119 $this->mFieldNames
['count'] = wfMsg( 'listfiles_count' );
122 return $this->mFieldNames
;
125 function isFieldSortable( $field ) {
126 if ( $this->mIncluding
) {
129 static $sortable = array( 'img_timestamp', 'img_name' );
130 if ( $field == 'img_size' ) {
131 # No index for both img_size and img_user_text
132 return !isset( $this->mQueryConds
['img_user_text'] );
134 return in_array( $field, $sortable );
137 function getQueryInfo() {
138 $tables = array( 'image' );
139 $fields = array_keys( $this->getFieldNames() );
140 $fields[] = 'img_user';
141 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
142 $options = $join_conds = array();
144 # Depends on $wgMiserMode
145 if( isset( $this->mFieldNames
['count'] ) ) {
146 $tables[] = 'oldimage';
148 # Need to rewrite this one
149 foreach ( $fields as &$field ) {
150 if ( $field == 'count' ) {
151 $field = 'COUNT(oi_archive_name) AS count';
156 $dbr = wfGetDB( DB_SLAVE
);
157 if( $dbr->implicitGroupby() ) {
158 $options = array( 'GROUP BY' => 'img_name' );
160 $columnlist = implode( ',',
161 preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
162 $options = array( 'GROUP BY' => "img_user, $columnlist" );
164 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
169 'conds' => $this->mQueryConds
,
170 'options' => $options,
171 'join_conds' => $join_conds
175 function getDefaultSort() {
176 return 'img_timestamp';
179 function getStartBody() {
180 # Do a link batch query for user pages
181 if ( $this->mResult
->numRows() ) {
183 $this->mResult
->seek( 0 );
184 foreach ( $this->mResult
as $row ) {
185 if ( $row->img_user
) {
186 $lb->add( NS_USER
, str_replace( ' ', '_', $row->img_user_text
) );
192 return parent
::getStartBody();
195 function formatValue( $field, $value ) {
199 $file = wfLocalFile( $value );
200 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
201 return $thumb->toHtml( array( 'desc-link' => true ) );
202 case 'img_timestamp':
203 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
205 static $imgfile = null;
206 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
208 // Weird files can maybe exist? Bug 22227
209 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
211 $link = $this->getSkin()->linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
212 $download = Xml
::element( 'a',
213 array( 'href' => wfLocalFile( $filePage )->getURL() ),
216 return "$link ($download)";
218 return htmlspecialchars( $value );
220 case 'img_user_text':
221 if ( $this->mCurrentRow
->img_user
) {
222 $link = $this->getSkin()->link(
223 Title
::makeTitle( NS_USER
, $value ),
224 htmlspecialchars( $value )
227 $link = htmlspecialchars( $value );
231 return $this->getSkin()->formatSize( $value );
232 case 'img_description':
233 return $this->getSkin()->commentBlock( $value );
235 return intval( $value ) +
1;
240 global $wgScript, $wgMiserMode;
241 $inputForm = array();
242 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
243 if ( !$wgMiserMode ) {
244 $inputForm['listfiles_search_for'] = Html
::input( 'ilsearch', $this->mSearch
, 'text',
247 'maxlength' => '255',
248 'id' => 'mw-ilsearch',
251 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
253 'maxlength' => '255',
254 'id' => 'mw-listfiles-user',
256 return Html
::openElement( 'form',
257 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
258 Xml
::fieldset( wfMsg( 'listfiles' ) ) .
259 Xml
::buildForm( $inputForm, 'table_pager_limit_submit' ) .
260 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
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
;