TablePager: Load images via CSS backgrounds rather than HTML <img>s
[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 * Get the formatted result list. Calls getStartBody(), formatRow() and getEndBody(), concatenates
55 * the results and returns them.
56 *
57 * Also adds the required styles to our OutputPage object (this means that if context wasn't
58 * passed to constructor or otherwise set up, you will get a pager with missing styles).
59 *
60 * This method has been made 'final' in 1.24. There's no reason to override it, and if there exist
61 * any subclasses that do, the style loading hack is probably broken in them. Let's fail fast
62 * rather than mysteriously render things wrong.
63 *
64 * @deprecated since 1.24, use getBodyOutput() or getFullOutput() instead
65 * @return string
66 */
67 final public function getBody() {
68 $this->getOutput()->addModuleStyles( $this->getModuleStyles() );
69 return parent::getBody();
70 }
71
72 /**
73 * Get the formatted result list.
74 *
75 * Calls getBody() and getModuleStyles() and builds a ParserOutput object. (This is a bit hacky
76 * but works well.)
77 *
78 * @since 1.24
79 * @return ParserOutput
80 */
81 public function getBodyOutput() {
82 $body = parent::getBody();
83
84 $pout = new ParserOutput;
85 $pout->setText( $body );
86 $pout->addModuleStyles( $this->getModuleStyles() );
87 return $pout;
88 }
89
90 /**
91 * Get the formatted result list, with navigation bars.
92 *
93 * Calls getBody(), getNavigationBar() and getModuleStyles() and
94 * builds a ParserOutput object. (This is a bit hacky but works well.)
95 *
96 * @since 1.24
97 * @return ParserOutput
98 */
99 public function getFullOutput() {
100 $navigation = $this->getNavigationBar();
101 $body = parent::getBody();
102
103 $pout = new ParserOutput;
104 $pout->setText( $navigation . $body . $navigation );
105 $pout->addModuleStyles( $this->getModuleStyles() );
106 return $pout;
107 }
108
109 /**
110 * @protected
111 * @return string
112 */
113 function getStartBody() {
114 global $wgStylePath;
115 $sortClass = $this->getSortHeaderClass();
116
117 $s = '';
118 $fields = $this->getFieldNames();
119
120 # Make table header
121 foreach ( $fields as $field => $name ) {
122 if ( strval( $name ) == '' ) {
123 $s .= Html::rawElement( 'th', array(), '&#160;' ) . "\n";
124 } elseif ( $this->isFieldSortable( $field ) ) {
125 $query = array( 'sort' => $field, 'limit' => $this->mLimit );
126 if ( $field == $this->mSort ) {
127 # This is the sorted column
128 # Prepare a link that goes in the other sort order
129 if ( $this->mDefaultDirection ) {
130 # Descending
131 $image = 'Arr_d.png';
132 $query['asc'] = '1';
133 $query['desc'] = '';
134 $alt = $this->msg( 'descending_abbrev' )->escaped();
135 } else {
136 # Ascending
137 $image = 'Arr_u.png';
138 $query['asc'] = '';
139 $query['desc'] = '1';
140 $alt = $this->msg( 'ascending_abbrev' )->escaped();
141 }
142 $image = "$wgStylePath/common/images/$image";
143 $link = $this->makeLink(
144 Html::element( 'img', array( 'width' => 12, 'height' => 12,
145 'alt' => $alt, 'src' => $image ) ) . htmlspecialchars( $name ), $query );
146 $s .= Html::rawElement( 'th', array( 'class' => $sortClass ), $link ) . "\n";
147 } else {
148 $s .= Html::rawElement( 'th', array(),
149 $this->makeLink( htmlspecialchars( $name ), $query ) ) . "\n";
150 }
151 } else {
152 $s .= Html::element( 'th', array(), $name ) . "\n";
153 }
154 }
155
156 $tableClass = $this->getTableClass();
157 $ret = Html::openElement( 'table', array(
158 'class' => "mw-datatable $tableClass" )
159 );
160 $ret .= Html::rawElement( 'thead', array(), Html::rawElement( 'tr', array(), "\n" . $s . "\n" ) );
161 $ret .= Html::openElement( 'tbody' ) . "\n";
162
163 return $ret;
164 }
165
166 /**
167 * @protected
168 * @return string
169 */
170 function getEndBody() {
171 return "</tbody></table>\n";
172 }
173
174 /**
175 * @protected
176 * @return string
177 */
178 function getEmptyBody() {
179 $colspan = count( $this->getFieldNames() );
180 $msgEmpty = $this->msg( 'table_pager_empty' )->text();
181 return Html::rawElement( 'tr', array(),
182 Html::element( 'td', array( 'colspan' => $colspan ), $msgEmpty ) );
183 }
184
185 /**
186 * @protected
187 * @param stdClass $row
188 * @return string HTML
189 */
190 function formatRow( $row ) {
191 $this->mCurrentRow = $row; // In case formatValue etc need to know
192 $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
193 $fieldNames = $this->getFieldNames();
194
195 foreach ( $fieldNames as $field => $name ) {
196 $value = isset( $row->$field ) ? $row->$field : null;
197 $formatted = strval( $this->formatValue( $field, $value ) );
198
199 if ( $formatted == '' ) {
200 $formatted = '&#160;';
201 }
202
203 $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
204 }
205
206 $s .= Html::closeElement( 'tr' ) . "\n";
207
208 return $s;
209 }
210
211 /**
212 * Get a class name to be applied to the given row.
213 *
214 * @protected
215 *
216 * @param object $row The database result row
217 * @return string
218 */
219 function getRowClass( $row ) {
220 return '';
221 }
222
223 /**
224 * Get attributes to be applied to the given row.
225 *
226 * @protected
227 *
228 * @param object $row The database result row
229 * @return array Array of attribute => value
230 */
231 function getRowAttrs( $row ) {
232 $class = $this->getRowClass( $row );
233 if ( $class === '' ) {
234 // Return an empty array to avoid clutter in HTML like class=""
235 return array();
236 } else {
237 return array( 'class' => $this->getRowClass( $row ) );
238 }
239 }
240
241 /**
242 * Get any extra attributes to be applied to the given cell. Don't
243 * take this as an excuse to hardcode styles; use classes and
244 * CSS instead. Row context is available in $this->mCurrentRow
245 *
246 * @protected
247 *
248 * @param string $field The column
249 * @param string $value The cell contents
250 * @return array Array of attr => value
251 */
252 function getCellAttrs( $field, $value ) {
253 return array( 'class' => 'TablePager_col_' . $field );
254 }
255
256 /**
257 * @protected
258 * @return string
259 */
260 function getIndexField() {
261 return $this->mSort;
262 }
263
264 /**
265 * @protected
266 * @return string
267 */
268 function getTableClass() {
269 return 'TablePager';
270 }
271
272 /**
273 * @protected
274 * @return string
275 */
276 function getNavClass() {
277 return 'TablePager_nav';
278 }
279
280 /**
281 * @protected
282 * @return string
283 */
284 function getSortHeaderClass() {
285 return 'TablePager_sort';
286 }
287
288 /**
289 * A navigation bar with images
290 * @return string HTML
291 */
292 public function getNavigationBar() {
293 if ( !$this->isNavigationBarShown() ) {
294 return '';
295 }
296
297 $labels = array(
298 'first' => 'table_pager_first',
299 'prev' => 'table_pager_prev',
300 'next' => 'table_pager_next',
301 'last' => 'table_pager_last',
302 );
303
304 $linkTexts = array();
305 $disabledTexts = array();
306 foreach ( $labels as $type => $label ) {
307 $msgLabel = $this->msg( $label )->escaped();
308 $linkTexts[$type] = "<div class='TablePager_nav-enabled'>$msgLabel</div>";
309 $disabledTexts[$type] = "<div class='TablePager_nav-disabled'>$msgLabel</div>";
310 }
311 $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
312
313 $s = Html::openElement( 'table', array( 'class' => $this->getNavClass() ) );
314 $s .= Html::openElement( 'tr' ) . "\n";
315 $width = 100 / count( $links ) . '%';
316 foreach ( $labels as $type => $label ) {
317 // We want every cell to have the same width. We could use table-layout: fixed; in CSS,
318 // but it only works if we specify the width of a cell or the table and we don't want to.
319 // There is no better way. <http://www.w3.org/TR/CSS2/tables.html#fixed-table-layout>
320 $s .= Html::rawElement( 'td',
321 array( 'style' => "width: $width;", 'class' => "TablePager_nav-$type" ),
322 $links[$type] ) . "\n";
323 }
324 $s .= Html::closeElement( 'tr' ) . Html::closeElement( 'table' ) . "\n";
325 return $s;
326 }
327
328 /**
329 * ResourceLoader modules that must be loaded to provide correct styling for this pager
330 * @since 1.24
331 * @return string[]
332 */
333 public function getModuleStyles() {
334 return array( 'mediawiki.pager.tablePager' );
335 }
336
337 /**
338 * Get a "<select>" element which has options for each of the allowed limits
339 *
340 * @param string $attribs Extra attributes to set
341 * @return string HTML fragment
342 */
343 public function getLimitSelect( $attribs = array() ) {
344 $select = new XmlSelect( 'limit', false, $this->mLimit );
345 $select->addOptions( $this->getLimitSelectList() );
346 foreach ( $attribs as $name => $value ) {
347 $select->setAttribute( $name, $value );
348 }
349 return $select->getHTML();
350 }
351
352 /**
353 * Get a list of items to show in a "<select>" element of limits.
354 * This can be passed directly to XmlSelect::addOptions().
355 *
356 * @since 1.22
357 * @return array
358 */
359 public function getLimitSelectList() {
360 # Add the current limit from the query string
361 # to avoid that the limit is lost after clicking Go next time
362 if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
363 $this->mLimitsShown[] = $this->mLimit;
364 sort( $this->mLimitsShown );
365 }
366 $ret = array();
367 foreach ( $this->mLimitsShown as $key => $value ) {
368 # The pair is either $index => $limit, in which case the $value
369 # will be numeric, or $limit => $text, in which case the $value
370 # will be a string.
371 if ( is_int( $value ) ) {
372 $limit = $value;
373 $text = $this->getLanguage()->formatNum( $limit );
374 } else {
375 $limit = $key;
376 $text = $value;
377 }
378 $ret[$text] = $limit;
379 }
380 return $ret;
381 }
382
383 /**
384 * Get \<input type="hidden"\> elements for use in a method="get" form.
385 * Resubmits all defined elements of the query string, except for a
386 * blacklist, passed in the $blacklist parameter.
387 *
388 * @param array $blacklist Parameters from the request query which should not be resubmitted
389 * @return string HTML fragment
390 */
391 function getHiddenFields( $blacklist = array() ) {
392 $blacklist = (array)$blacklist;
393 $query = $this->getRequest()->getQueryValues();
394 foreach ( $blacklist as $name ) {
395 unset( $query[$name] );
396 }
397 $s = '';
398 foreach ( $query as $name => $value ) {
399 $s .= Html::hidden( $name, $value ) . "\n";
400 }
401 return $s;
402 }
403
404 /**
405 * Get a form containing a limit selection dropdown
406 *
407 * @return string HTML fragment
408 */
409 function getLimitForm() {
410 global $wgScript;
411
412 return Html::rawElement(
413 'form',
414 array(
415 'method' => 'get',
416 'action' => $wgScript
417 ),
418 "\n" . $this->getLimitDropdown()
419 ) . "\n";
420 }
421
422 /**
423 * Gets a limit selection dropdown
424 *
425 * @return string
426 */
427 function getLimitDropdown() {
428 # Make the select with some explanatory text
429 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
430
431 return $this->msg( 'table_pager_limit' )
432 ->rawParams( $this->getLimitSelect() )->escaped() .
433 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
434 $this->getHiddenFields( array( 'limit' ) );
435 }
436
437 /**
438 * Return true if the named field should be sortable by the UI, false
439 * otherwise
440 *
441 * @param string $field
442 */
443 abstract function isFieldSortable( $field );
444
445 /**
446 * Format a table cell. The return value should be HTML, but use an empty
447 * string not &#160; for empty cells. Do not include the <td> and </td>.
448 *
449 * The current result row is available as $this->mCurrentRow, in case you
450 * need more context.
451 *
452 * @protected
453 *
454 * @param string $name The database field name
455 * @param string $value The value retrieved from the database
456 */
457 abstract function formatValue( $name, $value );
458
459 /**
460 * The database field name used as a default sort order.
461 *
462 * @protected
463 *
464 * @return string
465 */
466 abstract function getDefaultSort();
467
468 /**
469 * An array mapping database field names to a textual description of the
470 * field name, for use in the table header. The description should be plain
471 * text, it will be HTML-escaped later.
472 *
473 * @return array
474 */
475 abstract function getFieldNames();
476 }