Index: trunk/extensions/MoodBar/SpecialMoodBarFeedback.php |
— | — | @@ -11,37 +11,14 @@ |
12 | 12 | |
13 | 13 | public function execute( $par ) { |
14 | 14 | global $wgOut, $wgRequest; |
15 | | - |
16 | | - $limit = 20; |
17 | | - $offset = false; |
18 | | - $id = intval( $par ); |
19 | | - if ( $id > 0 ) { |
20 | | - $filters = array( 'id' => $id ); |
21 | | - } else { |
22 | | - // Determine filters and offset from the query string |
23 | | - $filters = array(); |
24 | | - $type = $wgRequest->getArray( 'type' ); |
25 | | - if ( $type ) { |
26 | | - $filters['type'] = $type; |
27 | | - } |
28 | | - $username = strval( $wgRequest->getVal( 'username' ) ); |
29 | | - if ( $username !== '' ) { |
30 | | - $filters['username'] = $username; |
31 | | - } |
32 | | - $offset = $wgRequest->getVal( 'offset', $offset ); |
33 | | - } |
34 | | - // Do the query |
35 | | - $backwards = $wgRequest->getVal( 'dir' ) === 'prev'; |
36 | | - $res = $this->doQuery( $filters, $limit, $offset, $backwards ); |
37 | | - |
38 | | - // Output HTML |
| 15 | + $formatter = MoodBarFeedbackFormatter::newFromRequest( $wgRequest, $par ); |
39 | 16 | $wgOut->setPageTitle( wfMsg( 'moodbar-feedback-title' ) ); |
40 | 17 | $wgOut->addHTML( $this->buildForm() ); |
41 | | - $wgOut->addHTML( $this->buildList( $res ) ); |
| 18 | + $wgOut->addHTML( $formatter->getHTML() ); |
42 | 19 | $wgOut->addModuleStyles( 'ext.moodBar.dashboard.styles' ); |
43 | 20 | } |
44 | 21 | |
45 | | - public function buildForm() { |
| 22 | + protected function buildForm() { |
46 | 23 | global $wgRequest, $wgMoodBarConfig; |
47 | 24 | $filtersMsg = wfMessage( 'moodbar-feedback-filters' )->escaped(); |
48 | 25 | $typeMsg = wfMessage( 'moodbar-feedback-filters-type' )->escaped(); |
— | — | @@ -93,9 +70,106 @@ |
94 | 71 | </div> |
95 | 72 | HTML; |
96 | 73 | } |
| 74 | +} |
| 75 | + |
| 76 | +class MoodBarFeedbackFormatter { |
| 77 | + protected $types, $username, $id, $backwards, $offset, $limit; |
97 | 78 | |
98 | | - public function buildList( $res ) { |
99 | | - global $wgLang, $wgRequest; |
| 79 | + public function __construct( $types, $username, $id, $backwards, $offset ) { |
| 80 | + $this->types = (array)$types; |
| 81 | + $this->username = strval( $username ) !== '' ? strval( $username ) : null; |
| 82 | + $this->id = intval( $id ); |
| 83 | + $this->backwards = (bool)$backwards; |
| 84 | + $this->offset = $offset; |
| 85 | + $this->limit = 20; // Hardcoded for now |
| 86 | + } |
| 87 | + |
| 88 | + public static function newFromRequest( WebRequest $request, $par = null ) { |
| 89 | + return new self( |
| 90 | + $request->getArray( 'type', array() ), |
| 91 | + $request->getVal( 'username', null ), |
| 92 | + intval( $par ), |
| 93 | + $request->getVal( 'dir' ) == 'prev', |
| 94 | + $request->getVal( 'offset', null ) |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + public function getHTML() { |
| 99 | + return $this->buildList( $this->doQuery() ); |
| 100 | + } |
| 101 | + |
| 102 | + protected function getTitle( $par = null ) { |
| 103 | + return SpecialPage::getTitleFor( 'MoodBarFeedback', $par ); |
| 104 | + } |
| 105 | + |
| 106 | + protected function doQuery() { |
| 107 | + $dbr = wfGetDB( DB_SLAVE ); |
| 108 | + $conds = array(); |
| 109 | + if ( $this->types ) { |
| 110 | + $conds['mbf_type'] = $this->types; |
| 111 | + } |
| 112 | + if ( $this->username !== null ) { |
| 113 | + $user = User::newFromName( $this->username ); // Returns false for IPs |
| 114 | + if ( !$user || $user->isAnon() ) { |
| 115 | + $conds['mbf_user_id'] = 0; |
| 116 | + $conds['mbf_user_ip'] = $this->username; |
| 117 | + } else { |
| 118 | + $conds['mbf_user_id'] = $user->getID(); |
| 119 | + $conds[] = 'mbf_user_ip IS NULL'; |
| 120 | + } |
| 121 | + } |
| 122 | + if ( $this->id > 0 ) { |
| 123 | + $conds['mbf_id'] = $this->id; |
| 124 | + } |
| 125 | + if ( $this->offset !== null ) { |
| 126 | + $arr = explode( '|', $this->offset, 2 ); |
| 127 | + $ts = $dbr->addQuotes( $dbr->timestamp( $arr[0] ) ); |
| 128 | + $id = isset( $arr[1] ) ? intval( $arr[1] ) : 0; |
| 129 | + $op = $this->backwards ? '>' : '<'; |
| 130 | + $conds[] = "mbf_timestamp $op $ts OR (mbf_timestamp = $ts AND mbf_id $op= $id)"; |
| 131 | + } |
| 132 | + |
| 133 | + $desc = $this->backwards ? '' : ' DESC'; |
| 134 | + $res = $dbr->select( array( 'moodbar_feedback', 'user' ), array( |
| 135 | + 'user_name', 'mbf_id', 'mbf_type', |
| 136 | + 'mbf_timestamp', 'mbf_user_id', 'mbf_user_ip', 'mbf_comment' |
| 137 | + ), |
| 138 | + $conds, |
| 139 | + __METHOD__, |
| 140 | + array( 'LIMIT' => $this->limit + 2, 'ORDER BY' => "mbf_timestamp$desc, mbf_id$desc" ), |
| 141 | + array( 'user' => array( 'LEFT JOIN', 'user_id=mbf_user_id' ) ) |
| 142 | + ); |
| 143 | + $rows = iterator_to_array( $res, /*$use_keys=*/false ); |
| 144 | + |
| 145 | + // Figure out whether there are newer and older rows |
| 146 | + $olderRow = $newerRow = null; |
| 147 | + $count = count( $rows ); |
| 148 | + if ( $this->offset && $count > 0 ) { |
| 149 | + // If there is an offset, drop the first row |
| 150 | + if ( $count > 1 ) { |
| 151 | + array_shift( $rows ); |
| 152 | + $count--; |
| 153 | + } |
| 154 | + // We now know there is a previous row |
| 155 | + $newerRow = $rows[0]; |
| 156 | + } |
| 157 | + if ( $count > $this->limit ) { |
| 158 | + // If there are rows past the limit, drop them |
| 159 | + array_splice( $rows, $this->limit ); |
| 160 | + // We now know there is a next row |
| 161 | + $olderRow = $rows[$this->limit - 1]; |
| 162 | + } |
| 163 | + |
| 164 | + // If we got everything backwards, reverse it |
| 165 | + if ( $this->backwards ) { |
| 166 | + $rows = array_reverse( $rows ); |
| 167 | + list( $olderRow, $newerRow ) = array( $newerRow, $olderRow ); |
| 168 | + } |
| 169 | + return array( 'rows' => $rows, 'olderRow' => $olderRow, 'newerRow' => $newerRow ); |
| 170 | + } |
| 171 | + |
| 172 | + protected function buildList( $res ) { |
| 173 | + global $wgLang; |
100 | 174 | $now = wfTimestamp( TS_UNIX ); |
101 | 175 | $list = ''; |
102 | 176 | foreach ( $res['rows'] as $row ) { |
— | — | @@ -170,79 +244,14 @@ |
171 | 245 | } |
172 | 246 | } |
173 | 247 | |
174 | | - public function doQuery( $filters, $limit, $offset, $backwards ) { |
175 | | - $dbr = wfGetDB( DB_SLAVE ); |
176 | | - $conds = array(); |
177 | | - if ( isset( $filters['type'] ) ) { |
178 | | - $conds['mbf_type'] = $filters['type']; |
179 | | - } |
180 | | - if ( isset( $filters['username'] ) ) { |
181 | | - $user = User::newFromName( $filters['username'] ); // Returns false for IPs |
182 | | - if ( !$user || $user->isAnon() ) { |
183 | | - $conds['mbf_user_id'] = 0; |
184 | | - $conds['mbf_user_ip'] = $filters['username']; |
185 | | - } else { |
186 | | - $conds['mbf_user_id'] = $user->getID(); |
187 | | - $conds[] = 'mbf_user_ip IS NULL'; |
188 | | - } |
189 | | - } |
190 | | - if ( isset( $filters['id'] ) ) { |
191 | | - $conds['mbf_id'] = $filters['id']; |
192 | | - } |
193 | | - if ( $offset !== false ) { |
194 | | - $arr = explode( '|', $offset, 2 ); |
195 | | - $ts = $dbr->addQuotes( $dbr->timestamp( $arr[0] ) ); |
196 | | - $id = isset( $arr[1] ) ? intval( $arr[1] ) : 0; |
197 | | - $op = $backwards ? '>' : '<'; |
198 | | - $conds[] = "mbf_timestamp $op $ts OR (mbf_timestamp = $ts AND mbf_id $op= $id)"; |
199 | | - } |
200 | | - |
201 | | - $desc = $backwards ? '' : ' DESC'; |
202 | | - $res = $dbr->select( array( 'moodbar_feedback', 'user' ), array( |
203 | | - 'user_name', 'mbf_id', 'mbf_type', |
204 | | - 'mbf_timestamp', 'mbf_user_id', 'mbf_user_ip', 'mbf_comment' |
205 | | - ), |
206 | | - $conds, |
207 | | - __METHOD__, |
208 | | - array( 'LIMIT' => $limit + 2, 'ORDER BY' => "mbf_timestamp$desc, mbf_id$desc" ), |
209 | | - array( 'user' => array( 'LEFT JOIN', 'user_id=mbf_user_id' ) ) |
210 | | - ); |
211 | | - $rows = iterator_to_array( $res, /*$use_keys=*/false ); |
212 | | - |
213 | | - // Figure out whether there are newer and older rows |
214 | | - $olderRow = $newerRow = null; |
215 | | - $count = count( $rows ); |
216 | | - if ( $offset && $count > 0 ) { |
217 | | - // If there is an offset, drop the first row |
218 | | - if ( $count > 1 ) { |
219 | | - array_shift( $rows ); |
220 | | - $count--; |
221 | | - } |
222 | | - // We now know there is a previous row |
223 | | - $newerRow = $rows[0]; |
224 | | - } |
225 | | - if ( $count > $limit ) { |
226 | | - // If there are rows past the limit, drop them |
227 | | - array_splice( $rows, $limit ); |
228 | | - // We now know there is a next row |
229 | | - $olderRow = $rows[$limit - 1]; |
230 | | - } |
231 | | - |
232 | | - // If we got everything backwards, reverse it |
233 | | - if ( $backwards ) { |
234 | | - $rows = array_reverse( $rows ); |
235 | | - list( $olderRow, $newerRow ) = array( $newerRow, $olderRow ); |
236 | | - } |
237 | | - return array( 'rows' => $rows, 'olderRow' => $olderRow, 'newerRow' => $newerRow ); |
238 | | - } |
239 | | - |
240 | 248 | protected function getQuery( $offset, $backwards ) { |
241 | | - global $wgRequest; |
242 | 249 | $query = array( |
243 | | - 'type' => $wgRequest->getArray( 'type', array() ), |
244 | | - 'username' => $wgRequest->getVal( 'username' ), |
| 250 | + 'type' => $this->types, |
245 | 251 | 'offset' => $offset, |
246 | 252 | ); |
| 253 | + if ( $this->username !== null ) { |
| 254 | + $query['username'] = $this->username; |
| 255 | + } |
247 | 256 | if ( $backwards ) { |
248 | 257 | $query['dir'] = 'prev'; |
249 | 258 | } |
Index: trunk/extensions/MoodBar/MoodBar.php |
— | — | @@ -34,6 +34,7 @@ |
35 | 35 | $wgAutoloadClasses['SpecialMoodBar'] = dirname(__FILE__).'/SpecialMoodBar.php'; |
36 | 36 | $wgSpecialPages['MoodBar'] = 'SpecialMoodBar'; |
37 | 37 | $wgAutoloadClasses['SpecialMoodBarFeedback'] = dirname( __FILE__ ) . '/SpecialMoodBarFeedback.php'; |
| 38 | +$wgAutoloadClasses['MoodBarFeedbackFormatter'] = dirname( __FILE__ ) . '/SpecialMoodBarFeedback.php'; |
38 | 39 | $wgSpecialPages['MoodBarFeedback'] = 'SpecialMoodBarFeedback'; |
39 | 40 | |
40 | 41 | // User rights |