Page MenuHomec4science

people_search.php
No OneTemporary

File Metadata

Created
Sat, Nov 9, 08:50

people_search.php

<?php
Class PeopleSearch {
protected $Http = null;
protected $base_url = null;
protected $base_params = null;
protected $results = null;
protected $body = null;
public function __construct() {
$this->Http = new HttpRequest();
$this->base_url = 'http://search.epfl.ch/psearch.action';
$this->base_params = array( 'request_locale' => 'en',
'q' => '',
'origin' => 'header' );
$headers = Array( 'Accept-Charset' => 'UTF-8' );
$this->Http->setHeaders($headers);
}
public function SetLocale($locale) {
if ($locale == 'fr' or $locale == 'en' ) {
$this->base_params['request_locale'] = $locale;
}
}
protected function SetStartUrl() {
$url = $this->base_url.'?'.http_build_str($this->base_params);
#echo $url."\n";
$this->Http->setUrl($url);
$this->results = null;
$this->body = null;
}
public function GetBody() {
return $this->body;
}
public function FindBySciper($sciper) {
if (! is_numeric($sciper)) throw new Exception('Provided SCIPER is not numeric.');
$this->base_params['q'] = $sciper;
$this->SetStartUrl();
$this->DoSearch();
return $this->results;
}
public function FindByName($name) {
$this->base_params['q'] = $name;
$this->SetStartUrl();
$this->DoSearch();
return $this->results;
}
protected function DoSearch() {
$final_url = null;
$redirected = false;
do {
$response = $this->Http->send();
if ($response->getResponseCode() != 301 && $response->getResponseCode() != 302) break;
$final_url = $response->getHeader("Location");
#echo $final_url."\n";
$redirected = true;
$this->Http->setUrl($final_url);
} while (1);
$this->body = $this->Http->getResponseBody();
$this->results = array('found' => (int)$redirected);
if ($this->results['found']) {
# Email
$matches = array();
preg_match("/msgto\('(.*)','(.*)'\)/", $this->body, $matches );
if (! empty($matches)) {
$this->results['email'] = $matches[1].'@'.$matches[2];
$matches = explode('.', $matches[1]);
$this->results['fname_email'] = $matches[0];
$this->results['lname_email'] = $matches[1];
} else {
$this->results['email'] = '';
$this->results['fname_email'] = '';
$this->results['lname_email'] = '';
}
# Full names
$matches = array();
preg_match("~\<h4\>(.*)\</h4\>~", $this->body, $matches );
$matches = explode('&nbsp;', $matches[1]);
$this->results['fname_full'] = $matches[0];
$this->results['lname_full'] = $matches[1];
if (empty($this->results['email'])) {
$this->results['fname_email'] = $this->results['fname_full'];
$this->results['lname_email'] = $this->results['lname_full'];
}
# Compute usual names
if (! empty($this->results['email'])) {
$fnames = explode(' ', $this->results['fname_full']);
$i = 0;
$this->results['fname_usual'] = '';
while (strlen($this->results['fname_usual']) < strlen($this->results['fname_email']) and (array_key_exists($i, $fnames))) {
if ($i > 0) $this->results['fname_usual'] .= ' ';
$this->results['fname_usual'] .= $fnames[$i];
$i++;
}
$lnames = explode(' ', $this->results['lname_full']);
$i = 0;
$this->results['lname_usual'] = '';
while (strlen($this->results['lname_usual']) < strlen($this->results['lname_email']) and (array_key_exists($i, $lnames))) {
if ($i > 0) $this->results['lname_usual'] .= ' ';
$this->results['lname_usual'] .= $lnames[$i];
$i++;
}
} else {
$this->results['fname_usual'] = $this->results['fname_full'];
$this->results['lname_usual'] = $this->results['lname_full'];
}
# Compute complete name
$this->results['cname_full'] = $this->results['fname_full'].' '.$this->results['lname_full'];
$this->results['cname_usual'] = $this->results['fname_usual'].' '.$this->results['lname_usual'];
# Compute complete reversed
$this->results['cname_full_reversed'] = $this->results['lname_full'].' '.$this->results['fname_full'];
$this->results['cname_usual_reversed'] = $this->results['lname_usual'].' '.$this->results['fname_usual'];
# Sciper
preg_match("/vCard\?id=([0-9]+)&/", $this->body, $matches);
$this->results['SCIPER'] = $matches[1];
# Section
preg_match('/a title="(.*)" href=.*>(.*)</', $this->body, $matches);
$this->results['section_description'] = $matches[1];
$this->results['section_acronym_full'] = $matches[2];
$matches = explode('-', $matches[2]);
$this->results['section'] = $matches[0];
if (array_key_exists(1, $matches)) {
$this->results['section_extended'] = $matches[1];
}
}
}
}
class BashWriter {
public function __construct($array = null) { if (!is_null($array)) $this->format($array); }
public function format($array) { if (is_array($array)) foreach($array as $key => $value) echo "$key:$value\n"; }
}
?>

Event Timeline