Index: trunk/extensions/MoodBar/SpecialMoodBarFeedback.php |
— | — | @@ -31,17 +31,13 @@ |
32 | 32 | $offset = $wgRequest->getVal( 'offset', $offset ); |
33 | 33 | } |
34 | 34 | // Do the query |
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 | | - } |
41 | | - |
| 35 | + $backwards = $wgRequest->getVal( 'dir' ) === 'prev'; |
| 36 | + $res = $this->doQuery( $filters, $limit, $offset, $backwards ); |
| 37 | + |
42 | 38 | // Output HTML |
43 | 39 | $wgOut->setPageTitle( wfMsg( 'moodbar-feedback-title' ) ); |
44 | 40 | $wgOut->addHTML( $this->buildForm() ); |
45 | | - $wgOut->addHTML( $this->buildList( $rows, $lastRow ) ); |
| 41 | + $wgOut->addHTML( $this->buildList( $res ) ); |
46 | 42 | $wgOut->addModuleStyles( 'ext.moodBar.dashboard.styles' ); |
47 | 43 | } |
48 | 44 | |
— | — | @@ -98,12 +94,11 @@ |
99 | 95 | HTML; |
100 | 96 | } |
101 | 97 | |
102 | | - public function buildList( $rows, $lastRow ) { |
| 98 | + public function buildList( $res ) { |
103 | 99 | global $wgLang, $wgRequest; |
104 | 100 | $now = wfTimestamp( TS_UNIX ); |
105 | 101 | $list = ''; |
106 | | - $firstRow = false; // TODO support the "Newer" link with this |
107 | | - foreach ( $rows as $row ) { |
| 102 | + foreach ( $res['rows'] as $row ) { |
108 | 103 | $type = $row->mbf_type; |
109 | 104 | $typeMsg = wfMessage( "moodbar-type-$type" )->escaped(); |
110 | 105 | $time = $wgLang->formatTimePeriod( $now - wfTimestamp( TS_UNIX, $row->mbf_timestamp ), |
— | — | @@ -134,23 +129,28 @@ |
135 | 130 | </li> |
136 | 131 | HTML; |
137 | 132 | } |
| 133 | + |
138 | 134 | if ( $list === '' ) { |
139 | 135 | return '<div id="fbd-list">' . wfMessage( 'moodbar-feedback-noresults' )->escaped() . '</div>'; |
140 | 136 | } else { |
141 | 137 | // Only show paging stuff if the result is not empty and there are more results |
| 138 | + $olderRow = $res['olderRow']; |
| 139 | + $newerRow = $res['newerRow']; |
142 | 140 | $html = "<ul id=\"fbd-list\">$list</ul>"; |
143 | | - if ( $lastRow ) { |
| 141 | + if ( $olderRow ) { |
144 | 142 | $moreText = wfMessage( 'moodbar-feedback-more' )->escaped(); |
145 | 143 | $html .= '<div id="fbd-list-more"><a href="#">' . $moreText . '</a></div>'; |
146 | 144 | } |
147 | 145 | |
148 | 146 | $olderURL = $newerURL = false; |
149 | | - if ( $lastRow ) { |
150 | | - $offset = wfTimestamp( TS_MW, $lastRow->mbf_timestamp ) . '|' . intval( $lastRow->mbf_id ); |
151 | | - $olderURL = htmlspecialchars( $this->getTitle()->getLinkURL( $this->getQuery( $offset ) ) ); |
| 147 | + if ( $olderRow ) { |
| 148 | + $olderOffset = wfTimestamp( TS_MW, $olderRow->mbf_timestamp ) . '|' . intval( $olderRow->mbf_id ); |
| 149 | + $olderURL = htmlspecialchars( $this->getTitle()->getLinkURL( $this->getQuery( $olderOffset, false ) ) ); |
152 | 150 | } |
153 | | - if ( $firstRow ) { |
154 | | - $newerURL = htmlspecialchars( '#' ); // TODO |
| 151 | + if ( $newerRow ) { |
| 152 | + // TODO: Figure out when there are no newer rows |
| 153 | + $newerOffset = wfTimestamp( TS_MW, $newerRow->mbf_timestamp ) . '|' . intval( $newerRow->mbf_id ); |
| 154 | + $newerURL = htmlspecialchars( $this->getTitle()->getLinkURL( $this->getQuery( $newerOffset, true ) ) ); |
155 | 155 | } |
156 | 156 | $olderText = wfMessage( 'moodbar-feedback-older' )->escaped(); |
157 | 157 | $newerText = wfMessage( 'moodbar-feedback-newer' )->escaped(); |
— | — | @@ -171,7 +171,7 @@ |
172 | 172 | } |
173 | 173 | } |
174 | 174 | |
175 | | - public function doQuery( $filters, $limit, $offset ) { |
| 175 | + public function doQuery( $filters, $limit, $offset, $backwards ) { |
176 | 176 | $dbr = wfGetDB( DB_SLAVE ); |
177 | 177 | $conds = array(); |
178 | 178 | if ( isset( $filters['type'] ) ) { |
— | — | @@ -194,26 +194,59 @@ |
195 | 195 | $arr = explode( '|', $offset, 2 ); |
196 | 196 | $ts = $dbr->addQuotes( $dbr->timestamp( $arr[0] ) ); |
197 | 197 | $id = isset( $arr[1] ) ? intval( $arr[1] ) : 0; |
198 | | - $conds[] = "mbf_timestamp < $ts OR (mbf_timestamp = $ts AND mbf_id <= $id)"; |
| 198 | + $op = $backwards ? '>' : '<'; |
| 199 | + $conds[] = "mbf_timestamp $op $ts OR (mbf_timestamp = $ts AND mbf_id $op= $id)"; |
199 | 200 | } |
200 | 201 | |
201 | | - return $dbr->select( array( 'moodbar_feedback', 'user' ), array( |
| 202 | + $desc = $backwards ? '' : ' DESC'; |
| 203 | + $res = $dbr->select( array( 'moodbar_feedback', 'user' ), array( |
202 | 204 | 'user_name', 'mbf_id', 'mbf_type', |
203 | 205 | 'mbf_timestamp', 'mbf_user_id', 'mbf_user_ip', 'mbf_comment' |
204 | 206 | ), |
205 | 207 | $conds, |
206 | 208 | __METHOD__, |
207 | | - array( 'LIMIT' => $limit, 'ORDER BY' => 'mbf_timestamp DESC, mbf_id DESC' ), |
| 209 | + array( 'LIMIT' => $limit + 2, 'ORDER BY' => "mbf_timestamp$desc, mbf_id$desc" ), |
208 | 210 | array( 'user' => array( 'LEFT JOIN', 'user_id=mbf_user_id' ) ) |
209 | 211 | ); |
| 212 | + $rows = iterator_to_array( $res, /*$use_keys=*/false ); |
| 213 | + |
| 214 | + // Figure out whether there are newer and older rows |
| 215 | + $olderRow = $newerRow = null; |
| 216 | + $count = count( $rows ); |
| 217 | + if ( $offset && $count > 0 ) { |
| 218 | + // If there is an offset, drop the first row |
| 219 | + if ( $count > 1 ) { |
| 220 | + array_shift( $rows ); |
| 221 | + $count--; |
| 222 | + } |
| 223 | + // We now know there is a previous row |
| 224 | + $newerRow = $rows[0]; |
| 225 | + } |
| 226 | + if ( $count > $limit ) { |
| 227 | + // If there are rows past the limit, drop them |
| 228 | + array_splice( $rows, $limit ); |
| 229 | + // We now know there is a next row |
| 230 | + $olderRow = $rows[$limit - 1]; |
| 231 | + } |
| 232 | + |
| 233 | + // If we got everything backwards, reverse it |
| 234 | + if ( $backwards ) { |
| 235 | + $rows = array_reverse( $rows ); |
| 236 | + list( $olderRow, $newerRow ) = array( $newerRow, $olderRow ); |
| 237 | + } |
| 238 | + return array( 'rows' => $rows, 'olderRow' => $olderRow, 'newerRow' => $newerRow ); |
210 | 239 | } |
211 | 240 | |
212 | | - protected function getQuery( $offset ) { |
| 241 | + protected function getQuery( $offset, $backwards ) { |
213 | 242 | global $wgRequest; |
214 | | - return array( |
| 243 | + $query = array( |
215 | 244 | 'type' => $wgRequest->getArray( 'type', array() ), |
216 | 245 | 'username' => $wgRequest->getVal( 'username' ), |
217 | 246 | 'offset' => $offset, |
218 | 247 | ); |
| 248 | + if ( $backwards ) { |
| 249 | + $query['dir'] = 'prev'; |
| 250 | + } |
| 251 | + return $query; |
219 | 252 | } |
220 | 253 | } |