Add all columns to GROUP BY for databases that need them (e.g. not MySQL).
[lhc/web/wiklou.git] / includes / specials / SpecialListfiles.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 function wfSpecialListfiles() {
11 global $wgOut;
12
13 $pager = new ImageListPager;
14
15 $limit = $pager->getForm();
16 $body = $pager->getBody();
17 $nav = $pager->getNavigationBar();
18 $wgOut->addHTML( "$limit<br />\n$body<br />\n$nav" );
19 }
20
21 /**
22 * @ingroup SpecialPage Pager
23 */
24 class ImageListPager extends TablePager {
25 var $mFieldNames = null;
26 var $mQueryConds = array();
27
28 function __construct() {
29 global $wgRequest, $wgMiserMode;
30 if ( $wgRequest->getText( 'sort', 'img_date' ) == 'img_date' ) {
31 $this->mDefaultDirection = true;
32 } else {
33 $this->mDefaultDirection = false;
34 }
35 $search = $wgRequest->getText( 'ilsearch' );
36 if ( $search != '' && !$wgMiserMode ) {
37 $nt = Title::newFromUrl( $search );
38 if( $nt ) {
39 $dbr = wfGetDB( DB_SLAVE );
40 $m = $dbr->strencode( strtolower( $nt->getDBkey() ) );
41 $m = str_replace( "%", "\\%", $m );
42 $m = str_replace( "_", "\\_", $m );
43 $this->mQueryConds = array( "LOWER(img_name) LIKE '%{$m}%'" );
44 }
45 }
46
47 parent::__construct();
48 }
49
50 function getFieldNames() {
51 if ( !$this->mFieldNames ) {
52 global $wgMiserMode;
53 $this->mFieldNames = array(
54 'img_timestamp' => wfMsg( 'listfiles_date' ),
55 'img_name' => wfMsg( 'listfiles_name' ),
56 'img_user_text' => wfMsg( 'listfiles_user' ),
57 'img_size' => wfMsg( 'listfiles_size' ),
58 'img_description' => wfMsg( 'listfiles_description' ),
59 );
60 if( !$wgMiserMode ) {
61 $this->mFieldNames['COUNT(oi_archive_name)'] = wfMsg( 'listfiles_count' );
62 }
63 }
64 return $this->mFieldNames;
65 }
66
67 function isFieldSortable( $field ) {
68 static $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
69 return in_array( $field, $sortable );
70 }
71
72 function getQueryInfo() {
73 $tables = array( 'image' );
74 $fields = array_keys( $this->getFieldNames() );
75 $fields[] = 'img_user';
76 $options = $join_conds = array();
77 # Depends on $wgMiserMode
78 if( isset($this->mFieldNames['COUNT(oi_archive_name)']) ) {
79 $tables[] = 'oldimage';
80 $dbr = wfGetDB( DB_SLAVE );
81 if( $dbr->implicitGroupby() ) {
82 $options = array('GROUP BY' => 'img_name');
83 } else {
84 $columnlist = implode( ',', preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
85 $options = array('GROUP BY' => "img_user, $columnlist");
86 }
87 $join_conds = array('oldimage' => array('LEFT JOIN','oi_name = img_name') );
88 }
89 return array(
90 'tables' => $tables,
91 'fields' => $fields,
92 'conds' => $this->mQueryConds,
93 'options' => $options,
94 'join_conds' => $join_conds
95 );
96 }
97
98 function getDefaultSort() {
99 return 'img_timestamp';
100 }
101
102 function getStartBody() {
103 # Do a link batch query for user pages
104 if ( $this->mResult->numRows() ) {
105 $lb = new LinkBatch;
106 $this->mResult->seek( 0 );
107 while ( $row = $this->mResult->fetchObject() ) {
108 if ( $row->img_user ) {
109 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
110 }
111 }
112 $lb->execute();
113 }
114
115 return parent::getStartBody();
116 }
117
118 function formatValue( $field, $value ) {
119 global $wgLang;
120 switch ( $field ) {
121 case 'img_timestamp':
122 return $wgLang->timeanddate( $value, true );
123 case 'img_name':
124 static $imgfile = null;
125 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
126
127 $name = $this->mCurrentRow->img_name;
128 $link = $this->getSkin()->makeKnownLinkObj( Title::makeTitle( NS_FILE, $name ), $value );
129 $image = wfLocalFile( $value );
130 $url = $image->getURL();
131 $download = Xml::element('a', array( 'href' => $url ), $imgfile );
132 return "$link ($download)";
133 case 'img_user_text':
134 if ( $this->mCurrentRow->img_user ) {
135 $link = $this->getSkin()->makeLinkObj( Title::makeTitle( NS_USER, $value ),
136 htmlspecialchars( $value ) );
137 } else {
138 $link = htmlspecialchars( $value );
139 }
140 return $link;
141 case 'img_size':
142 return $this->getSkin()->formatSize( $value );
143 case 'img_description':
144 return $this->getSkin()->commentBlock( $value );
145 case 'COUNT(oi_archive_name)':
146 return intval($value)+1;
147 }
148 }
149
150 function getForm() {
151 global $wgRequest, $wgMiserMode;
152 $search = $wgRequest->getText( 'ilsearch' );
153
154 $s = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $this->getTitle()->getLocalURL(), 'id' => 'mw-listfiles-form' ) ) .
155 Xml::openElement( 'fieldset' ) .
156 Xml::element( 'legend', null, wfMsg( 'listfiles' ) ) .
157 Xml::tags( 'label', null, wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() ) );
158
159 if ( !$wgMiserMode ) {
160 $s .= "<br />\n" .
161 Xml::inputLabel( wfMsg( 'listfiles_search_for' ), 'ilsearch', 'mw-ilsearch', 20, $search );
162 }
163 $s .= ' ' .
164 Xml::submitButton( wfMsg( 'table_pager_limit_submit' ) ) ."\n" .
165 $this->getHiddenFields( array( 'limit', 'ilsearch' ) ) .
166 Xml::closeElement( 'fieldset' ) .
167 Xml::closeElement( 'form' ) . "\n";
168 return $s;
169 }
170
171 function getTableClass() {
172 return 'listfiles ' . parent::getTableClass();
173 }
174
175 function getNavClass() {
176 return 'listfiles_nav ' . parent::getNavClass();
177 }
178
179 function getSortHeaderClass() {
180 return 'listfiles_sort ' . parent::getSortHeaderClass();
181 }
182 }