Index: branches/liquidthreads/extensions/LqtModel.php |
— | — | @@ -254,58 +254,47 @@ |
255 | 255 | $tables = ""; |
256 | 256 | } |
257 | 257 | |
258 | | - /* Select the client's threads, AND all their children: */ |
| 258 | + /* Select the client's threads, AND all their children. |
| 259 | + The ones the client actually asked for are marked with root_test. */ |
259 | 260 | |
| 261 | + $root_test = str_replace( 'thread.', 'children.', $where ); // TODO fragile? |
| 262 | + |
260 | 263 | $sql = <<< SQL |
261 | | -SELECT children.* FROM $tables thread, thread children |
| 264 | +SELECT children.*, ($root_test) as is_root FROM $tables thread, thread children |
262 | 265 | WHERE $where AND |
263 | 266 | children.thread_path LIKE CONCAT(thread.thread_path, "%") |
264 | 267 | $options |
265 | 268 | SQL; |
266 | 269 | $res = $dbr->query($sql); |
267 | 270 | |
268 | | - /* |
269 | | - God probably kills a kitten whenever this next section of code is run. |
270 | | - We're creating a tree of objects from the flat list of rows. Please someone |
271 | | - think of a way to do this in one pass. |
272 | | - */ |
| 271 | + $lines = array(); |
| 272 | + $threads = array(); |
273 | 273 | |
274 | | - $tree = array(); |
275 | 274 | while ( $line = $dbr->fetchObject($res) ) { |
276 | | - $path = explode('.', $line->thread_path); |
277 | | - Threads::setDeepArray( $tree, $line, $path ); |
| 275 | + $lines[] = $line; |
278 | 276 | } |
279 | | - var_dump($tree); |
280 | 277 | |
281 | | - $threads = array(); |
282 | | - foreach( $tree as $root ) { |
283 | | - $threads[] = Threads::createThreads($root); |
| 278 | + foreach( $lines as $l ) { |
| 279 | + if( $l->is_root ) { |
| 280 | + $threads[] = Threads::buildLiveThread( &$lines, $l ); |
| 281 | + } |
284 | 282 | } |
285 | 283 | |
286 | 284 | return $threads; |
287 | 285 | } |
288 | 286 | |
289 | | - private static function createThreads( $thread ) { |
290 | | - $subthreads = array(); |
291 | | - foreach( $thread as $key => $val ) { |
292 | | - if ( $key != 'root' ) { |
293 | | - $subthreads[] = Threads::createThreads( $val ); |
| 287 | + private static function buildLiveThread( $lines, $l ) { |
| 288 | + $children = array(); |
| 289 | + foreach( $lines as $m ) { |
| 290 | + if ( $m->thread_path != $l->thread_path && |
| 291 | + strpos( $m->thread_path, $l->thread_path ) === 0 ) { |
| 292 | + // $m->path begins with $l->path; this is a child. |
| 293 | + $children[] = Threads::buildLiveThread( &$lines, $m ); |
294 | 294 | } |
295 | 295 | } |
296 | | - return new LiveThread( $thread['root'], $subthreads ); |
| 296 | + return new LiveThread($l, $children); |
297 | 297 | } |
298 | 298 | |
299 | | - /** setDeepArray( $a, $v, array(1,2,3) ) <=> $a[1][2][3]['root'] = $v; */ |
300 | | - private static function setDeepArray( &$a, $v, $p ) { |
301 | | - if( count($p) == 1 ) { |
302 | | - $a[$p[0]]["root"] = $v; |
303 | | - } else { |
304 | | - if( !array_key_exists( $p[0], $a ) ) |
305 | | - $a[$p[0]] = array(); |
306 | | - Threads::setDeepArray( $a[$p[0]], $v, array_slice($p, 1) ); |
307 | | - } |
308 | | - } |
309 | | - |
310 | 299 | static function withRoot( $post ) { |
311 | 300 | return Threads::where( array('thread.thread_root' => $post->getID()) ); |
312 | 301 | } |