Author: dr Date: Tue Feb 19 15:03:56 2008 New Revision: 7414 Log: - Work-in-progress of Search prototype.
Added: trunk/Search/src/abstraction/ trunk/Search/src/abstraction/query.php (with props) trunk/Search/src/abstraction/query_find.php (with props) trunk/Search/src/abstraction/query_index.php (with props) trunk/Search/src/exceptions/ trunk/Search/src/exceptions/can_not_connect.php (with props) trunk/Search/src/exceptions/exception.php (with props) trunk/Search/src/handlers/ trunk/Search/src/handlers/solr.php (with props) trunk/Search/src/interfaces/ trunk/Search/src/interfaces/definition_manager.php (with props) trunk/Search/src/interfaces/handler.php (with props) trunk/Search/src/interfaces/index_handler.php (with props) trunk/Search/src/managers/ trunk/Search/src/managers/code_manager.php (with props) trunk/Search/src/search_autoload.php (with props) trunk/Search/src/search_result.php (with props) trunk/Search/src/search_session.php (with props) trunk/Search/src/search_session_instance.php (with props) trunk/Search/src/structs/ Added: trunk/Search/src/abstraction/query.php ============================================================================== --- trunk/Search/src/abstraction/query.php (added) +++ trunk/Search/src/abstraction/query.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,57 @@ +<?php +/** + * File containing the ezcSearchQuery class. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * The ezcSearchQuery class provides the common API for all Query objects. + * + * Subclasses should provide functionality to build an actual query. + * + * @package Search + * @version //autogentag// + * @private + */ +abstract class ezcSearchQuery +{ + /** + * A pointer to the search handler to use for this query. + * + * @var ezcSearchHandler + */ + protected $handler; + + /** + * Constructs a new ezcSearchQuery that works on the handler $handler. + * + * @param ezcSearchHandler $handler + */ + public function __construct( ezcSearchHandler $handler ) + { + $this->handler = $handler; + } + + /** + * Return query string. + * + * @return string + */ + public function __toString() + { + return $this->getQuery(); + } + + /** + * Returns the query string for this query object. + * + * @throws ezcSearchQueryInvalidException if it was not possible to build a valid query. + * @return string + */ + abstract public function getQuery(); +} +?> Propchange: trunk/Search/src/abstraction/query.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/abstraction/query_find.php ============================================================================== --- trunk/Search/src/abstraction/query_find.php (added) +++ trunk/Search/src/abstraction/query_find.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,303 @@ +<?php +/** + * File containing the ezcSearchFindQuery class. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Class to create select search backend indepentent search queries. + * + * @package Search + * @version //autogentag// + * @mainclass + */ +class ezcSearchFindQuery extends ezcSearchQuery +{ + /** + * Sort the result ascending. + */ + const ASC = 'ASC'; + + /** + * Sort the result descending. + */ + const DESC = 'DESC'; + + /** + * Stores the SELECT part of the SQL. + * + * Everything from 'SELECT' until 'FROM' is stored. + * @var string + */ + protected $selectString = null; + + /** + * Stores the FROM part of the SQL. + * + * Everything from 'FROM' until 'WHERE' is stored. + * @var string + */ + protected $fromString = null; + + /** + * Stores the WHERE part of the SQL. + * + * Everything from 'WHERE' until 'GROUP', 'LIMIT', 'ORDER' or 'SORT' is stored. + * @var string + */ + protected $whereString = null; + + /** + * Stores the FACET part of SQL + * + * @var string + */ + protected $facetString = null; + + /** + * Stores the ORDER BY part of the SQL. + * + * @var string + */ + protected $orderString = null; + + /** + * Stores the LIMIT part of the SQL. + * + * @var string + */ + protected $limitString = null; + + /** + * Stores the name of last invoked SQL clause method. + * + * Could be 'select', 'from', 'where', 'group', 'having', 'order', 'limit' + * @var string + */ + protected $lastInvokedMethod = null; + + /** + * Constructs a new ezcSearchQuery object. + * + * @param ezcSearchHandler $handler a pointer to the search backend + */ + public function __construct( ezcSearchHandler $handler ) + { + parent::__construct( $handler ); + } + + /** + * Resets the query object for reuse. + * + * @return void + */ + public function reset() + { + $this->selectString = null; + $this->fromString = null; + $this->whereString = null; + $this->facetString = null; + $this->orderString = null; + $this->limitString = null; + $this->lastInvokedClauseMethod = null; + } + + /** + * Opens the query and selects which columns you want to return with + * the query. + * + * select() accepts an arbitrary number of parameters. Each parameter + * must contain either the name of a column or an array containing + * the names of the columns. + * Each call to select() appends columns to the list of columns that will be + * used in the query. + * + * Example: + * <code> + * $q->select( 'column1', 'column2' ); + * </code> + * The same could also be written + * <code> + * $columns[] = 'column1'; + * $columns[] = 'column2; + * $q->select( $columns ); + * </code> + * or using several calls + * <code> + * $q->select( 'column1' )->select( 'column2' ); + * </code> + * + * Each of above code produce SQL clause 'SELECT column1, column2' for the query. + * + * @throws ezcQueryVariableParameterException if called with no parameters.. + * @param string|array(string) $... Either a string with a column name or an array of column names. + * @return ezcQuery returns a pointer to $this. + */ + public function select() + { + if ( $this->selectString == null ) + { + $this->selectString = 'SELECT '; + } + + $args = func_get_args(); + $cols = self::arrayFlatten( $args ); + + if ( count( $cols ) < 1 ) + { + throw new ezcQueryVariableParameterException( 'select', count( $args ), 1 ); + } + $this->lastInvokedMethod = 'select'; + $cols = $this->getIdentifiers( $cols ); + + // glue string should be inserted each time but not before first entry + if ( ( $this->selectString !== 'SELECT ' ) && + ( $this->selectString !== 'SELECT DISTINCT ' ) ) + { + $this->selectString .= ', '; + } + + $this->selectString .= join( ', ', $cols ); + return $this; + } + + /** + * Adds a where clause with logical expressions to the query. + * + * where() accepts an arbitrary number of parameters. Each parameter + * must contain a logical expression or an array with logical expressions. + * If you specify multiple logical expression they are connected using + * a logical and. + * + * Multiple calls to where() will join the expressions using a logical and. + * + * Example: + * <code> + * $q->select( '*' )->from( 'table' )->where( $q->expr->eq( 'id', 1 ) ); + * </code> + * + * @throws ezcQueryVariableParameterException if called with no parameters. + * @param string|array(string) $... Either a string with a logical expression name + * or an array with logical expressions. + * @return ezcQuerySelect + */ + public function where() + { + if ( $this->whereString == null ) + { + $this->whereString = 'WHERE '; + } + + $args = func_get_args(); + $expressions = self::arrayFlatten( $args ); + if ( count( $expressions ) < 1 ) + { + throw new ezcQueryVariableParameterException( 'where', count( $args ), 1 ); + } + + $this->lastInvokedMethod = 'where'; + + // glue string should be inserted each time but not before first entry + if ( $this->whereString != 'WHERE ' ) + { + $this->whereString .= ' AND '; + } + + $this->whereString .= join( ' AND ', $expressions ); + return $this; + } + + + /** + * Returns SQL that limits the result set. + * + * $limit controls the maximum number of rows that will be returned. + * $offset controls which row that will be the first in the result + * set from the total amount of matching rows. + * + * Example: + * <code> + * $q->select( '*' )->from( 'table' ) + * ->limit( 10, 0 ); + * </code> + * + * LIMIT is not part of SQL92. It is implemented here anyway since all + * databases support it one way or the other and because it is + * essential. + * + * @param string $limit integer expression + * @param string $offset integer expression + * @return ezcQuerySelect + */ + public function limit( $limit, $offset = '' ) + { + if ( $offset === '' ) + { + $this->limitString = "LIMIT {$limit}"; + } + else + { + $this->limitString = "LIMIT {$limit} OFFSET {$offset}"; + } + $this->lastInvokedMethod = 'limit'; + + return $this; + } + + /** + * Returns SQL that orders the result set by a given column. + * + * You can call orderBy multiple times. Each call will add a + * column to order by. + * + * Example: + * <code> + * $q->select( '*' )->from( 'table' ) + * ->orderBy( 'id' ); + * </code> + * + * @param string $column a column name in the result set + * @param string $type if the column should be sorted ascending or descending. + * you can specify this using ezcQuerySelect::ASC or ezcQuerySelect::DESC + * @return ezcQuery a pointer to $this + */ + public function orderBy( $column, $type = self::ASC ) + { + $string = $this->getIdentifier( $column ); + if ( $type == self::DESC ) + { + $string .= ' DESC'; + } + if ( $this->orderString == '' ) + { + $this->orderString = "ORDER BY {$string}"; + } + else + { + $this->orderString .= ", {$string}"; + } + $this->lastInvokedMethod = 'order'; + + return $this; + } + + /** + * Returns the complete select query string. + * + * This method uses the build methods to build the + * various parts of the select query. + * + * @todo add newlines? easier for debugging + * @throws ezcQueryInvalidException if it was not possible to build a valid query. + * @return string + */ + public function getQuery() + { + } +} + +?> Propchange: trunk/Search/src/abstraction/query_find.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/abstraction/query_index.php ============================================================================== --- trunk/Search/src/abstraction/query_index.php (added) +++ trunk/Search/src/abstraction/query_index.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,24 @@ +<?php +/** + * File containing the ezcSearchQueryIndex class. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Class to create select database independent INSERT queries. + * + * @package Search + * @version //autogentag// + * @private + */ +class ezcSearchQueryInsert extends ezcSearchQuery +{ + public function getQuery() + { + } +} +?> Propchange: trunk/Search/src/abstraction/query_index.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/exceptions/can_not_connect.php ============================================================================== --- trunk/Search/src/exceptions/can_not_connect.php (added) +++ trunk/Search/src/exceptions/can_not_connect.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,32 @@ +<?php +/** + * File containing the ezcSearchCanNotConnectException class. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * This exception is thrown when no connection can be made against a search backend. + * + * @package Search + * @version //autogentag// + */ +class ezcSearchCanNotConnectException extends ezcSearchException +{ + /** + * Constructs an ezcSearchCanNotConnectException for type $type at location $location + * + * @param string $type + * @param string $location + * @return void + */ + public function __construct( $type, $location ) + { + $message = "Could not connect to '$type' at '$location'."; + parent::__construct( $message ); + } +} +?> Propchange: trunk/Search/src/exceptions/can_not_connect.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/exceptions/exception.php ============================================================================== --- trunk/Search/src/exceptions/exception.php (added) +++ trunk/Search/src/exceptions/exception.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,30 @@ +<?php +/** + * File containing the ezcSearchException class. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * This class provides the base exception for exception in the Search component. + * + * @package Search + * @version //autogentag// + */ +abstract class ezcSearchException extends ezcBaseException +{ + /** + * Constructs an ezcSearchException + * + * @param string $message + * @return void + */ + public function __construct( $message ) + { + parent::__construct( $message ); + } +} +?> Propchange: trunk/Search/src/exceptions/exception.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/handlers/solr.php ============================================================================== --- trunk/Search/src/handlers/solr.php (added) +++ trunk/Search/src/handlers/solr.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,194 @@ +<?php +/** + * File containing the ezcSearchSolrHandler class. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Solr backend implementation + * + * @package Search + * @version //autogentag// + */ +class ezcSearchSolrHandler implements ezcSearchHandler, ezcSearchIndexHandler +{ + /** + * Holds the connection to Solr + * + * @var resource(stream) + */ + public $connection; + + public $host; + public $port; + public $location; + + /** + * Creates a new Solr handler connection + * + * @param string + */ + public function __construct( $host = 'localhost', $port = 8983, $location = '/solr' ) + { + $this->host = $host; + $this->port = $port; + $this->location = $location; + $this->connection = @stream_socket_client( "tcp://{$this->host}:{$this->port}" ); + if ( !$this->connection ) + { + throw new ezcSearchCanNotConnectException( 'solr', "http://{$this->host}:{$this->port}/{$this->location}" ); + } + + } + + public function getLine( $maxLength = false ) + { + $line = ''; $data = ''; + while ( strpos( $line, "\n" ) === false ) + { + $line = fgets( $this->connection, $maxLength ? $maxLength + 1: 512 ); + + /* If solr aborts the connection, fgets() will + * return false. We need to throw an exception here to prevent + * the calling code from looping indefinitely. */ + if ( $line === false ) + { + $this->connection = null; + throw new ezcSearchNetworkException( 'Could not read from the stream. It was probably terminated by the host.' ); + } + + $data .= $line; + if ( strlen( $data ) >= $maxLength ) + { + break; + } + } + return $data; + } + + public function sendRawGetCommand( $type, $queryString = array() ) + { + $queryPart = ''; + if ( count( $queryString ) ) + { + $queryPart = '/?'. http_build_query( $queryString ); + } + $cmd = "GET {$this->location}/{$type}{$queryPart} HTTP/1.1\n"; + $cmd .= "Host {$this->host}:{$this->port}\n"; + $cmd .= "User-Agent: eZ Components Search\n"; + $cmd .= "\n"; + + fwrite( $this->connection, $cmd ); + + // read http header + $line = ''; + while ( $line != "\r\n" ) + { + $line = $this->getLine(); + if ( preg_match( '@Content-Length: (\d+)@', $line, $m ) ) + { + $expectedLength = $m[1]; + } + } + + // read http content + $size = 1; + $data = ''; + while ( $size < $expectedLength ) + { + $line = $this->getLine( $expectedLength ); + $size += strlen( $line ); + $data .= $line; + } + return $data; + } + + public function sendRawPostCommand( $type, $queryString, $data ) + { + $queryPart = ''; + if ( count( $queryString ) ) + { + $queryPart = '/?'. http_build_query( $queryString ); + } + $length = strlen( $data ); + $cmd = "Post {$this->location}/{$type}{$queryPart} HTTP/1.1\n"; + $cmd .= "Host {$this->host}:{$this->port}\n"; + $cmd .= "User-Agent: eZ Components Search\n"; + $cmd .= "Content-Type: text/xml\n"; + $cmd .= "Content-Length: $length\n"; + $cmd .= "\n"; + $cmd .= $data; + + fwrite( $this->connection, $cmd ); + + // read http header + $line = ''; + while ( $line != "\r\n" ) + { + $line = $this->getLine(); + if ( preg_match( '@Content-Length: (\d+)@', $line, $m ) ) + { + $expectedLength = $m[1]; + } + } + + // read http content + $size = 1; + $data = ''; + while ( $size < $expectedLength ) + { + $line = $this->getLine( $expectedLength ); + $size += strlen( $line ); + $data .= $line; + } + return $data; + } + + public function search( $queryString, $defaultField, $fieldList = array() ) + { + $queryFlags = array( 'q' => $queryString, 'wt' => 'json', 'df' => $defaultField ); + if ( count( $fieldList ) ) + { + $queryFlags['fl'] = join( ' ', $fieldList ); + } + + $result = $this->sendRawGetCommand( 'select', $queryFlags ); + $result = json_decode( $result ); + return ezcSearchResult::createFromResponse( $result ); + } + + /** + * Returns 'solr'. + * + * @return string + */ + static public function getName() + { + return 'solr'; + } + + public function createFindQuery( $type = false ) + { + } + + public function find( ezcSearchFindQuery $query ) + { + } + + public function index( $document ) + { + } + + public function createDeleteQuery() + { + } + + public function delete( ezcSearchDeleteQuery $query ) + { + } +} +?> Propchange: trunk/Search/src/handlers/solr.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/interfaces/definition_manager.php ============================================================================== --- trunk/Search/src/interfaces/definition_manager.php (added) +++ trunk/Search/src/interfaces/definition_manager.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,32 @@ +<?php +/** + * File containing the ezcSearchDefinitionManager class + * + * @package Search + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Defines the interface for all persistent object definition managers. + * + * Definition managers are used to fetch the definition of a specific + * persistent object. The definition is returned in form of a + * ezcSearchDocumentDefinition structure. + * + * @version //autogen// + * @package Search + */ +interface ezcPersistentDefinitionManager +{ + /** + * Returns the definition of the document type $type. + * + * @throws ezcSearchDefinitionNotFoundException if no such definition can be found. + * @param string $type + * @return ezcSearchDocumentDefinition + */ + public function fetchDefinition( $type ); +} +?> Propchange: trunk/Search/src/interfaces/definition_manager.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/interfaces/handler.php ============================================================================== --- trunk/Search/src/interfaces/handler.php (added) +++ trunk/Search/src/interfaces/handler.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,22 @@ +<?php +/** + * File containing the ezcSearchHandler interface. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Defines interface for all the search backend implementations. + * + * @version //autogentag// + * @package Search + */ +interface ezcSearchHandler +{ + public function createFindQuery( $type = false ); + public function find( ezcSearchFindQuery $query ); +} +?> Propchange: trunk/Search/src/interfaces/handler.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/interfaces/index_handler.php ============================================================================== --- trunk/Search/src/interfaces/index_handler.php (added) +++ trunk/Search/src/interfaces/index_handler.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,23 @@ +<?php +/** + * File containing the ezcSearchIndexHandler interface. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Defines interface for all the search backend implementations. + * + * @version //autogentag// + * @package Search + */ +interface ezcSearchIndexHandler +{ + public function index( $document ); + public function createDeleteQuery(); + public function delete( ezcSearchDeleteQuery $query ); +} +?> Propchange: trunk/Search/src/interfaces/index_handler.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/managers/code_manager.php ============================================================================== --- trunk/Search/src/managers/code_manager.php (added) +++ trunk/Search/src/managers/code_manager.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,103 @@ +<?php +/** + * File containing the ezcSearchCodeManager class + * + * @package Search + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Handles document type definitions in plain code style. + * + * Each definition must be in a separate file in the directory specified to the + * constructor. The filename must be the same as the lowercase name of the + * document type with .php appended. Each file should return the definition of + * one document type. + * + * Example exampleclass.php: + * <code> + * <?php + * $definition = new ezcSearchDocumentDefinition; + * return $definition; + * ?> + * </code> + * + * @version //autogen// + * @package Search + */ +class ezcSearchCodeManager extends ezcSearchDefinitionManager +{ + /** + * Holds the path to the directory where the definitions are stored. + * + * @var string + */ + private $dir; + + /** + * Holds the search document definitions that are currently cached. + * + * @var array(string=>ezcSearchDocumentDefinition) + */ + private $cache = array(); + + /** + * Constructs a new code manager that will look for search document definitions in the directory $dir. + * + * @param string $dir + */ + public function __construct( $dir ) + { + // append trailing / to $dir if it does not exist. + if ( substr( $dir, -1 ) != DIRECTORY_SEPARATOR ) + { + $dir .= DIRECTORY_SEPARATOR; + } + $this->dir = $dir; + } + + /** + * Returns the definition of the search document with the type $type. + * + * @throws ezcSearchDefinitionNotFoundException if no such definition can be found. + * @throws ezcSearchDefinitionMissingIdPropertyException + * if the definition does not have an "idProperty" attribute. + * @param string $type + * @return ezcSearchDocumentDefinition + */ + public function fetchDefinition( $type ) + { + // check the cache + if ( isset( $this->cache[$type] ) ) + { + return $this->cache[$type]; + } + + // load definition + $definition = null; + $path = $this->dir . strtolower( $type ) . '.php'; + if ( file_exists( $path ) ) + { + $definition = require $path; + } + if ( !( $definition instanceof ezcSearchDocumentDefinition ) ) + { + throw new ezcSearchDefinitionNotFoundException( $type, + "Searched for '" . realpath( dirname( $path ) ) . "/" . basename( $path ) . "'." ); + } + if ( $definition->idProperty === null ) + { + throw new ezcSearchDefinitionMissingIdPropertyException( $type ); + } + + // store in cache + $this->cache[$type] = $definition; + + // return + return $definition; + } +} + +?> Propchange: trunk/Search/src/managers/code_manager.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/search_autoload.php ============================================================================== --- trunk/Search/src/search_autoload.php (added) +++ trunk/Search/src/search_autoload.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,26 @@ +<?php +/** + * Autoloader definition for the Search component. + * + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @version //autogentag// + * @filesource + * @package Search + */ + +return array( + 'ezcSearchException' => 'Search/exceptions/exception.php', + 'ezcSearchCanNotConnectException' => 'Search/exceptions/can_not_connect.php', + 'ezcSearchHandler' => 'Search/interfaces/handler.php', + 'ezcSearchIndexHandler' => 'Search/interfaces/index_handler.php', + 'ezcSearchQuery' => 'Search/abstraction/query.php', + 'ezcSearchCodeManager' => 'Search/managers/code_manager.php', + 'ezcSearchFindQuery' => 'Search/abstraction/query_find.php', + 'ezcSearchQueryInsert' => 'Search/abstraction/query_index.php', + 'ezcSearchResult' => 'Search/search_result.php', + 'ezcSearchSession' => 'Search/search_session.php', + 'ezcSearchSessionInstance' => 'Search/search_session_instance.php', + 'ezcSearchSolrHandler' => 'Search/handlers/solr.php', +); +?> Propchange: trunk/Search/src/search_autoload.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/search_result.php ============================================================================== --- trunk/Search/src/search_result.php (added) +++ trunk/Search/src/search_result.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,39 @@ +<?php +/** + * File containing the ezcSearchResult class. + * + * @package Search + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Solr backend implementation + * + * @package Search + * @version //autogentag// + */ +class ezcSearchResult +{ + // private so that we can only use the factory to create it + private function __construct() + { + } + + static public function createFromResponse( $response ) + { + $s = new ezcSearchResult(); + $s->status = $response->responseHeader->status; + $s->queryTime = $response->responseHeader->QTime; + $s->resultCount = $response->response->numFound; + $s->start = $response->response->start; + + foreach ( $response->response->docs as $document ) + { + var_dump( $document ); + } + + return $s; + } +} Propchange: trunk/Search/src/search_result.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/search_session.php ============================================================================== --- trunk/Search/src/search_session.php (added) +++ trunk/Search/src/search_session.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,295 @@ +<?php +/** + * File containing the ezcSearchSession class. + * + * @package Search + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * ezcSearchSession is the main runtime interface for searching documents. + * + * @property-read ezcSearchHandler $handler + * The handler set in the constructor. + * @property-read ezcSearchDefinitionManager $definitionManager + * The persistent definition manager set in the constructor. + * + * @package Search + * @version //autogen// + * @mainclass + */ +class ezcSearchSession +{ + /** + * Holds the properties of this class. + * + * @var array(string=>mixed) + */ + private $properties = array(); + + /** + * Constructs a new search session that works on the handler $handler. + * + * The $manager provides valid search document definitions to the + * session. The $handler will be used to perform all search operations. + * + * @param ezcSearchHandler $backend + * @param ezcSearchDefinitionManager $manager + */ + public function __construct( ezcSearchHandler $handler, ezcSearchDefinitionManager $manager ) + { + $this->properties['handler'] = $handler; + $this->properties['definitionManager'] = $manager; + $this->properties['findHandler'] = new ezcPersistentFindHandler( $this ); + $this->properties['indexHandler'] = new ezcPersistentIndexHandler( $this ); + $this->properties['deleteHandler'] = new ezcPersistentDeleteHandler( $this ); + } + + /** + * Returns the result of the search query $query as a list of objects. + * + * Returns the documents found for document type $type using the submitted + * $query. $query should be created using [EMAIL PROTECTED] createFindQuery()}. + * + * Example: + * <code> + * $q = $session->createFindQuery(); + * $allPersons = $session->find( $q ); + * </code> + * + * If you are retrieving large result set, consider using [EMAIL PROTECTED] + * findIterator()} instead. + * + * Example: + * <code> + * $q = $session->createFindQuery(); + * $documents = $session->findIterator( $q ); + * + * foreach ( $documents as $document ) + * { + * // ... + * } + * </code> + * + * @throws ezcSearchDefinitionNotFoundException + * if there is no such persistent class. + * @throws ezcSearchQueryException + * if the find query failed. + * + * @param ezcSearchQuery $query + * @param string $type + * + * @return array(object($class)) + */ + public function find( ezcSearchQuery $query, $type ) + { + return $this->findHandler->find( $query, $type ); + } + + /** + * Returns the result of $query for the $class as an iterator. + * + * This method is similar to [EMAIL PROTECTED] find()} but returns an [EMAIL PROTECTED] + * ezcSearchFindIterator} instead of an array of documents. This is + * useful if you are going to loop over the documents and just need them one + * at the time. Because you only instantiate one document it is faster than + * [EMAIL PROTECTED] find()}. In addition, only 1 record is retrieved from the + * database in each iteration, which may reduce the data transfered between + * the search backend and PHP, if you iterate only through a small subset + * of the affected records. + * + * @throws ezcSearchDefinitionNotFoundException + * if there is no such persistent class. + * @throws ezcSearchQueryException + * if the find query failed. + * + * @param ezcSearchQuery $query + * + * @return ezcSearchFindIterator + */ + public function findIterator( ezcQuerySelect $query, $type ) + { + return $this->findHandler->findIterator( $query, $type ); + } + + /** + * Returns a search query for the given document type $type. + * + * The query is initialized to fetch all properties. + * + * Example: + * <code> + * $q = $session->createFindQuery( 'Person' ); + * $allPersons = $session->find( $q, 'Person' ); + * </code> + * + * @throws ezcSearchException + * if there is no such document type. + * + * @param string $type + * + * @return ezcQuerySelect + */ + public function createFindQuery( $type ) + { + return $this->findHandler->createFindQuery( $type ); + } + + /** + * Indexes the new document $document to the search index. + * + * @throws ezcSearchException if $document + * is not of a valid document type. + * @throws ezcSearchException + * if it was not possible to generate a unique identifier for the + * new object. + * @throws ezcSearchException + * if the indexing failed + * + * @param object $document + */ + public function index( $document ) + { + return $this->indexHandler->index( $document ); + } + + /** + * Indexes a new document after removing the old one first. + * + * @throws ezcSearchDefinitionNotFoundException if $document is not of a valid document type. + * @throws ezcSearchDocumentNotAvailableException if $document is not stored in the database already. + * @param object $document + * @return void + */ + public function update( $document ) + { + $this->indexHandler->delete( $document ); + return $this->indexHandler->index( $document ); + } + + /** + * Deletes the document $document from the index. + * + * @throws ezcSearchDefinitionNotFoundxception + * if the object is not recognized as valid document type. + * @throws ezcSearchDocumentNotAvailableException if $document is not stored in the database already + * @throws ezcSearchQueryException + * if the object could not be deleted. + * + * @param object $document The document to delete + */ + public function delete( $document ) + { + return $this->deleteHandler->delete( $document ); + } + + /** + * Deletes documents using the query $query. + * + * The $query should be created using [EMAIL PROTECTED] createDeleteQuery()}. + * + * @throws ezcSearchQueryException + * if the delete query failed. + * + * @param ezcSearchDeleteQuery $query + */ + public function deleteFromQuery( ezcSearchDeleteQuery $query ) + { + return $this->deleteHandler->deleteFromQuery( $query ); + } + + /** + * Returns a delete query for the given document type $type. + * + * Example: + * <code> + * $q = $session->createDeleteQuery( 'Person' ); + * $q->where( $q->gt( 'age', $q->bindValue( 15 ) ) ); + * $session->deleteFromQuery( $q ); + * </code> + * + * @throws ezcSearchException + * if there is no such document type. + * + * @param string $type + * + * @return ezcQueryDelete + */ + public function createDeleteQuery( $type ) + { + return $this->deleteHandler->createDeleteQuery( $type ); + } + + /** + * Sets the property $name to $value. + * + * @throws ezcBasePropertyNotFoundException + * if the property does not exist. + * + * @param string $name + * @param mixed $value + * + * @ignore + */ + public function __set( $name, $value ) + { + switch ( $name ) + { + case 'database': + case 'definitionManager': + case 'findHandler': + case 'indexHandler': + case 'deleteHandler': + throw new ezcBasePropertyPermissionException( $name, ezcBasePropertyPermissionException::READ ); + + default: + throw new ezcBasePropertyNotFoundException( $name ); + break; + } + + } + + /** + * Property get access. + * + * Simply returns a given property. + * + * @throws ezcBasePropertyNotFoundException + * If a the value for the property propertys is not an instance of + * @param string $propertyName The name of the property to get. + * @return mixed The property value. + * + * @ignore + * + * @throws ezcBasePropertyNotFoundException + * if the given property does not exist. + * @throws ezcBasePropertyPermissionException + * if the property to be set is a write-only property. + */ + public function __get( $propertyName ) + { + if ( $this->__isset( $propertyName ) === true ) + { + return $this->properties[$propertyName]; + } + throw new ezcBasePropertyNotFoundException( $propertyName ); + } + + /** + * Returns if a property exists. + * + * Returns true if the property exists in the [EMAIL PROTECTED] $properties} array + * (even if it is null) and false otherwise. + * + * @param string $propertyName Option name to check for. + * @return void + * @ignore + */ + public function __isset( $propertyName ) + { + return array_key_exists( $propertyName, $this->properties ); + } +} +?> Propchange: trunk/Search/src/search_session.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Search/src/search_session_instance.php ============================================================================== --- trunk/Search/src/search_session_instance.php (added) +++ trunk/Search/src/search_session_instance.php [iso-8859-1] Tue Feb 19 15:03:56 2008 @@ -1,0 +1,138 @@ +<?php +/** + * File containing the ezcSearchSessionInstance class + * + * @package Search + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Holds search object session instances for global access throughout an application. + * + * Typical usage example: + * <code> + * $session = new ezcSearchSession( new ezcSearchSolrHandler( 'localhost', 6983 ), + * new ezcSessionDefinitionManager( ... ) ); + * ezcSearchSessionInstance::set( $session ); // set default session + * $session2 = new ezcSearchSession( ezcDbInstance::get( 'other_db' ), + * new ezcSessionDefinitionManager( ... ) ); + * ezcSearchSessionInstance::set( $session2, 'extra' ); // set the extra session + * + * // retrieve the sessions + * $session = ezcSearchSessionInstance::get(); + * $session2 = ezcSearchSessionInstance::get( 'extra' ); + * </code> + * + * @package Search + * @version //autogen// + * @mainclass + */ +class ezcSearchSessionInstance +{ + /** + * Identifier of the instance that will be returned + * when you call get() without arguments. + * + * @see ezcSearchSessionInstance::get() + * @var string + */ + static private $defaultInstanceIdentifier = null; + + /** + * Holds the session instances. + * + * Example: + * <code> + * array( 'server1' => [object], + * 'server2' => [object] ); + * </code> + * + * @var array(string=>ezcSearchSession) + */ + static private $instances = array(); + + /** + * Returns the search session instance named $identifier. + * + * If $identifier is ommited the default search session + * specified by chooseDefault() is returned. + * + * @throws ezcSearchSessionNotFoundException if the specified instance is not found. + * @param string $identifier + * @return ezcSearchSession + */ + public static function get( $identifier = null ) + { + if ( $identifier === null && self::$defaultInstanceIdentifier ) + { + $identifier = self::$defaultInstanceIdentifier; + } + + if ( !isset( self::$instances[$identifier] ) ) + { + throw new ezcSearchSessionNotFoundException( $identifier ); + } + + return self::$instances[$identifier]; + } + + /** + * Adds the search session $session to the list of known instances. + * + * If $identifier is specified the search session instance can be + * retrieved later using the same identifier. If $identifier is ommited + * the default instance will be set. + * + * @param ezcSearchSession $session + * @param string $identifier the identifier of the search handler + * @return void + */ + public static function set( ezcSearchSession $session, $identifier = null ) + { + if ( $identifier === null ) + { + $identifier = self::$defaultInstanceIdentifier; + } + + self::$instances[$identifier] = $session; + } + + /** + * Sets the search $identifier as default search instance. + * + * To retrieve the default search instance + * call get() with no parameters.. + * + * @see ezcSearchSessionInstance::get(). + * @param string $identifier + * @return void + */ + public static function chooseDefault( $identifier ) + { + self::$defaultInstanceIdentifier = $identifier; + } + + /** + * Resets the default instance holder. + * + * @return void + */ + public static function resetDefault() + { + self::$defaultInstanceIdentifier = false; + } + + /** + * Resets this object to its initial state. + * + * @return void + */ + public function reset() + { + $this->defaultInstanceIdentifier = false; + $this->instances = array(); + } +} +?> Propchange: trunk/Search/src/search_session_instance.php ------------------------------------------------------------------------------ svn:eol-style = native -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components