Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.69% |
43 / 52 |
|
58.33% |
7 / 12 |
CRAP | |
0.00% |
0 / 1 |
| RepositoryService | |
82.69% |
43 / 52 |
|
58.33% |
7 / 12 |
24.51 | |
0.00% |
0 / 1 |
| getStorage | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getFileSystem | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getTriplesManagerService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAllRevisions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRevision | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| commit | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
4.00 | |||
| restore | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| searchRevisionResources | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNextVersion | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| getUpdatedItemEventDispatcher | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getQtiService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| updateItemsRelations | |
16.67% |
1 / 6 |
|
0.00% |
0 / 1 |
4.31 | |||
| 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) 2015-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoRevision\model; |
| 22 | |
| 23 | use common_Exception; |
| 24 | use common_exception_Error; |
| 25 | use common_session_SessionManager; |
| 26 | use core_kernel_classes_Resource as Resource; |
| 27 | use oat\generis\model\OntologyAwareTrait; |
| 28 | use oat\oatbox\filesystem\FileSystem; |
| 29 | use oat\oatbox\filesystem\FileSystemService; |
| 30 | use oat\oatbox\service\exception\InvalidService; |
| 31 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 32 | use oat\oatbox\service\ConfigurableService; |
| 33 | use oat\taoQtiItem\model\qti\event\UpdatedItemEventDispatcher; |
| 34 | use oat\taoQtiItem\model\qti\Item; |
| 35 | use oat\taoQtiItem\model\qti\Service; |
| 36 | use oat\taoQtiTest\models\creator\CreatorItems; |
| 37 | |
| 38 | /** |
| 39 | * A simple repository implementation that stores the information |
| 40 | * in a dedicated rds table and all related files at separate FS |
| 41 | * |
| 42 | * @author bout |
| 43 | */ |
| 44 | class RepositoryService extends ConfigurableService implements RepositoryInterface |
| 45 | { |
| 46 | use OntologyAwareTrait; |
| 47 | |
| 48 | public const FILE_SYSTEM_NAME = 'revisions'; |
| 49 | |
| 50 | public const OPTION_STORAGE = 'storage'; |
| 51 | public const OPTION_FILE_SYSTEM = 'filesystem'; |
| 52 | |
| 53 | private $storage; |
| 54 | |
| 55 | /** @var FileSystem */ |
| 56 | private $fileSystem; |
| 57 | |
| 58 | /** |
| 59 | * @return RevisionStorageInterface |
| 60 | * @throws InvalidService |
| 61 | * @throws InvalidServiceManagerException |
| 62 | */ |
| 63 | protected function getStorage() |
| 64 | { |
| 65 | if ($this->storage === null) { |
| 66 | $this->storage = $this->getSubService(self::OPTION_STORAGE); |
| 67 | } |
| 68 | |
| 69 | return $this->storage; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * |
| 74 | * @return FileSystem |
| 75 | */ |
| 76 | protected function getFileSystem() |
| 77 | { |
| 78 | if ($this->fileSystem === null) { |
| 79 | $this->fileSystem = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID)->getFileSystem( |
| 80 | $this->getOption(self::OPTION_FILE_SYSTEM) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | return $this->fileSystem; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return TriplesManagerService |
| 89 | */ |
| 90 | public function getTriplesManagerService() |
| 91 | { |
| 92 | return $this->getServiceLocator()->get(TriplesManagerService::SERVICE_ID); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param string $resourceId |
| 97 | * |
| 98 | * @return Revision[] |
| 99 | * @throws InvalidService |
| 100 | * @throws InvalidServiceManagerException |
| 101 | */ |
| 102 | public function getAllRevisions(string $resourceId) |
| 103 | { |
| 104 | return $this->getStorage()->getAllRevisions($resourceId); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param string $resourceId |
| 109 | * @param int $version |
| 110 | * |
| 111 | * @return Revision |
| 112 | * @throws InvalidService |
| 113 | * @throws InvalidServiceManagerException |
| 114 | */ |
| 115 | public function getRevision(string $resourceId, int $version) |
| 116 | { |
| 117 | return $this->getStorage()->getRevision($resourceId, $version); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param Resource $resource |
| 122 | * @param string $message |
| 123 | * @param int|null $version |
| 124 | * @param string|null $author |
| 125 | * |
| 126 | * @return Revision |
| 127 | * @throws InvalidService |
| 128 | * @throws InvalidServiceManagerException |
| 129 | * @throws common_Exception |
| 130 | * @throws common_exception_Error |
| 131 | */ |
| 132 | public function commit(Resource $resource, string $message, int $version = null, string $author = null) |
| 133 | { |
| 134 | $triplesManager = $this->getTriplesManagerService(); |
| 135 | |
| 136 | if ($author === null) { |
| 137 | $user = common_session_SessionManager::getSession()->getUser(); |
| 138 | if ($user !== null) { |
| 139 | $author = $user->getIdentifier(); |
| 140 | } |
| 141 | if ($author === null) { |
| 142 | $author = ''; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | $version = $version ?? $this->getNextVersion($resource->getUri()); |
| 147 | |
| 148 | $triples = $resource->getRdfTriples(); |
| 149 | |
| 150 | $fileSystemMap = array_fill_keys( |
| 151 | array_keys($triplesManager->getPropertyStorageMap($triples)), |
| 152 | $this->getFileSystem()->getId() |
| 153 | ); |
| 154 | |
| 155 | $revision = new Revision($resource->getUri(), $version, time(), $author, $message); |
| 156 | $clonedTriples = $triplesManager->cloneTriples($triples, $fileSystemMap); |
| 157 | |
| 158 | return $this->getStorage()->addRevision($revision, $clonedTriples); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @param Revision $revision |
| 163 | * |
| 164 | * @return bool |
| 165 | * @throws common_Exception |
| 166 | */ |
| 167 | public function restore(Revision $revision) |
| 168 | { |
| 169 | $triplesManager = $this->getTriplesManagerService(); |
| 170 | |
| 171 | $data = $this->getStorage()->getData($revision); |
| 172 | |
| 173 | $resource = $this->getResource($revision->getResourceId()); |
| 174 | $originFilesystemMap = $this->getTriplesManagerService()->getPropertyStorageMap($resource->getRdfTriples()); |
| 175 | |
| 176 | $triplesManager->deleteTriplesFor($resource); |
| 177 | |
| 178 | $clonedTriples = $triplesManager->cloneTriples($data, $originFilesystemMap); |
| 179 | |
| 180 | foreach ($clonedTriples as $triple) { |
| 181 | $this->getModel()->getRdfInterface()->add($triple); |
| 182 | } |
| 183 | |
| 184 | $this->updateItemsRelations($originFilesystemMap, $resource); |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * @param string $query |
| 192 | * |
| 193 | * @param array $options |
| 194 | * @return Resource[] |
| 195 | * @throws InvalidService |
| 196 | * @throws InvalidServiceManagerException |
| 197 | * @todo Fix usage in the NEC project |
| 198 | */ |
| 199 | public function searchRevisionResources(string $query, array $options = []) |
| 200 | { |
| 201 | return $this->getStorage()->getResourcesDataByQuery($query, $options); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Helper to determine suitable next version |
| 206 | * |
| 207 | * @param string $resourceId |
| 208 | * |
| 209 | * @return int |
| 210 | * @throws InvalidService |
| 211 | * @throws InvalidServiceManagerException |
| 212 | */ |
| 213 | protected function getNextVersion(string $resourceId): int |
| 214 | { |
| 215 | $candidate = 0; |
| 216 | foreach ($this->getAllRevisions($resourceId) as $revision) { |
| 217 | $version = $revision->getVersion(); |
| 218 | if (is_numeric($version) && $version > $candidate) { |
| 219 | $candidate = $version; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return $candidate + 1; |
| 224 | } |
| 225 | |
| 226 | private function getUpdatedItemEventDispatcher(): UpdatedItemEventDispatcher |
| 227 | { |
| 228 | return $this->getServiceLocator()->get(UpdatedItemEventDispatcher::class); |
| 229 | } |
| 230 | |
| 231 | private function getQtiService(): Service |
| 232 | { |
| 233 | return $this->getServiceLocator()->get(Service::class); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @throws common_Exception |
| 238 | */ |
| 239 | private function updateItemsRelations(array $originFilesystemMap, Resource $resource): void |
| 240 | { |
| 241 | if (key($originFilesystemMap) === CreatorItems::PROPERTY_ITEM_CONTENT_URI) { |
| 242 | $item = $this->getQtiService()->getDataItemByRdfItem($resource); |
| 243 | |
| 244 | $this->getUpdatedItemEventDispatcher()->dispatch( |
| 245 | $item ?? new Item(), |
| 246 | $resource |
| 247 | ); |
| 248 | } |
| 249 | } |
| 250 | } |