/titania/includes/library/ezcomponents/Search/search_session.php

https://github.com/TheDgtl/customisation-db · PHP · 333 lines · 108 code · 22 blank · 203 comment · 5 complexity · dd4a642849c7cc72dc01faebf5e88d02 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcSearchSession class.
  4. *
  5. * @package Search
  6. * @version //autogen//
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * ezcSearchSession is the main runtime interface for searching documents.
  12. *
  13. * @property-read ezcSearchHandler $handler
  14. * The handler set in the constructor.
  15. * @property-read ezcSearchDefinitionManager $definitionManager
  16. * The persistent definition manager set in the constructor.
  17. *
  18. * @package Search
  19. * @version //autogen//
  20. * @mainclass
  21. */
  22. class ezcSearchSession
  23. {
  24. /**
  25. * Holds the properties of this class.
  26. *
  27. * @var array(string=>mixed)
  28. */
  29. private $properties = array();
  30. /**
  31. * Constructs a new search session that works on the handler $handler.
  32. *
  33. * The $manager provides valid search document definitions to the
  34. * session. The $handler will be used to perform all search operations.
  35. *
  36. * @param ezcSearchHandler $handler
  37. * @param ezcSearchDefinitionManager $manager
  38. */
  39. public function __construct( ezcSearchHandler $handler, ezcSearchDefinitionManager $manager )
  40. {
  41. $this->properties['handler'] = $handler;
  42. $this->properties['definitionManager'] = $manager;
  43. }
  44. /**
  45. * Returns the result of the search query $query as a list of objects.
  46. *
  47. * Returns the documents found for document type $type using the submitted
  48. * $query. $query should be created using {@link createFindQuery()}.
  49. *
  50. * Example:
  51. * <code>
  52. * $q = $session->createFindQuery();
  53. * $allPersons = $session->find( $q );
  54. * </code>
  55. *
  56. * @throws ezcSearchDefinitionNotFoundException
  57. * if there is no such persistent class.
  58. * @throws ezcSearchQueryException
  59. * if the find query failed.
  60. *
  61. * @param ezcSearchQuery $query
  62. *
  63. * @return ezcSearchResult
  64. */
  65. public function find( ezcSearchQuery $query )
  66. {
  67. return $this->handler->find( $query );
  68. }
  69. /**
  70. * Returns a search query for the given document type $type.
  71. *
  72. * The query is initialized to fetch all properties.
  73. *
  74. * Example:
  75. * <code>
  76. * $q = $session->createFindQuery( 'Person' );
  77. * $allPersons = $session->find( $q, 'Person' );
  78. * </code>
  79. *
  80. * @throws ezcSearchException
  81. * if there is no such document type.
  82. *
  83. * @param string $type
  84. *
  85. * @return ezcSearchFindQuery
  86. */
  87. public function createFindQuery( $type )
  88. {
  89. $def = $this->definitionManager->fetchDefinition( $type );
  90. /* We add the ezcsearch_type field to the definition automatically here, but we delete it as well */
  91. $def->fields['ezcsearch_type'] = new ezcSearchDefinitionDocumentField( 'ezcsearch_type', ezcSearchDocumentDefinition::STRING );
  92. $res = $this->handler->createFindQuery( $type, $def );
  93. unset( $def->fields['ezcsearch_type'] );
  94. return $res;
  95. }
  96. /**
  97. * Starts a transaction for indexing.
  98. *
  99. * When using a transaction, the amount of processing that the search
  100. * backend does decreases, increasing indexing performance. Without this,
  101. * the component sends a commit after every document that is indexed.
  102. * Transactions can be nested, when commit() is called the same number of
  103. * times as beginTransaction(), the component sends a commit.
  104. */
  105. public function beginTransaction()
  106. {
  107. $this->handler->beginTransaction();
  108. }
  109. /**
  110. * Ends a transaction and calls commit.
  111. *
  112. * @throws ezcSearchTransactionException if no transaction is active.
  113. */
  114. public function commit()
  115. {
  116. $this->handler->commit();
  117. }
  118. /**
  119. * Indexes the new document $document to the search index.
  120. *
  121. * @throws ezcSearchException if $document
  122. * is not of a valid document type.
  123. * @throws ezcSearchException
  124. * if it was not possible to generate a unique identifier for the
  125. * new object.
  126. * @throws ezcSearchException
  127. * if the indexing failed.
  128. *
  129. * @param object $document
  130. */
  131. public function index( $document )
  132. {
  133. $class = get_class( $document );
  134. $def = $this->definitionManager->fetchDefinition( $class );
  135. $state = $document->getState();
  136. if ( $state[$def->idProperty] == null )
  137. {
  138. $state[$def->idProperty] = uniqid();
  139. $document->setState( array( $def->idProperty => $state[$def->idProperty] ) );
  140. }
  141. $this->verifyState( $def, $state );
  142. $this->handler->index( $def, $state );
  143. }
  144. /**
  145. * Checks whether the state contains all the elements from the definition.
  146. *
  147. * @param ezcSearchDocumentDefinition $def
  148. * @param array(string=>mixed) $state
  149. *
  150. * @throws ezcSearchIncompleteStateException if the state is not complete
  151. */
  152. private function verifyState( ezcSearchDocumentDefinition $def, array $state )
  153. {
  154. foreach ( $def->fields as $field )
  155. {
  156. if ( !array_key_exists( $field->field, $state ) )
  157. {
  158. throw new ezcSearchIncompleteStateException( $field->field );
  159. }
  160. }
  161. }
  162. /**
  163. * Indexes a new document after removing the old one first.
  164. *
  165. * @throws ezcSearchDefinitionNotFoundException if $document is not of a valid document type.
  166. * @throws ezcSearchDocumentNotAvailableException if $document is not stored in the database already.
  167. * @param object $document
  168. * @return void
  169. */
  170. public function update( $document )
  171. {
  172. $type = get_class( $document );
  173. $def = $this->definitionManager->fetchDefinition( $type );
  174. $idProperty = $def->idProperty;
  175. $this->deleteById( $document->$idProperty, $type );
  176. return $this->index( $document );
  177. }
  178. /**
  179. * Deletes the document $document from the index.
  180. *
  181. * @throws ezcSearchDefinitionNotFoundxception
  182. * if the object is not recognized as valid document type.
  183. * @throws ezcSearchDocumentNotAvailableException if $document is not stored in the database already
  184. * @throws ezcSearchQueryException
  185. * if the object could not be deleted.
  186. *
  187. * @param ezcSearchDeleteQuery $query
  188. */
  189. public function delete( ezcSearchDeleteQuery $query )
  190. {
  191. $this->handler->delete( $query );
  192. }
  193. /**
  194. * Returns a delete query for the given document type $type.
  195. *
  196. * Example:
  197. * <code>
  198. * $q = $session->createDeleteQuery( 'Person' );
  199. * $q->where( $q->gt( 'age', $q->bindValue( 15 ) ) );
  200. * $session->delete( $q );
  201. * </code>
  202. *
  203. * @throws ezcSearchException
  204. * if there is no such document type.
  205. *
  206. * @param string $type
  207. *
  208. * @return ezcSearchDeleteQuery
  209. */
  210. public function createDeleteQuery( $type )
  211. {
  212. $def = $this->definitionManager->fetchDefinition( $type );
  213. /* We add the ezcsearch_type field to the definition automatically
  214. * here, but we delete it as well */
  215. $def->fields['ezcsearch_type'] = new ezcSearchDefinitionDocumentField( 'ezcsearch_type', ezcSearchDocumentDefinition::STRING );
  216. $res = $this->handler->createDeleteQuery( $type, $def );
  217. unset( $def->fields['ezcsearch_type'] );
  218. return $res;
  219. }
  220. /**
  221. * Deletes a document by the document's $id
  222. *
  223. * @throws ezcSearchException
  224. * if there is no such document type.
  225. *
  226. * @param mixed $id
  227. * @param string $type
  228. */
  229. public function deleteById( $id, $type )
  230. {
  231. $def = $this->definitionManager->fetchDefinition( $type );
  232. return $this->handler->deleteById( $id, $def );
  233. }
  234. /**
  235. * Find a document by its ID.
  236. *
  237. * @throws ezcSearchException
  238. * if there is no such document type.
  239. *
  240. * @param mixed $id
  241. * @param string $type
  242. * @return ezcSearchResult
  243. */
  244. public function findById( $id, $type )
  245. {
  246. $def = $this->definitionManager->fetchDefinition( $type );
  247. return $this->handler->findById( $id, $def );
  248. }
  249. /**
  250. * Sets the property $name to $value.
  251. *
  252. * @throws ezcBasePropertyNotFoundException if the property does not exist.
  253. * @throws ezcBasePropertyPermissionException if a read-only property is
  254. * tried to be modified.
  255. *
  256. * @param string $name
  257. * @param mixed $value
  258. *
  259. * @ignore
  260. */
  261. public function __set( $name, $value )
  262. {
  263. switch ( $name )
  264. {
  265. case 'definitionManager':
  266. case 'handler':
  267. throw new ezcBasePropertyPermissionException( $name, ezcBasePropertyPermissionException::READ );
  268. default:
  269. throw new ezcBasePropertyNotFoundException( $name );
  270. break;
  271. }
  272. }
  273. /**
  274. * Property get access.
  275. *
  276. * Simply returns a given property.
  277. *
  278. * @param string $propertyName The name of the property to get.
  279. * @return mixed The property value.
  280. *
  281. * @throws ezcBasePropertyNotFoundException
  282. * if the given property does not exist.
  283. * @throws ezcBasePropertyPermissionException
  284. * if the property to be set is a write-only property.
  285. *
  286. * @ignore
  287. */
  288. public function __get( $propertyName )
  289. {
  290. if ( $this->__isset( $propertyName ) === true )
  291. {
  292. return $this->properties[$propertyName];
  293. }
  294. throw new ezcBasePropertyNotFoundException( $propertyName );
  295. }
  296. /**
  297. * Returns if a property exists.
  298. *
  299. * Returns true if the property exists in the {@link $properties} array
  300. * (even if it is null) and false otherwise.
  301. *
  302. * @param string $propertyName Option name to check for.
  303. * @return void
  304. * @ignore
  305. */
  306. public function __isset( $propertyName )
  307. {
  308. return array_key_exists( $propertyName, $this->properties );
  309. }
  310. }
  311. ?>