Index: trunk/extensions/MoodBar/SpecialMoodBarFeedback.php |
— | — | @@ -11,14 +11,37 @@ |
12 | 12 | |
13 | 13 | public function execute( $par ) { |
14 | 14 | global $wgOut, $wgRequest; |
15 | | - $formatter = MoodBarFeedbackFormatter::newFromRequest( $wgRequest, $par ); |
| 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 |
16 | 39 | $wgOut->setPageTitle( wfMsg( 'moodbar-feedback-title' ) ); |
17 | 40 | $wgOut->addHTML( $this->buildForm() ); |
18 | | - $wgOut->addHTML( $formatter->getHTML() ); |
| 41 | + $wgOut->addHTML( $this->buildList( $res ) ); |
19 | 42 | $wgOut->addModuleStyles( 'ext.moodBar.dashboard.styles' ); |
20 | 43 | } |
21 | 44 | |
22 | | - protected function buildForm() { |
| 45 | + public function buildForm() { |
23 | 46 | global $wgRequest, $wgMoodBarConfig; |
24 | 47 | $filtersMsg = wfMessage( 'moodbar-feedback-filters' )->escaped(); |
25 | 48 | $typeMsg = wfMessage( 'moodbar-feedback-filters-type' )->escaped(); |
— | — | @@ -70,106 +93,9 @@ |
71 | 94 | </div> |
72 | 95 | HTML; |
73 | 96 | } |
74 | | -} |
75 | | - |
76 | | -class MoodBarFeedbackFormatter { |
77 | | - protected $types, $username, $id, $backwards, $offset, $limit; |
78 | 97 | |
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; |
| 98 | + public function buildList( $res ) { |
| 99 | + global $wgLang, $wgRequest; |
174 | 100 | $now = wfTimestamp( TS_UNIX ); |
175 | 101 | $list = ''; |
176 | 102 | foreach ( $res['rows'] as $row ) { |
— | — | @@ -244,14 +170,79 @@ |
245 | 171 | } |
246 | 172 | } |
247 | 173 | |
| 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 | + |
248 | 240 | protected function getQuery( $offset, $backwards ) { |
| 241 | + global $wgRequest; |
249 | 242 | $query = array( |
250 | | - 'type' => $this->types, |
| 243 | + 'type' => $wgRequest->getArray( 'type', array() ), |
| 244 | + 'username' => $wgRequest->getVal( 'username' ), |
251 | 245 | 'offset' => $offset, |
252 | 246 | ); |
253 | | - if ( $this->username !== null ) { |
254 | | - $query['username'] = $this->username; |
255 | | - } |
256 | 247 | if ( $backwards ) { |
257 | 248 | $query['dir'] = 'prev'; |
258 | 249 | } |
Index: trunk/extensions/MoodBar/MoodBar.php |
— | — | @@ -34,7 +34,6 @@ |
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'; |
39 | 38 | $wgSpecialPages['MoodBarFeedback'] = 'SpecialMoodBarFeedback'; |
40 | 39 | |
41 | 40 | // User rights |