From 4c35a6b0d7e2f2431571d63888816ecff446d258 Mon Sep 17 00:00:00 2001
From: friendica <info@friendica.com>
Date: Mon, 2 Jan 2012 16:54:37 -0800
Subject: [PATCH] conversation sql optimisations

---
 include/conversation.php | 59 ++++++++++++++++++++++++++++++++++++++++
 mod/network.php          | 30 +++++---------------
 mod/profile.php          | 11 ++++----
 3 files changed, 72 insertions(+), 28 deletions(-)

diff --git a/include/conversation.php b/include/conversation.php
index fae57c565..d40b2ea7e 100644
--- a/include/conversation.php
+++ b/include/conversation.php
@@ -884,3 +884,62 @@ function status_editor($a,$x, $notes_cid = 0) {
 
 	return $o;
 }
+
+
+function conv_sort($arr,$order) {
+
+	if((!(is_array($arr) && count($arr))))
+		return array();
+
+	$parents = array();
+
+	foreach($arr as $x)
+		if($x['id'] == $x['parent'])
+				$parents[] = $x;
+
+	if(stristr($order,'created'))
+		usort($parents,'sort_thr_created');
+	elseif(stristr($order,'commented'))
+		usort($parents,'sort_thr_commented');
+
+	foreach($parents as $x) 
+		$x['children'] = array();
+
+	foreach($arr as $x) {
+		if($x['id'] != $x['parent']) {
+			$p = find_thread_parent_index($parents,$x);
+			$parents[$p]['children'][] = $x;
+		}
+	}
+	foreach($parents as $x)
+		if(count($x['children']))
+			usort($x['children'],'sort_thr_created_rev');
+
+	$ret = array();
+	foreach($parents as $x) {
+		$ret[] = $x;
+		foreach($x['children'] as $y)
+			$ret[] = $y;
+	}
+
+	return $ret;
+}
+
+
+function sort_thr_created($a,$b) {
+	return strcmp($b['created'],$a['created']);
+}
+
+function sort_thr_created_rev($a,$b) {
+	return strcmp($a['created'],$b['created']);
+}
+
+function sort_thr_commented($a,$b) {
+	return strcmp($b['commented'],$a['commented']);
+}
+
+function find_thread_parent_index($arr,$x) {
+	foreach($arr as $k => $v)
+		if($v['id'] == $x['parent'])
+			return $k;
+}
\ No newline at end of file
diff --git a/mod/network.php b/mod/network.php
index 55f5a8c95..b493c419b 100644
--- a/mod/network.php
+++ b/mod/network.php
@@ -15,14 +15,6 @@ function network_init(&$a) {
 
 	$search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');
 
-	// We need a better way of managing a growing argument list
-
-	// moved into savedsearches()
-	// $srchurl = '/network' 
-	// 		. ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : '') 
-	// 		. ((x($_GET,'star')) ? '?star=' . $_GET['star'] : '')
-	// 		. ((x($_GET,'bmark')) ? '?bmark=' . $_GET['bmark'] : '');
-	
 	if(x($_GET,'save')) {
 		$r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
 			intval(local_user()),
@@ -46,14 +38,10 @@ function network_init(&$a) {
 	
 	// search terms header
 	if(x($_GET,'search')) {
-		$a->page['content'] .= '<h2>Search Results For: '  . $search . '</h2>';
+		$a->page['content'] .= '<h2>' . t('Search Results For:') . ' '  . $search . '</h2>';
 	}
 	
 	$a->page['aside'] .= group_side('network','network',true,$group_id);
-	
-	// moved to saved searches to have it in the same div
-	//$a->page['aside'] .= search($search,'netsearch-box',$srchurl,true);
-
 	$a->page['aside'] .= saved_searches($search);
 
 }
@@ -275,12 +263,6 @@ function network_content(&$a, $update = 0) {
 	$sql_options  = (($star) ? " and starred = 1 " : '');
 	$sql_options .= (($bmark) ? " and bookmark = 1 " : '');
 
-
-	$sql_new = '';
-	$sql_items = '';
-	$sql_update = '';
-
-
 	$sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
 
 	if($group) {
@@ -467,16 +449,18 @@ function network_content(&$a, $update = 0) {
 				`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
 				`contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
 				`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
-				FROM `item`, (SELECT `p`.`id`,`p`.`created`,`p`.`commented` FROM `item` AS `p` WHERE `p`.`parent`=`p`.`id`) as `parentitem`, `contact`
+				FROM `item`, `contact`
 				WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
 				AND `contact`.`id` = `item`.`contact-id`
 				AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
-				AND `item`.`parent` = `parentitem`.`id` AND `item`.`parent` IN ( %s )
-				$sql_extra
-				ORDER BY `parentitem`.$ordering DESC, `parentitem`.`id` ASC, `item`.`gravity` ASC, `item`.`created` ASC ",
+				AND `item`.`parent` IN ( %s )
+				$sql_extra ",
 				intval(local_user()),
 				dbesc($parents_str)
 			);
+
+			$items = conv_sort($items,$ordering);
+
 		}	
 	}
 
diff --git a/mod/profile.php b/mod/profile.php
index 946499895..90a609d03 100644
--- a/mod/profile.php
+++ b/mod/profile.php
@@ -167,7 +167,7 @@ function profile_content(&$a, $update = 0) {
 		$r = q("SELECT distinct(parent) AS `item_id`, `contact`.`uid` AS `contact-uid`
 			FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
 			WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
-			and `item`.`parent` in (select parent from item where unseen = 1 )
+			and `item`.`unseen` = 1
 			AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
 			AND `item`.`wall` = 1
 			$sql_extra
@@ -218,18 +218,19 @@ function profile_content(&$a, $update = 0) {
 			`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, `contact`.`rel`, 
 			`contact`.`thumb`, `contact`.`self`, `contact`.`writable`, 
 			`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
-			FROM `item`, (SELECT `p`.`id`,`p`.`created` FROM `item` AS `p` WHERE `p`.`parent` = `p`.`id`) AS `parentitem`, `contact`
+			FROM `item`, `contact`
 			WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
 			AND `contact`.`id` = `item`.`contact-id`
 			AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
-			AND `item`.`parent` = `parentitem`.`id` AND `item`.`parent` IN ( %s )
-			$sql_extra
-			ORDER BY `parentitem`.`created` DESC, `gravity` ASC, `item`.`created` ASC ",
+			AND `item`.`parent` IN ( %s )
+			$sql_extra ",
 			intval($a->profile['profile_uid']),
 			dbesc($parents_str)
 		);
 	}
 
+	$items = conv_sort($items,'created');
+
 	if($is_owner && ! $update) {
 		$o .= get_birthdays();
 		$o .= get_events();