Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CsvBasicImporter | |
0.00% |
0 / 54 |
|
0.00% |
0 / 3 |
156 | |
0.00% |
0 / 1 |
| import | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCsvMapping | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
90 | |||
| getDataSample | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\import; |
| 23 | |
| 24 | use oat\generis\model\OntologyRdfs; |
| 25 | |
| 26 | /** |
| 27 | * Basic import of csv files |
| 28 | * |
| 29 | * @access public |
| 30 | * @author Antoine Robin, <antoine.robin@vesperiagroup.com> |
| 31 | * @package tao |
| 32 | */ |
| 33 | class CsvBasicImporter extends CsvAbstractImporter |
| 34 | { |
| 35 | public const OPTION_POSTFIX = '_O'; |
| 36 | |
| 37 | public function import($class, $options) |
| 38 | { |
| 39 | return parent::importFile($class, $options); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param \core_kernel_classes_Class $class |
| 44 | * @param string $file |
| 45 | * @param array $options |
| 46 | * @return array |
| 47 | */ |
| 48 | public function getCsvMapping($class, $file, $options) |
| 49 | { |
| 50 | $properties = $this->getClassProperties($class); |
| 51 | $csv_data = new \tao_helpers_data_CsvFile($options); |
| 52 | $csv_data->load($file); |
| 53 | $firstRowAsColumnNames = $options[\tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES] ?? false; |
| 54 | $headers = $this->getColumnMapping($csv_data, $firstRowAsColumnNames); |
| 55 | $modifiedHeader = $headers; |
| 56 | array_walk($modifiedHeader, function (&$value) { |
| 57 | $value = str_replace(' ', '', strtolower($value)); |
| 58 | }); |
| 59 | $properties[] = new \core_kernel_classes_Property(OntologyRdfs::RDFS_LABEL); |
| 60 | $map = []; |
| 61 | /** @var \core_kernel_classes_Property $property */ |
| 62 | foreach ($properties as $property) { |
| 63 | if (!in_array($property->getUri(), $this->getExludedProperties())) { |
| 64 | $propertiesMap[$property->getUri()] = $property->getLabel(); |
| 65 | |
| 66 | //map properties in many ways |
| 67 | //look for label (lower case without spaces) |
| 68 | //look for uri (without namespace) |
| 69 | if ( |
| 70 | ( |
| 71 | $index = array_search( |
| 72 | str_replace(' ', '', strtolower($property->getLabel())), |
| 73 | $modifiedHeader |
| 74 | ) |
| 75 | ) !== false |
| 76 | || ( |
| 77 | $index = array_search( |
| 78 | substr(strtolower($property->getUri()), strpos($property->getUri(), '#') + 1), |
| 79 | $modifiedHeader |
| 80 | ) |
| 81 | ) !== false |
| 82 | ) { |
| 83 | $map[$property->getUri()] = $index; |
| 84 | //look for label or uri with eventually one error |
| 85 | } else { |
| 86 | $maximumError = 1; |
| 87 | $closest = null; |
| 88 | foreach ($modifiedHeader as $index => $header) { |
| 89 | $levLabel = levenshtein(strtolower($property->getLabel()), $header); |
| 90 | $levUri = levenshtein( |
| 91 | substr(strtolower($property->getUri()), strpos($property->getUri(), '#') + 1), |
| 92 | $header |
| 93 | ); |
| 94 | |
| 95 | if ($levLabel <= $maximumError || $levUri <= $maximumError) { |
| 96 | $closest = $index; |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | if (!is_null($closest)) { |
| 101 | $map[$property->getUri()] = $closest; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | $csvMap = [ |
| 107 | 'classProperties' => $propertiesMap, |
| 108 | 'headerList' => $headers, |
| 109 | 'mapping' => $map |
| 110 | ]; |
| 111 | |
| 112 | return $csvMap; |
| 113 | } |
| 114 | |
| 115 | public function getDataSample($file, $options = [], $size = 5, $associative = true) |
| 116 | { |
| 117 | $csv_data = new \tao_helpers_data_CsvFile($options); |
| 118 | $csv_data->load($file); |
| 119 | |
| 120 | $count = min($size, $csv_data->count()); |
| 121 | $data = []; |
| 122 | for ($i = 0; $i < $count; $i++) { |
| 123 | $data[] = $csv_data->getRow($i, $associative); |
| 124 | } |
| 125 | return $data; |
| 126 | } |
| 127 | } |