w/s diff. mostly eol w/s and using only tabs of width 4 to indent.
[lhc/web/wiklou.git] / includes / specials / SpecialListfiles.php
1 <?php
2 /**
3 * Implements Special:Listfiles
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 function wfSpecialListfiles( $par = null ) {
25 global $wgOut;
26
27 $pager = new ImageListPager( $par );
28
29 $limit = $pager->getForm();
30 $body = $pager->getBody();
31 $nav = $pager->getNavigationBar();
32 $wgOut->addHTML( "$limit<br />\n$body<br />\n$nav" );
33 }
34
35 /**
36 * @ingroup SpecialPage Pager
37 */
38 class ImageListPager extends TablePager {
39 var $mFieldNames = null;
40 var $mQueryConds = array();
41 var $mUserName = null;
42
43 function __construct( $par = null ) {
44 global $wgRequest, $wgMiserMode;
45 if ( $wgRequest->getText( 'sort', 'img_date' ) == 'img_date' ) {
46 $this->mDefaultDirection = true;
47 } else {
48 $this->mDefaultDirection = false;
49 }
50
51 $userName = $wgRequest->getText( 'user', $par );
52 if ( $userName ) {
53 $nt = Title::newFromText( $userName, NS_USER );
54 if ( !is_null( $nt ) ) {
55 $this->mUserName = $nt->getText();
56 $this->mQueryConds['img_user_text'] = $this->mUserName;
57 }
58 }
59
60 $search = $wgRequest->getText( 'ilsearch' );
61 if ( $search != '' && !$wgMiserMode ) {
62 $nt = Title::newFromURL( $search );
63 if ( $nt ) {
64 $dbr = wfGetDB( DB_SLAVE );
65 $this->mQueryConds[] = 'LOWER(img_name)' . $dbr->buildLike( $dbr->anyString(),
66 strtolower( $nt->getDBkey() ), $dbr->anyString() );
67 }
68 }
69
70 parent::__construct();
71 }
72
73 /**
74 * @return Array
75 */
76 function getFieldNames() {
77 if ( !$this->mFieldNames ) {
78 global $wgMiserMode;
79 $this->mFieldNames = array(
80 'img_timestamp' => wfMsg( 'listfiles_date' ),
81 'img_name' => wfMsg( 'listfiles_name' ),
82 'thumb' => wfMsg( 'listfiles_thumb' ),
83 'img_size' => wfMsg( 'listfiles_size' ),
84 'img_user_text' => wfMsg( 'listfiles_user' ),
85 'img_description' => wfMsg( 'listfiles_description' ),
86 );
87 if( !$wgMiserMode ) {
88 $this->mFieldNames['count'] = wfMsg( 'listfiles_count' );
89 }
90 }
91 return $this->mFieldNames;
92 }
93
94 function isFieldSortable( $field ) {
95 static $sortable = array( 'img_timestamp', 'img_name' );
96 if ( $field == 'img_size' ) {
97 # No index for both img_size and img_user_text
98 return !isset( $this->mQueryConds['img_user_text'] );
99 }
100 return in_array( $field, $sortable );
101 }
102
103 function getQueryInfo() {
104 $tables = array( 'image' );
105 $fields = array_keys( $this->getFieldNames() );
106 $fields[] = 'img_user';
107 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
108 $options = $join_conds = array();
109
110 # Depends on $wgMiserMode
111 if( isset( $this->mFieldNames['count'] ) ) {
112 $tables[] = 'oldimage';
113
114 # Need to rewrite this one
115 foreach ( $fields as &$field ) {
116 if ( $field == 'count' ) {
117 $field = 'COUNT(oi_archive_name) AS count';
118 }
119 }
120 unset( $field );
121
122 $dbr = wfGetDB( DB_SLAVE );
123 if( $dbr->implicitGroupby() ) {
124 $options = array( 'GROUP BY' => 'img_name' );
125 } else {
126 $columnlist = implode( ',', preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
127 $options = array( 'GROUP BY' => "img_user, $columnlist" );
128 }
129 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
130 }
131 return array(
132 'tables' => $tables,
133 'fields' => $fields,
134 'conds' => $this->mQueryConds,
135 'options' => $options,
136 'join_conds' => $join_conds
137 );
138 }
139
140 function getDefaultSort() {
141 return 'img_timestamp';
142 }
143
144 function getStartBody() {
145 # Do a link batch query for user pages
146 if ( $this->mResult->numRows() ) {
147 $lb = new LinkBatch;
148 $this->mResult->seek( 0 );
149 foreach ( $this->mResult as $row ) {
150 if ( $row->img_user ) {
151 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
152 }
153 }
154 $lb->execute();
155 }
156
157 return parent::getStartBody();
158 }
159
160 function formatValue( $field, $value ) {
161 global $wgLang;
162 switch ( $field ) {
163 case 'thumb':
164 $file = wfLocalFile( $value );
165 $thumb = $file->transform( array( 'width' => 180 ) );
166 return $thumb->toHtml( array( 'desc-link' => true ) );
167 case 'img_timestamp':
168 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
169 case 'img_name':
170 static $imgfile = null;
171 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
172
173 $filePage = Title::makeTitle( NS_FILE, $value );
174 $link = $this->getSkin()->linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
175 $image = wfLocalFile( $value );
176 $url = $image->getURL();
177 $download = Xml::element('a', array( 'href' => $url ), $imgfile );
178 return "$link ($download)";
179 case 'img_user_text':
180 if ( $this->mCurrentRow->img_user ) {
181 $link = $this->getSkin()->link(
182 Title::makeTitle( NS_USER, $value ),
183 htmlspecialchars( $value )
184 );
185 } else {
186 $link = htmlspecialchars( $value );
187 }
188 return $link;
189 case 'img_size':
190 return $this->getSkin()->formatSize( $value );
191 case 'img_description':
192 return $this->getSkin()->commentBlock( $value );
193 case 'count':
194 return intval($value)+1;
195 }
196 }
197
198 function getForm() {
199 global $wgRequest, $wgScript, $wgMiserMode;
200 $search = $wgRequest->getText( 'ilsearch' );
201 $inputForm = array();
202 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
203 if ( !$wgMiserMode ) {
204 $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $search, 'text', array(
205 'size' => '40',
206 'maxlength' => '255',
207 'id' => 'mw-ilsearch',
208 ) );
209 }
210 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
211 'size' => '40',
212 'maxlength' => '255',
213 'id' => 'mw-listfiles-user',
214 ) );
215 $s = Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
216 Xml::fieldset( wfMsg( 'listfiles' ) ) .
217 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
218 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
219 Html::closeElement( 'fieldset' ) .
220 Html::closeElement( 'form' ) . "\n";
221 return $s;
222 }
223
224 function getTableClass() {
225 return 'listfiles ' . parent::getTableClass();
226 }
227
228 function getNavClass() {
229 return 'listfiles_nav ' . parent::getNavClass();
230 }
231
232 function getSortHeaderClass() {
233 return 'listfiles_sort ' . parent::getSortHeaderClass();
234 }
235
236 function getPagingQueries() {
237 $queries = parent::getPagingQueries();
238 if ( !is_null( $this->mUserName ) ) {
239 # Append the username to the query string
240 foreach ( $queries as &$query ) {
241 $query['user'] = $this->mUserName;
242 }
243 }
244 return $queries;
245 }
246
247 function getDefaultQuery() {
248 $queries = parent::getDefaultQuery();
249 if ( !isset( $queries['user'] )
250 && !is_null( $this->mUserName ) )
251 {
252 $queries['user'] = $this->mUserName;
253 }
254 return $queries;
255 }
256 }