PageRenderTime 67ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/Index/Client.php

https://github.com/Kalyse/IndexTank-API--Zend-Framework-
PHP | 314 lines | 250 code | 62 blank | 2 comment | 44 complexity | e041867d38b1de77ee9b6e0e20d13abb MD5 | raw file
  1. <?php
  2. require_once 'Zend/Http/Client.php';
  3. class IndexTank_Index_Client {
  4. protected $_indexName;
  5. protected $_public;
  6. protected $_code;
  7. protected $_creationDate;
  8. protected $_started;
  9. protected $_size;
  10. protected $_client;
  11. public function __construct($client = null, $indexName = null, $options = null){
  12. if(!($client instanceof IndexTank_Client)){
  13. $indexName = $client;
  14. $metaData = $indexName;
  15. } else {
  16. $this->_setClient( $client );
  17. }
  18. if($indexName !== null){
  19. $this->setIndexName($indexName);
  20. }
  21. $this->setOptions($options);
  22. }
  23. private function _setClient(IndexTank_Client $client){
  24. $this->_client = $client;
  25. }
  26. public function getClient(){
  27. return $this->_getClient();
  28. }
  29. private function _getClient(){
  30. if(!isset($this->_client)){
  31. require_once 'IndexTank/Exception/IndexTank.php';
  32. throw new IndexTank_Exception_IndexTank("The Client has not been set yet for the IndexTank_Index_Client");
  33. }
  34. return $this->_client;
  35. }
  36. public function setIndexName($indexName){
  37. $this->_indexName = str_replace('/','',$indexName);
  38. }
  39. public function getIndexName(){
  40. return $this->_indexName;
  41. }
  42. public function clearOptions(){
  43. $this->_code = null;
  44. $this->_status = null;
  45. $this->_creationDate = null;
  46. $this->_public = null;
  47. $this->_size = null;
  48. $this->_started = null;
  49. }
  50. public function setOptions($metaData){
  51. $this->clearOptions();
  52. if ($metaData instanceof Zend_Config){
  53. $metaData = $metaData->toArray();
  54. }
  55. if(is_bool($metaData)){
  56. $this->_options['public_search'] = $metaData;
  57. } elseif ( is_null($metaData)) {
  58. $this->_options['public_search'] = false;
  59. } elseif ( is_array($metaData)){
  60. if(array_key_exists("public_search", $metaData)){
  61. $this->setPublic ( $metaData['public_search'] );
  62. }
  63. if(array_key_exists("status", $metaData)){
  64. $this->setStatus( $metaData['status'] );
  65. }
  66. if(array_key_exists("code", $metaData)){
  67. $this->setCode( $metaData['code'] );
  68. }
  69. if(array_key_exists("creation_time", $metaData)){
  70. $this->setCreationDate( $metaData['creation_time'] );
  71. }
  72. if(array_key_exists("size", $metaData)){
  73. $this->setSize($metaData['size'] );
  74. }
  75. if(array_key_exists("started", $metaData)){
  76. $this->setStarted ( $metaData['started'] );
  77. }
  78. }
  79. }
  80. public function setPublic($option){
  81. $this->_public = $option;
  82. }
  83. public function setCode($option){
  84. $this->_code = (string) $option;
  85. }
  86. public function setCreationDate($option){
  87. $this->_creationDate = $option;
  88. }
  89. public function setSize($option){
  90. $this->_size = (integer) $option;
  91. }
  92. public function setStatus($option){
  93. $this->_size = (boolean) $option;
  94. }
  95. public function setStarted($option){
  96. $this->_started = (boolean) $option;
  97. }
  98. public function isStarted(){
  99. return ( $this->_started == true );
  100. }
  101. public function useIndex($name){
  102. if(is_string($name)){
  103. $this->setIndexName( $name );
  104. }
  105. return $this;
  106. }
  107. public function getIndex($name){
  108. if(is_string($name)){
  109. $this->setIndexName( $name );
  110. }
  111. if(!$this->indexNameSet()){
  112. require_once 'IndexTank/Exception/IndexTank.php';
  113. throw new IndexTank_Exception_IndexTank("The IndexName you are attempting to retrieve is not valid.");
  114. }
  115. $uri = $this->_getClient()->getUri( 'indexes', $this->getIndexName( ));
  116. $response = $this->_getClient()->get( $uri );
  117. $this->setOptions( $response );
  118. return( $this );
  119. }
  120. public function addDocument($docId, $fields, $variables = null, $categories = null, $removeEmptyFields = false){
  121. $this->_checkDocId($docId);
  122. $this->_checkIndexNameIsSet();
  123. if(!$this->indexNameSet()){
  124. require_once 'IndexTank/Exception/IndexTank.php';
  125. throw new IndexTank_Exception_IndexTank("The IndexName you are attempting to retrieve is not valid.");
  126. }
  127. // Remove Empty Field Results
  128. if($removeEmptyFields){
  129. foreach($fields as $key=>$item){
  130. if($item == "" || is_null($item)){
  131. unset($fields[$key]);
  132. }
  133. }
  134. }
  135. foreach($variables as $key=>$item){
  136. if($item == "" || is_null($item)){
  137. unset($variables[$key]);
  138. }
  139. }
  140. // Need to check for the length of $fields so that the sum does not exceed 100kbytes.
  141. $uri = $this->_getClient()->getUri( 'indexing', $this->getIndexName( ));
  142. $document = array(
  143. 'docid' => $docId,
  144. 'fields' => $fields,
  145. 'variables' => $variables,
  146. 'categories' => $categories,
  147. );
  148. if($variables == null || empty($variables)){
  149. unset($document['variables']);
  150. }
  151. if($categories == null || empty($categories)){
  152. unset($document['categories']);
  153. }
  154. $response = $this->_getClient()->put( $uri, $document
  155. );
  156. return $response;
  157. }
  158. public function deleteDocument($docId){
  159. $this->_checkDocId($docId);
  160. $this->_checkIndexNameIsSet();
  161. $uri = $this->_getClient()->getUri( 'indexing', $this->getIndexName( ));
  162. $response = $this->_getClient()->delete( $uri, array('docid' => $docId));
  163. }
  164. public function addVariables($docId, $variables){
  165. $this->_checkDocId($docId);
  166. $this->_checkIndexNameIsSet();
  167. $uri = $this->_getClient()->getUri( 'variables', $this->getIndexName( ));
  168. $response = $this->_getClient()->put( $uri, array(
  169. 'docid' => $docId,
  170. 'variables' => $variables,
  171. )
  172. );
  173. return $response;
  174. }
  175. public function addCategories($docId, $categories){
  176. $this->_checkDocId($docId);
  177. $this->_checkIndexNameIsSet();
  178. $uri = $this->_getClient()->getUri( 'categories', $this->getIndexName( ));
  179. $response = $this->_getClient()->put( $uri, array(
  180. 'docid' => $docId,
  181. 'categories' => $categories,
  182. )
  183. );
  184. return $response;
  185. }
  186. public function getSearchQueryBuilder(){
  187. return new IndexTank_Index_Search($this);
  188. }
  189. public function search( $query, array $fetch = array('text'), $options = array()){
  190. $options = $this->_processQueryOptions($options);
  191. }
  192. private function _processQueryOptions($options){
  193. $item = array();
  194. if(isset($options["function"])){
  195. if(is_int((int) $options["function"]) && $options["function"] <= 5){
  196. $item['function'] = (string) (int) $options['function'];
  197. } else {
  198. require_once 'IndexTank/Exception/IndexTank.php';
  199. throw new IndexTank_Exception_IndexTank("Function Options must be an integer between 0 and 5.");
  200. }
  201. }
  202. if(isset($options["fetch_variables"])){
  203. $item['fetch_variables'] = (boolean) $options['fetch_variables'];
  204. }
  205. if(isset($options["fetch_categories"])){
  206. $item['fetch_categories'] = (boolean) $options['fetch_categories'];
  207. }
  208. if(isset($options["snippet_fields"])){
  209. $item['snippet_fields'] = (string) $options['snippet_fields'];
  210. }
  211. if(isset($options["variables"])){
  212. foreach( $options["variables"] as $k => $v)
  213. {
  214. $options["var".strval($k)] = $v;
  215. }
  216. unset($options['variables']);
  217. }
  218. if(isset($options["category_filters"])){
  219. $item['category_filters'] = (string) $options['category_filters'];
  220. }
  221. return $item;
  222. }
  223. private function _checkIndexNameIsSet(){
  224. if(!$this->indexNameSet()){
  225. require_once 'IndexTank/Exception/IndexTank.php';
  226. throw new IndexTank_Exception_IndexTank("The IndexName you are attempting to retrieve is not valid.");
  227. }
  228. }
  229. public function indexNameSet(){
  230. return ( $this->_indexName !== "" && isset( $this->_indexName ) );
  231. }
  232. private function _checkDocId($docId){
  233. if(strlen($docId) < 1){
  234. require_once 'IndexTank/Exception/IndexTank.php';
  235. throw new IndexTank_Exception_IndexTank("Your document identifier a non-empty string no longer than 1024 bytes");
  236. }
  237. if( mb_strlen($docId, '8bit') > 1024){
  238. require_once 'IndexTank/Exception/IndexTank.php';
  239. throw new IndexTank_Exception_IndexTank(sprintf("Your document identifier %s is longer than 1024 bytes.", $docId));
  240. }
  241. }
  242. public function toArray()
  243. {
  244. return array(
  245. 'name' => $this->_indexName,
  246. 'started' => $this->_started,
  247. 'code' => $this->_code,
  248. 'creationDate' => $this->_creationDate,
  249. 'size' => $this->_size
  250. );
  251. }
  252. }