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