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

Language PHP Lines 220
MD5 Hash e2deef904506b9b617442760a791a758
Repository https://github.com/libis/Flandrica.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
 * SolrSearch Omeka Plugin helpers.
 *
 * Default helpers for the SolrSearch plugin
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
 * applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * @package    omeka
 * @subpackage SolrSearch
 * @author     "Scholars Lab"
 * @copyright  2010 The Board and Visitors of the University of Virginia
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
 * @version    $Id$
 * @link       http://www.scholarslab.org
 *
 * PHP version 5
 *
 */

/**
 * This manages the process of getting the addon information from the config files and using 
 * them to index a document.
 **/
class SolrSearch_Addon_Manager
{
    // {{{Properties

    /**
     * The database this will interface with.
     *
     * @var Omeka_Db
     **/
    var $db;

    /**
     * The addon directory.
     *
     * @var string
     **/
    var $addonDir;

    /**
     * The parsed addons
     *
     * @var array of SolrSearch_Addon_Addon
     **/
    var $addons;

    // }}}
    // {{{Methods
    
    /**
     * This instantiates a SolrSearch_Addon_Manager
     *
     * @param Omeka_Db $db       The database to initialize everything with.
     * @param string   $addonDir The directory for the addon config files.
     **/
    function __construct($db, $addonDir=null)
    {
        $this->db       = $db;
        $this->addonDir = $addonDir;
        $this->addons   = null;

        if ($this->addonDir === null) {
            $this->addonDir = SOLR_SEARCH_PLUGIN_DIR . '/addons';
        }
    }

    /**
     * This parses all the JSON configuration files in the addon directory and 
     * returns the addons.
     *
     * @param SolrSearch_Addon_Config $config The configuration parser. If 
     * null, this is created.
     *
     * @return array of SolrSearch_Addon_Addon
     * @author Eric Rochester <erochest@virginia.edu>
     **/
    public function parseAll($config=null)
    {
        if (is_null($config)) {
            $config = new SolrSearch_Addon_Config($this->db);
        }
        if (is_null($this->addons)) {
            $this->addons = array();
        }

        $this->addons = array_merge(
            $this->addons, $config->parseDir($this->addonDir)
        );

        return $this->addons;
    }

    /**
     * A helper method to the return the addon for the record.
     *
     * @param Omeka_Record $record The record to find an addon for.
     *
     * @return SolrSearch_Addon_Addon|null $addon The addon for the input 
     * record.
     * @author Eric Rochester <erochest@virginia.edu>
     **/
    public function findAddonForRecord($record)
    {
        $hit = null;

        $recordTable = get_class($record->getTable());
        foreach ($this->addons as $key => $addon) {
            $tableName = get_class($this->db->getTable($addon->table));
            if ($tableName == $recordTable) {
                $hit = $addon;
                break;
            }
        }

        return $hit;
    }

    /**
     * This reindexes all the addons and returns the Solr documents created.
     *
     * @param SolrSearch_Addon_Config $config The configuration parser. If 
     * null, this is created. If given, this forces the Addons to be re-parsed; 
     * otherwise, they're only re-parsed if they haven't been yet.
     *
     * @return array of Apache_Solr_Document $docs The documents generated by 
     * indexing the Addon records.
     * @author Eric Rochester <erochest@virginia.edu>
     **/
    public function reindexAddons($config=null)
    {
        $docs = array();
        $idxr = new SolrSearch_Addon_Indexer($this->db);

        if (is_null($this->addons) || !is_null($config)) {
            $this->parseAll($config);
        }

        $docs = $idxr->indexAll($this->addons);

        return $docs;
    }

    /**
     * This indexes a single record.
     *
     * @param Omeka_Record $record The record to index.
     * @param SolrSearch_Addon_Config $config The configuration parser. If 
     * null, this is created. If given, this forces the Addons to be re-parsed; 
     * otherwise, they're only re-parsed if they haven't been yet.
     *
     * @return Apache_Solr_Document|null $doc The indexed document or null, if 
     * the record's not to be indexed.
     * @author Eric Rochester <erochest@virginia.edu>
     **/
    public function indexRecord($record, $config=null)
    {
        $doc  = null;
        $idxr = new SolrSearch_Addon_Indexer($this->db);

        if (is_null($this->addons) || !is_null($config)) {
            $this->parseAll($config);
        }

        $addon = $this->findAddonForRecord($record);
        if (!is_null($addon) && $idxr->isRecordIndexed($record, $addon)) {
            $doc = $idxr->indexRecord($record, $addon);
        }

        return $doc;
    }

    /**
     * This returns the Solr ID for the record.
     *
     * @param Omeka_Record $record The record to index.
     * @param SolrSearch_Addon_Config $config The configuration parser. If 
     * null, this is created. If given, this forces the Addons to be re-parsed; 
     * otherwise, they're only re-parsed if they haven't been yet.
     *
     * @return string|null
     * @author Eric Rochester <erochest@virginia.edu>
     **/
    public function getId($record, $config=null)
    {
        $id  = null;
        $idxr = new SolrSearch_Addon_Indexer($this->db);

        if (is_null($this->addons) || !is_null($config)) {
            $this->parseAll($config);
        }

        $addon = $this->findAddonForRecord($record);
        if (!is_null($addon)) {
            $id = $record->id;
        }

        return $id;
    }

    // }}}

}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * c-hanging-comment-ender-p: nil
 * End:
 */
Back to Top