From ed4dc232c282ed6c9fe3a2df3cddbcaa22ead878 Mon Sep 17 00:00:00 2001 From: Paul J R Date: Thu, 20 Sep 2012 09:18:49 +1000 Subject: [PATCH] getting basic search functionality working --- gwvpmini/gwvpmini_db.php | 72 +++++++++++++++++++++++++++++++- gwvpmini/gwvpmini_search.php | 94 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 164 insertions(+), 2 deletions(-) diff --git a/gwvpmini/gwvpmini_db.php b/gwvpmini/gwvpmini_db.php index 00f20b1..1995e7d 100644 --- a/gwvpmini/gwvpmini_db.php +++ b/gwvpmini/gwvpmini_db.php @@ -55,6 +55,7 @@ function gwvpmini_getUser($username=null, $email=null, $id=null) } + function gwvpmini_getRepo($ownerid=null, $name=null, $id=null) { $conn = gwvpmini_ConnectDB(); @@ -505,16 +506,19 @@ function gwvpmini_GetOwnedRepos($username) */ $conn = gwvpmini_ConnectDB(); + $uid = gwvpmini_GetUserId($username); $sql = "select * from repos where repos_owner='$uid'"; - error_log("owned repos sql $sql"); + error_log("owned repos sql $sql for username $username"); $res = $conn->query($sql); + if($username == "") return false; $retval = false; foreach($res as $row) { $id = $row["repos_id"]; $retval[$id]["name"] = $row["repos_name"]; $retval[$id]["desc"] = $row["repos_description"]; + $retval[$id]["id"] = $row["repos_id"]; error_log(print_r($row, true)); } @@ -569,11 +573,43 @@ function gwvpmini_GetUsers($startat = 0, $num = 10) $retval[$id]["desc"] = $row["user_desc"]; $retval[$id]["level"] = $row["user_level"]; $retval[$id]["status"] = $row["user_status"]; + $retval[$id]["id"] = $row["user_id"]; } return $retval; } +function gwvp_findPeopleLike($search) +{ + $conn = gwvpmini_ConnectDB(); + + $sql = "select * from users where user_username like '%$search%' or user_full_name like '%$search%'"; + + $res = $conn->query($sql); + + if(!$res) { + error_log("SERACHUSER: $sql returned false"); + return false; + } else { + error_log("SERACHUSER: $sql returned true"); + } + + $retval = false; + foreach($res as $row) { + $id = $row["user_id"]; + $retval[$id]["fullname"] = $row["user_full_name"]; + $retval[$id]["username"] = $row["user_username"]; + $retval[$id]["email"] = $row["user_email"]; + $retval[$id]["desc"] = $row["user_desc"]; + $retval[$id]["level"] = $row["user_level"]; + $retval[$id]["status"] = $row["user_status"]; + $retval[$id]["id"] = $row["user_id"]; + } + + error_log("SEARCHUSER: array is ".print_r($retval, true)); + return $retval; +} + function gwvpmini_GetRepos($startat=0, $num=200) { $conn = gwvpmini_ConnectDB(); @@ -598,6 +634,7 @@ function gwvpmini_GetRepos($startat=0, $num=200) $retval[$id]["name"] = $row["repos_name"]; $retval[$id]["desc"] = $row["repos_description"]; $retval[$id]["owner"] = $row["repos_owner"]; + $retval[$id]["id"] = $row["repos_id"]; } return $retval; @@ -605,6 +642,39 @@ function gwvpmini_GetRepos($startat=0, $num=200) } +function gwvp_findReposLike($search) +{ + $conn = gwvpmini_ConnectDB(); + + /* + * CREATE TABLE "repos" ( + "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT, + "repos_name" TEXT, + "repos_description" TEXT, + "repos_owner" INTEGER + )'; + + */ + + $sql = "select * from repos where repos_name like '%$search%' or repos_description like '%$search%'"; + + $res = $conn->query($sql); + + if(!$res) return false; + + $retval = false; + foreach($res as $row) { + $id = $row["repos_id"]; + $retval[$id]["name"] = $row["repos_name"]; + $retval[$id]["desc"] = $row["repos_description"]; + $retval[$id]["owner"] = $row["repos_owner"]; + $retval[$id]["id"] = $row["repos_id"]; + } + + return $retval; + +} + function gwvpmini_GetNRepos() { $conn = gwvpmini_ConnectDB(); diff --git a/gwvpmini/gwvpmini_search.php b/gwvpmini/gwvpmini_search.php index 1583c3f..6bcf4c1 100644 --- a/gwvpmini/gwvpmini_search.php +++ b/gwvpmini/gwvpmini_search.php @@ -45,7 +45,99 @@ function gwvpmini_SearchMainPage() function gwvpmini_SearchMainPageBody() { - echo "You searched for ".$_REQUEST["searchstring"]; + global $BASE_URL; + + $search = $_REQUEST["searchstring"]; + + $reps = gwvp_findReposLike($search); + $ppls = gwvp_findPeopleLike($search); + + + + /*echo "
repos\n\n";
+	print_r($reps);
+	echo "\n\nppl\n\n";
+	print_r($ppls);
+	echo "
";*/ + + // we need to process the search string into tokens here + if($search == "") { + echo "You need to enter a search string
"; + return; + } + + $vl = gwvpmini_SearchTokenizeString($search); + $search_str = $vl["str"]; + + echo "

Results

"; + echo "Searching for \"$search_str\"
Note: Search does not look INSIDE repos
"; + echo ""; + // repos + echo "

Repo's

People

"; + + + // get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) { + if($reps != false) { + echo ""; + foreach($reps as $rep) { + $ownerinfo = gwvpmini_getUser(null, null, $rep["owner"]); + $userdets = get_gravatar($ownerinfo["email"], 40, 'mm', 'g', true); + $userdets .= "
".$ownerinfo["username"].""; + + $repodets = "".$rep["name"]."
".$rep["desc"]; + echo ""; + } + echo "
$userdets$repodets
"; + } else echo "No Repo's Match"; + + + echo "
"; + // people + if($ppls != false) { + echo ""; + $ownedrepos = "BLAHBLAH"; + foreach($ppls as $ppl) { + $userdets = get_gravatar($ppl["email"], 40, 'mm', 'g', true); + $userdets .= "
".$ppl["username"].""; + $repos = gwvpmini_GetOwnedRepos($ppl["username"]); + if($repos == false) $ownedrepos = "No Repos"; + else { + $ownedrepos = ""; + foreach($repos as $repo) { + $ownedrepos .= "".$repo["name"]." - ".$repo["desc"]."
"; + } + } + echo ""; + } + + echo "
$userdets$ownedrepos
"; + } else echo "No People Match"; + + + echo "
"; +} + +function gwvpmini_SearchTokenizeString($search) +{ + $inp = preg_replace("/[^a-zA-Z0-9 ]+/", "", $search); + + $res = preg_split("/ +/", trim($inp)); + + $ret["str"] = ""; + $i = 0; + foreach($res as $r) { + if($i == 0) $ret["str"] = "$r"; + else $ret["str"] .= " and $r"; + $ret["words"][$i] = $r; + $i++; + } + + /*echo "
";
+	print_r($ret);
+	echo "
";*/ + + return $ret; + } ?> \ No newline at end of file -- 1.7.0.4