PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/indexer/sfLuceneModelIndexer.class.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 214 lines | 125 code | 38 blank | 51 comment | 12 complexity | bcd1badc9b7b597da7181150b720567f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the sfLucenePlugin package
  4. * (c) 2007 - 2008 Carl Vondrick <carl@carlsoft.net>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Model indexing engine.
  11. * @package sfLucenePlugin
  12. * @subpackage Indexer
  13. * @author Carl Vondrick
  14. * @version SVN: $Id: sfLuceneModelIndexer.class.php 12678 2008-11-06 09:23:10Z rande $
  15. */
  16. abstract class sfLuceneModelIndexer extends sfLuceneIndexer
  17. {
  18. private $instance;
  19. /**
  20. * Constructs a new instance for a model
  21. */
  22. public function __construct($search, $instance)
  23. {
  24. parent::__construct($search);
  25. if ($search->getParameter('models')->get(get_class($instance), null) == null)
  26. {
  27. throw new sfLuceneIndexerException(sprintf('Model "%s" is not registered.', get_class($instance)));
  28. }
  29. $this->instance = $instance;
  30. }
  31. /**
  32. * Calculates the GUID for the model
  33. */
  34. abstract protected function getModelGuid();
  35. /**
  36. * Returns the instance of the model
  37. */
  38. protected function getModel()
  39. {
  40. return $this->instance;
  41. }
  42. /**
  43. * Returns the model name
  44. */
  45. protected function getModelName()
  46. {
  47. return get_class($this->getModel());
  48. }
  49. /**
  50. * Returns the properties of the given model.
  51. */
  52. protected function getModelProperties()
  53. {
  54. return $this->getSearch()->getParameter('models')->get($this->getModelName());
  55. }
  56. /**
  57. * Returns an array of the model categories
  58. */
  59. protected function getModelCategories()
  60. {
  61. $properties = $this->getModelProperties();
  62. if (!$properties->has('categories'))
  63. {
  64. return array();
  65. }
  66. $categories = $properties->get('categories');
  67. if (!is_array($categories))
  68. {
  69. $categories = array($categories);
  70. }
  71. return $categories;
  72. }
  73. /**
  74. * Configures meta data about the document
  75. */
  76. protected function configureDocumentMetas(Zend_Search_Lucene_Document $doc)
  77. {
  78. $doc->addField($this->getLuceneField('unindexed', 'sfl_model', $this->getModelName()));
  79. $doc->addField($this->getLuceneField('unindexed', 'sfl_type', 'model'));
  80. return $doc;
  81. }
  82. /**
  83. * Configures categories into the document
  84. */
  85. protected function configureDocumentCategories(Zend_Search_Lucene_Document $doc)
  86. {
  87. $categories = $this->getModelCategories();
  88. if (count($categories) > 0)
  89. {
  90. foreach ($categories as $category)
  91. {
  92. $this->addCategory($category);
  93. }
  94. $doc->addField( $this->getLuceneField('text', 'sfl_category', implode(' ', $categories)) );
  95. }
  96. $doc->addField( $this->getLuceneField('unindexed', 'sfl_categories_cache', serialize($categories)) );
  97. return $doc;
  98. }
  99. /**
  100. * Returns the base document to work with. Most of the time this will just
  101. * return an empty Zend_Search_Lucene_Document, but if a callback is specified
  102. * it will return that.
  103. */
  104. protected function getBaseDocument()
  105. {
  106. $properties = $this->getModelProperties();
  107. // get our base document from callback?
  108. if ($properties->get('callback'))
  109. {
  110. $cb = $properties->get('callback');
  111. if (!is_callable(array($this->getModel(), $cb)))
  112. {
  113. throw new sfLuceneIndexerException(sprintf('Callback "%s::%s()" does not exist', $this->getModelName(), $cb));
  114. }
  115. $doc = $this->getModel()->$cb();
  116. if (!($doc instanceof Zend_Search_Lucene_Document))
  117. {
  118. throw new sfLuceneIndexerException(sprintf('"%s::%s()" did not return a valid document (must be an instance of Zend_Search_Lucene_Document)', $this->getModelName(), $cb));
  119. }
  120. }
  121. else
  122. {
  123. $doc = new Zend_Search_Lucene_Document();
  124. }
  125. return $doc;
  126. }
  127. /**
  128. * Builds the fields into the document as configured by the parameters.
  129. */
  130. protected function configureDocumentFields(Zend_Search_Lucene_Document $doc)
  131. {
  132. $properties = $this->getModelProperties();
  133. // loop through each field
  134. foreach ($properties->get('fields')->getNames() as $field)
  135. {
  136. $field_properties = $properties->get('fields')->get($field);
  137. // build getter by converting from underscore case to camel case
  138. $getter = 'get' . sfInflector::camelize($field);
  139. $value = $this->getModel()->$getter();
  140. $type = $field_properties->get('type');
  141. $boost = $field_properties->get('boost');
  142. // validate value to make sure we can really index this
  143. if (is_object($value))
  144. {
  145. if(!$value->exists())
  146. {
  147. $this->getModel()->clearRelated(sfInflector::camelize($field));
  148. $value = '';
  149. }
  150. elseif(method_exists($value, '__toString'))
  151. $value = $value->__toString();
  152. }
  153. elseif (is_null($value))
  154. {
  155. $value = '';
  156. }
  157. elseif (!is_scalar($value))
  158. {
  159. throw new sfLuceneIndexerException('Field value returned is not a string (got a ' . gettype($value) . ' ) and it could be casted to a string for field ' . $field);
  160. }
  161. // handle a possible transformation function
  162. if ($transform = $field_properties->get('transform'))
  163. {
  164. if (!is_callable($transform))
  165. {
  166. throw new sfLuceneIndexerException('Transformation function ' . $transform . ' does not exist');
  167. }
  168. $value = call_user_func($transform, $value);
  169. }
  170. $zsl_field = $this->getLuceneField($type, strtolower($field), $value);
  171. $zsl_field->boost = $boost;
  172. $doc->addField($zsl_field);
  173. }
  174. return $doc;
  175. }
  176. }