Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| OntologyIndexService | |
0.00% |
0 / 42 |
|
0.00% |
0 / 4 |
210 | |
0.00% |
0 / 1 |
| createIndex | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| getIndexById | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getIndexes | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getIndexesByClass | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\search\index; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use oat\generis\model\GenerisRdf; |
| 27 | use oat\generis\model\OntologyRdfs; |
| 28 | use oat\tao\model\TaoOntology; |
| 29 | |
| 30 | /** |
| 31 | * Index service |
| 32 | * |
| 33 | * @author Joel Bout <joel@taotesting.com> |
| 34 | */ |
| 35 | class OntologyIndexService |
| 36 | { |
| 37 | /** |
| 38 | * Create a new index |
| 39 | * |
| 40 | * @param \core_kernel_classes_Property $property |
| 41 | * @param unknown $identifier |
| 42 | * @param \core_kernel_classes_Resource $tokenizer |
| 43 | * @param unknown $isFuzzyMatching |
| 44 | * @param unknown $isDefaultSearchable |
| 45 | * @return OntologyIndex |
| 46 | */ |
| 47 | public static function createIndex( |
| 48 | \core_kernel_classes_Property $property, |
| 49 | $identifier, |
| 50 | \core_kernel_classes_Resource $tokenizer, |
| 51 | $isFuzzyMatching, |
| 52 | $isDefaultSearchable |
| 53 | ) { |
| 54 | $class = new \core_kernel_classes_Class(OntologyIndex::RDF_TYPE); |
| 55 | $existingIndex = self::getIndexById($identifier); |
| 56 | if (!is_null($existingIndex)) { |
| 57 | throw new \common_Exception('Index ' . $identifier . ' already in use'); |
| 58 | } |
| 59 | // verify identifier is unused |
| 60 | $resource = $class->createInstanceWithProperties([ |
| 61 | OntologyRdfs::RDFS_LABEL => $identifier, |
| 62 | OntologyIndex::PROPERTY_INDEX_IDENTIFIER => $identifier, |
| 63 | OntologyIndex::PROPERTY_INDEX_TOKENIZER => $tokenizer, |
| 64 | OntologyIndex::PROPERTY_INDEX_FUZZY_MATCHING => $isFuzzyMatching |
| 65 | ? GenerisRdf::GENERIS_TRUE |
| 66 | : GenerisRdf::GENERIS_FALSE, |
| 67 | OntologyIndex::PROPERTY_DEFAULT_SEARCH => $isDefaultSearchable |
| 68 | ? GenerisRdf::GENERIS_TRUE |
| 69 | : GenerisRdf::GENERIS_FALSE |
| 70 | ]); |
| 71 | $property->setPropertyValue(new \core_kernel_classes_Property(OntologyIndex::PROPERTY_INDEX), $resource); |
| 72 | return new OntologyIndex($resource); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get an index by its unique index id |
| 77 | * |
| 78 | * @param string $identifier |
| 79 | * @throws \common_exception_InconsistentData |
| 80 | * @return OntologyIndex |
| 81 | */ |
| 82 | public static function getIndexById($identifier) |
| 83 | { |
| 84 | |
| 85 | $indexClass = new core_kernel_classes_Class(OntologyIndex::RDF_TYPE); |
| 86 | $resources = $indexClass->searchInstances([ |
| 87 | OntologyIndex::PROPERTY_INDEX_IDENTIFIER => $identifier |
| 88 | ], ['like' => false]); |
| 89 | if (count($resources) > 1) { |
| 90 | throw new \common_exception_InconsistentData("Several index exist with the identifier " . $identifier); |
| 91 | } |
| 92 | return count($resources) > 0 |
| 93 | ? new OntologyIndex(array_shift($resources)) |
| 94 | : null; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get all indexes of a property |
| 99 | * |
| 100 | * @param \core_kernel_classes_Property $property |
| 101 | * @return multitype:OntologyIndex |
| 102 | */ |
| 103 | public static function getIndexes(\core_kernel_classes_Property $property) |
| 104 | { |
| 105 | $indexUris = $property->getPropertyValues(new \core_kernel_classes_Property(OntologyIndex::PROPERTY_INDEX)); |
| 106 | $indexes = []; |
| 107 | |
| 108 | foreach ($indexUris as $indexUri) { |
| 109 | $indexes[] = new OntologyIndex($indexUri); |
| 110 | } |
| 111 | |
| 112 | return $indexes; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the Search Indexes of a given $class. |
| 117 | * |
| 118 | * The returned array is an associative array where keys are the Property URI |
| 119 | * the Search Index belongs to, and the values are core_kernel_classes_Resource objects |
| 120 | * corresponding to Search Index definitions. |
| 121 | * |
| 122 | * @param \core_kernel_classes_Class $class |
| 123 | * @param boolean $recursive Whether or not to look for Search Indexes that belong to sub-classes of $class. |
| 124 | * Default is true. |
| 125 | * @return OntologyIndex[] An array of Search Index to $class. |
| 126 | */ |
| 127 | public static function getIndexesByClass(\core_kernel_classes_Class $class, $recursive = true) |
| 128 | { |
| 129 | $returnedIndexes = []; |
| 130 | |
| 131 | // Get properties to the root class hierarchy. |
| 132 | $properties = $class->getProperties(true); |
| 133 | |
| 134 | foreach ($properties as $prop) { |
| 135 | $propUri = $prop->getUri(); |
| 136 | $indexes = self::getIndexes($prop); |
| 137 | |
| 138 | if (count($indexes) > 0) { |
| 139 | if (isset($returnedIndexes[$propUri]) === false) { |
| 140 | $returnedIndexes[$propUri] = []; |
| 141 | } |
| 142 | |
| 143 | foreach ($indexes as $index) { |
| 144 | $returnedIndexes[$propUri][] = new OntologyIndex($index->getUri()); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return $returnedIndexes; |
| 150 | } |
| 151 | } |