From 297447823ee809c8d727c4623219b16a5fd5d549 Mon Sep 17 00:00:00 2001
From: rloth <romain.loth@iscpif.fr>
Date: Mon, 5 Dec 2016 19:49:51 +0100
Subject: [PATCH] new MySQL connectors for comex directory/filter operations

---
 TODO_RESTORE.md             |  12 ++
 comex_library.php           |   8 +-
 directory_content.php       | 278 +++++++++++++++++++++---------------
 parametres.php              |  42 +++---
 print_directory.php         | 129 +++++++++--------
 print_scholar_directory.php |  97 +++++++------
 search_filter.php           |  26 ++--
 search_scholar.php          | 149 +++++++++++--------
 8 files changed, 430 insertions(+), 311 deletions(-)
 create mode 100644 TODO_RESTORE.md

diff --git a/TODO_RESTORE.md b/TODO_RESTORE.md
new file mode 100644
index 0000000..ee1e2b3
--- /dev/null
+++ b/TODO_RESTORE.md
@@ -0,0 +1,12 @@
+### List of features that where deactivated
+
+During rewrite we deactivated some retrieval to match the new sql tables from registration:
+
+  - tables organizations, labs don't exist => TODO RESTORE in `directory_content.php` (and therefore empty arrays are passed to `labs_list.php` and `orga_list.php`)
+
+  - in `search_scholar.php`, column nb_keywords doesn't exist (and now keywords are never 0) => TODO RESTORE
+
+  - in `print_directory.php`, all the following columns are now ignored: 'nb_keywords', 'css_voter', 'css_member', 'keywords_ids', 'status', 'homepage', 'lab2', 'affiliation2', 'homepage', **'position'** (used in `stats_prep_from_array.php`), 'photo_url', 'address', 'city', 'postal_code', 'phone', 'mobile', 'fax', 'affiliation_acronym' => TODO RESTORE
+  - in `print_scholar_directory.php`
+    - similar changes as above
+    - additional change to keywords_ids search in old "scholars2terms" table => becomes `LIKE` search in keywords: TODO index
diff --git a/comex_library.php b/comex_library.php
index 341803f..e424cc3 100755
--- a/comex_library.php
+++ b/comex_library.php
@@ -16,14 +16,16 @@ function clean_exp($string){
     }
 }
 
-function get_value($cle_value,$table='data',$cle='cle',$valeur='valeur'){    
+
+// NB actually (2016-12) this is unused TODO use !!!
+function get_value($cle_value,$table='data',$cle='cle',$valeur='valeur'){
 // renvoie la valeur correspondant à la clé $cle dans la table data
-   
+
 $sql = 'SELECT '.$valeur.' from '.$table.' WHERE '.$cle.'="'.trim($cle_value).'"';
 $resultat=mysql_query($sql);
     while ($ligne=mysql_fetch_array($resultat)) {
         $out=$ligne[$valeur];
         }
 return $out;
-}   
+}
 ?>
diff --git a/directory_content.php b/directory_content.php
index 167398b..7a2ac12 100755
--- a/directory_content.php
+++ b/directory_content.php
@@ -40,7 +40,51 @@ foreach ($scholars as $scholar) {
     $content .= '<div>';
     if ($scholar['photo_url'] != null) {
         $content .= '<img style="margin: 7px 10px 10px 0px" src="http://main.csregistry.org/' . $scholar['photo_url'] . '" width="' . $imsize . 'px" align="left">';
-    } else {
+    }
+    // raw binary picture
+    elseif ($scholar['pic_file'] != null) {
+
+        // create temp file
+        // -----------------
+
+
+
+        $finfo    = finfo_open(FILEINFO_MIME_TYPE);
+        // $mimetype = finfo_buffer($finfo, base64_encode(convert_uudecode($scholar['pic_file'])));
+        // $mimetype = finfo_buffer($finfo, convert_uudecode($scholar['pic_file']));
+        $mimetype = finfo_buffer($finfo, $scholar['pic_file']);
+        finfo_close($finfo);
+
+
+        // £TODO more experiments to fix display here
+
+        // find format
+        $raw_beginning = substr($scholar['pic_file'], 0,8);
+        $mimeguess = null;
+        $JPEG = "/^xffxd8xf/";
+        $GIF  = "/^GIF/";
+        $PNG  = "/^x89PNG/";
+        if (preg_match ($JPEG, $raw_beginning)) {
+            // echo "<p>got JPEG</p>";
+            $mimeguess="image/jpeg";
+        }
+        else if (preg_match ($GIF, $raw_beginning)) {
+            // echo "<p>got GIF</p>";
+            $mimeguess="image/gif";
+        }
+        else if (preg_match ($PNG, $raw_beginning)) {
+            // echo "<p>got PNG</p>";
+            $mimeguess="image/png";
+        }
+        else {
+            // echo "<p>no match :(</p>";
+        }
+
+        echo "<p>mimetype:".$mimeguess."</p>";
+
+        $content .= '<img style="margin: 7px 10px 10px 0px" src="data:'.$mimeguess.';base64,'.$perhaps_decoded.'"/>';
+    }
+    else {
         if (count($scholars) < 2000) {
             $im_id = floor(rand(0, 11));
             $content .= '<img style="margin: 7px 10px 10px 0px" src="img/' . $im_id . '.png" width="' . $imsize . 'px" align="left">';
@@ -77,43 +121,42 @@ foreach ($scholars as $scholar) {
     }
 
 
-    $affiliation2 = '';
-    if ($scholar['lab2'] != null) {
-        $affiliation2.=$scholar['lab2'] . ', ';
-        $lab_list[]=$scholar['lab2'];
-    }
-    if ($scholar['affiliation2'] != null) {
-        $affiliation2.=$scholar['affiliation2'];
-        $orga_list[]=$scholar['affiliation2'];
-        //echo $scholar['affiliation2'].'<br/>';
-    }
-    if (($scholar['affiliation2'] != null) | ($scholar['lab2'] != null)) {
-        $content .= '<dd><i>Second affiliation: </i>' . clean_exp($affiliation2) . '</dd>';
-    }
-
-    if ((strcmp($affiliation2, '') != 0) | (strcmp($affiliation, '') != 0)) {
-        $content .= '<br/>';
-    }
-
-    $www = '';
-    if (substr($scholar['homepage'], 0, 3) === 'www') {
-        $www.=' <a href=' . trim(str_replace('&', ' and ', 'http://' . $scholar['homepage'])) . ' target=blank > ' . trim(str_replace('&', ' and ', 'http://' . $scholar['homepage'])) . '  </a ><br/>';
-    } elseif (substr($scholar['homepage'], 0, 4) === 'http') {
-        $www.=' <a href=' . trim(str_replace('&', ' and ', $scholar['homepage'])) . ' target=blank > ' . trim(str_replace('&', ' and ', $scholar['homepage'])) . ' </a ><br/>';
-    }
-
-    if (strcmp($www, '') != 0) {
-        $content .= '<dd><i class="icon-home"></i>' . $www . '</dd> ';
-    }
-
-    if ($scholar['css_member'] === 'Yes') {
-        if ($scholar['css_voter'] === 'Yes') {
-            $content .= '<dd><i class="icon-user"></i> CSS Voting Member</dd> ';
-        } else {
-            $content .= '<dd><i class="icon-user"></i> CSS Member</dd> ';
-        }
-
-    }
+    // $affiliation2 = '';
+    // if ($scholar['lab2'] != null) {
+    //     $affiliation2.=$scholar['lab2'] . ', ';
+    //     $lab_list[]=$scholar['lab2'];
+    // }
+    // if ($scholar['affiliation2'] != null) {
+    //     $affiliation2.=$scholar['affiliation2'];
+    //     $orga_list[]=$scholar['affiliation2'];
+    //     //echo $scholar['affiliation2'].'<br/>';
+    // }
+    // if (($scholar['affiliation2'] != null) | ($scholar['lab2'] != null)) {
+    //     $content .= '<dd><i>Second affiliation: </i>' . clean_exp($affiliation2) . '</dd>';
+    // }
+    //
+    // if ((strcmp($affiliation2, '') != 0) | (strcmp($affiliation, '') != 0)) {
+    //     $content .= '<br/>';
+    // }
+
+    // $www = '';
+    // if (substr($scholar['homepage'], 0, 3) === 'www') {
+    //     $www.=' <a href=' . trim(str_replace('&', ' and ', 'http://' . $scholar['homepage'])) . ' target=blank > ' . trim(str_replace('&', ' and ', 'http://' . $scholar['homepage'])) . '  </a ><br/>';
+    // } elseif (substr($scholar['homepage'], 0, 4) === 'http') {
+    //     $www.=' <a href=' . trim(str_replace('&', ' and ', $scholar['homepage'])) . ' target=blank > ' . trim(str_replace('&', ' and ', $scholar['homepage'])) . ' </a ><br/>';
+    // }
+    //
+    // if (strcmp($www, '') != 0) {
+    //     $content .= '<dd><i class="icon-home"></i>' . $www . '</dd> ';
+    // }
+    //
+    // if ($scholar['css_member'] === 'Yes') {
+    //     if ($scholar['css_voter'] === 'Yes') {
+    //         $content .= '<dd><i class="icon-user"></i> CSS Voting Member</dd> ';
+    //     } else {
+    //         $content .= '<dd><i class="icon-user"></i> CSS Member</dd> ';
+    //     }
+    // }
 
    if (($scholar['position'] != null)||($scholar['lab'] != null)||($scholar['affiliation'] != null)) {
        $content .= '</dl>';
@@ -139,20 +182,20 @@ foreach ($scholars as $scholar) {
                  $content .= '<i class="icon-tags"></i> ' . clean_exp($scholar['keywords']). '.<br/><br/>';
         }
 
-        if ($scholar['address'] != null) {
-            $content .= '<address><i class="icon-envelope"></i> ' . $scholar['address'] . '<br/>' . $scholar['city'] . '<br/>' . $scholar['postal_code'] . '<br/></address>';
-        }
-
-
-        if ($scholar['phone'] != null) {
-            $content .= '<address><strong>Phone</strong>: '.$scholar['phone'] . '<br/>';
-            if ($scholar['mobile'] != null) {
-                $content .='<strong>Mobile</strong>: '.$scholar['mobile']. '<br/>';
-            }
-            if ($scholar['fax'] != null) {
-                $content .='<strong>Fax</strong>: '.$scholar['fax'] . '<br/>';
-            }
-        }
+        // if ($scholar['address'] != null) {
+        //     $content .= '<address><i class="icon-envelope"></i> ' . $scholar['address'] . '<br/>' . $scholar['city'] . '<br/>' . $scholar['postal_code'] . '<br/></address>';
+        // }
+        //
+        //
+        // if ($scholar['phone'] != null) {
+        //     $content .= '<address><strong>Phone</strong>: '.$scholar['phone'] . '<br/>';
+        //     if ($scholar['mobile'] != null) {
+        //         $content .='<strong>Mobile</strong>: '.$scholar['mobile']. '<br/>';
+        //     }
+        //     if ($scholar['fax'] != null) {
+        //         $content .='<strong>Fax</strong>: '.$scholar['fax'] . '<br/>';
+        //     }
+        // }
 
         $content .= '</div>';
     }
@@ -181,38 +224,39 @@ if (strcmp(substr($orga_query, 0,2),'OR')==0){
 // liste des labs ////////
 //////////////////////////
 $labs = array();
-sort($lab_list);
-
-foreach ($lab_list as $name) {
-    if ((trim($name)) != NULL) {
-        $sql = 'SELECT * FROM labs where name="' . $name . '" OR acronym="' . $name . '"';
-        //echo $sql.'<br/>';
-        foreach ($base->query($sql) as $row) {
-            //echo 'toto';
-            $info = array();
-            $info['unique_id'] = $row['id'];
-            $info['name'] = $row['name'];
-            $info['acronym'] = $row['acronym'];
-            $info['homepage'] = $row['homepage'];
-            $info['keywords'] = $row['keywords'];
-            $info['country'] = $row['country'];
-            $info['address'] = $row['address'];
-            $info['organization'] = $row['organization'];
-            $info['organization2'] = $row['organization2'];
-            $orga_list[] = $row['organization'];
-            $orga_list[] = $row['organization2'];
-            $info['object'] = $row['object'];
-            $info['methods'] = $row['methods'];
-            $info['director'] = $row['director'];
-            $info['admin'] = $row['admin'];
-            $info['phone'] = $row['phone'];
-            $info['fax'] = $row['fax'];
-            $info['login'] = $row['login'];
-            //print_r($info);
-            $labs[$row['id']] = $info;
-        }
-    }
-}
+// TODO RESTORE
+// sort($lab_list);
+
+// foreach ($lab_list as $name) {
+//     if ((trim($name)) != NULL) {
+//         $sql = 'SELECT * FROM labs where name="' . $name . '" OR acronym="' . $name . '"';
+//         //echo $sql.'<br/>';
+//         foreach ($base->query($sql) as $row) {
+//             //echo 'toto';
+//             $info = array();
+//             $info['unique_id'] = $row['id'];
+//             $info['name'] = $row['name'];
+//             $info['acronym'] = $row['acronym'];
+//             $info['homepage'] = $row['homepage'];
+//             $info['keywords'] = $row['keywords'];
+//             $info['country'] = $row['country'];
+//             $info['address'] = $row['address'];
+//             $info['organization'] = $row['organization'];
+//             $info['organization2'] = $row['organization2'];
+//             $orga_list[] = $row['organization'];
+//             $orga_list[] = $row['organization2'];
+//             $info['object'] = $row['object'];
+//             $info['methods'] = $row['methods'];
+//             $info['director'] = $row['director'];
+//             $info['admin'] = $row['admin'];
+//             $info['phone'] = $row['phone'];
+//             $info['fax'] = $row['fax'];
+//             $info['login'] = $row['login'];
+//             //print_r($info);
+//             $labs[$row['id']] = $info;
+//         }
+//     }
+// }
 
 //print_r($labs);
 
@@ -222,37 +266,38 @@ foreach ($lab_list as $name) {
 // liste des organizations ////////
 //////////////////////////
 $organiz = array();
-sort($orga_list);
-foreach ($orga_list as $name) {
-    if ((trim($name))!=NULL){
-    $sql = "SELECT * FROM organizations where name='" . $name. "' OR acronym='".$name."'";
-
-    $temp=true;
-    foreach ($base->query($sql) as $row) {
-        if ($temp){
-        $info = array();
-        $info['unique_id'] = $row['id'];
-        $info['name'] = $row['name'];
-        $info['acronym'] = $row['acronym'];
-        $info['homepage'] = $row['homepage'];
-        $info['keywords'] = $row['keywords'];
-        $info['country'] = $row['country'];
-        $info['street'] = $row['street'];
-        $info['city'] = $row['city'];
-        $info['state'] = $row['state'];
-        $info['postal_code'] = $row['postal_code'];
-        $info['fields'] = $row['fields'];
-        $info['admin'] = $row['admin'];
-        $info['phone'] = $row['phone'];
-        $info['fax'] = $row['fax'];
-        $info['login'] = $row['login'];
-        $organiz[$row['id']] = $info;
-        $temp=false;
-        }
-    }
-    }
-
-}
+// TODO RESTORE
+// sort($orga_list);
+// foreach ($orga_list as $name) {
+//     if ((trim($name))!=NULL){
+//     $sql = "SELECT * FROM organizations where name='" . $name. "' OR acronym='".$name."'";
+//
+//     $temp=true;
+//     foreach ($base->query($sql) as $row) {
+//         if ($temp){
+//         $info = array();
+//         $info['unique_id'] = $row['id'];
+//         $info['name'] = $row['name'];
+//         $info['acronym'] = $row['acronym'];
+//         $info['homepage'] = $row['homepage'];
+//         $info['keywords'] = $row['keywords'];
+//         $info['country'] = $row['country'];
+//         $info['street'] = $row['street'];
+//         $info['city'] = $row['city'];
+//         $info['state'] = $row['state'];
+//         $info['postal_code'] = $row['postal_code'];
+//         $info['fields'] = $row['fields'];
+//         $info['admin'] = $row['admin'];
+//         $info['phone'] = $row['phone'];
+//         $info['fax'] = $row['fax'];
+//         $info['login'] = $row['login'];
+//         $organiz[$row['id']] = $info;
+//         $temp=false;
+//         }
+//     }
+//     }
+//
+// }
 
 
 
@@ -261,6 +306,8 @@ foreach ($orga_list as $name) {
 $content .='<br/> <A NAME="labs"> </A>
 <h1>Labs by alphabetical order</h1>
 <p><i>List of labs to which scholars are affiliated</i></p>';
+
+// TODO RESTORE
 include('labs_list.php');
 
 
@@ -271,6 +318,7 @@ $content .= '<br/> <A NAME="orga"> </A>
 <h1>Organizations by alphabetical order</h1>
 <br/>
 <p><i>List of organizations to which scholars are affiliated</i></p>';
+// TODO RESTORE
 include('orga_list.php');
 
 /// ajout des organisations
diff --git a/parametres.php b/parametres.php
index 306604b..7d47c84 100755
--- a/parametres.php
+++ b/parametres.php
@@ -1,33 +1,27 @@
 <?php
 
-/*
-list de parametres.
- */
+/* liste de parametres */
 
-$dbname='/home/jbilcke/databases/scholar_test_data.db';
 $dbname='community.db';
-//$dbname='scholar_test_data.db';
 
-//$scholars_db ="raw_scholars";
-$fichier = "CSSscholars20Oct2011.csv";
-$fichier = "CSSscholars2Oct2011.csv";
-$lab_csv="CSLabs13Fev2012.csv";
-$orga_csv='';
 $min_num_friends=0;// nombre minimal de voisin que doit avoir un scholar pour être affiché
-//$fichier = "Scholars13Sept2011.csv";
-//$fichier = "test2.csv";
-$drop_tables=true; // on efface les tables
-$language='english';
-$file_sep=',';
-//$scholar_filter=" where country='France' AND status='o'";
-//$scholar_filter=" where country='France' AND want_whoswho='Yes' AND css_member='Yes'";
-//$scholar_filter=" where css_member='Yes' AND want_whoswho='Yes'";
-//$scholar_filter=" where css_member='Yes' OR want_whoswho='Yes'";
-$scholar_filter="where css_member='Yes'";
-//$scholar_filter="";
-//$scholar_filter=" where country='France' AND want_whoswho='Yes'";
-//$scholar_filter=" where country='France'";
+// $compress='No';
 
-$compress='No';
 
+/* parametres externes: nom de l'hôte SQL*/
+$params = parse_ini_file("parametres_comex.ini");
+
+/* tout pour le MySQL */
+$host =  $params['SQL_HOST'];
+$db   = 'comex_shared';
+$user = 'root';
+$pass = 'very-safe-pass';
+$charset = 'utf8';
+
+$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
+$opt = [
+    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
+    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
+    PDO::ATTR_EMULATE_PREPARES   => false,
+];
 ?>
diff --git a/print_directory.php b/print_directory.php
index a9d798c..031780a 100755
--- a/print_directory.php
+++ b/print_directory.php
@@ -55,7 +55,7 @@ $meta = '<!DOCTYPE html>
 define('_is_utf8_split', 5000);
 
 function is_utf8($string) {
-   
+
     // From http://w3.org/International/questions/qa-forms-utf-8.html
     return preg_match('%^(?:
           [\x09\x0A\x0D\x20-\x7E]            # ASCII
@@ -67,7 +67,7 @@ function is_utf8($string) {
         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
     )*$%xs', $string);
-   
+
 }
 
 $data = json_decode($_GET['query']);
@@ -78,7 +78,7 @@ function objectToArray($d) {
 			// with get_object_vars function
 			$d = get_object_vars($d);
 		}
- 
+
 		if (is_array($d)) {
 			/*
 			* Return array converted to object
@@ -103,11 +103,21 @@ $laboratories = $data["laboratories"];
 $organizations = $data["organizations"];
 $tags = $data["tags"];
 
+
+// echo '<p style="color:white">params:  $categorya =====> '. var_dump($categorya) ."<p>";
+// echo '<p style="color:white">params:  $categoryb =====> '. var_dump($categoryb)."<p>";
+// echo '<p style="color:white">params:  $countries =====> '. var_dump($countries) ."<p>";
+// echo '<p style="color:white">params:  $keywords =====> '. var_dump($keywords) ."<p>";
+// echo '<p style="color:white">params:  $laboratories =====> '. var_dump($laboratories) ."<p>";
+// echo '<p style="color:white">params:  $organizations =====> '. var_dump($organizations) ."<p>";
+// echo '<p style="color:white">params:  $tags =====> '. var_dump($tags) ."<p>";
+
 $query_details='<ul>';
 
 $f = "";// requête
 $labfilter='';
 if ($tags) {
+    echo '<p style="color:white">MATCHING ON tags<p>';
 	if (sizeof($tags) > 0) {
 		$f .= 'AND (';
 	}
@@ -121,14 +131,15 @@ if ($tags) {
 			if ($i > 0)
 				$f .= " OR ";
 			$f .= 'tags LIKE "%' . $word . '%" ';
-                        $query_details.=$word.', '; 
+                        $query_details.=$word.', ';
 			$i++;
 		}
 	}
-	$f .= ") ";	
+	$f .= ") ";
 }
 
 if ($keywords) {
+    echo '<p style="color:white">MATCHING ON keywords<p>';
 	if (sizeof($keywords) > 0) {
 		$f .= 'AND (';
 	}
@@ -140,16 +151,16 @@ if ($keywords) {
 			$word = sanitize_input(trim(strtolower($word)));
 			if ($word == "") continue;
                         $query_details.=$word.', ';
-			if ($i > 0)                            
+			if ($i > 0)
 				$f .= " OR ";
 			$f .= 'keywords LIKE "%' . $word . '%" ';
 			$i++;
 		}
 	}
-	$f .= ")  ";	
+	$f .= ")  ";
 }
 if ($countries) {
-
+    echo '<p style="color:white">MATCHING ON countries<p>';
 	if (sizeof($countries) > 0) {
 		$f .= 'AND (';
 	}
@@ -161,14 +172,14 @@ if ($countries) {
 		if ($country == "") continue;
 		if ($i > 0)
 			$f .= " OR ";
-		$f .= 'country = "' . $country . '" ';                
-                $query_details.=$country.', '; 
+		$f .= 'country = "' . $country . '" ';
+                $query_details.=$country.', ';
 		$i++;
 	}
 	$f .= ")  ";
 }
 if ($laboratories) {
-
+    echo '<p style="color:white">MATCHING ON labs<p>';
 	if (sizeof($laboratories) > 0) {
 		$f .= 'AND (';
 	}
@@ -179,40 +190,40 @@ if ($laboratories) {
 		if ($lab == "") continue;
 		if ($i > 0)
 			$f .= " OR ";
-		$f .= 'lab LIKE "%' . $lab . '%" ';
-                $query_details.=$lab.', '; 
+		$f .= 'team_lab LIKE "%' . $lab . '%" ';
+                $query_details.=$lab.', ';
 		$i++;
 	}
-	$f .= ")  ";        
+	$f .= ")  ";
 }
 
 if ($organizations) {
-
+    echo '<p style="color:white">MATCHING ON organizations<p>';
 	if (sizeof($organizations) > 0) {
 		$f .= 'AND (';
 	}
         $query_details.='<li><strong>In the organization named : </strong>';
 	$i = 0;
 	foreach ($organizations as $org) {
+        // echo '<p style="color:white">========> org =====> '. $org ."<p>";
 		$org = sanitize_input(trim(strtolower($org)));
-		
+
 		if ($org == "") continue;
-                $query_details.=$org.', '; 
-		$f .= 'affiliation LIKE "%' . $org . '%" OR affiliation2 LIKE "%' . $org . '%" ';                
+                $query_details.=$org.', ';
+		$f .= 'institution LIKE "%' . $org . '%" ';
                 //'affiliation LIKE "%' . $org . '% OR affiliation2 LIKE "%' . $org . '%"';
 		$i++;
 	}
-	$f .= ")  ";	
+	$f .= ")  ";
 }
 
 $query_details.='</ul>';
 
-$base = new PDO("sqlite:" . $dbname);
+$base = new PDO($dsn, $user, $pass, $opt);
 $termsMatrix = array(); // liste des termes présents chez les scholars avec leurs cooc avec les autres termes
 $scholarsMatrix = array(); // liste des scholars avec leurs cooc avec les autres termes
 $scholarsIncluded = 0;
 
-
 // liste des chercheurs
 if (substr($f, 0,3)=='AND'){
     $f=substr($f,3,-1);
@@ -224,14 +235,15 @@ if (substr($labfilter, 0,3)=='AND'){
 $imsize = 150;
 
 $content='';
-        
+
 if (strlen($f)>0){
-$sql = "SELECT * FROM scholars where " . " " . $f.' ORDER BY last_name';
+$sql = "SELECT * FROM comex_registrations where " . " " . $f.' ORDER BY last_name';
 }else{
-    $sql = "SELECT * FROM scholars ORDER BY last_name";
+    $sql = "SELECT * FROM comex_registrations ORDER BY last_name";
 }
 
 //echo $sql.'<br/>';
+echo '<p style="color:white">query:'. $sql ."<p>";
 
 // liste des chercheurs
 $scholars = array();
@@ -239,43 +251,46 @@ $scholars = array();
 //$query = "SELECT * FROM scholars";
 foreach ($base->query($sql) as $row) {
 
+
+    // TODO RESTORE
     $info = array();
-    $info['unique_id'] = $row['unique_id'];
+    $info['unique_id'] = $row['doors_uid'];
     $info['first_name'] = $row['first_name'];
     $info['initials'] = $row['initials'];
     $info['last_name'] = $row['last_name'];
-    $info['nb_keywords'] = $row['nb_keywords'];
-    $info['css_voter'] = $row['css_voter'];
-    $info['css_member'] = $row['css_member'];
-    $info['keywords_ids'] = explode(',', $row['keywords_ids']);
+
+    // => TODO RESTORE
+    // $info['nb_keywords'] = $row['nb_keywords'];
+    // $info['css_voter'] = $row['css_voter'];
+    // $info['css_member'] = $row['css_member'];
+    // $info['keywords_ids'] = explode(',', $row['keywords_ids']);
     $info['keywords'] = $row['keywords'];
-    $info['status'] = $row['status'];
+    // $info['status'] = $row['status'];
     $info['country'] = $row['country'];
-    $info['homepage'] = $row['homepage'];
-    $info['lab'] = $row['lab'];
-    $info['affiliation'] = $row['affiliation'];
-    $info['lab2'] = $row['lab2'];
-    $info['affiliation2'] = $row['affiliation2'];
-    $info['homepage'] = $row['homepage'];
-    $info['title'] = $row['title'];
-    $info['position'] = $row['position'];
-    $info['photo_url'] = $row['photo_url'];
-    $info['interests'] = $row['interests'];
-    $info['address'] = $row['address'];
-    $info['city'] = $row['city'];
-    $info['postal_code'] = $row['postal_code'];
-    $info['phone'] = $row['phone'];
-    $info['mobile'] = $row['mobile'];
-    $info['fax'] = $row['fax'];
-    $info['affiliation_acronym'] = $row['affiliation_acronym'];
-    $scholars[$row['unique_id']] = $info;
+    // $info['homepage'] = $row['homepage'];
+    $info['lab'] = $row['team_lab'];
+    $info['affiliation'] = $row['institution'];
+    // $info['lab2'] = $row['lab2'];
+    // $info['affiliation2'] = $row['affiliation2'];
+    // $info['homepage'] = $row['homepage'];
+    $info['title'] = $row['jobtitle'];
+    $info['position'] = $row['jobtitle'];
+    // $info['photo_url'] = $row['photo_url'];
+    $info['pic_file'] = $row['pic_file'];
+    $info['interests'] = $row['interests_text'];
+    // $info['address'] = $row['address'];
+    // $info['city'] = $row['city'];
+    // $info['postal_code'] = $row['postal_code'];
+    // $info['phone'] = $row['phone'];
+    // $info['mobile'] = $row['mobile'];
+    // $info['fax'] = $row['fax'];
+    // $info['affiliation_acronym'] = $row['affiliation_acronym'];
+    $scholars[$row['doors_uid']] = $info;
 }
 /// stats
-//$base = new PDO('sqlite:' . $dbname);
 include ('stat-prep_from_array.php');///
 
 
-
 include ("directory_content.php");
 
 // liste des chercheurs
@@ -285,7 +300,7 @@ include ("directory_content.php");
 $content .= '</div>';
 $content .= '</div>
             <footer>
-                GENERATED BY <a href="http://iscpif.fr"><img src="css/branding/logo-iscpif_medium.png" alt="iscpif.fr" style="border: none; margin-bottom : -6px;" title="isc-pif" /></a>-  <a href="http://sciencemapping.com" target="_BLANK">MOMA</a> - <a href="http://www.crea.polytechnique.fr/LeCREA/" target="_BLANK">CREA</a> - <a href="http://www.cnrs.fr/fr/recherche/index.htm" target="_BLANK">CNRS</a> 
+                GENERATED BY <a href="http://iscpif.fr"><img src="css/branding/logo-iscpif_medium.png" alt="iscpif.fr" style="border: none; margin-bottom : -6px;" title="isc-pif" /></a>-  <a href="http://sciencemapping.com" target="_BLANK">MOMA</a> - <a href="http://www.crea.polytechnique.fr/LeCREA/" target="_BLANK">CREA</a> - <a href="http://www.cnrs.fr/fr/recherche/index.htm" target="_BLANK">CNRS</a>
             </footer>
         </div>
 </body>
@@ -304,20 +319,20 @@ This directory presents the profiles of <a href="#scholars">'.  count($scholars)
 .  count($labs).' labs</a> and <a href="#orga">'.$orga_count.' organizations</a> in
 the field of Complex Systems';
 if (strlen(trim($query_details))>3){
-$header .= ': </p>'.$query_details;    
+$header .= ': </p>'.$query_details;
 }else{
     $header .='.</p> ';
 }
-$header .='<p>Its aims are to foster interactions 
+$header .='<p>Its aims are to foster interactions
 between protagonists in the fields of Complex Systems science and Complexity
 science,   as well as  to increase their visibility at the international scale.</p>
-    
+
 <ul>
 <li><b><i>This directory is open</i></b>. Anybody can have her profile included
 provided it is related to Complex Systems science and Complexity science. Personal data are given on a
 voluntary basis and people are responsible for the validity and integrity of their data.
 <li><i><b>This directory is browsable online</b> on the website of the complex systems society :</i> http://csbrowser.cssociety.org
-</ul> 
+</ul>
 </p>
 
 <p>
@@ -325,7 +340,7 @@ This directory is edited by the Complex Systems Registry. This initiative is sup
 Society</i> (<a href="http://cssociety.org">http://cssociety.org</a>).
 </p>
 <br/>
-<p>Contributions and ideas are welcome to improve this directory. 
+<p>Contributions and ideas are welcome to improve this directory.
 <a href="http://css.csregistry.org/whoswho+feedback">Please feedback</a></p>
 <br/>
 <h2>Global statistics</h2>
@@ -348,4 +363,4 @@ echo $content;
 
 
 
-?>
\ No newline at end of file
+?>
diff --git a/print_scholar_directory.php b/print_scholar_directory.php
index 7640f2c..d3376e3 100755
--- a/print_scholar_directory.php
+++ b/print_scholar_directory.php
@@ -72,27 +72,33 @@ function is_utf8($string) {
 $login = $_GET['query'];
 
 
-$base = new PDO("sqlite:" . $dbname);
+// $base = new PDO("sqlite:" . $dbname);
+$base = new PDO($dsn, $user, $pass, $opt);
 
 if ($login) {
     if (sizeof($login) > 0) {
 // liste des chercheurs
-        $sql = "SELECT keywords_ids,last_name,first_name FROM scholars where unique_id='" . $login . "'";
+        $sql = "SELECT keywords,last_name,first_name FROM comex_registrations WHERE doors_uid='" . $login . "'";
         foreach ($base->query($sql) as $row) {
-            $keywords_ids = explode(',', $row['keywords_ids']);
+            $keywords = explode(',', $row['keywords']);
             $scholar_array = array();
             $target_name=$row['first_name'].' '.$row['last_name'];
-            foreach ($keywords_ids as $keywords_id) {
-
-                $sql2 = "SELECT * FROM scholars2terms where term_id=" . trim($keywords_id);
-                #pt($sql2);
-                foreach ($base->query($sql2) as $row) {
-                    if (array_key_exists($row['scholar'], $scholar_array)){
-                        $scholar_array[$row['scholar']] += 1;
-                    }else{
-                        $scholar_array[$row['scholar']] = 1;
-                    }
+            foreach ($keywords as $keyword) {
+
+                if (strlen($keyword) > 0) {
 
+                    // TODO RESTORE index keywords
+                    // $sql2 = "SELECT * FROM scholars2terms where term_id=" . trim($keywords_id);
+                    $sql2 = "SELECT doors_uid FROM comex_registrations WHERE keywords LIKE \"%" . trim($keyword)."%\"";
+                    // echo($sql2."<br/>");
+                    foreach ($base->query($sql2) as $row) {
+                        if (array_key_exists($row['doors_uid'], $scholar_array)){
+                            $scholar_array[$row['doors_uid']] += 1;
+                        }else{
+                            $scholar_array[$row['doors_uid']] = 1;
+                        }
+
+                    }
                 }
             }
         }
@@ -100,48 +106,51 @@ if ($login) {
     }
 }
 
-        // les scholars sont affichés par ordre de pertinence
-        arsort($scholar_array);
+// les scholars sont affichés par ordre de pertinence
+arsort($scholar_array);
+
+$scholar_id_array=array_keys($scholar_array);
+// echo var_dump($scholar_id_array)."<br/>" ;
 
-        $scholar_id_array=array_keys($scholar_array);
 // liste des chercheurs
 $scholars = array();
-
+//
 foreach ($scholar_id_array as $scholar_id){
-$sql = "SELECT * FROM scholars where unique_id='" . $scholar_id. "'";
-
+$sql = "SELECT * FROM comex_registrations where doors_uid='" . $scholar_id. "'";
+// echo var_dump($scholar_id)."<br/>" ;
 //$query = "SELECT * FROM scholars";
 foreach ($base->query($sql) as $row) {
     $info = array();
-    $info['unique_id'] = $row['unique_id'];
+    $info['unique_id'] = $row['doors_uid'];
     $info['first_name'] = $row['first_name'];
     $info['initials'] = $row['initials'];
     $info['last_name'] = $row['last_name'];
-    $info['nb_keywords'] = $row['nb_keywords'];
-    $info['css_voter'] = $row['css_voter'];
-    $info['css_member'] = $row['css_member'];
-    $info['keywords_ids'] = explode(',', $row['keywords_ids']);
+    // $info['nb_keywords'] = $row['nb_keywords'];
+    // $info['css_voter'] = $row['css_voter'];
+    // $info['css_member'] = $row['css_member'];
+    // $info['keywords_ids'] = explode(',', $row['keywords_ids']);
     $info['keywords'] = $row['keywords'];
-    $info['status'] = $row['status'];
+    // $info['status'] = $row['status'];
     $info['country'] = $row['country'];
-    $info['homepage'] = $row['homepage'];
-    $info['lab'] = $row['lab'];
-    $info['affiliation'] = $row['affiliation'];
-    $info['lab2'] = $row['lab2'];
-    $info['affiliation2'] = $row['affiliation2'];
-    $info['homepage'] = $row['homepage'];
-    $info['title'] = $row['title'];
-    $info['position'] = $row['position'];
-    $info['photo_url'] = $row['photo_url'];
-    $info['interests'] = $row['interests'];
-    $info['address'] = $row['address'];
-    $info['city'] = $row['city'];
-    $info['postal_code'] = $row['postal_code'];
-    $info['phone'] = $row['phone'];
-    $info['mobile'] = $row['mobile'];
-    $info['fax'] = $row['fax'];
-    $info['affiliation_acronym'] = $row['affiliation_acronym'];
-    $scholars[$row['unique_id']] = $info;
+    // $info['homepage'] = $row['homepage'];
+    $info['lab'] = $row['team_lab'];
+    $info['affiliation'] = $row['institution'];
+    // $info['lab2'] = $row['lab2'];
+    // $info['affiliation2'] = $row['affiliation2'];
+    // $info['homepage'] = $row['homepage'];
+    $info['title'] = $row['jobtitle'];
+    $info['position'] = $row['jobtitle'];
+    // $info['photo_url'] = $row['photo_url'];
+    $info['pic_file'] = $row['pic_file'];
+    $info['interests'] = $row['interests_text'];
+    // $info['address'] = $row['address'];
+    // $info['city'] = $row['city'];
+    // $info['postal_code'] = $row['postal_code'];
+    // $info['phone'] = $row['phone'];
+    // $info['mobile'] = $row['mobile'];
+    // $info['fax'] = $row['fax'];
+    // $info['affiliation_acronym'] = $row['affiliation_acronym'];
+    $scholars[$row['doors_uid']] = $info;
 }
 
 }
@@ -210,7 +219,7 @@ Contributions and ideas are welcome to improve this directory.
 
 echo $meta.' '.$stats.'</head>';
 if (count($scholars)==0){
-echo  '<h2>Sorry, '.$target_name.' did not mentioned any keywords ... we cannot process it\'s network.</h2><br/>
+echo  '<h2>Sorry, '.$target_name.' did not mention any keywords ... we cannot process its network.</h2><br/>
     If you are '.$target_name.', you can  <a href="http://main.csregistry.org/Whoswhodata"  target="_BLANK">modify your profile</a> and see your
         network in few minutes.';
 }else{
diff --git a/search_filter.php b/search_filter.php
index 07fca3e..49fa830 100755
--- a/search_filter.php
+++ b/search_filter.php
@@ -1,14 +1,14 @@
 <?php
 
 /*
- * Génère le gexf des scholars à partir de la base sqlite
+ * Génère le gexf des scholars à partir de la base mysql
  */
 include("parametres.php");
 //include("../common/library/fonctions_php.php");
 include("normalize.php");
 
 
-$base = new PDO("sqlite:" . $dbname);
+$base = new PDO($dsn, $user, $pass, $opt);
 
 $category = trim(strtolower($_GET['category']));
 $term =  trim(strtolower($_GET['term']));
@@ -20,7 +20,10 @@ if ($category == 'country' || $category == 'countries') {
   $cat = "country";
   $query = 'LIKE upper(\''.strtoupper($q).'\')';
 } elseif ($category == 'organization' || $category == 'organizations') {
-  $cat = "affiliation";
+
+  // POSSIBLE: `concat(institution, ", ", IFNULL(team_lab, ""))``
+  //           (change in $cat here and in print_directory args downstream)
+  $cat = 'institution';
   $query = 'LIKE upper(\''.strtoupper($q).'\')';
 } elseif ($category == 'keyword' || $category == 'keywords') {
   $cat = "keywords";
@@ -29,7 +32,7 @@ if ($category == 'country' || $category == 'countries') {
   $cat = "tags";
   $query = 'LIKE upper(\''.strtoupper($q).'\')';
 } elseif ($category == 'labs' || $category == 'laboratories' || $category == 'laboratory') {
-  $cat = "lab";
+  $cat = "team_lab";
   $query = 'LIKE upper(\''.strtoupper($q).'\')';
 } else {
   echo ("ERROR");
@@ -41,17 +44,18 @@ $filtered = array (
 );
 function filter_word($value) {
   if ($value == null) return true;
-  return ! in_array(strtolower($value),$filtered); 
+  return ! in_array(strtolower($value),$filtered);
 }
 
-$req = "SELECT ".$cat." AS key, count(".$cat.") AS value FROM scholars WHERE ".$cat." ".$query." GROUP BY ".$cat." ORDER BY value DESC";
+$req = "SELECT ".$cat." AS clef, count(".$cat.") AS value FROM comex_registrations WHERE ".$cat." ".$query." GROUP BY ".$cat." ORDER BY value DESC";
+// echo $req;
 $results = array();
 $i = 0;
 foreach ($base->query($req) as $row) {
   $nb = $row['value'];
   if ($cat == "keywords" || $cat == "tags") {
     //echo "in keywords\n";
-     $words = explode(",", $row["key"]);
+     $words = explode(",", $row["clef"]);
     foreach ($words as $word) {
 
     $pos = strpos($word,$term);
@@ -69,9 +73,9 @@ foreach ($base->query($req) as $row) {
         }
     }
   } else {
-    $word = $row["key"];
+    $word = $row["clef"];
      if ($cat == "country") {
-        $word = normalize_country($word);  
+        $word = normalize_country($word);
     }
 
     if (filter_word($word)) {
@@ -98,7 +102,7 @@ foreach($results as $key => $value) {
         'label' => $key,
        // 'value' => $value,
        'score' => $value,
-       
+
      // F*** it, I'll put the meta data here...
        'category' => $cat,
           "term" => $term,
@@ -110,4 +114,4 @@ foreach($results as $key => $value) {
 $i = 0;
 
 echo json_encode($completion);
-?>
\ No newline at end of file
+?>
diff --git a/search_scholar.php b/search_scholar.php
index 977037c..7b9d42d 100755
--- a/search_scholar.php
+++ b/search_scholar.php
@@ -8,79 +8,114 @@ include("parametres.php");
 include("normalize.php");
 
 
-$base = new PDO("sqlite:" . $dbname);
 
-$category = trim(strtolower($_GET['category']));
-$login = trim(strtolower($_GET['login']));
-$q = "%".sanitize_input($login)."%";
+// ===================== SQL connect =====================
+$base = new PDO($dsn, $user, $pass, $opt);
+// ==================== /SQL connect =====================
 
-//echo "category: ".$category.";";
-//echo "login: ".$login.";";
-//echo "q: ".$q.";";
 
-$cat = '';
-$query = '';
-if ($category == 'login') {
-  $cat = "unique_id";
-  $query = 'unique_id LIKE upper(\''.strtoupper($q).'\')';
-} elseif ($category == 'firstname') {
-  $cat = "firstname";
-  $query = 'first_name LIKE upper(\''.strtoupper($q).'\')';
-} elseif ($category == 'lastname') {
-  $cat = "lastname";
-  $query = 'last_name LIKE upper(\''.strtoupper($q).'\')';
-} else {
-  echo ("ERROR");
-  exit();
-}
 
-$filtered = array (
-  "yes", "1", "0", "nvgfpmeilym", "no", "mr", "ms", "", " ", "   "
-);
-function filter_word($value) {
-  if ($value == null) return true;
-  return ! in_array(strtolower($value),$filtered); 
-}
+// ==================== new retrieval ======================
+$login = trim(strtolower($_GET['login']));
+$q = "%".sanitize_input($login)."%";
 
-$req = "SELECT unique_id, first_name, last_name,nb_keywords FROM scholars WHERE ".$query." GROUP BY unique_id";
-#echo "req: ".$req.";";
+$query = 'concat(first_name, " ", IFNULL(middle_name,""), " ", last_name) LIKE "%'.$q.'%"';
+
+$req = "SELECT doors_uid, email, first_name, last_name, keywords FROM comex_registrations WHERE ".$query." GROUP BY doors_uid";
+// echo "req: ".$req.";";
 $results = array();
 $i = 0;
 $res = $base->query($req);
 
-foreach ($res as $row) {
-  if ($row["nb_keywords"]==0){
-      $row["nb_keywords"]=' (no keyword to map)';
-  }  else {
-      $row["nb_keywords"]='';
-  }
-  array_push($results,$row);
-}
-
-$nbresults = sizeof($results);
-$results = array_slice($results,0,20);
-$nbresults2 = sizeof($results);
 $completion = array(
-   "results" => array()
+   "results" => array(),
+   "query" => $login
 );
 
-
-foreach($results as $row) {
+foreach($res as $row) {
      array_push($completion["results"], array(
-        'id' => $row["unique_id"],
+        'id' => $row["doors_uid"],
         'term' => $login,
         'firstname' => $row["first_name"],
-        'lastname' => $row["last_name"].$row["nb_keywords"],
-       'score' => 1,
-       
-	   // F*** it, I'll put the meta data here...
-       'category' => $cat,
-   "size" => $nbresults2,
-   "total" => $nbresults,
-   "remaining" => ($nbresults - $nbresults2)
+        'lastname' => $row["last_name"],
       ));
 }
-$i = 0;
+// ==================== /new retrieval =====================
+
+// $base = new PDO("sqlite:" . $dbname);
+//
+// $category = trim(strtolower($_GET['category']));
+// $login = trim(strtolower($_GET['login']));
+// $q = "%".sanitize_input($login)."%";
+//
+// //echo "category: ".$category.";";
+// //echo "login: ".$login.";";
+// //echo "q: ".$q.";";
+//
+// $cat = '';
+// $query = '';
+// if ($category == 'login') {
+//   $cat = "unique_id";
+//   $query = 'unique_id LIKE upper(\''.strtoupper($q).'\')';
+// } elseif ($category == 'firstname') {
+//   $cat = "firstname";
+//   $query = 'first_name LIKE upper(\''.strtoupper($q).'\')';
+// } elseif ($category == 'lastname') {
+//   $cat = "lastname";
+//   $query = 'last_name LIKE upper(\''.strtoupper($q).'\')';
+// } else {
+//   echo ("ERROR");
+//   exit();
+// }
+//
+// $filtered = array (
+//   "yes", "1", "0", "nvgfpmeilym", "no", "mr", "ms", "", " ", "   "
+// );
+// function filter_word($value) {
+//   if ($value == null) return true;
+//   return ! in_array(strtolower($value),$filtered);
+// }
+//
+// $req = "SELECT unique_id, first_name, last_name,nb_keywords FROM scholars WHERE ".$query." GROUP BY unique_id";
+// #echo "req: ".$req.";";
+// $results = array();
+// $i = 0;
+// $res = $base->query($req);
+//
+// foreach ($res as $row) {
+//   if ($row["nb_keywords"]==0){
+//       $row["nb_keywords"]=' (no keyword to map)';
+//   }  else {
+//       $row["nb_keywords"]='';
+//   }
+//   array_push($results,$row);
+// }
+//
+// $nbresults = sizeof($results);
+// $results = array_slice($results,0,20);
+// $nbresults2 = sizeof($results);
+// $completion = array(
+//    "results" => array()
+// );
+//
+//
+// foreach($results as $row) {
+//      array_push($completion["results"], array(
+//         'id' => $row["unique_id"],
+//         'term' => $login,
+//         'firstname' => $row["first_name"],
+//         'lastname' => $row["last_name"].$row["nb_keywords"],
+//        'score' => 1,
+//
+//        // TODO change: general metadata is uselessly copied x nbresults
+// 	   // F*** it, I'll put the meta data here...
+//        'category' => $cat,
+//    "size" => $nbresults2,
+//    "total" => $nbresults,
+//    "remaining" => ($nbresults - $nbresults2)
+//       ));
+// }
+// $i = 0;
 
 echo json_encode($completion);
-?>
\ No newline at end of file
+?>
-- 
2.21.0