xml-xindice-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jill Rhoads" <j...@rhoads.nu>
Subject RE: PHP Xindice Classes and Xindice 1.1b1?
Date Mon, 17 Nov 2003 13:37:07 GMT
Here ya go!  If you or anyone has questions or suggestions, I would be
more than happy to take them.  (jill (at) rhoads dot (nu)) As you can see,
it's not 100% done, but I can post the class again when I get a better
handle on Indexers and Meta data.  For now, it should give you most of
what you need.

/Jill

<?php

/**
 * Time-stamp: <2003-11-17 14:31:50 jrhoads>
 * Connection class for Xindice 1.1 b
 * @author Jill Rhoads
 * @copyright ©2003 Use at your own risk
 * @version Beta 0.9
 * @package Xindice
 *
 * TODO:
 *  Indexer functions
 *  function GetCollectionMeta(){}
 *  function SetCollectionMeta(){}
 *  function SetDocument(){}
 *  function SetDocumentMeta(){}
 *  maybe more sophisticated error handling
 *
 */

class Xindice {
  var $host;
  var $url;
  var $port;
  var $debug;


  /**
   * Class constructor for Xindice
   *
   * @var $host ex. localhost
   * @var $url  ex. '/xindice'
   * @var $port  ex. 8888
   *
   * ex.
   * $xi = new Xindice();
  */

  function Xindice($host='localhost', $url='/xindice', $port='8888'){
    $this->host = $host;
    $this->port = $port;
    $this->url = $url;
  }


   /**
   * Sets debug level
   *
   * @var $debug level of debug: 0 for no debug > 0 for debug
   *
   * ex.
   * $xi->setDebug();
   */
  function setDebug($debug=1){
    $this->debug = $debug;
  }

  /**
   * Version functions
   */

   /**
   * Gets server version
   *
   * @return string containing server version text ex. 'Xindice 1.1b2
(Birthday+.1)'
   *
   * ex.
   * echo $xi->GetServerVersion();
   */
  function GetServerVersion(){
    $params = array ("message"=>"GetServerVersion");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

   /**
   * Gets api version
   *
   * @return string containing api version text ex. 'Xindice XML-RPC 0.1'
   *
   * ex.
   * echo $xi->GetApiVersion();
   */
  function GetApiVersion(){
    $params = array ("message"=>"GetApiVersion");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  /*
   * Returns a new  Universal Object Identifier
   *
   * ex.
   * echo $xi->CreateNewOID($collection);
   */
  function CreateNewOID($collection){
    $params = array ( "collection"=>$collection,
		      "message"=>"CreateNewOID");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  /**
   * Queries database with either XPath or XQuery
   *
   * @var $collection
   * @var $query
   * @ var $type
   * @return XML from the database
   *
   * ex.
   * echo $xi->Query($collection, '/catalog/cd[price>10.80]');
   */
  function Query($collection, $query, $type='XPath'){
    // Gosh this is ugly, but it turns out that Xindice requires an empty
    // namespace stuct - thanks to Donald Lachan's info in the mailing list
    // http://marc.theaimsgroup.com/?l=xindice-users&m=103909310311743&w=2
    $empty = array(''=>'');
    $params = array ("collection"=> $collection,
		     "type"      => $type,
		     "query"     => $query,
		     "namespaces" => $empty,
		     "message"   => "Query");

    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  /*
   * Indexer Functions
   */

  // TODO
  function CreateIndexer($collection){}

  function RemoveIndexer($collection){}

  function ListIndexers($collection){
    $params = array ( "collection"=>$collection,
		      "message"=>"ListIndexers");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  function UpdateIndexer(){ }
  function GetIndexerConfiguration(){ }

  /**
   * Collection Functions
   */

  /**
   * Lists collections
   *
   * @var $collections string variable containing collection name\
   * you want to get subcollections from ex. '/db/test'
   * *
   * @var $collection
   * @return array with names of collections
   *
   * ex.
   * foreach ($xi->listCollections("/db") as $value){
   *      echo $value."\n";
   * }
   */
  function ListCollections($collection='/db'){
    $params = array ( "collection"=>$collection,
		      "message"=>"ListCollections");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  /**
   * Create a collection
   *
   * @var $collections string variable containing collection name\
   * you want to get subcollections from ex. '/db/test'
   * @var $name name of the collection to be created
   *
   * @return boolean if the collection has been sucessfully created
   *
   * ex.
   *  if ( $xi->CreateCollection('/db', 'test') )
   *       echo "Collection Created";
   *    else
   *       echo "Could not Create Collection";
   */
  function CreateCollection($collection, $name){
    $params = array ( "collection"=>$collection,
		      "name" => $name,
		      "message"=>"CreateCollection");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    if ($response['faultString'])
      return FALSE; //$response['faultString'];

    return TRUE; //$response['result'];
  }

   /**
   * Remove a collection
   *
   * @var $collections string variable containing collection name\
   * you want to get subcollections from ex. '/db/test'
   * @var $name name of the collection to be created
   *
   * @return boolean if the collection has been sucessfully created
   *
   * ex.
   *  if ( $xi->RemoveCollection('/db', 'test') )
   *       echo "Collection Removed";
   *    else
   *       echo "Could not Remove Collection";
   */
  function RemoveCollection($collection, $name){
    $params = array ( "collection"=>$collection,
		      "name" => $name,
		      "message"=>"RemoveCollection");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    if (trim($response['result']) =='no')
      return FALSE; //$response['faultString'];

    return TRUE; //$response['result'];
  }

  // TODO
  function GetCollectionMeta(){}
  function SetCollectionMeta(){}

  /*
   * Document Functions
   */

  /*
   * Lists documents in a collection
   *
   * @var $collection the name of the collection
   * @returns an array with the names of the documents
   */
  function ListDocuments($collection){
    $params = array ( "collection"=>$collection,
		      "message"=>"ListDocuments");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  /*
   * Gets a document in a collection
   *
   * @var $collection the name of the collection
   * @returns the XML document
   */
  function GetDocument($collection, $name){
    $params = array ("collection"=>$collection,
		     "name"=>$name,
		     "message"=>"GetDocument");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  /*
   * Inserts a document into the collection
   *
   * @var $collection the name of the collection
   * @var $document variable that contains the XML data
   * @var $name desired name of the document
   * @returns boolean
   */
  // seems not to care if two documents have same name,
  // must be primitive update functionality
  function InsertDocument($collection, $document, $name=''){
    $params = array ("collection"=>$collection,
		     "name"=>$name,
		     "document"=>$document,
		     "message"=>"InsertDocument");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

   /*
   * Removed a document into the collection
   *
   * @var $collection the name of the collection
   * @var $name document to be removed
   * @returns boolean
   */
  function RemoveDocument($collection, $name){
    $params = array ("collection"=>$collection,
		     "name"=>$name,
		     "message"=>"RemoveDocument");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    if ($response['faultString'])
      return FALSE; //$response['faultString'];

    return TRUE; //$response['result'];
  }

   /*
   * Returns information about a document
   *
   * @var $collection the name of the collection
   * @var $name document name
   * @returns XML data
   */
  function GetDocumentMeta($collection, $name){
    $params = array ("collection"=>$collection,
		     "name"=>$name,
		     "message"=>"GetDocumentMeta");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

   /*
   * Returns number of documents in a collection
   *
   * @var $collection the name of the collection
   * @returns integer
   */
  function GetDocumentCount($collection='/db'){
    $params = array ( "collection"=>$collection,
		      "message"=>"GetDocumentCount");
    $xml = xmlrpc_encode_request("run", $params);

    $response = $this->send($xml);

    return $response['result'];
  }

  // TODO
  function SetDocument(){}
  function SetDocumentMeta(){}

  /**
   * Communication functions
   */

  /**
   * @access private
   */
  // Sends XML-RPC over HTTP to the server and recieves the XML-RPC and
decodes it
  // returns the decoded PHP variable
  //
  function send($xml){
    $header  = "POST " .$this->url  . " HTTP/1.0\n";
    $header .= "User-Agent: simple\n";
    $header .= "Host: " . $host  . "\n";
    // if ($this->username != "") {
//       $header .= "Authorization: Basic " . base64_encode($username .
":" . $password) . "\n";
//     }
    $header .= "Content-Type: text/xml\n";
    $header .= "Content-Length: " . strlen($xml) . "\n\n";

    $message = $header.$xml;

    $fp = fsockopen($this->host, $this->port, $errno, $errstr, 0);

    if (!$fp) {
      //echo ("Fsocks error: $errno, $errstr");
      die();
    } else {

      if ($this->debug){
	echo "Sending XML:\n";
	echo $xml."\n";
      }

      fputs($fp, $message, strlen($message));

      while (!feof($fp)) {
	$received .=fgets ($fp,4096);
      }

      fclose($fp);
    }

    $xml=(substr($received, strpos($received, "\r\n\r\n")+4));
    $phpvars = xmlrpc_decode($xml);

    if ($this->debug){
      echo "Received XML:\n";
      echo $xml."\n";
    }

    return $phpvars;
  }
}
?>

Don Stocks said:
> I would LOVE a working PHP Xindice class.  Even though it's not too
difficult to put one together, I would much rather put my attention
towards my core project.  If you get something working please post it.
I
> would really appreciate it.
>
>  Thanks, Don
>
> Jill Rhoads <jill@rhoads.nu> wrote:
> You'll have to excuse me for not being able to directly follow your
thread, but I just joined the mailing list.
>
> It seems that the XMLRPC calls have changed significantly between
versions. Not to mention PHP has just baked a XMLRPC implementation into
the latest version. As a result, I have yet to find a working PHP
implementation of a Xindice class; however, it's not too difficult to
create your own (or wait about 4 days until I finish mine) using the new
libraries.
>
> My question is if the xindice project is interested in a php class
library? I was planning on make a browser with PHP. If the project wants
it, I'm willing to post it along with a tutorial.
>
> /Jill
>
> Here's some of my proof-of-concept (really fugly) sample code:
>
>
> $host = "localhost";
> $url = "/xindice";
> $params = array ( "collection"=>"/db","message"=>"ListCollections");
>
> $response = xmlrpc_encode_request("run", $params);
>
> $header = "POST " .$url . " HTTP/1.0\n";
> $header .= "User-Agent: simple\n";
> $header .= "Host: " . $host . "\n";
> if ($this->username != "") {
> $header .= "Authorization: Basic " . base64_encode($username . ":" .
$password) . "\n";
> }
> $header .= "Content-Type: text/xml\n";
> $header .= "Content-Length: " . strlen($response) . "\n\n";
>
> $whole = $header.$response;
>
> echo ($whole);
>
> $fp = fsockopen($host, 8888, $errno, $errstr, 0);
>
> if (!$fp) {
> echo ("Fsocks error: $errno, $errstr");
> die();
> } else {
> fputs($fp, $whole, strlen($whole));
>
> while (!feof($fp)) {
> $received .=fgets ($fp,128);
> }
>
> fclose($fp);
> }
>
> echo $received;
>
> $xml=(substr($received, strpos($received, "\r\n\r\n")+4));
> echo $xml;
>
>
> //$phpvars = xmlrpc_decode_request($xml, $method);
>
> $phpvars = xmlrpc_decode($xml);
>
> //echo $method;
> echo ($phpvars."\n");
>
> foreach ($phpvars as $key => $value){
> echo $key."=>".$value."\n";
> foreach ($value as $key2 => $value2){
> echo $key2."=>".$value2."\n";
> }
> }
>
>
>
> ?>
>
>
> -------------------------------
> I have been trying to get the PHP Xidice class from \
> http://phpxmlclasses.sourceforge.net/ to work with Xindice 1.1b1. I can
make a \
> connection to the server no problem. But when I make an XML-RPC call I
always get \
> back the following 404 error message (see message below). No matter what
method I \
> call I always get the same 404 error. Even when I try to create a
collection or list \
> collections.
> I have a feeling that this has something to do with the change in the
XML-RPC syntax \
> between Xindice 1.0 and v. 1.1b1. But that's just my guess.
> Are there any suggestions for getting this class working with 1.1b1? Or
better yet, \
> is there a resource out there for a PHP Class that works with 1.1b1. Or
maybe I'm \
> just doing something wrong. After all, I'm a rookie at this!
> Any help would be appreciated. I would really like to work with Xindice
using PHP.
>
> Thanks, Don
>
> Here's the message I'm getting:
>
> ---GOT---
> HTTP/1.1 404 %2F+Not+Found
> Date: Tue, 11 Nov 2003 06:29:18 GMT
> Server: Jetty/4.2.8 (Windows 2000/5.0 x86 java/1.4.1_05)
> Content-Type: text/html
> Content-Length: 1104
>
>
>
>
> HTTP ERROR: 404 / Not Found
> RequestURI=/
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Protect your identity with Yahoo! Mail AddressGuard




Mime
View raw message