return 1;
        }
    }
    return 0;
}
$client = new Everyman\Neo4j\Client('localhost', 7474);
$client->getTransport()->setAuth('neo4j', 'muresearch');
$q = "match (u:Person) return u.name as name";
$query = new Query($client, $q);
$result = $query->getResultSet();
$mysqli = new mysqli(HOSTNAME, USERNAME, PASSWD, DATABASE);
if ($mysqli->connect_errno) {
    die("error: " . $mysqli->connect_error);
}
$localTable = "MOSpacePeople";
$transaction = $client->beginTransaction();
foreach ($result as $r) {
    $parser = new HumanNameParser_Parser($r['name']);
    $last = $parser->getLast();
    $first = $parser->getFirst();
    //@Debug
    echo "Checking " . $last . ", " . $first . ".....";
    $q = "SELECT * from " . $localTable . " where firstname='" . $mysqli->real_escape_string($first) . "' and lastname='" . $mysqli->real_escape_string($last) . "'";
    $fromMU = 0;
    $isProfessor = 0;
    if ($result = $mysqli->query($q)) {
        if ($result->num_rows == 1) {
            //@Debug
            echo "found in local database.....";
            $fromMU = 1;
            if ($resAssocArray = $result->fetch_assoc()) {
 public function insertDB($host, $port, $username, $auth, $custom_data = null)
 {
     require 'vendor/autoload.php';
     $client = new Everyman\Neo4j\Client($host, $port);
     $client->getTransport()->setAuth($username, $auth);
     if (is_null($custom_data)) {
         $values = $this->crawlPubmed();
     } else {
         $values = $custom_data;
     }
     //$values = array_merge($this->crawlPubmed(), $this->crawlIEEE());
     //print_r($values);
     //return;
     if (!is_array($values) || !isset($values[0])) {
         print_r($values);
         die("something is wrong with the values");
     }
     $transaction = $client->beginTransaction();
     $neo4jQueryArray = array();
     //$queryForCentralPerson = "merge (u:Person {name: \"".$this->lastName.", ".$this->firstName."\"}) set u.affiliation = \"".$this->affiliation."\"";
     //$query = new Query($client, $queryForCentralPerson);
     //$result = $transaction->addStatements($query);
     foreach ($values as $one) {
         //print_r($one) has the format:
         //[17] => Array
         //(
         //	[title] => RNA-protein distance patterns in ribosomes reveal the mechanism of translational attenuation.
         //  [url] => http://www.ncbi.nlm.nih.gov/pubmed/25326828
         //  [people] => Array
         //  (
         //	  [0] => Array
         //	  (
         //		[firstName] => DongMei
         //      [lastName] => Yu
         //      [affiliation] => Department of Biological Engineering, University of Missouri, Columbia, MO, 65211, USA.
         //    )
         //    [1] => Array
         //    (
         //	     [firstName] => Chao
         //       [lastName] => Zhang
         //       [affiliation] =>
         //    )
         //  )
         //)
         array_key_exists('title', $one) && !empty($one['title']) ? $articleTitle = $one['title'] : die("why the title has not been set?");
         if (!array_key_exists('url', $one)) {
             $articleURL = "";
         } else {
             $articleURL = $one['url'];
         }
         array_key_exists('people', $one) && is_array($one['people']) && isset($one['people'][0]) ? $articleAuthorList = $one['people'] : die("People list might be wrong");
         $queryToCreatePublication = "merge (paper:Publication {title: \"" . addslashes($articleTitle) . "\"}) set paper.url = \"" . $articleURL . "\"";
         $neo4jQueryArray[] = new Query($client, $queryToCreatePublication);
         //$result = $transaction->addStatements($query);
         //$queryToCreateWroteRelation = "match (u:Person {name: \"". $this->lastName.", ".$this->firstName."\"}), (p:Publication {title: \"".addslashes($theTitle)."\"}) create unique (u)-[r:Wrote]->(p)";
         //echo $queryToCreateWroteRelation."\n";
         //$query = new Query($client, $queryToCreateWroteRelation);
         //$result = $transaction->addStatements($query);
         foreach ($articleAuthorList as $p) {
             if (!is_array($p) || !array_key_exists('firstName', $p) || !array_key_exists('lastName', $p) || !array_key_exists('affiliation', $p)) {
                 die("Ariticle Author List format Error");
             }
             if (empty($p['firstName']) || empty($p['lastName'])) {
                 continue;
             }
             if (empty($p['affiliation'])) {
                 $queryToCreatePerson = "merge (author:Person {name: \"" . $p['lastName'] . ", " . $p['firstName'] . "\"})";
             } else {
                 $queryToCreatePerson = "merge (author:Person {name: \"" . $p['lastName'] . ", " . $p['firstName'] . "\"}) set author.affiliation = \"" . addslashes($p['affiliation']) . "\"";
             }
             $neo4jQueryArray[] = new Query($client, $queryToCreatePerson);
             $queryToCreateWroteRelation = "match (u:Person {name: \"" . $p['lastName'] . ", " . $p['firstName'] . "\"}), (p:Publication {title: \"" . addslashes($articleTitle) . "\"}) create unique (u)-[r:Wrote]->(p)";
             $neo4jQueryArray[] = new Query($client, $queryToCreateWroteRelation);
             $queryToAdjustNumOfPublication = "match (u:Person {name: \"" . $p['lastName'] . ", " . $p['firstName'] . "\"}) - [:Wrote] - (p) with u, count(distinct p) as num set u.publicationNum = num";
             $neo4jQueryArray[] = new Query($client, $queryToAdjustNumOfPublication);
             //addMOProperty($p['lastName'].", ".$p['firstName'], $client);
             //should be after transition->commit()
         }
         for ($i = 0; $i < sizeof($articleAuthorList); $i++) {
             for ($j = $i + 1; $j < sizeof($articleAuthorList); $j++) {
                 $queryToCreateCoauthoredRelation = "match (p:Publication), (u1:Person {name: \"" . $articleAuthorList[$i]['lastName'] . ", " . $articleAuthorList[$i]['firstName'] . "\"}), (u2:Person {name: \"" . $articleAuthorList[$j]['lastName'] . ", " . $articleAuthorList[$j]['firstName'] . "\"})\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\twhere (u1)-[:Wrote]->(p) AND (u2)-[:Wrote]->(p) with count(p) as n\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tmatch (u1:Person {name: \"" . $articleAuthorList[$i]['lastName'] . ", " . $articleAuthorList[$i]['firstName'] . "\"}), (u2:Person {name: \"" . $articleAuthorList[$j]['lastName'] . ", " . $articleAuthorList[$j]['firstName'] . "\"})\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tcreate unique (u1)-[r:COAUTHORED]-(u2) set r.numOfPapers = n";
                 //echo $queryToCreateCoauthoredRelation."\n";
                 $neo4jQueryArray[] = new Query($client, $queryToCreateCoauthoredRelation);
                 //$result = $query->getResultSet();
                 //$result = $transaction->addStatements($query);
             }
         }
     }
     $transaction->addStatements($neo4jQueryArray, true);
     foreach ($values as $one) {
         array_key_exists('people', $one) && is_array($one['people']) && isset($one['people'][0]) ? $articleAuthorList = $one['people'] : die("People list might be wrong");
         foreach ($articleAuthorList as $p) {
             if (!is_array($p) || !array_key_exists('firstName', $p) || !array_key_exists('lastName', $p) || !array_key_exists('affiliation', $p)) {
                 die("Ariticle Author List format Error");
             }
             if (empty($p['firstName']) || empty($p['lastName'])) {
                 continue;
             }
             addMOProperty($p['lastName'] . ", " . $p['firstName'], $client);
         }
     }
 }