PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/SolrSearch/lib/SolrSearch/Addon/Manager.php

https://github.com/libis/Flandrica
PHP | 219 lines | 78 code | 28 blank | 113 comment | 14 complexity | e2deef904506b9b617442760a791a758 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * SolrSearch Omeka Plugin helpers.
  4. *
  5. * Default helpers for the SolrSearch plugin
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
  10. * applicable law or agreed to in writing, software distributed under the
  11. * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  12. * OF ANY KIND, either express or implied. See the License for the specific
  13. * language governing permissions and limitations under the License.
  14. *
  15. * @package omeka
  16. * @subpackage SolrSearch
  17. * @author "Scholars Lab"
  18. * @copyright 2010 The Board and Visitors of the University of Virginia
  19. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
  20. * @version $Id$
  21. * @link http://www.scholarslab.org
  22. *
  23. * PHP version 5
  24. *
  25. */
  26. /**
  27. * This manages the process of getting the addon information from the config files and using
  28. * them to index a document.
  29. **/
  30. class SolrSearch_Addon_Manager
  31. {
  32. // {{{Properties
  33. /**
  34. * The database this will interface with.
  35. *
  36. * @var Omeka_Db
  37. **/
  38. var $db;
  39. /**
  40. * The addon directory.
  41. *
  42. * @var string
  43. **/
  44. var $addonDir;
  45. /**
  46. * The parsed addons
  47. *
  48. * @var array of SolrSearch_Addon_Addon
  49. **/
  50. var $addons;
  51. // }}}
  52. // {{{Methods
  53. /**
  54. * This instantiates a SolrSearch_Addon_Manager
  55. *
  56. * @param Omeka_Db $db The database to initialize everything with.
  57. * @param string $addonDir The directory for the addon config files.
  58. **/
  59. function __construct($db, $addonDir=null)
  60. {
  61. $this->db = $db;
  62. $this->addonDir = $addonDir;
  63. $this->addons = null;
  64. if ($this->addonDir === null) {
  65. $this->addonDir = SOLR_SEARCH_PLUGIN_DIR . '/addons';
  66. }
  67. }
  68. /**
  69. * This parses all the JSON configuration files in the addon directory and
  70. * returns the addons.
  71. *
  72. * @param SolrSearch_Addon_Config $config The configuration parser. If
  73. * null, this is created.
  74. *
  75. * @return array of SolrSearch_Addon_Addon
  76. * @author Eric Rochester <erochest@virginia.edu>
  77. **/
  78. public function parseAll($config=null)
  79. {
  80. if (is_null($config)) {
  81. $config = new SolrSearch_Addon_Config($this->db);
  82. }
  83. if (is_null($this->addons)) {
  84. $this->addons = array();
  85. }
  86. $this->addons = array_merge(
  87. $this->addons, $config->parseDir($this->addonDir)
  88. );
  89. return $this->addons;
  90. }
  91. /**
  92. * A helper method to the return the addon for the record.
  93. *
  94. * @param Omeka_Record $record The record to find an addon for.
  95. *
  96. * @return SolrSearch_Addon_Addon|null $addon The addon for the input
  97. * record.
  98. * @author Eric Rochester <erochest@virginia.edu>
  99. **/
  100. public function findAddonForRecord($record)
  101. {
  102. $hit = null;
  103. $recordTable = get_class($record->getTable());
  104. foreach ($this->addons as $key => $addon) {
  105. $tableName = get_class($this->db->getTable($addon->table));
  106. if ($tableName == $recordTable) {
  107. $hit = $addon;
  108. break;
  109. }
  110. }
  111. return $hit;
  112. }
  113. /**
  114. * This reindexes all the addons and returns the Solr documents created.
  115. *
  116. * @param SolrSearch_Addon_Config $config The configuration parser. If
  117. * null, this is created. If given, this forces the Addons to be re-parsed;
  118. * otherwise, they're only re-parsed if they haven't been yet.
  119. *
  120. * @return array of Apache_Solr_Document $docs The documents generated by
  121. * indexing the Addon records.
  122. * @author Eric Rochester <erochest@virginia.edu>
  123. **/
  124. public function reindexAddons($config=null)
  125. {
  126. $docs = array();
  127. $idxr = new SolrSearch_Addon_Indexer($this->db);
  128. if (is_null($this->addons) || !is_null($config)) {
  129. $this->parseAll($config);
  130. }
  131. $docs = $idxr->indexAll($this->addons);
  132. return $docs;
  133. }
  134. /**
  135. * This indexes a single record.
  136. *
  137. * @param Omeka_Record $record The record to index.
  138. * @param SolrSearch_Addon_Config $config The configuration parser. If
  139. * null, this is created. If given, this forces the Addons to be re-parsed;
  140. * otherwise, they're only re-parsed if they haven't been yet.
  141. *
  142. * @return Apache_Solr_Document|null $doc The indexed document or null, if
  143. * the record's not to be indexed.
  144. * @author Eric Rochester <erochest@virginia.edu>
  145. **/
  146. public function indexRecord($record, $config=null)
  147. {
  148. $doc = null;
  149. $idxr = new SolrSearch_Addon_Indexer($this->db);
  150. if (is_null($this->addons) || !is_null($config)) {
  151. $this->parseAll($config);
  152. }
  153. $addon = $this->findAddonForRecord($record);
  154. if (!is_null($addon) && $idxr->isRecordIndexed($record, $addon)) {
  155. $doc = $idxr->indexRecord($record, $addon);
  156. }
  157. return $doc;
  158. }
  159. /**
  160. * This returns the Solr ID for the record.
  161. *
  162. * @param Omeka_Record $record The record to index.
  163. * @param SolrSearch_Addon_Config $config The configuration parser. If
  164. * null, this is created. If given, this forces the Addons to be re-parsed;
  165. * otherwise, they're only re-parsed if they haven't been yet.
  166. *
  167. * @return string|null
  168. * @author Eric Rochester <erochest@virginia.edu>
  169. **/
  170. public function getId($record, $config=null)
  171. {
  172. $id = null;
  173. $idxr = new SolrSearch_Addon_Indexer($this->db);
  174. if (is_null($this->addons) || !is_null($config)) {
  175. $this->parseAll($config);
  176. }
  177. $addon = $this->findAddonForRecord($record);
  178. if (!is_null($addon)) {
  179. $id = $record->id;
  180. }
  181. return $id;
  182. }
  183. // }}}
  184. }
  185. /*
  186. * Local variables:
  187. * tab-width: 4
  188. * c-basic-offset: 4
  189. * c-hanging-comment-ender-p: nil
  190. * End:
  191. */