024f6f9ad8031da094696d6867b0eb36ffd6b098
[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 $this->mQueryConds = array( 'LOWER(img_name)' . $dbr->buildLike( $dbr->anyString(),
41 strtolower( $nt->getDBkey() ), $dbr->anyString() ) );
42 }
43 }
44
45 parent::__construct();
46 }
47
48 function getFieldNames() {
49 if ( !$this->mFieldNames ) {
50 global $wgMiserMode;
51 $this->mFieldNames = array(
52 'img_timestamp' => wfMsg( 'listfiles_date' ),
53 'img_name' => wfMsg( 'listfiles_name' ),
54 'img_user_text' => wfMsg( 'listfiles_user' ),
55 'img_size' => wfMsg( 'listfiles_size' ),
56 'img_description' => wfMsg( 'listfiles_description' ),
57 );
58 if( !$wgMiserMode ) {
59 $this->mFieldNames['count'] = wfMsg( 'listfiles_count' );
60 }
61 }
62 return $this->mFieldNames;
63 }
64
65 function isFieldSortable( $field ) {
66 static $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
67 return in_array( $field, $sortable );
68 }
69
70 function getQueryInfo() {
71 $tables = array( 'image' );
72 $fields = array_keys( $this->getFieldNames() );
73 $fields[] = 'img_user';
74 $options = $join_conds = array();
75
76 # Depends on $wgMiserMode
77 if( isset( $this->mFieldNames['count'] ) ) {
78 $tables[] = 'oldimage';
79
80 # Need to rewrite this one
81 foreach ( $fields as &$field )
82 if ( $field == 'count' )
83 $field = 'COUNT(oi_archive_name) as count';
84 unset( $field );
85
86 $dbr = wfGetDB( DB_SLAVE );
87 if( $dbr->implicitGroupby() ) {
88 $options = array( 'GROUP BY' => 'img_name' );
89 } else {
90 $columnlist = implode( ',', preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
91 $options = array( 'GROUP BY' => "img_user, $columnlist" );
92 }
93 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
94 }
95 return array(
96 'tables' => $tables,
97 'fields' => $fields,
98 'conds' => $this->mQueryConds,
99 'options' => $options,
100 'join_conds' => $join_conds
101 );
102 }
103
104 function getDefaultSort() {
105 return 'img_timestamp';
106 }
107
108 function getStartBody() {
109 # Do a link batch query for user pages
110 if ( $this->mResult->numRows() ) {
111 $lb = new LinkBatch;
112 $this->mResult->seek( 0 );
113 while ( $row = $this->mResult->fetchObject() ) {
114 if ( $row->img_user ) {
115 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
116 }
117 }
118 $lb->execute();
119 }
120
121 return parent::getStartBody();
122 }
123
124 function formatValue( $field, $value ) {
125 global $wgLang;
126 switch ( $field ) {
127 case 'img_timestamp':
128 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
129 case 'img_name':
130 static $imgfile = null;
131 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
132
133 $name = $this->mCurrentRow->img_name;
134 $link = $this->getSkin()->linkKnown( Title::makeTitle( NS_FILE, $name ), htmlspecialchars( $value ) );
135 $image = wfLocalFile( $value );
136 $url = $image->getURL();
137 $download = Xml::element('a', array( 'href' => $url ), $imgfile );
138 return "$link ($download)";
139 case 'img_user_text':
140 if ( $this->mCurrentRow->img_user ) {
141 $link = $this->getSkin()->link(
142 Title::makeTitle( NS_USER, $value ),
143 htmlspecialchars( $value )
144 );
145 } else {
146 $link = htmlspecialchars( $value );
147 }
148 return $link;
149 case 'img_size':
150 return $this->getSkin()->formatSize( $value );
151 case 'img_description':
152 return $this->getSkin()->commentBlock( $value );
153 case 'count':
154 return intval($value)+1;
155 }
156 }
157
158 function getForm() {
159 global $wgRequest, $wgScript, $wgMiserMode;
160 $search = $wgRequest->getText( 'ilsearch' );
161
162 $s = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
163 Xml::openElement( 'fieldset' ) .
164 Xml::element( 'legend', null, wfMsg( 'listfiles' ) ) .
165 Xml::tags( 'label', null, wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() ) );
166
167 if ( !$wgMiserMode ) {
168 $s .= "<br />\n" .
169 Xml::inputLabel( wfMsg( 'listfiles_search_for' ), 'ilsearch', 'mw-ilsearch', 20, $search );
170 }
171 $s .= ' ' .
172 Xml::submitButton( wfMsg( 'table_pager_limit_submit' ) ) ."\n" .
173 $this->getHiddenFields( array( 'limit', 'ilsearch' ) ) .
174 Xml::closeElement( 'fieldset' ) .
175 Xml::closeElement( 'form' ) . "\n";
176 return $s;
177 }
178
179 function getTableClass() {
180 return 'listfiles ' . parent::getTableClass();
181 }
182
183 function getNavClass() {
184 return 'listfiles_nav ' . parent::getNavClass();
185 }
186
187 function getSortHeaderClass() {
188 return 'listfiles_sort ' . parent::getSortHeaderClass();
189 }
190 }