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
25 * Table-based display with a user-selectable sort order
28 abstract class TablePager
extends IndexPager
{
31 protected $mCurrentRow;
33 public function __construct( IContextSource
$context = null ) {
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
)
42 $this->mSort
= $this->getDefaultSort();
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 */
50 parent
::__construct();
57 function getStartBody() {
59 $sortClass = $this->getSortHeaderClass();
62 $fields = $this->getFieldNames();
65 foreach ( $fields as $field => $name ) {
66 if ( strval( $name ) == '' ) {
67 $s .= Html
::rawElement( 'th', array(), ' ' ) . "\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
) {
78 $alt = $this->msg( 'descending_abbrev' )->escaped();
84 $alt = $this->msg( 'ascending_abbrev' )->escaped();
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";
92 $s .= Html
::rawElement( 'th', array(),
93 $this->makeLink( htmlspecialchars( $name ), $query ) ) . "\n";
96 $s .= Html
::element( 'th', array(), $name ) . "\n";
100 $tableClass = $this->getTableClass();
101 $ret = Html
::openElement( 'table', array(
102 'style' => 'border:1px;',
103 'class' => "mw-datatable $tableClass" )
105 $ret .= Html
::rawElement( 'thead', array(), Html
::rawElement( 'tr', array(), "\n" . $s . "\n" ) );
106 $ret .= Html
::openElement( 'tbody' ) . "\n";
115 function getEndBody() {
116 return "</tbody></table>\n";
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 ) );
132 * @param stdClass $row
133 * @return string HTML
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();
140 foreach ( $fieldNames as $field => $name ) {
141 $value = isset( $row->$field ) ?
$row->$field : null;
142 $formatted = strval( $this->formatValue( $field, $value ) );
144 if ( $formatted == '' ) {
145 $formatted = ' ';
148 $s .= Html
::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
151 $s .= Html
::closeElement( 'tr' ) . "\n";
157 * Get a class name to be applied to the given row.
161 * @param object $row The database result row
164 function getRowClass( $row ) {
169 * Get attributes to be applied to the given row.
173 * @param object $row The database result row
174 * @return array Array of attribute => value
176 function getRowAttrs( $row ) {
177 $class = $this->getRowClass( $row );
178 if ( $class === '' ) {
179 // Return an empty array to avoid clutter in HTML like class=""
182 return array( 'class' => $this->getRowClass( $row ) );
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
193 * @param string $field The column
194 * @param string $value The cell contents
195 * @return array Array of attr => value
197 function getCellAttrs( $field, $value ) {
198 return array( 'class' => 'TablePager_col_' . $field );
205 function getIndexField() {
213 function getTableClass() {
221 function getNavClass() {
222 return 'TablePager_nav';
229 function getSortHeaderClass() {
230 return 'TablePager_sort';
234 * A navigation bar with images
235 * @return string HTML
237 public function getNavigationBar() {
240 if ( !$this->isNavigationBarShown() ) {
244 $path = "$wgStylePath/common/images";
246 'first' => 'table_pager_first',
247 'prev' => 'table_pager_prev',
248 'next' => 'table_pager_next',
249 'last' => 'table_pager_last',
252 'first' => 'arrow_first_25.png',
253 'prev' => 'arrow_left_25.png',
254 'next' => 'arrow_right_25.png',
255 'last' => 'arrow_last_25.png',
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',
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 ) );
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";
278 $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
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";
286 $s .= Html
::closeElement( 'tr' ) . Html
::closeElement( 'table' ) . "\n";
291 * Get a "<select>" element which has options for each of the allowed limits
293 * @param string $attribs Extra attributes to set
294 * @return string HTML fragment
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 );
302 return $select->getHTML();
306 * Get a list of items to show in a "<select>" element of limits.
307 * This can be passed directly to XmlSelect::addOptions().
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
);
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
324 if ( is_int( $value ) ) {
326 $text = $this->getLanguage()->formatNum( $limit );
331 $ret[$text] = $limit;
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.
341 * @param array $blacklist Parameters from the request query which should not be resubmitted
342 * @return string HTML fragment
344 function getHiddenFields( $blacklist = array() ) {
345 $blacklist = (array)$blacklist;
346 $query = $this->getRequest()->getQueryValues();
347 foreach ( $blacklist as $name ) {
348 unset( $query[$name] );
351 foreach ( $query as $name => $value ) {
352 $s .= Html
::hidden( $name, $value ) . "\n";
358 * Get a form containing a limit selection dropdown
360 * @return string HTML fragment
362 function getLimitForm() {
365 return Html
::rawElement(
369 'action' => $wgScript
371 "\n" . $this->getLimitDropdown()
376 * Gets a limit selection dropdown
380 function getLimitDropdown() {
381 # Make the select with some explanatory text
382 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
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' ) );
391 * Return true if the named field should be sortable by the UI, false
394 * @param string $field
396 abstract function isFieldSortable( $field );
399 * Format a table cell. The return value should be HTML, but use an empty
400 * string not   for empty cells. Do not include the <td> and </td>.
402 * The current result row is available as $this->mCurrentRow, in case you
407 * @param string $name The database field name
408 * @param string $value The value retrieved from the database
410 abstract function formatValue( $name, $value );
413 * The database field name used as a default sort order.
419 abstract function getDefaultSort();
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.
428 abstract function getFieldNames();