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