Merge "Fix font of mw-ui-button"
[lhc/web/wiklou.git] / includes / pager / TablePager.php
1 <?php
2 /**
3 * Efficient paging for SQL queries.
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 Pager
22 */
23
24 /**
25 * Table-based display with a user-selectable sort order
26 * @ingroup Pager
27 */
28 abstract class TablePager extends IndexPager {
29 protected $mSort;
30
31 protected $mCurrentRow;
32
33 public function __construct( IContextSource $context = null ) {
34 if ( $context ) {
35 $this->setContext( $context );
36 }
37
38 $this->mSort = $this->getRequest()->getText( 'sort' );
39 if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
40 || !$this->isFieldSortable( $this->mSort )
41 ) {
42 $this->mSort = $this->getDefaultSort();
43 }
44 if ( $this->getRequest()->getBool( 'asc' ) ) {
45 $this->mDefaultDirection = false;
46 } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
47 $this->mDefaultDirection = true;
48 } /* Else leave it at whatever the class default is */
49
50 parent::__construct();
51 }
52
53 /**
54 * @protected
55 * @return string
56 */
57 function getStartBody() {
58 global $wgStylePath;
59 $sortClass = $this->getSortHeaderClass();
60
61 $s = '';
62 $fields = $this->getFieldNames();
63
64 # Make table header
65 foreach ( $fields as $field => $name ) {
66 if ( strval( $name ) == '' ) {
67 $s .= Html::rawElement( 'th', array(), '&#160;' ) . "\n";
68 } elseif ( $this->isFieldSortable( $field ) ) {
69 $query = array( 'sort' => $field, 'limit' => $this->mLimit );
70 if ( $field == $this->mSort ) {
71 # This is the sorted column
72 # Prepare a link that goes in the other sort order
73 if ( $this->mDefaultDirection ) {
74 # Descending
75 $image = 'Arr_d.png';
76 $query['asc'] = '1';
77 $query['desc'] = '';
78 $alt = $this->msg( 'descending_abbrev' )->escaped();
79 } else {
80 # Ascending
81 $image = 'Arr_u.png';
82 $query['asc'] = '';
83 $query['desc'] = '1';
84 $alt = $this->msg( 'ascending_abbrev' )->escaped();
85 }
86 $image = "$wgStylePath/common/images/$image";
87 $link = $this->makeLink(
88 Html::element( 'img', array( 'width' => 12, 'height' => 12,
89 'alt' => $alt, 'src' => $image ) ) . htmlspecialchars( $name ), $query );
90 $s .= Html::rawElement( 'th', array( 'class' => $sortClass ), $link ) . "\n";
91 } else {
92 $s .= Html::rawElement( 'th', array(),
93 $this->makeLink( htmlspecialchars( $name ), $query ) ) . "\n";
94 }
95 } else {
96 $s .= Html::element( 'th', array(), $name ) . "\n";
97 }
98 }
99
100 $tableClass = $this->getTableClass();
101 $ret = Html::openElement( 'table', array(
102 'style' => 'border:1px;',
103 'class' => "mw-datatable $tableClass" )
104 );
105 $ret .= Html::rawElement( 'thead', array(), Html::rawElement( 'tr', array(), "\n" . $s . "\n" ) );
106 $ret .= Html::openElement( 'tbody' ) . "\n";
107
108 return $ret;
109 }
110
111 /**
112 * @protected
113 * @return string
114 */
115 function getEndBody() {
116 return "</tbody></table>\n";
117 }
118
119 /**
120 * @protected
121 * @return string
122 */
123 function getEmptyBody() {
124 $colspan = count( $this->getFieldNames() );
125 $msgEmpty = $this->msg( 'table_pager_empty' )->text();
126 return Html::rawElement( 'tr', array(),
127 Html::element( 'td', array( 'colspan' => $colspan ), $msgEmpty ) );
128 }
129
130 /**
131 * @protected
132 * @param stdClass $row
133 * @return string HTML
134 */
135 function formatRow( $row ) {
136 $this->mCurrentRow = $row; // In case formatValue etc need to know
137 $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
138 $fieldNames = $this->getFieldNames();
139
140 foreach ( $fieldNames as $field => $name ) {
141 $value = isset( $row->$field ) ? $row->$field : null;
142 $formatted = strval( $this->formatValue( $field, $value ) );
143
144 if ( $formatted == '' ) {
145 $formatted = '&#160;';
146 }
147
148 $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
149 }
150
151 $s .= Html::closeElement( 'tr' ) . "\n";
152
153 return $s;
154 }
155
156 /**
157 * Get a class name to be applied to the given row.
158 *
159 * @protected
160 *
161 * @param object $row The database result row
162 * @return string
163 */
164 function getRowClass( $row ) {
165 return '';
166 }
167
168 /**
169 * Get attributes to be applied to the given row.
170 *
171 * @protected
172 *
173 * @param object $row The database result row
174 * @return array Array of attribute => value
175 */
176 function getRowAttrs( $row ) {
177 $class = $this->getRowClass( $row );
178 if ( $class === '' ) {
179 // Return an empty array to avoid clutter in HTML like class=""
180 return array();
181 } else {
182 return array( 'class' => $this->getRowClass( $row ) );
183 }
184 }
185
186 /**
187 * Get any extra attributes to be applied to the given cell. Don't
188 * take this as an excuse to hardcode styles; use classes and
189 * CSS instead. Row context is available in $this->mCurrentRow
190 *
191 * @protected
192 *
193 * @param string $field The column
194 * @param string $value The cell contents
195 * @return array Array of attr => value
196 */
197 function getCellAttrs( $field, $value ) {
198 return array( 'class' => 'TablePager_col_' . $field );
199 }
200
201 /**
202 * @protected
203 * @return string
204 */
205 function getIndexField() {
206 return $this->mSort;
207 }
208
209 /**
210 * @protected
211 * @return string
212 */
213 function getTableClass() {
214 return 'TablePager';
215 }
216
217 /**
218 * @protected
219 * @return string
220 */
221 function getNavClass() {
222 return 'TablePager_nav';
223 }
224
225 /**
226 * @protected
227 * @return string
228 */
229 function getSortHeaderClass() {
230 return 'TablePager_sort';
231 }
232
233 /**
234 * A navigation bar with images
235 * @return string HTML
236 */
237 public function getNavigationBar() {
238 global $wgStylePath;
239
240 if ( !$this->isNavigationBarShown() ) {
241 return '';
242 }
243
244 $path = "$wgStylePath/common/images";
245 $labels = array(
246 'first' => 'table_pager_first',
247 'prev' => 'table_pager_prev',
248 'next' => 'table_pager_next',
249 'last' => 'table_pager_last',
250 );
251 $images = array(
252 'first' => 'arrow_first_25.png',
253 'prev' => 'arrow_left_25.png',
254 'next' => 'arrow_right_25.png',
255 'last' => 'arrow_last_25.png',
256 );
257 $disabledImages = array(
258 'first' => 'arrow_disabled_first_25.png',
259 'prev' => 'arrow_disabled_left_25.png',
260 'next' => 'arrow_disabled_right_25.png',
261 'last' => 'arrow_disabled_last_25.png',
262 );
263 if ( $this->getLanguage()->isRTL() ) {
264 $keys = array_keys( $labels );
265 $images = array_combine( $keys, array_reverse( $images ) );
266 $disabledImages = array_combine( $keys, array_reverse( $disabledImages ) );
267 }
268
269 $linkTexts = array();
270 $disabledTexts = array();
271 foreach ( $labels as $type => $label ) {
272 $msgLabel = $this->msg( $label )->escaped();
273 $linkTexts[$type] = Html::element( 'img', array( 'src' => "$path/{$images[$type]}",
274 'alt' => $msgLabel ) ) . "<br />$msgLabel";
275 $disabledTexts[$type] = Html::element( 'img', array( 'src' => "$path/{$disabledImages[$type]}",
276 'alt' => $msgLabel ) ) . "<br />$msgLabel";
277 }
278 $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
279
280 $s = Html::openElement( 'table', array( 'class' => $this->getNavClass() ) );
281 $s .= Html::openElement( 'tr' ) . "\n";
282 $width = 100 / count( $links ) . '%';
283 foreach ( $labels as $type => $label ) {
284 $s .= Html::rawElement( 'td', array( 'style' => "width:$width;" ), $links[$type] ) . "\n";
285 }
286 $s .= Html::closeElement( 'tr' ) . Html::closeElement( 'table' ) . "\n";
287 return $s;
288 }
289
290 /**
291 * Get a "<select>" element which has options for each of the allowed limits
292 *
293 * @param string $attribs Extra attributes to set
294 * @return string HTML fragment
295 */
296 public function getLimitSelect( $attribs = array() ) {
297 $select = new XmlSelect( 'limit', false, $this->mLimit );
298 $select->addOptions( $this->getLimitSelectList() );
299 foreach ( $attribs as $name => $value ) {
300 $select->setAttribute( $name, $value );
301 }
302 return $select->getHTML();
303 }
304
305 /**
306 * Get a list of items to show in a "<select>" element of limits.
307 * This can be passed directly to XmlSelect::addOptions().
308 *
309 * @since 1.22
310 * @return array
311 */
312 public function getLimitSelectList() {
313 # Add the current limit from the query string
314 # to avoid that the limit is lost after clicking Go next time
315 if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
316 $this->mLimitsShown[] = $this->mLimit;
317 sort( $this->mLimitsShown );
318 }
319 $ret = array();
320 foreach ( $this->mLimitsShown as $key => $value ) {
321 # The pair is either $index => $limit, in which case the $value
322 # will be numeric, or $limit => $text, in which case the $value
323 # will be a string.
324 if ( is_int( $value ) ) {
325 $limit = $value;
326 $text = $this->getLanguage()->formatNum( $limit );
327 } else {
328 $limit = $key;
329 $text = $value;
330 }
331 $ret[$text] = $limit;
332 }
333 return $ret;
334 }
335
336 /**
337 * Get \<input type="hidden"\> elements for use in a method="get" form.
338 * Resubmits all defined elements of the query string, except for a
339 * blacklist, passed in the $blacklist parameter.
340 *
341 * @param array $blacklist Parameters from the request query which should not be resubmitted
342 * @return string HTML fragment
343 */
344 function getHiddenFields( $blacklist = array() ) {
345 $blacklist = (array)$blacklist;
346 $query = $this->getRequest()->getQueryValues();
347 foreach ( $blacklist as $name ) {
348 unset( $query[$name] );
349 }
350 $s = '';
351 foreach ( $query as $name => $value ) {
352 $s .= Html::hidden( $name, $value ) . "\n";
353 }
354 return $s;
355 }
356
357 /**
358 * Get a form containing a limit selection dropdown
359 *
360 * @return string HTML fragment
361 */
362 function getLimitForm() {
363 global $wgScript;
364
365 return Html::rawElement(
366 'form',
367 array(
368 'method' => 'get',
369 'action' => $wgScript
370 ),
371 "\n" . $this->getLimitDropdown()
372 ) . "\n";
373 }
374
375 /**
376 * Gets a limit selection dropdown
377 *
378 * @return string
379 */
380 function getLimitDropdown() {
381 # Make the select with some explanatory text
382 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
383
384 return $this->msg( 'table_pager_limit' )
385 ->rawParams( $this->getLimitSelect() )->escaped() .
386 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
387 $this->getHiddenFields( array( 'limit' ) );
388 }
389
390 /**
391 * Return true if the named field should be sortable by the UI, false
392 * otherwise
393 *
394 * @param string $field
395 */
396 abstract function isFieldSortable( $field );
397
398 /**
399 * Format a table cell. The return value should be HTML, but use an empty
400 * string not &#160; for empty cells. Do not include the <td> and </td>.
401 *
402 * The current result row is available as $this->mCurrentRow, in case you
403 * need more context.
404 *
405 * @protected
406 *
407 * @param string $name The database field name
408 * @param string $value The value retrieved from the database
409 */
410 abstract function formatValue( $name, $value );
411
412 /**
413 * The database field name used as a default sort order.
414 *
415 * @protected
416 *
417 * @return string
418 */
419 abstract function getDefaultSort();
420
421 /**
422 * An array mapping database field names to a textual description of the
423 * field name, for use in the table header. The description should be plain
424 * text, it will be HTML-escaped later.
425 *
426 * @return array
427 */
428 abstract function getFieldNames();
429 }