r97997 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97996‎ | r97997 | r97998 >
Date:12:30, 24 September 2011
Author:catrope
Status:ok (Comments)
Tags:
Comment:
SpecialMoodBarFeedback: Implement paging, and fix the form target for ugly URLs
Modified paths:
  • /trunk/extensions/MoodBar/SpecialMoodBarFeedback.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/SpecialMoodBarFeedback.php
@@ -12,11 +12,13 @@
1313 public function execute( $par ) {
1414 global $wgOut, $wgRequest;
1515
 16+ $limit = 20;
 17+ $offset = false;
1618 $id = intval( $par );
1719 if ( $id > 0 ) {
1820 $filters = array( 'id' => $id );
1921 } else {
20 - // Determine filters from the query string
 22+ // Determine filters and offset from the query string
2123 $filters = array();
2224 $type = $wgRequest->getArray( 'type' );
2325 if ( $type ) {
@@ -26,14 +28,20 @@
2729 if ( $username !== '' ) {
2830 $filters['username'] = $username;
2931 }
 32+ $offset = $wgRequest->getVal( 'offset', $offset );
3033 }
3134 // Do the query
32 - $res = $this->doQuery( $filters );
 35+ $res = $this->doQuery( $filters, $limit + 1, $offset );
 36+ $rows = iterator_to_array( $res );
 37+ $lastRow = null;
 38+ if ( count( $rows ) == $limit + 1 ) {
 39+ $lastRow = array_pop( $rows );
 40+ }
3341
3442 // Output HTML
3543 $wgOut->setPageTitle( wfMsg( 'moodbar-feedback-title' ) );
3644 $wgOut->addHTML( $this->buildForm() );
37 - $wgOut->addHTML( $this->buildList( $res ) );
 45+ $wgOut->addHTML( $this->buildList( $rows, $lastRow ) );
3846 $wgOut->addModuleStyles( 'ext.moodBar.dashboard.styles' );
3947 }
4048
@@ -48,6 +56,7 @@
4957 $setFiltersMsg = wfMessage( 'moodbar-feedback-filters-button' )->escaped();
5058 $whatIsMsg = wfMessage( 'moodbar-feedback-whatis' )->escaped();
5159 $whatIsURL = htmlspecialchars( $wgMoodBarConfig['infoUrl'] );
 60+ $actionURL = htmlspecialchars( $this->getTitle()->getLinkURL() );
5261
5362 $types = $wgRequest->getArray( 'type', array() );
5463 $happyCheckbox = Xml::check( 'type[]', in_array( 'happy', $types ),
@@ -61,7 +70,7 @@
6271
6372 return <<<HTML
6473 <div id="fbd-filters">
65 - <form>
 74+ <form action="$actionURL">
6675 <h3 id="fbd-filters-title">$filtersMsg</h3>
6776 <fieldset id="fbd-filters-types">
6877 <legend class="fbd-filters-label">$typeMsg</legend>
@@ -89,8 +98,8 @@
9099 HTML;
91100 }
92101
93 - public function buildList( $rows ) {
94 - global $wgLang;
 102+ public function buildList( $rows, $lastRow ) {
 103+ global $wgLang, $wgRequest;
95104 $now = wfTimestamp( TS_UNIX );
96105 $list = '';
97106 foreach ( $rows as $row ) {
@@ -127,14 +136,20 @@
128137 if ( $list === '' ) {
129138 return '<div id="fbd-list">' . wfMessage( 'moodbar-feedback-noresults' )->escaped() . '</div>';
130139 } else {
131 - // Only show paging stuff if the result is not empty
132 - $moreURL = '#'; //TODO
133 - $moreText = wfMessage( 'moodbar-feedback-more' )->escaped();
134 - return "<ul id=\"fbd-list\">$list</ul>" . '<div id="fbd-list-more"><a href="#">More</a></div>';
 140+ // Only show paging stuff if the result is not empty and there are more results
 141+ $html = "<ul id=\"fbd-list\">$list</ul>";
 142+ if ( $lastRow ) {
 143+ $offset = wfTimestamp( TS_MW, $lastRow->mbf_timestamp ) . '|' . intval( $lastRow->mbf_id );
 144+ $moreURL = htmlspecialchars( $this->getTitle()->getLinkURL( $this->getQuery( $offset ) ) );
 145+ $moreText = wfMessage( 'moodbar-feedback-more' )->escaped();
 146+ $html .= "<div id=\"fbd-list-more\"><a href=\"$moreURL\">More</a></div>";
 147+ }
 148+ return $html;
135149 }
136150 }
137151
138 - public function doQuery( $filters ) {
 152+ public function doQuery( $filters, $limit, $offset ) {
 153+ $dbr = wfGetDB( DB_SLAVE );
139154 $conds = array();
140155 if ( isset( $filters['type'] ) ) {
141156 $conds['mbf_type'] = $filters['type'];
@@ -152,16 +167,30 @@
153168 if ( isset( $filters['id'] ) ) {
154169 $conds['mbf_id'] = $filters['id'];
155170 }
 171+ if ( $offset !== false ) {
 172+ $arr = explode( '|', $offset, 2 );
 173+ $ts = $dbr->addQuotes( $dbr->timestamp( $arr[0] ) );
 174+ $id = isset( $arr[1] ) ? intval( $arr[1] ) : 0;
 175+ $conds[] = "mbf_timestamp < $ts OR (mbf_timestamp = $ts AND mbf_id <= $id)";
 176+ }
156177
157 - $dbr = wfGetDB( DB_SLAVE );
158178 return $dbr->select( array( 'moodbar_feedback', 'user' ), array(
159179 'user_name', 'mbf_id', 'mbf_type',
160180 'mbf_timestamp', 'mbf_user_id', 'mbf_user_ip', 'mbf_comment'
161181 ),
162182 $conds,
163183 __METHOD__,
164 - array( 'LIMIT' => 20 /*TODO*/, 'ORDER BY' => 'mbf_timestamp DESC' ),
 184+ array( 'LIMIT' => $limit, 'ORDER BY' => 'mbf_timestamp DESC' ),
165185 array( 'user' => array( 'LEFT JOIN', 'user_id=mbf_user_id' ) )
166186 );
167187 }
 188+
 189+ protected function getQuery( $offset ) {
 190+ global $wgRequest;
 191+ return array(
 192+ 'type' => $wgRequest->getArray( 'type', array() ),
 193+ 'username' => $wgRequest->getVal( 'username' ),
 194+ 'offset' => $offset,
 195+ );
 196+ }
168197 }

Comments

#Comment by Tim Starling (talk | contribs)   05:41, 30 December 2011
+		if ( $offset !== false ) {
+			$arr = explode( '|', $offset, 2 );
+			$ts = $dbr->addQuotes( $dbr->timestamp( $arr[0] ) );
+			$id = isset( $arr[1] ) ? intval( $arr[1] ) : 0;
+			$conds[] = "mbf_timestamp < $ts OR (mbf_timestamp = $ts AND mbf_id <= $id)";
+		}

I think IndexPager or an IndexPager subclass would be a good place to put a lot of this kind of boilerplate. The idea of the Pager hierarchy was to factor out the tedious code that efficient paging requires.

Status & tagging log