r23092 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23091‎ | r23092 | r23093 >
Date:07:58, 19 June 2007
Author:david
Status:old
Tags:
Comment:
Saner tree-builder. Yay.
Modified paths:
  • /branches/liquidthreads/extensions/LqtModel.php (modified) (history)

Diff [purge]

Index: branches/liquidthreads/extensions/LqtModel.php
@@ -254,58 +254,47 @@
255255 $tables = "";
256256 }
257257
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. */
259260
 261+ $root_test = str_replace( 'thread.', 'children.', $where ); // TODO fragile?
 262+
260263 $sql = <<< SQL
261 -SELECT children.* FROM $tables thread, thread children
 264+SELECT children.*, ($root_test) as is_root FROM $tables thread, thread children
262265 WHERE $where AND
263266 children.thread_path LIKE CONCAT(thread.thread_path, "%")
264267 $options
265268 SQL;
266269 $res = $dbr->query($sql);
267270
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();
273273
274 - $tree = array();
275274 while ( $line = $dbr->fetchObject($res) ) {
276 - $path = explode('.', $line->thread_path);
277 - Threads::setDeepArray( $tree, $line, $path );
 275+ $lines[] = $line;
278276 }
279 - var_dump($tree);
280277
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+ }
284282 }
285283
286284 return $threads;
287285 }
288286
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 );
294294 }
295295 }
296 - return new LiveThread( $thread['root'], $subthreads );
 296+ return new LiveThread($l, $children);
297297 }
298298
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 -
310299 static function withRoot( $post ) {
311300 return Threads::where( array('thread.thread_root' => $post->getID()) );
312301 }

Status & tagging log