Revert r70270; per CR comments.
[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 function getFieldNames() {
74 if ( !$this->mFieldNames ) {
75 global $wgMiserMode;
76 $this->mFieldNames = array(
77 'thumb' => wfMsg( 'listfiles_thumb' ),
78 'img_timestamp' => wfMsg( 'listfiles_date' ),
79 'img_name' => wfMsg( 'listfiles_name' ),
80 'img_user_text' => wfMsg( 'listfiles_user' ),
81 'img_size' => wfMsg( 'listfiles_size' ),
82 'img_description' => wfMsg( 'listfiles_description' ),
83 );
84 if( !$wgMiserMode ) {
85 $this->mFieldNames['count'] = wfMsg( 'listfiles_count' );
86 }
87 }
88 return $this->mFieldNames;
89 }
90
91 function isFieldSortable( $field ) {
92 static $sortable = array( 'img_timestamp', 'img_name' );
93 if ( $field == 'img_size' ) {
94 # No index for both img_size and img_user_text
95 return !isset( $this->mQueryConds['img_user_text'] );
96 }
97 return in_array( $field, $sortable );
98 }
99
100 function getQueryInfo() {
101 $tables = array( 'image' );
102 $fields = array_keys( $this->getFieldNames() );
103 $fields[] = 'img_user';
104 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
105 $options = $join_conds = array();
106
107 # Depends on $wgMiserMode
108 if( isset( $this->mFieldNames['count'] ) ) {
109 $tables[] = 'oldimage';
110
111 # Need to rewrite this one
112 foreach ( $fields as &$field ) {
113 if ( $field == 'count' ) {
114 $field = 'COUNT(oi_archive_name) AS count';
115 }
116 }
117 unset( $field );
118
119 $dbr = wfGetDB( DB_SLAVE );
120 if( $dbr->implicitGroupby() ) {
121 $options = array( 'GROUP BY' => 'img_name' );
122 } else {
123 $columnlist = implode( ',', preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
124 $options = array( 'GROUP BY' => "img_user, $columnlist" );
125 }
126 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
127 }
128 return array(
129 'tables' => $tables,
130 'fields' => $fields,
131 'conds' => $this->mQueryConds,
132 'options' => $options,
133 'join_conds' => $join_conds
134 );
135 }
136
137 function getDefaultSort() {
138 return 'img_timestamp';
139 }
140
141 function getStartBody() {
142 # Do a link batch query for user pages
143 if ( $this->mResult->numRows() ) {
144 $lb = new LinkBatch;
145 $this->mResult->seek( 0 );
146 foreach ( $this->mResult as $row ) {
147 if ( $row->img_user ) {
148 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
149 }
150 }
151 $lb->execute();
152 }
153
154 return parent::getStartBody();
155 }
156
157 function formatValue( $field, $value ) {
158 global $wgLang;
159 switch ( $field ) {
160 case 'thumb':
161 $file = wfLocalFile( $value );
162 $thumb = $file->transform( array( 'width' => 180 ) );
163 return $thumb->toHtml( array( 'desc-link' => true ) );
164 case 'img_timestamp':
165 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
166 case 'img_name':
167 static $imgfile = null;
168 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
169
170 $filePage = Title::makeTitle( NS_FILE, $value );
171 $link = $this->getSkin()->linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
172 $image = wfLocalFile( $value );
173 $url = $image->getURL();
174 $download = Xml::element('a', array( 'href' => $url ), $imgfile );
175 return "$link ($download)";
176 case 'img_user_text':
177 if ( $this->mCurrentRow->img_user ) {
178 $link = $this->getSkin()->link(
179 Title::makeTitle( NS_USER, $value ),
180 htmlspecialchars( $value )
181 );
182 } else {
183 $link = htmlspecialchars( $value );
184 }
185 return $link;
186 case 'img_size':
187 return $this->getSkin()->formatSize( $value );
188 case 'img_description':
189 return $this->getSkin()->commentBlock( $value );
190 case 'count':
191 return intval($value)+1;
192 }
193 }
194
195 function getForm() {
196 global $wgRequest, $wgScript, $wgMiserMode;
197 $search = $wgRequest->getText( 'ilsearch' );
198 $inputForm = array();
199 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
200 if ( !$wgMiserMode ) {
201 $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $search, 'text', array(
202 'size' => '40',
203 'maxlength' => '255',
204 'id' => 'mw-ilsearch',
205 ) );
206 }
207 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
208 'size' => '40',
209 'maxlength' => '255',
210 'id' => 'mw-listfiles-user',
211 ) );
212 $s = Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
213 Xml::fieldset( wfMsg( 'listfiles' ) ) .
214 Html::openElement( 'table', array( 'id' => 'mw-listfiles-table' ) ) .
215 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
216 $this->getHiddenFields( array( 'limit', 'ilsearch', 'username' ) ) .
217 Html::closeElement( 'table' ) .
218 Html::closeElement( 'fieldset' ) .
219 Html::closeElement( 'form' ) . "\n";
220 return $s;
221 }
222
223 function getTableClass() {
224 return 'listfiles ' . parent::getTableClass();
225 }
226
227 function getNavClass() {
228 return 'listfiles_nav ' . parent::getNavClass();
229 }
230
231 function getSortHeaderClass() {
232 return 'listfiles_sort ' . parent::getSortHeaderClass();
233 }
234
235 function getPagingQueries() {
236 $queries = parent::getPagingQueries();
237 if ( !is_null( $this->mUserName ) ) {
238 # Append the username to the query string
239 foreach ( $queries as &$query ) {
240 $query['username'] = $this->mUserName;
241 }
242 }
243 return $queries;
244 }
245 }