Permalink
Browse files

all tests passing

  • Loading branch information...
1 parent 0e6f35f commit d0f5eba9b931f3bf60d105b4a6a5c1f7f19271b8 @karpet committed Oct 2, 2011
Showing with 180 additions and 33 deletions.
  1. +45 −12 lib/Dezi_Client.php
  2. +92 −15 lib/Dezi_Doc.php
  3. +40 −3 lib/Dezi_Response.php
  4. +3 −3 t/001-client.t
View
@@ -52,12 +52,13 @@ class Dezi_Client {
public $server;
public $search_uri;
public $index_uri;
+ public $last_response;
protected $VERSION = '0.001000';
/**
- * Constructor.
+ * Constructor. Returns a new Client.
*
- * @param unknown $args (optional)
+ * @param array $args (optional)
*/
public function __construct($args=array('server'=>'http://localhost:5000')) {
$this->server = $args['server'];
@@ -91,12 +92,15 @@ public function __construct($args=array('server'=>'http://localhost:5000')) {
/**
+ * index() - add or update a document in the index
*
+ * Expects either a string file, string in-memory document,
+ * or a Dezi_Doc object.
*
- * @param unknown $doc
- * @param unknown $uri (optional)
- * @param unknown $content_type (optional)
- * @return unknown
+ * @param string_or_object $doc
+ * @param string $uri (optional)
+ * @param string $content_type (optional)
+ * @return Dezi_HTTPResponse $resp
*/
public function index($doc, $uri=null, $content_type=null) {
$buf = null;
@@ -129,6 +133,7 @@ public function index($doc, $uri=null, $content_type=null) {
$content_type = Dezi_Doc::get_mime_type($uri);
}
+ //error_log("content_type=$content_type");
$pest = new Pest($this->index_uri);
$resp = $pest->post("/$uri", $buf, array('Content-Type: '.$content_type));
$http_resp = new Dezi_HTTPResponse();
@@ -142,21 +147,49 @@ public function index($doc, $uri=null, $content_type=null) {
/**
+ * search() - Fetch search results from a Dezi server.
*
+ * $params may be any key/value pair as described in Search::OpenSearch.
+ *
+ * Returns a Dezi_Response on success and 0 on failure. Check
+ * $client->last_response on failure.
+ *
+ * @param array $params
+ * @return Dezi_Response object
*/
- public function search() {
-
-
+ public function search($params) {
+ $pest = new PestJSON($this->search_uri);
+ $params['format'] = 'json'; // force response type
+ $query = http_build_query($params);
+ $resp = $pest->get("?$query");
+ $http_resp = new Dezi_HTTPResponse();
+ $http_resp->status = $pest->lastStatus();
+ $http_resp->content = $resp;
+ $this->last_response = $http_resp;
+ if ($pest->lastStatus() != '200') {
+ return 0;
+ }
+ $dezi_response = new Dezi_Response($resp);
+ return $dezi_response;
}
/**
+ * delete() - remove a document from the index.
*
+ * $uri should be the document URI.
+ *
+ * @param string $uri
+ * @return Dezi_HTTPResponse $resp
*/
- public function delete() {
-
-
+ public function delete($uri) {
+ $pest = new Pest($this->index_uri);
+ $resp = $pest->delete("/$uri");
+ $http_resp = new Dezi_HTTPResponse();
+ $http_resp->status = $pest->lastStatus();
+ $http_resp->content = $resp;
+ return $http_resp;
}
View
@@ -1,5 +1,30 @@
<?php
+/*
+
+Dezi_Doc - a Dezi client document
+
+SYNOPSIS
+
+ // add doc to the index
+ require 'Dezi_Doc.php';
+ $html = "<html>hello world</html>";
+ $doc = new Dezi_Doc(array(
+ 'mime_type' => 'text/html',
+ 'uri' => 'foo/bar.html',
+ 'mtime' => time(),
+ 'size' => length $html,
+ 'content' => $html,
+ ));
+ $client->index( $doc );
+
+ // search results are also Dezi_Doc objects
+ foreach ($response->results as $doc) {
+ printf("hit: %s %s\n", $doc->score, $doc->uri);
+ }
+
+*/
+
class Dezi_Doc {
public $mime_type;
@@ -12,6 +37,61 @@ class Dezi_Doc {
public $score;
protected $VERSION = '0.001000';
+ private static $mime_types = array(
+ 'txt' => 'text/plain',
+ 'htm' => 'text/html',
+ 'html' => 'text/html',
+ 'php' => 'text/html',
+ 'css' => 'text/css',
+ 'js' => 'application/javascript',
+ 'json' => 'application/json',
+ 'xml' => 'application/xml',
+ 'swf' => 'application/x-shockwave-flash',
+ 'flv' => 'video/x-flv',
+
+ // images
+ 'png' => 'image/png',
+ 'jpe' => 'image/jpeg',
+ 'jpeg' => 'image/jpeg',
+ 'jpg' => 'image/jpeg',
+ 'gif' => 'image/gif',
+ 'bmp' => 'image/bmp',
+ 'ico' => 'image/vnd.microsoft.icon',
+ 'tiff' => 'image/tiff',
+ 'tif' => 'image/tiff',
+ 'svg' => 'image/svg+xml',
+ 'svgz' => 'image/svg+xml',
+
+ // archives
+ 'zip' => 'application/zip',
+ 'rar' => 'application/x-rar-compressed',
+ 'exe' => 'application/x-msdownload',
+ 'msi' => 'application/x-msdownload',
+ 'cab' => 'application/vnd.ms-cab-compressed',
+
+ // audio/video
+ 'mp3' => 'audio/mpeg',
+ 'qt' => 'video/quicktime',
+ 'mov' => 'video/quicktime',
+
+ // adobe
+ 'pdf' => 'application/pdf',
+ 'psd' => 'image/vnd.adobe.photoshop',
+ 'ai' => 'application/postscript',
+ 'eps' => 'application/postscript',
+ 'ps' => 'application/postscript',
+
+ // ms office
+ 'doc' => 'application/msword',
+ 'rtf' => 'application/rtf',
+ 'xls' => 'application/vnd.ms-excel',
+ 'ppt' => 'application/vnd.ms-powerpoint',
+
+ // open office
+ 'odt' => 'application/vnd.oasis.opendocument.text',
+ 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
+ );
+
/**
@@ -27,28 +107,25 @@ public function __construct($args=array()) {
/**
+ * Static method. Returns MIME type string for a file.
*
- *
- * @param unknown $file
- * @return unknown
+ * @param string $file
+ * @return string mime_type
*/
- public function get_mime_type($file) {
- if (function_exists('finfo_open')) {
- $finfo = finfo_open(FILEINFO_MIME);
-
- if (!$finfo) {
- throw new Exception("Opening fileinfo database failed");
- }
-
- $types = finfo_file($finfo, $file);
+ public static function get_mime_type($file) {
+ $ext = strtolower(array_pop(explode('.', $file)));
+ //error_log("file=$file ext=$ext");
- return $types[0];
+ if (array_key_exists($ext, self::$mime_types)) {
+ //error_log("mime=".self::$mime_types[$ext]);
+ return self::$mime_types[$ext];
}
- elseif (function_exists('mime_content_type')) {
+ elseif (file_exists($file) && function_exists('mime_content_type')) {
+ //error_log("mime_content_type for $file");
return mime_content_type($file);
}
else {
- throw new Exception("No MIME content type detection support in this PHP version");
+ return 'application/octet-stream';
}
}
View
@@ -1,5 +1,33 @@
<?php
+/*
+
+Dezi_Response - Dezi search server response
+
+SYNOPSIS
+
+ require 'Dezi_Response.php';
+ $client = new Dezi_Client(array(
+ 'server' => 'http://localhost:5000'
+ ));
+
+ $response = $client->search(array( 'q' => 'foo' ));
+ // $response isa Dezi_Response
+
+ // iterate over results
+ foreach ($response->results as $doc) {
+ printf("--\n uri: %s\n title: %s\n score: %s\n",
+ $doc->uri, $doc->title, $doc->score);
+ }
+
+ # print stats
+ printf(" hits: %d\n", $response->total);
+ printf("search time: %s\n", $response->search_time);
+ printf(" build time: %s\n", $response->build_time);
+ printf(" query: %s\n", $response->query);
+
+*/
+
class Dezi_Response {
public $results;
@@ -12,13 +40,22 @@ class Dezi_Response {
protected $VERSION = '0.001000';
/**
+ * Constructor. Returns Dezi_Reponse object.
*
- *
- * @param unknown $args (optional)
+ * @param array $args (optional)
*/
public function __construct($args=array()) {
foreach ($args as $k=>$v) {
- $self->$k = $v;
+ $this->$k = $v;
+ }
+
+ // turn results into objects
+ if ($this->results) {
+ $docs = array();
+ foreach ($this->results as $r) {
+ $docs[] = new Dezi_Doc($r);
+ }
+ $this->results = $docs;
}
}
View
@@ -4,7 +4,7 @@
require_once 'TestMore.php';
set_include_path('../pest:.');
-plan(20);
+plan(16);
require_once 'lib/Dezi_Client.php';
@@ -24,14 +24,14 @@ ok( $resp = $client->index( $html_doc, 'foo/bar.html' ),
is( $resp->status, 200, "index scalar_ref success" );
// add/update a Dezi::Doc to the index
-$dezi_doc = new Dezi_Doc(array( uri => 't/test-dezi-doc.xml' ));
+$dezi_doc = new Dezi_Doc(array( 'uri' => 't/test-dezi-doc.xml' ));
$dezi_doc->content = file_get_contents( $dezi_doc->uri );
ok( $resp = $client->index($dezi_doc), "index Dezi_Doc" );
is( $resp->status, 200, "index Dezi_Doc success" );
// remove a document from the index
ok( $resp = $client->delete('foo/bar.html'), "delete foo/bar.html" );
-is( $resp->status, 200, "delete success" );
+is( $resp->status, 204, "delete success" );
// search the index
ok( $response = $client->search(array( 'q' => 'dezi' )), "search" );

0 comments on commit d0f5eba

Please sign in to comment.