wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / pager / TablePager.php
blobea619f1a72f3e20368c0118a3cc1c48b48129921
1 <?php
2 /**
3 * Efficient paging for SQL queries.
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.
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.
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
20 * @file
21 * @ingroup Pager
24 /**
25 * Table-based display with a user-selectable sort order
26 * @ingroup Pager
28 abstract class TablePager extends IndexPager {
29 protected $mSort;
31 protected $mCurrentRow;
33 public function __construct( IContextSource $context = null ) {
34 if ( $context ) {
35 $this->setContext( $context );
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();
44 if ( $this->getRequest()->getBool( 'asc' ) ) {
45 $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
46 } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
47 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
48 } /* Else leave it at whatever the class default is */
50 parent::__construct();
53 /**
54 * Get the formatted result list. Calls getStartBody(), formatRow() and getEndBody(), concatenates
55 * the results and returns them.
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).
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.
64 * @deprecated since 1.24, use getBodyOutput() or getFullOutput() instead
65 * @return string
67 final public function getBody() {
68 $this->getOutput()->addModuleStyles( $this->getModuleStyles() );
69 return parent::getBody();
72 /**
73 * Get the formatted result list.
75 * Calls getBody() and getModuleStyles() and builds a ParserOutput object. (This is a bit hacky
76 * but works well.)
78 * @since 1.24
79 * @return ParserOutput
81 public function getBodyOutput() {
82 $body = parent::getBody();
84 $pout = new ParserOutput;
85 $pout->setText( $body );
86 $pout->addModuleStyles( $this->getModuleStyles() );
87 return $pout;
90 /**
91 * Get the formatted result list, with navigation bars.
93 * Calls getBody(), getNavigationBar() and getModuleStyles() and
94 * builds a ParserOutput object. (This is a bit hacky but works well.)
96 * @since 1.24
97 * @return ParserOutput
99 public function getFullOutput() {
100 $navigation = $this->getNavigationBar();
101 $body = parent::getBody();
103 $pout = new ParserOutput;
104 $pout->setText( $navigation . $body . $navigation );
105 $pout->addModuleStyles( $this->getModuleStyles() );
106 return $pout;
110 * @protected
111 * @return string
113 function getStartBody() {
114 $sortClass = $this->getSortHeaderClass();
116 $s = '';
117 $fields = $this->getFieldNames();
119 // Make table header
120 foreach ( $fields as $field => $name ) {
121 if ( strval( $name ) == '' ) {
122 $s .= Html::rawElement( 'th', array(), '&#160;' ) . "\n";
123 } elseif ( $this->isFieldSortable( $field ) ) {
124 $query = array( 'sort' => $field, 'limit' => $this->mLimit );
125 $linkType = null;
126 $class = null;
128 if ( $this->mSort == $field ) {
129 // The table is sorted by this field already, make a link to sort in the other direction
130 // We don't actually know in which direction other fields will be sorted by default…
131 if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
132 $linkType = 'asc';
133 $class = "$sortClass TablePager_sort-descending";
134 $query['asc'] = '1';
135 $query['desc'] = '';
136 } else {
137 $linkType = 'desc';
138 $class = "$sortClass TablePager_sort-ascending";
139 $query['asc'] = '';
140 $query['desc'] = '1';
144 $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
145 $s .= Html::rawElement( 'th', array( 'class' => $class ), $link ) . "\n";
146 } else {
147 $s .= Html::element( 'th', array(), $name ) . "\n";
151 $tableClass = $this->getTableClass();
152 $ret = Html::openElement( 'table', array(
153 'class' => "mw-datatable $tableClass" )
155 $ret .= Html::rawElement( 'thead', array(), Html::rawElement( 'tr', array(), "\n" . $s . "\n" ) );
156 $ret .= Html::openElement( 'tbody' ) . "\n";
158 return $ret;
162 * @protected
163 * @return string
165 function getEndBody() {
166 return "</tbody></table>\n";
170 * @protected
171 * @return string
173 function getEmptyBody() {
174 $colspan = count( $this->getFieldNames() );
175 $msgEmpty = $this->msg( 'table_pager_empty' )->text();
176 return Html::rawElement( 'tr', array(),
177 Html::element( 'td', array( 'colspan' => $colspan ), $msgEmpty ) );
181 * @protected
182 * @param stdClass $row
183 * @return string HTML
185 function formatRow( $row ) {
186 $this->mCurrentRow = $row; // In case formatValue etc need to know
187 $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
188 $fieldNames = $this->getFieldNames();
190 foreach ( $fieldNames as $field => $name ) {
191 $value = isset( $row->$field ) ? $row->$field : null;
192 $formatted = strval( $this->formatValue( $field, $value ) );
194 if ( $formatted == '' ) {
195 $formatted = '&#160;';
198 $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
201 $s .= Html::closeElement( 'tr' ) . "\n";
203 return $s;
207 * Get a class name to be applied to the given row.
209 * @protected
211 * @param object $row The database result row
212 * @return string
214 function getRowClass( $row ) {
215 return '';
219 * Get attributes to be applied to the given row.
221 * @protected
223 * @param object $row The database result row
224 * @return array Array of attribute => value
226 function getRowAttrs( $row ) {
227 $class = $this->getRowClass( $row );
228 if ( $class === '' ) {
229 // Return an empty array to avoid clutter in HTML like class=""
230 return array();
231 } else {
232 return array( 'class' => $this->getRowClass( $row ) );
237 * @return stdClass
239 protected function getCurrentRow() {
240 return $this->mCurrentRow;
244 * Get any extra attributes to be applied to the given cell. Don't
245 * take this as an excuse to hardcode styles; use classes and
246 * CSS instead. Row context is available in $this->mCurrentRow
248 * @protected
250 * @param string $field The column
251 * @param string $value The cell contents
252 * @return array Array of attr => value
254 function getCellAttrs( $field, $value ) {
255 return array( 'class' => 'TablePager_col_' . $field );
259 * @protected
260 * @return string
262 function getIndexField() {
263 return $this->mSort;
267 * @protected
268 * @return string
270 function getTableClass() {
271 return 'TablePager';
275 * @protected
276 * @return string
278 function getNavClass() {
279 return 'TablePager_nav';
283 * @protected
284 * @return string
286 function getSortHeaderClass() {
287 return 'TablePager_sort';
291 * A navigation bar with images
292 * @return string HTML
294 public function getNavigationBar() {
295 if ( !$this->isNavigationBarShown() ) {
296 return '';
299 $labels = array(
300 'first' => 'table_pager_first',
301 'prev' => 'table_pager_prev',
302 'next' => 'table_pager_next',
303 'last' => 'table_pager_last',
306 $linkTexts = array();
307 $disabledTexts = array();
308 foreach ( $labels as $type => $label ) {
309 $msgLabel = $this->msg( $label )->escaped();
310 $linkTexts[$type] = "<div class='TablePager_nav-enabled'>$msgLabel</div>";
311 $disabledTexts[$type] = "<div class='TablePager_nav-disabled'>$msgLabel</div>";
313 $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
315 $s = Html::openElement( 'table', array( 'class' => $this->getNavClass() ) );
316 $s .= Html::openElement( 'tr' ) . "\n";
317 $width = 100 / count( $links ) . '%';
318 foreach ( $labels as $type => $label ) {
319 // We want every cell to have the same width. We could use table-layout: fixed; in CSS,
320 // but it only works if we specify the width of a cell or the table and we don't want to.
321 // There is no better way. <http://www.w3.org/TR/CSS2/tables.html#fixed-table-layout>
322 $s .= Html::rawElement( 'td',
323 array( 'style' => "width: $width;", 'class' => "TablePager_nav-$type" ),
324 $links[$type] ) . "\n";
326 $s .= Html::closeElement( 'tr' ) . Html::closeElement( 'table' ) . "\n";
327 return $s;
331 * ResourceLoader modules that must be loaded to provide correct styling for this pager
332 * @since 1.24
333 * @return string[]
335 public function getModuleStyles() {
336 return array( 'mediawiki.pager.tablePager' );
340 * Get a "<select>" element which has options for each of the allowed limits
342 * @param string $attribs Extra attributes to set
343 * @return string HTML fragment
345 public function getLimitSelect( $attribs = array() ) {
346 $select = new XmlSelect( 'limit', false, $this->mLimit );
347 $select->addOptions( $this->getLimitSelectList() );
348 foreach ( $attribs as $name => $value ) {
349 $select->setAttribute( $name, $value );
351 return $select->getHTML();
355 * Get a list of items to show in a "<select>" element of limits.
356 * This can be passed directly to XmlSelect::addOptions().
358 * @since 1.22
359 * @return array
361 public function getLimitSelectList() {
362 # Add the current limit from the query string
363 # to avoid that the limit is lost after clicking Go next time
364 if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
365 $this->mLimitsShown[] = $this->mLimit;
366 sort( $this->mLimitsShown );
368 $ret = array();
369 foreach ( $this->mLimitsShown as $key => $value ) {
370 # The pair is either $index => $limit, in which case the $value
371 # will be numeric, or $limit => $text, in which case the $value
372 # will be a string.
373 if ( is_int( $value ) ) {
374 $limit = $value;
375 $text = $this->getLanguage()->formatNum( $limit );
376 } else {
377 $limit = $key;
378 $text = $value;
380 $ret[$text] = $limit;
382 return $ret;
386 * Get \<input type="hidden"\> elements for use in a method="get" form.
387 * Resubmits all defined elements of the query string, except for a
388 * blacklist, passed in the $blacklist parameter.
390 * @param array $blacklist Parameters from the request query which should not be resubmitted
391 * @return string HTML fragment
393 function getHiddenFields( $blacklist = array() ) {
394 $blacklist = (array)$blacklist;
395 $query = $this->getRequest()->getQueryValues();
396 foreach ( $blacklist as $name ) {
397 unset( $query[$name] );
399 $s = '';
400 foreach ( $query as $name => $value ) {
401 $s .= Html::hidden( $name, $value ) . "\n";
403 return $s;
407 * Get a form containing a limit selection dropdown
409 * @return string HTML fragment
411 function getLimitForm() {
412 return Html::rawElement(
413 'form',
414 array(
415 'method' => 'get',
416 'action' => wfScript(),
418 "\n" . $this->getLimitDropdown()
419 ) . "\n";
423 * Gets a limit selection dropdown
425 * @return string
427 function getLimitDropdown() {
428 # Make the select with some explanatory text
429 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
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' ) );
438 * Return true if the named field should be sortable by the UI, false
439 * otherwise
441 * @param string $field
443 abstract function isFieldSortable( $field );
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>.
449 * The current result row is available as $this->mCurrentRow, in case you
450 * need more context.
452 * @protected
454 * @param string $name The database field name
455 * @param string $value The value retrieved from the database
457 abstract function formatValue( $name, $value );
460 * The database field name used as a default sort order.
462 * @protected
464 * @return string
466 abstract function getDefaultSort();
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.
473 * @return array
475 abstract function getFieldNames();