From 2341826c9a12f66150c8dfa6eb4ad045a4748bc4 Mon Sep 17 00:00:00 2001 From: Jaider Andrade Ferreira Date: Mon, 23 Dec 2024 21:46:08 -0300 Subject: [PATCH 1/3] formatting changes --- config/pergamum2dspace.json | 49 +++++++++--------- config/xls2dspace.json | 23 +++++---- pergamumws/marc.php | 17 +++---- pergamumws/parametros.php | 6 +-- pergamumws/teses.php | 66 ++++++++++++------------- src/classes/GenericItemSynchronizer.php | 35 ++++++++----- src/classes/Pergamum.php | 40 ++++++++------- 7 files changed, 122 insertions(+), 114 deletions(-) diff --git a/config/pergamum2dspace.json b/config/pergamum2dspace.json index 77ba425..1179b9d 100644 --- a/config/pergamum2dspace.json +++ b/config/pergamum2dspace.json @@ -1,26 +1,25 @@ - { - "100-a-1":"dc.contributor.author", - "245-a-1":"dc.title", - "260-a-1":"dc.publisher", - "260-c-1":"dc.date.issued", - "300-a-1":"dc.format.extent", - "502-a-1":"dc.description", - "520-a-1":"dc.description.abstract", - "520-a-2":"dc.description.abstract.en", - "650-a-1":"dc.subject.classification", - "650-a-2":"dc.subject.classification", - "650-a-3":"dc.subject.classification", - "650-a-4":"dc.subject.classification", - "650-a-5":"dc.subject.classification", - "650-a-6":"dc.subject.classification", - "650-a-7":"dc.subject.classification", - "650-a-8":"dc.subject.classification", - "650-a-9":"dc.subject.classification", - "700-a-1":"dc.contributor.advisor", - "700-a-2":"dc.contributor.advisor-co", - "710-a-1":"dc.contributor", - "cod_acervo": "dc.identifier.other", - "idioma": "dc.language.iso", - "nota_monog": "dc.type" -} \ No newline at end of file + "100-a-1": "dc.contributor.author", + "245-a-1": "dc.title", + "260-a-1": "dc.publisher", + "260-c-1": "dc.date.issued", + "300-a-1": "dc.format.extent", + "502-a-1": "dc.description", + "520-a-1": "dc.description.abstract", + "520-a-2": "dc.description.abstract.en", + "650-a-1": "dc.subject.classification", + "650-a-2": "dc.subject.classification", + "650-a-3": "dc.subject.classification", + "650-a-4": "dc.subject.classification", + "650-a-5": "dc.subject.classification", + "650-a-6": "dc.subject.classification", + "650-a-7": "dc.subject.classification", + "650-a-8": "dc.subject.classification", + "650-a-9": "dc.subject.classification", + "700-a-1": "dc.contributor.advisor", + "700-a-2": "dc.contributor.advisor-co", + "710-a-1": "dc.contributor", + "cod_acervo": "dc.identifier.other", + "idioma": "dc.language.iso", + "nota_monog": "dc.type" +} diff --git a/config/xls2dspace.json b/config/xls2dspace.json index 8292a27..5bb25db 100644 --- a/config/xls2dspace.json +++ b/config/xls2dspace.json @@ -1,13 +1,12 @@ - { - "B": "dc.identifier.other", - "C":"dc.title", - "D":"dc.description.abstract", - "E":"dc.date.issued", - "G":"dc.subject", - "J":"dc.contributor.author", - "K":"dc.contributor.advisor", - "L":"dc.contributor.other", - "descricao": "dc.description", - "tipo": "dc.type" -} \ No newline at end of file + "B": "dc.identifier.other", + "C": "dc.title", + "D": "dc.description.abstract", + "E": "dc.date.issued", + "G": "dc.subject", + "J": "dc.contributor.author", + "K": "dc.contributor.advisor", + "L": "dc.contributor.other", + "descricao": "dc.description", + "tipo": "dc.type" +} diff --git a/pergamumws/marc.php b/pergamumws/marc.php index 960b7dd..e13ffeb 100644 --- a/pergamumws/marc.php +++ b/pergamumws/marc.php @@ -4,18 +4,17 @@ //$query = sybase_query("select distinct ams.cod_acervo, r.desc_titulo, r.ano_publicacao, t.desc_tipo_obra //$query = sybase_query("select ams.*, r.*, t.* #$query = sybase_query("select * from acervo_marc_secao where cod_acervo=$cod_acervo -$cod_acervo = isset($_GET['cod_acervo'])? intval($_GET['cod_acervo']) : 0; -$query = "select * from acervo_marc_secao where cod_acervo=$cod_acervo order by paragrafo, seq_paragrafo"; -$result = mssql_query($query,$db); +$cod_acervo = isset($_GET['cod_acervo']) ? intval($_GET['cod_acervo']) : 0; +$query = "SELECT * FROM acervo_marc_secao WHERE cod_acervo=$cod_acervo ORDER BY paragrafo, seq_paragrafo"; +$result = mssql_query($query, $db); $trabalhos = array(); -while($array1 = mssql_fetch_assoc($result)) -{ +while ($array1 = mssql_fetch_assoc($result)) { $t = array(); - foreach($array1 as $k => $v){ - $t[$k] = utf8_encode(htmlentities($v, ENT_COMPAT, "ISO-8859-1" )); - #$t[$k] = utf8_encode(htmlentities($v )); - //$t[$k] = $v; + foreach ($array1 as $k => $v) { + $t[$k] = utf8_encode(htmlentities($v, ENT_COMPAT, "ISO-8859-1")); + #$t[$k] = utf8_encode(htmlentities($v )); + //$t[$k] = $v; } $trabalhos[] = $t; } diff --git a/pergamumws/parametros.php b/pergamumws/parametros.php index b6c4ea3..9bcf1ba 100644 --- a/pergamumws/parametros.php +++ b/pergamumws/parametros.php @@ -7,9 +7,7 @@ $db = mssql_connect($server, $user, $password) or die('MSSQL error: ' . mssql_get_last_message()); -mssql_select_db( $database , $db ) or die ("Problema ao setar o DB do Pergamum: " . mssql_get_last_message()); +mssql_select_db($database, $db) or die("Problema ao setar o DB do Pergamum: " . mssql_get_last_message()); -foreach( $_GET as $key => $value ) +foreach ($_GET as $key => $value) $$key = $value; - - diff --git a/pergamumws/teses.php b/pergamumws/teses.php index c79d438..9510d15 100644 --- a/pergamumws/teses.php +++ b/pergamumws/teses.php @@ -3,47 +3,45 @@ $color = true; -$query = mssql_query("select r.*, t.* -from acervo_geral ag, referencia r, tipo_obra t -where ag.cod_sit_acervo='0' -and not exists (select * from acervos_incompletos ai -where ai.cod_acervo = ag.cod_acervo -and ai.cod_tipo_obra = ag.cod_tipo_obra -and ai.cod_empresa = ag.cod_empresa) -and ag.cod_acervo = r.cod_acervo -and ag.cod_empresa = r.cod_empresa -and t.cod_empresa=18 -and t.cod_tipo_obra=ag.cod_tipo_obra -and ag.cod_tipo_obra in(6,9) -and (r.ano_publicacao = '".$ano."' or '".$ano."'='' ) -and (r.cod_acervo = '".$acervo."' or '".$acervo."'='' ) ---and r.ano_publicacao >= '2009' and r.ano_publicacao<='2009' ---and r.cod_acervo=348186 -order by r.ano_publicacao desc -",$db); +$query = mssql_query("SELECT r.*, t.* +FROM acervo_geral ag, referencia r, tipo_obra t +WHERE ag.cod_sit_acervo = '0' +AND NOT EXISTS (SELECT * FROM acervos_incompletos ai +WHERE ai.cod_acervo = ag.cod_acervo +AND ai.cod_tipo_obra = ag.cod_tipo_obra +AND ai.cod_empresa = ag.cod_empresa) +AND ag.cod_acervo = r.cod_acervo +AND ag.cod_empresa = r.cod_empresa +AND t.cod_empresa = 18 +AND t.cod_tipo_obra = ag.cod_tipo_obra +AND ag.cod_tipo_obra in(6,9) +AND (r.ano_publicacao = '" . $ano . "' or '" . $ano . "'='') +AND (r.cod_acervo = '" . $acervo . "' or '" . $acervo . "'='') +--AND r.ano_publicacao >= '2009' AND r.ano_publicacao <= '2009' +--AND r.cod_acervo=348186 +ORDER BY r.ano_publicacao DESC +", $db); $trabalhos = array(); -while($array1 = mssql_fetch_assoc($query)) -{ - #print_r($array1); - #die("teste"); +while ($array1 = mssql_fetch_assoc($query)) { + #print_r($array1); + #die("teste"); $t = array(); $url_link = null; - foreach($array1 as $k => $v){ - $t[$k] = utf8_encode(htmlentities($v,ENT_COMPAT, "ISO-8859-1" )); - //$t[$k] = utf8_encode(htmlentities($v )); + foreach ($array1 as $k => $v) { + $t[$k] = utf8_encode(htmlentities($v, ENT_COMPAT, "ISO-8859-1")); + //$t[$k] = utf8_encode(htmlentities($v )); } - $link1 = mssql_query("spwper_busca_links856 18,".$t['cod_acervo'],$db); + $link1 = mssql_query("spwper_busca_links856 18," . $t['cod_acervo'], $db); - while($reg_link = @mssql_fetch_array($link1)) - { - $url_link = $reg_link["descricao"]; - if(trim($url_link)=="") - $url_link = $reg_link["texto_descricao"]; - $t['links'] = htmlentities($url_link); - //$t['links'] = $url_link; + while ($reg_link = @mssql_fetch_array($link1)) { + $url_link = $reg_link["descricao"]; + if (trim($url_link) == "") + $url_link = $reg_link["texto_descricao"]; + $t['links'] = htmlentities($url_link); + //$t['links'] = $url_link; } $trabalhos[] = $t; } -header('Content-Type: text/javascript; charset=utf8'); +header('Content-Type: application/json; charset=utf8'); echo json_encode($trabalhos); diff --git a/src/classes/GenericItemSynchronizer.php b/src/classes/GenericItemSynchronizer.php index e30d6bc..ca60fa3 100644 --- a/src/classes/GenericItemSynchronizer.php +++ b/src/classes/GenericItemSynchronizer.php @@ -1,51 +1,61 @@ origin; } - public function setOriginRepository($o) { + public function setOriginRepository($o) + { $this->origin = $o; } //geters and seters for the repository to where the item will be imported - public function getTargetRepository() { + public function getTargetRepository() + { return $this->target; } - public function setTargetRepository($t) { + public function setTargetRepository($t) + { $this->target = $t; } //geters and seters for the metadata converter - public function getMetadataConverter() { + public function getMetadataConverter() + { return $this->metadataConverter; } - public function setMetadataConverter($m) { + public function setMetadataConverter($m) + { $this->metadataConverter = $m; } //synchronize repositories of a specific year - public function syncReposByYear($year) { + public function syncReposByYear($year) + { $itemList = $this->origin->getItemsByYear($year); $this->__syncRepos($itemList); } //synchronize entire repositories - public function syncRepos() { + public function syncRepos() + { $itemList = $this->origin->getAllItems(); $this->__syncRepos($itemList); } //synchronize entire repositories - private function __syncRepos($itemList) { + private function __syncRepos($itemList) + { if (!isset($this->metadataConverter)) { - throw Exception("Metadata converter is not set. Use setMetadataConverter before calling this method"); + throw new Exception("Metadata converter is not set. Use setMetadataConverter before calling this method"); } - echo "Syncing ".count($itemList)." items".PHP_EOL; + echo "Syncing " . count($itemList) . " items" . PHP_EOL; while (count($itemList) > 0) { $itemOrigin = array_shift($itemList); $itemTarget = $this->metadataConverter->convert($itemOrigin); @@ -58,4 +68,3 @@ private function __syncRepos($itemList) { } } } -?> diff --git a/src/classes/Pergamum.php b/src/classes/Pergamum.php index 2965e68..9a6a068 100644 --- a/src/classes/Pergamum.php +++ b/src/classes/Pergamum.php @@ -1,9 +1,11 @@ getItemsFromWS(); - } + public function getAllItems() + { + return $this->getItemsFromWS(); + } - public function getItemsByYear($year) { + public function getItemsByYear($year) + { return $this->getItemsFromWS("ano=" . $year); } - function setDefaultCollection($collection) { + function setDefaultCollection($collection) + { $this->defaultCollection = $collection; } - function __construct($wsURL) { + function __construct($wsURL) + { $this->wsURL = $wsURL; } - private function getItemsFromWS($args="") { + private function getItemsFromWS($args = "") + { $url = $this->wsURL; if ($args != "") { $url = $url . "teses.php?" . $args; } - echo "Calling ".$url.PHP_EOL; + echo "Calling " . $url . PHP_EOL; $html = implode('', file($url)); // Return a String. $phpNative = Zend\Json\Encoder::encodeUnicodeString($html); // Encodes the String $html $teses = Zend\Json\Json::decode($phpNative, Zend\Json\Json::TYPE_ARRAY); - $r = Array(); + $r = array(); for ($i = 0; $i <= sizeof($teses); $i++) { if (array_key_exists($i, $teses)) { $t = new ItemImpl(); @@ -71,10 +78,8 @@ private function getItemsFromWS($args="") { //Abstract comes from another database field if ($camposMarc[$j]["paragrafo"] == self::MARC_PARAGRAFO_ABSTRACT) { $value = html_entity_decode($camposMarc[$j]["texto_descricao"]); - //remove CR/LF from abstract - $value = str_replace(chr(13), '', $value); - $value = str_replace(chr(10), '', $value); - $value = str_replace("", '', $value); + //remove control characters from abstract + $value = preg_replace('/[[:cntrl:]]/', ' ', $value); } else { $value = html_entity_decode($camposMarc[$j]["descricao"]); } @@ -97,14 +102,15 @@ private function getItemsFromWS($args="") { } //returns a Thesis with a given id - public function getItem($id) { + public function getItem($id) + { return $this->getItemFromWS("acervo=" . $id); } //save a Thesis - public function saveItem($t) { + public function saveItem($t) + { //TODO: implement this throw new Exception("Saving items on Pergamum not available yet"); } } -?> From c9ead8b518213991cb217c8a75499a26398b44ee Mon Sep 17 00:00:00 2001 From: jaideraf Date: Thu, 26 Dec 2024 11:23:28 -0300 Subject: [PATCH 2/3] Update teses.php --- pergamumws/teses.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pergamumws/teses.php b/pergamumws/teses.php index 9510d15..43678f9 100644 --- a/pergamumws/teses.php +++ b/pergamumws/teses.php @@ -17,8 +17,8 @@ AND ag.cod_tipo_obra in(6,9) AND (r.ano_publicacao = '" . $ano . "' or '" . $ano . "'='') AND (r.cod_acervo = '" . $acervo . "' or '" . $acervo . "'='') ---AND r.ano_publicacao >= '2009' AND r.ano_publicacao <= '2009' ---AND r.cod_acervo=348186 +-- AND r.ano_publicacao >= '2009' AND r.ano_publicacao <= '2009' +-- AND r.cod_acervo=348186 ORDER BY r.ano_publicacao DESC ", $db); $trabalhos = array(); From 2109cba058e896cb54db56f75fdb69625820e027 Mon Sep 17 00:00:00 2001 From: Jaider Andrade Ferreira Date: Sun, 5 Jan 2025 11:45:16 -0300 Subject: [PATCH 3/3] Add .gitignore, EditorConfig, and improve code formatting across multiple files --- .editorconfig | 12 + .gitignore | 1 + composer.json | 5 + composer.lock | 99 ++ pergamumws/marc.php | 24 +- pergamumws/parametros.php | 7 +- pergamumws/teses.php | 78 +- ...Pergamum2DSPaceThesisMetadataConverter.php | 58 -- src/Pergamum2DSpace.php | 38 +- ...Pergamum2DSpaceThesisMetadataConverter.php | 62 ++ src/Pergamum2DSpaceThesisSynchronizer.php | 83 +- src/XLS2DSpace.php | 12 +- src/XLS2DSpaceThesisSynchronizer.php | 73 +- src/classes/DSPace.php | 845 +++++++++--------- src/classes/GenericItemSynchronizer.php | 120 +-- src/classes/Item.php | 39 +- src/classes/ItemImpl.php | 172 ++-- src/classes/ItemMetadataConverter.php | 64 +- src/classes/ItemSynchronizer.php | 23 +- src/classes/MetadataConverter.php | 7 +- src/classes/Observer.php | 7 +- src/classes/Pergamum.php | 214 ++--- src/classes/Repository.php | 19 +- src/classes/Subject.php | 5 +- src/classes/XLS.php | 231 ++--- tests/PergamumTest.php | 10 +- 26 files changed, 1255 insertions(+), 1053 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock delete mode 100644 src/Pergamum2DSPaceThesisMetadataConverter.php create mode 100644 src/Pergamum2DSpaceThesisMetadataConverter.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a1027e5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1122441 --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require-dev": { + "squizlabs/php_codesniffer": "*" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..7d6bdfb --- /dev/null +++ b/composer.lock @@ -0,0 +1,99 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "0ae1576e556ebadd2933ecd2483a4b26", + "packages": [], + "packages-dev": [ + { + "name": "squizlabs/php_codesniffer", + "version": "3.11.2", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", + "reference": "1368f4a58c3c52114b86b1abe8f4098869cb0079" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/1368f4a58c3c52114b86b1abe8f4098869cb0079", + "reference": "1368f4a58c3c52114b86b1abe8f4098869cb0079", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.3.4" + }, + "bin": [ + "bin/phpcbf", + "bin/phpcs" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "Former lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "Current lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/PHPCSStandards/PHP_CodeSniffer/issues", + "security": "https://github.com/PHPCSStandards/PHP_CodeSniffer/security/policy", + "source": "https://github.com/PHPCSStandards/PHP_CodeSniffer", + "wiki": "https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki" + }, + "funding": [ + { + "url": "https://github.com/PHPCSStandards", + "type": "github" + }, + { + "url": "https://github.com/jrfnl", + "type": "github" + }, + { + "url": "https://opencollective.com/php_codesniffer", + "type": "open_collective" + } + ], + "time": "2024-12-11T16:04:26+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": {}, + "prefer-stable": false, + "prefer-lowest": false, + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.6.0" +} diff --git a/pergamumws/marc.php b/pergamumws/marc.php index e13ffeb..986a2c4 100644 --- a/pergamumws/marc.php +++ b/pergamumws/marc.php @@ -1,22 +1,22 @@ - $v) { - $t[$k] = utf8_encode(htmlentities($v, ENT_COMPAT, "ISO-8859-1")); - #$t[$k] = utf8_encode(htmlentities($v )); - //$t[$k] = $v; - } - $trabalhos[] = $t; + $t = array(); + foreach ($array1 as $k => $v) { + $t[$k] = utf8_encode(htmlentities($v, ENT_COMPAT, "ISO-8859-1")); + // $t[$k] = utf8_encode(htmlentities($v )); + // $t[$k] = $v; + } + $trabalhos[] = $t; } header('Content-Type: text/javascript; charset=utf8'); echo json_encode($trabalhos); diff --git a/pergamumws/parametros.php b/pergamumws/parametros.php index 9bcf1ba..6026ae4 100644 --- a/pergamumws/parametros.php +++ b/pergamumws/parametros.php @@ -1,4 +1,4 @@ - $value) - $$key = $value; +foreach ($_GET as $key => $value) { + $$key = $value; +} diff --git a/pergamumws/teses.php b/pergamumws/teses.php index 43678f9..0c0babb 100644 --- a/pergamumws/teses.php +++ b/pergamumws/teses.php @@ -1,47 +1,51 @@ -= '2009' AND r.ano_publicacao <= '2009' --- AND r.cod_acervo=348186 -ORDER BY r.ano_publicacao DESC -", $db); +$query = mssql_query( + "SELECT r.*, t.* + FROM acervo_geral ag, referencia r, tipo_obra t + WHERE ag.cod_sit_acervo = '0' + AND NOT EXISTS (SELECT * FROM acervos_incompletos ai + WHERE ai.cod_acervo = ag.cod_acervo + AND ai.cod_tipo_obra = ag.cod_tipo_obra + AND ai.cod_empresa = ag.cod_empresa) + AND ag.cod_acervo = r.cod_acervo + AND ag.cod_empresa = r.cod_empresa + AND t.cod_empresa = 18 + AND t.cod_tipo_obra = ag.cod_tipo_obra + AND ag.cod_tipo_obra in(6,9) + AND (r.ano_publicacao = '" . $ano . "' or '" . $ano . "'='') + AND (r.cod_acervo = '" . $acervo . "' or '" . $acervo . "'='') + -- AND r.ano_publicacao >= '2009' AND r.ano_publicacao <= '2009' + -- AND r.cod_acervo=348186 + ORDER BY r.ano_publicacao DESC + ", + $db +); $trabalhos = array(); while ($array1 = mssql_fetch_assoc($query)) { - #print_r($array1); - #die("teste"); - $t = array(); - $url_link = null; - foreach ($array1 as $k => $v) { - $t[$k] = utf8_encode(htmlentities($v, ENT_COMPAT, "ISO-8859-1")); - //$t[$k] = utf8_encode(htmlentities($v )); - } + // print_r($array1); + // die("teste"); + $t = array(); + $url_link = null; + foreach ($array1 as $k => $v) { + $t[$k] = utf8_encode(htmlentities($v, ENT_COMPAT, "ISO-8859-1")); + // $t[$k] = utf8_encode(htmlentities($v )); + } - $link1 = mssql_query("spwper_busca_links856 18," . $t['cod_acervo'], $db); + $link1 = mssql_query("spwper_busca_links856 18," . $t['cod_acervo'], $db); - while ($reg_link = @mssql_fetch_array($link1)) { - $url_link = $reg_link["descricao"]; - if (trim($url_link) == "") - $url_link = $reg_link["texto_descricao"]; - $t['links'] = htmlentities($url_link); - //$t['links'] = $url_link; - } - $trabalhos[] = $t; + while ($reg_link = @mssql_fetch_array($link1)) { + $url_link = $reg_link["descricao"]; + if (trim($url_link) == "") { + $url_link = $reg_link["texto_descricao"]; + } + $t['links'] = htmlentities($url_link); + // $t['links'] = $url_link; + } + $trabalhos[] = $t; } header('Content-Type: application/json; charset=utf8'); echo json_encode($trabalhos); diff --git a/src/Pergamum2DSPaceThesisMetadataConverter.php b/src/Pergamum2DSPaceThesisMetadataConverter.php deleted file mode 100644 index b5d0e75..0000000 --- a/src/Pergamum2DSPaceThesisMetadataConverter.php +++ /dev/null @@ -1,58 +0,0 @@ -getMetadata(self::MARC_TITLE))[0]; - if ($marc->hasMetadataField(self::MARC_SUBTITLE)) { - $fullTitle = $fullTitle . ": " . array_values($marc->getMetadata(self::MARC_SUBTITLE))[0]; - } - - $dc->setMetadata(self::DC_TITLE, array($fullTitle)); - } - - //join MARC format fields in a single dublin core field - private function convertMarcFormat2DC($marc, $dc) { - if ($marc->hasMetadataField(self::MARC_FORMAT_A)) { - $format = array_values($marc->getMetadata(self::MARC_FORMAT_A))[0]; - if ($marc->hasMetadataField(self::MARC_FORMAT_B)) { - $format = $format . "| " . array_values($marc->getMetadata(self::MARC_FORMAT_B))[0]; - } - - $dc->setMetadata(self::DC_FORMAT, array($format)); - } - } - - private function formatType($r) { - if ($r->hasMetadataField(self::DC_TYPE_FIELD)) { - $type = array_values($r->getMetadata(self::DC_TYPE_FIELD))[0]; - $endIndex = strpos($type, ")"); - $type = substr($type, 0, $endIndex + 1); - $r->setMetadata(self::DC_TYPE_FIELD, array($type)); - } - } - - //returns all thesis available of a given year - public function convert($object) { - $r = parent::convert($object); - //fix title - $this->convertMarcTitle2DC($object, $r); - //fix format field - $this->convertMarcFormat2DC($object, $r); - //format type field - $this->formatType($r); - return $r; - } -} -?> diff --git a/src/Pergamum2DSpace.php b/src/Pergamum2DSpace.php index 338c5ca..7989472 100644 --- a/src/Pergamum2DSpace.php +++ b/src/Pergamum2DSpace.php @@ -1,25 +1,25 @@ ".PHP_EOL; - } +function printUsage() +{ + echo "Usage: Pergamum2DSpace.php -c " . PHP_EOL; +} - $configFile=""; - $opts=getopt("c:"); - if (array_key_exists("c", $opts)){ - $configFile=$opts["c"]; - }else{ - printUsage(); - die; - } +$configFile = ""; +$opts = getopt("c:"); +if (array_key_exists("c", $opts)) { + $configFile = $opts["c"]; +} else { + printUsage(); + die; +} - $sync = new Pergamum2DSpaceThesisSynchronizer($configFile); - $sync->syncReposByYear(date("Y")-1); - $sync->syncReposByYear(date("Y")); -?> +$sync = new Pergamum2DSpaceThesisSynchronizer($configFile); +$sync->syncReposByYear(date("Y") - 1); +$sync->syncReposByYear(date("Y")); diff --git a/src/Pergamum2DSpaceThesisMetadataConverter.php b/src/Pergamum2DSpaceThesisMetadataConverter.php new file mode 100644 index 0000000..0705df9 --- /dev/null +++ b/src/Pergamum2DSpaceThesisMetadataConverter.php @@ -0,0 +1,62 @@ +getMetadata(self::MARC_TITLE))[0]; + if ($marc->hasMetadataField(self::MARC_SUBTITLE)) { + $fullTitle = $fullTitle . ": " . array_values($marc->getMetadata(self::MARC_SUBTITLE))[0]; + } + + $dc->setMetadata(self::DC_TITLE, array($fullTitle)); + } + + // join MARC format fields in a single dublin core field + private function convertMarcFormat2DC($marc, $dc) + { + if ($marc->hasMetadataField(self::MARC_FORMAT_A)) { + $format = array_values($marc->getMetadata(self::MARC_FORMAT_A))[0]; + if ($marc->hasMetadataField(self::MARC_FORMAT_B)) { + $format = $format . "| " . array_values($marc->getMetadata(self::MARC_FORMAT_B))[0]; + } + + $dc->setMetadata(self::DC_FORMAT, array($format)); + } + } + + private function formatType($r) + { + if ($r->hasMetadataField(self::DC_TYPE_FIELD)) { + $type = array_values($r->getMetadata(self::DC_TYPE_FIELD))[0]; + $endIndex = strpos($type, ")"); + $type = substr($type, 0, $endIndex + 1); + $r->setMetadata(self::DC_TYPE_FIELD, array($type)); + } + } + + //r eturns all thesis available of a given year + public function convert($object) + { + $r = parent::convert($object); + // fix title + $this->convertMarcTitle2DC($object, $r); + // fix format field + $this->convertMarcFormat2DC($object, $r); + // format type field + $this->formatType($r); + return $r; + } +} diff --git a/src/Pergamum2DSpaceThesisSynchronizer.php b/src/Pergamum2DSpaceThesisSynchronizer.php index 20053d1..1547b96 100644 --- a/src/Pergamum2DSpaceThesisSynchronizer.php +++ b/src/Pergamum2DSpaceThesisSynchronizer.php @@ -1,48 +1,53 @@ config = $zc->fromFile($configFile); - } + private function loadConfig($configFile) + { + if (!file_exists($configFile)) { + throw new Exception($configFile . " does not exist"); + } + $zc = new Zend\Config\Reader\Ini(); + $this->config = $zc->fromFile($configFile); + } - private function getMapFile() { - $mapfile = $this->config['mapfile']; - if ($mapfile == "") { - throw new Exception("config.ini must have a mapfile specified"); - } - if (!file_exists($mapfile)) { - throw new Exception("Map file '" . $mapfile . "' does not exist"); - } - return $mapfile; - } + private function getMapFile() + { + $mapfile = $this->config['mapfile']; + if ($mapfile == "") { + throw new Exception("config.ini must have a mapfile specified"); + } + if (!file_exists($mapfile)) { + throw new Exception("Map file '" . $mapfile . "' does not exist"); + } + return $mapfile; + } - function __construct($configFile) { - $this->loadConfig($configFile); - $this->setMetadataConverter(new Pergamum2DSPaceThesisMetadataConverter($this->getMapFile())); - $pergamum = new Pergamum($this->config['pergamum-ws-url']); - $pergamum->setDefaultCollection($this->config['pergamum-default-collection']); - $this->setOriginRepository($pergamum); - $dspace = new DSpace($this->config['dspace-url'], $this->config['dspace-username'], $this->config['dspace-password']); - $dspace->setBaseCommunity($this->config['dspace-thesis-community']); - $dspace->register($this); - $this->setTargetRepository($dspace); - } + function __construct($configFile) + { + $this->loadConfig($configFile); + $this->setMetadataConverter(new Pergamum2DSpaceThesisMetadataConverter($this->getMapFile())); + $pergamum = new Pergamum($this->config['pergamum-ws-url']); + $pergamum->setDefaultCollection($this->config['pergamum-default-collection']); + $this->setOriginRepository($pergamum); + $dspace = new DSpace($this->config['dspace-url'], $this->config['dspace-username'], $this->config['dspace-password']); + $dspace->setBaseCommunity($this->config['dspace-thesis-community']); + $dspace->register($this); + $this->setTargetRepository($dspace); + } } -?> diff --git a/src/XLS2DSpace.php b/src/XLS2DSpace.php index 113d0f9..8c07b46 100644 --- a/src/XLS2DSpace.php +++ b/src/XLS2DSpace.php @@ -3,19 +3,19 @@ require_once realpath('C:\Users\04574440961\vendor/autoload.php'); require 'XLS2DSpaceThesisSynchronizer.php'; -function printUsage() { - echo "Usage: XLS2DSpace.php -c " . PHP_EOL; +function printUsage() +{ + echo "Usage: XLS2DSpace.php -c " . PHP_EOL; } $configFile = ""; $opts = getopt("c:"); if (array_key_exists("c", $opts)) { - $configFile = $opts["c"]; + $configFile = $opts["c"]; } else { - printUsage(); - die; + printUsage(); + die; } $sync = new XLS2DSpaceThesisSynchronizer($configFile); $sync->syncRepos(); -?> \ No newline at end of file diff --git a/src/XLS2DSpaceThesisSynchronizer.php b/src/XLS2DSpaceThesisSynchronizer.php index 388ec46..4c12fd4 100644 --- a/src/XLS2DSpaceThesisSynchronizer.php +++ b/src/XLS2DSpaceThesisSynchronizer.php @@ -5,44 +5,49 @@ require 'classes\XLS.php'; require 'classes\DSpace.php'; require 'classes\Observer.php'; + use Zend\Config; -class XLS2DSpaceThesisSynchronizer extends GenericItemSynchronizer implements Observer { - private $config; +class XLS2DSpaceThesisSynchronizer extends GenericItemSynchronizer implements Observer +{ + private $config; - public function update($event) { - echo $event . PHP_EOL; - } + public function update($event) + { + echo $event . PHP_EOL; + } - private function loadConfig($configFile) { - if (!file_exists($configFile)) { - throw new Exception($configFile . " does not exist"); - } - $zc = new Zend\Config\Reader\Ini(); - $this->config = $zc->fromFile($configFile); - } + private function loadConfig($configFile) + { + if (!file_exists($configFile)) { + throw new Exception($configFile . " does not exist"); + } + $zc = new Zend\Config\Reader\Ini(); + $this->config = $zc->fromFile($configFile); + } - private function getMapFile() { - $mapfile = $this->config['mapfile']; - if ($mapfile == "") { - throw new Exception("config.ini must have a mapfile specified"); - } - if (!file_exists($mapfile)) { - throw new Exception("Map file '" . $mapfile . "' does not exist"); - } - return $mapfile; - } + private function getMapFile() + { + $mapfile = $this->config['mapfile']; + if ($mapfile == "") { + throw new Exception("config.ini must have a mapfile specified"); + } + if (!file_exists($mapfile)) { + throw new Exception("Map file '" . $mapfile . "' does not exist"); + } + return $mapfile; + } - function __construct($configFile) { - $this->loadConfig($configFile); - $this->setMetadataConverter(new ItemMetadataConverter($this->getMapFile())); - $xls = new XLS($this->config['source']); - $xls->setDefaultCollection($this->config['default-collection']); - $this->setOriginRepository($xls); - $dspace = new DSpace($this->config['dspace-url'], $this->config['dspace-username'], $this->config['dspace-password']); - $dspace->setBaseCommunity($this->config['dspace-thesis-community']); - $dspace->register($this); - $this->setTargetRepository($dspace); - } + function __construct($configFile) + { + $this->loadConfig($configFile); + $this->setMetadataConverter(new ItemMetadataConverter($this->getMapFile())); + $xls = new XLS($this->config['source']); + $xls->setDefaultCollection($this->config['default-collection']); + $this->setOriginRepository($xls); + $dspace = new DSpace($this->config['dspace-url'], $this->config['dspace-username'], $this->config['dspace-password']); + $dspace->setBaseCommunity($this->config['dspace-thesis-community']); + $dspace->register($this); + $this->setTargetRepository($dspace); + } } -?> \ No newline at end of file diff --git a/src/classes/DSPace.php b/src/classes/DSPace.php index 8615153..968fafe 100644 --- a/src/classes/DSPace.php +++ b/src/classes/DSPace.php @@ -6,410 +6,443 @@ require 'Subject.php'; -class DSPace implements Repository, Subject { - - const ITEM_KEY_FIELD = "dc.identifier.other"; - const ITEM_TITLE_FIELD = 'dc.title'; - private $baseCommunity; - private $collections; - private $dspaceURL; - private $dspaceRestCookie; - private $dspaceUsername; - private $dspacePassword; - private $observers; - - function __construct($dspaceURL, $dspaceUsername, $dspacePassword) { - $this->dspaceURL = $dspaceURL; - $this->dspaceUsername = $dspaceUsername; - $this->dspacePassword = $dspacePassword; - $this->observers = Array(); - } - - //returns all items available of a given year - public function getItemsByYear($year) { - //TODO: implement this - } - - public function getAllItems() { - } - - //returns a item with a given id - public function getItem($id) { - //TODO - } - - public function setBaseCommunity($c) { - $this->baseCommunity = $c; - } - - public function register($observer) { - $this->observers[] = $observer; - } - - public function notify($event) { - foreach ($this->observers as $obs) { - $obs->update($event); - } - } - - /*private function generateXml($t) { - $doc = new DOMDocument(); - $doc->version = '1.0'; - $doc->encoding = 'UTF-8'; - $doc->standalone = "no"; - $dublinCoreElement = $doc->createElement('dublin_core'); - $dublinCoreAttribute = $doc->createAttribute('schema'); - $dublinCoreAttribute->value = 'dc'; - $dublinCoreElement->appendChild($dublinCoreAttribute); - $doc->appendChild($dublinCoreElement); - - foreach ($t->getMedatataFields() as $field) { - $value = $t->getMetadata($field); - $dcvalueElement = $doc->createElement('dcvalue', $value); - $elements = explode('.', $field); - foreach ($elements as $key => $value) { - $attr = ""; - switch ($key) { - case 0: - continue; //ignore dc - case 1: - $attr = "element"; - break; - case 2: - $attr = "qualifier"; - break; - case 3: - $attr = "language"; - break; - default: - continue; - } - if ($attr != "" && $value != "") { - $dcvalueAttribute = $doc->createAttribute($attr); - $dcvalueAttribute->value = $value; - $dcvalueElement->appendChild($dcvalueAttribute); - $dublinCoreElement->appendChild($dcvalueElement); - } - } - } - $doc->formatOutput = true; - return $doc; - }*/ - - private function saveUsingSimpleArchiveFormat($t) { - $xml = $this->generateXml($t); - print_r($xml); - $xml->save('teste.xml'); - $xml->save($diretorio_import . "$i/" . 'dublin_core.xml'); - //TODO: finish this implementation - } - - private function restGet($url) { - //Disable paging - $url = $url . "?limit=10000000&itemsLimit=10000000"; - $html = implode('', file($url)); // Return a String. - $phpNative = Zend\Json\Encoder::encodeUnicodeString($html); // Encodes the String $html - return Zend\Json\Json::decode($phpNative, Zend\Json\Json::TYPE_ARRAY); - } - - private function restGetZend($url) { - $request = new Request(); - $request->setMethod(Request::METHOD_GET); - //Disable paging - $url = $url . "?limit=0&itemsLimit=0"; - $request->setUri($url); - //$request->setContent($data); - $client = new Zend\Http\Client(); - - $request->getHeaders()->addHeaders(array( - 'limit' => '1000', - 'itemsLimit' => '1000', - )); - - $response = $client->send($request); - if ($response->getStatusCode() != 200) { - throw new Exception("Error Sending GET rest request at " . $url . " Error message: " . $response->getContent()); - } - print_r($response); - die; - return Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); - } - - private function restAuth() { - if (!isset($this->dspaceRestCookie)) { - $url = $this->dspaceURL . "/rest/login"; - $data = "email=" . $this->dspaceUsername . "&password=" . $this->dspacePassword; - $response = $this->restPost($url, $data); - if ($response->getStatusCode() == 200) { - $cookie = $response->getHeaders()->get('setcookie')->current()->getFieldValue(); - $this->dspaceRestCookie = substr($cookie, strpos($cookie, '=') + 1, strpos($cookie, ';') - strpos($cookie, '=') - 1); - } else { - throw new Exception("Fail login in rest API: " . $response->getContent()); - } - } - } - - private function restPost($url, $data) { - //echo "URL: " . $url . PHP_EOL; - $request = new Request(); - $request->setMethod(Request::METHOD_POST); - $request->setUri($url); - if ($data != "") { - //echo "Data: " . $data . PHP_EOL; - $request->setContent($data); - } - $client = new Zend\Http\Client(); - $client->setOptions(array( - 'maxredirects' => 0, - 'timeout' => 600, - )); - if (isset($this->dspaceRestCookie)) { - $request->getHeaders()->addHeader(new Cookie(array('JSESSIONID' => $this->dspaceRestCookie))); - $request->getHeaders()->addHeaders(array( - 'accept' => 'application/json', - )); - $request->getHeaders()->addHeaders(array( - 'Content-Type' => 'application/json', - )); - } - $response = $client->send($request); - if ($response->getStatusCode() != 200) { - echo "Data: " . $data . PHP_EOL; - throw new Exception("Error Sending POST rest request at " . $url . " Error message: " . $response->getContent() . PHP_EOL); - } - return $response; - } - - private function restUpload($url, $file) { - /* CURL version of upload - $ch = curl_init(); - - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_HTTPHEADER, array( - 'Cookie: JSESSIONID=' . $this->dspaceRestCookie, - )); - - if (function_exists('curl_file_create')) { - // php 5.5+ - $cFile = curl_file_create($file); - } else { - // - $cFile = '@' . realpath($file); - } - $post = array('extra_info' => '123456', 'file_contents' => $cFile); - echo "URL: " . $url . PHP_EOL; - echo "Uploading File: " . $file . PHP_EOL; - curl_setopt($ch, CURLOPT_POSTFIELDS, $post); - - $result = curl_exec($ch); - curl_close($ch); - //echo PHP_EOL . "Resultado: " . $result . PHP_EOL; - */ - - //echo "URL: " . $url . PHP_EOL; - $client = new Zend\Http\Client(); - $client->setMethod(Request::METHOD_POST); - $client->setUri($url); - - $client->setHeaders(array( - 'accept' => 'application/json', - )); - if (isset($this->dspaceRestCookie)) { - $client->addCookie('JSESSIONID', $this->dspaceRestCookie); - } - //echo "File: " . $file . PHP_EOL; - $client->setFileUpload($file, basename($file)); - $response = $client->send(); - if ($response->getStatusCode() != 200) { - throw new Exception("Error Sending POST UPLOAD rest request at " . $url . " Error message: " . $response->getContent()); - } - return $response; - } - - private function restDelete($url) { - $request = new Request(); - $request->setMethod(Request::METHOD_DELETE); - $request->setUri($url); - $client = new Zend\Http\Client(); - if (isset($this->dspaceRestCookie)) { - $request->getHeaders()->addHeader(new Cookie(array('JSESSIONID' => $this->dspaceRestCookie))); - } - $response = $client->send($request); - if ($response->getStatusCode() != 200) { - throw new Exception("Error Sending DELETE rest request at " . $url . " Error message: " . $response->getContent()); - } - return $response; - } - - private function getCollections() { - $url = $this->dspaceURL . "/rest/communities/" . $this->baseCommunity . "/collections"; - return $this->restGet($url); - } - - private function getCollection($name) { - if (!isset($this->collections)) { - $this->collections = $this->getCollections(); - } - - foreach ($this->collections as $collection) { - if (trim($collection['name']) == trim($name)) { - return $collection['uuid']; - } - } - - return ""; - } - - private function getBitstreams($itemUUID) { - $url = $this->dspaceURL . "/rest/items/" . $itemUUID . "/bitstreams"; - $response = $this->restGet($url); - return $response; - } - - private function getItemByField($field, $value) { - $this->notify("Finding item '" . $value . "' by field '" . $field . "'"); - $url = $this->dspaceURL . "/rest/items/find-by-metadata-field"; - $data = '{"key":"' . $field . '", "value":"' . $value . '"}'; - $response = $this->restPost($url, $data); - $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); - return $response; - } - - private function deleteItem($itemUUID) { - $url = $this->dspaceURL . "/rest/items/" . $itemUUID; - $response = $this->restDelete($url); - return $response; - } - - private function createItem($collectionUUID, $data) { - $url = $this->dspaceURL . "/rest/collections/" . $collectionUUID . "/items"; - $response = $this->restPost($url, $data); - $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); - return $response['uuid']; - } - - private function addItemMetadata($itemUUID, $newMetadata) { - $url = $this->dspaceURL . "/rest/items/" . $itemUUID . "/metadata"; - $response = $this->restPost($url, $newMetadata); - $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); - return $response['uuid']; - } - - private function addItemBitstream($itemUUID, $file) { - //Create bitstream - $url = $this->dspaceURL . "/rest/items/" . $itemUUID . '/bitstreams?name=' . basename($file); - $tempDir = "./tmp"; - if (!file_exists($tempDir)) { - mkdir($tempDir); - } - $tempFile = $tempDir . "/" . basename($file); - $filePDF = fopen($tempFile, "w+"); - fwrite($filePDF, file_get_contents($file)); - fclose($filePDF); - - $response = $this->restUpload($url, $tempFile); - $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); - $bitstreamUUID = $response['uuid']; - unlink($tempFile); - return $bitstreamUUID; - } - - private function createCollection($name) { - $url = $this->dspaceURL . "/rest/communities/" . $this->baseCommunity . "/collections"; - $data = '{"name":"' . $name . '"}'; - $response = $this->restPost($url, $data); - - //Invalidate collections cache - unset($this->collections); - $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); - $this->notify("Collection created:" . $response['uuid']); - return $response['uuid']; - } - - private function generateItemJson($i) { - $output = "{\"metadata\":["; - $first = 0; - foreach ($i->getMedatataFields() as $field) { - foreach ($i->getMetadata($field) as $value) { - if ($first == 0) { - $first = 1; - } else { - $output = $output . ','; - } - //Check if field has language component - $language = ""; - if (substr_count($field, '.') > 2) { - $pos = strrpos($field, '.'); - $language = substr($field, $pos + 1); - $field = substr($field, 0, $pos); - } - - $output = $output . '{"key":"' . $field . '","value":"' . str_replace("\t", '\t', str_replace("\n", '\n', str_replace('"', '\"', $value))) . '"'; - if ($language != "") { - $output = $output . ',"language":"' . $language . '"'; - } - $output = $output . '}'; - } - } - $output = $output . "]}"; - return $output; - } - - private function saveUsingRestApi($t) { - if (!isset($this->dspaceURL)) { - throw new Exception("Variable dspaceURL is not set."); - } - - //Auth - $this->restAuth(); - - //Check target collection - $collectionUUID = $this->getCollection($t->getCollection()); - if ($collectionUUID == "") { - $collectionUUID = $this->createCollection($t->getCollection()); - } - - //Check if item exists - $items = $this->getItemByField(self::ITEM_KEY_FIELD, $t->getId()); - if (count($items) == 0) { - $this->notify("Creating item:" . $t->getId()); - $itemUUID = $this->createItem($collectionUUID, $this->generateItemJson($t)); - $this->notify("Item created:" . $itemUUID . " on collection " . $collectionUUID); - - //add bitstreams - foreach ($t->getFiles() as $file) { - $bitstreamUUID = $this->addItemBitstream($itemUUID, $file); - $this->notify("Item bitstream added:" . $bitstreamUUID); - } - } else { - $item = array_shift($items); - $itemUUID = $item['uuid']; - //TODO: update item metadata - //$this->addItemMetadata($itemUUID, $t->metadataToString()); - - if (count($this->getBitstreams($itemUUID)) == 0) { - //add bitstreams - foreach ($t->getFiles() as $file) { - $bitstreamUUID = $this->addItemBitstream($itemUUID, $file); - $this->notify("Item bitstream added:" . $bitstreamUUID); - } - } - } - - } - - //save a Thesis - public function saveItem($t) { - if (!isset($this->baseCommunity)) { - throw new Exception("Variable baseCommunity is not set. Use setBaseCommunity"); - } - try { - $this->saveUsingRestApi($t); - } catch (Exception $e) { - $this->notify("Error saving item: " . $e->getMessage()); - } - - } +class DSPace implements Repository, Subject +{ + const ITEM_KEY_FIELD = "dc.identifier.other"; + const ITEM_TITLE_FIELD = 'dc.title'; + private $baseCommunity; + private $collections; + private $dspaceURL; + private $dspaceRestCookie; + private $dspaceUsername; + private $dspacePassword; + private $observers; + + function __construct($dspaceURL, $dspaceUsername, $dspacePassword) + { + $this->dspaceURL = $dspaceURL; + $this->dspaceUsername = $dspaceUsername; + $this->dspacePassword = $dspacePassword; + $this->observers = array(); + } + + //returns all items available of a given year + public function getItemsByYear($year) + { + //TODO: implement this + } + + public function getAllItems() + { + } + + //returns a item with a given id + public function getItem($id) + { + //TODO + } + + public function setBaseCommunity($c) + { + $this->baseCommunity = $c; + } + + public function register($observer) + { + $this->observers[] = $observer; + } + + public function notify($event) + { + foreach ($this->observers as $obs) { + $obs->update($event); + } + } + + /*private function generateXml($t) { + $doc = new DOMDocument(); + $doc->version = '1.0'; + $doc->encoding = 'UTF-8'; + $doc->standalone = "no"; + $dublinCoreElement = $doc->createElement('dublin_core'); + $dublinCoreAttribute = $doc->createAttribute('schema'); + $dublinCoreAttribute->value = 'dc'; + $dublinCoreElement->appendChild($dublinCoreAttribute); + $doc->appendChild($dublinCoreElement); + + foreach ($t->getMedatataFields() as $field) { + $value = $t->getMetadata($field); + $dcvalueElement = $doc->createElement('dcvalue', $value); + $elements = explode('.', $field); + foreach ($elements as $key => $value) { + $attr = ""; + switch ($key) { + case 0: + continue; //ignore dc + case 1: + $attr = "element"; + break; + case 2: + $attr = "qualifier"; + break; + case 3: + $attr = "language"; + break; + default: + continue; + } + if ($attr != "" && $value != "") { + $dcvalueAttribute = $doc->createAttribute($attr); + $dcvalueAttribute->value = $value; + $dcvalueElement->appendChild($dcvalueAttribute); + $dublinCoreElement->appendChild($dcvalueElement); + } + } + } + $doc->formatOutput = true; + return $doc; + }*/ + + private function saveUsingSimpleArchiveFormat($t) + { + $xml = $this->generateXml($t); + print_r($xml); + $xml->save('teste.xml'); + $xml->save($diretorio_import . "$i/" . 'dublin_core.xml'); + //TODO: finish this implementation + } + + private function restGet($url) + { + //Disable paging + $url = $url . "?limit=10000000&itemsLimit=10000000"; + $html = implode('', file($url)); // Return a String. + $phpNative = Zend\Json\Encoder::encodeUnicodeString($html); // Encodes the String $html + return Zend\Json\Json::decode($phpNative, Zend\Json\Json::TYPE_ARRAY); + } + + private function restGetZend($url) + { + $request = new Request(); + $request->setMethod(Request::METHOD_GET); + //Disable paging + $url = $url . "?limit=0&itemsLimit=0"; + $request->setUri($url); + //$request->setContent($data); + $client = new Zend\Http\Client(); + + $request->getHeaders()->addHeaders( + array( + 'limit' => '1000', + 'itemsLimit' => '1000', + ) + ); + + $response = $client->send($request); + if ($response->getStatusCode() != 200) { + throw new Exception("Error Sending GET rest request at " . $url . " Error message: " . $response->getContent()); + } + print_r($response); + die; + return Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); + } + + private function restAuth() + { + if (!isset($this->dspaceRestCookie)) { + $url = $this->dspaceURL . "/rest/login"; + $data = "email=" . $this->dspaceUsername . "&password=" . $this->dspacePassword; + $response = $this->restPost($url, $data); + if ($response->getStatusCode() == 200) { + $cookie = $response->getHeaders()->get('setcookie')->current()->getFieldValue(); + $this->dspaceRestCookie = substr($cookie, strpos($cookie, '=') + 1, strpos($cookie, ';') - strpos($cookie, '=') - 1); + } else { + throw new Exception("Fail login in rest API: " . $response->getContent()); + } + } + } + + private function restPost($url, $data) + { + //echo "URL: " . $url . PHP_EOL; + $request = new Request(); + $request->setMethod(Request::METHOD_POST); + $request->setUri($url); + if ($data != "") { + //echo "Data: " . $data . PHP_EOL; + $request->setContent($data); + } + $client = new Zend\Http\Client(); + $client->setOptions( + array( + 'maxredirects' => 0, + 'timeout' => 600, + ) + ); + if (isset($this->dspaceRestCookie)) { + $request->getHeaders()->addHeader(new Cookie(array('JSESSIONID' => $this->dspaceRestCookie))); + $request->getHeaders()->addHeaders( + array( + 'accept' => 'application/json', + ) + ); + $request->getHeaders()->addHeaders( + array( + 'Content-Type' => 'application/json', + ) + ); + } + $response = $client->send($request); + if ($response->getStatusCode() != 200) { + echo "Data: " . $data . PHP_EOL; + throw new Exception("Error Sending POST rest request at " . $url . " Error message: " . $response->getContent() . PHP_EOL); + } + return $response; + } + + private function restUpload($url, $file) + { + /* CURL version of upload + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Cookie: JSESSIONID=' . $this->dspaceRestCookie, + )); + + if (function_exists('curl_file_create')) { + // php 5.5+ + $cFile = curl_file_create($file); + } else { + // + $cFile = '@' . realpath($file); + } + $post = array('extra_info' => '123456', 'file_contents' => $cFile); + echo "URL: " . $url . PHP_EOL; + echo "Uploading File: " . $file . PHP_EOL; + curl_setopt($ch, CURLOPT_POSTFIELDS, $post); + + $result = curl_exec($ch); + curl_close($ch); + //echo PHP_EOL . "Resultado: " . $result . PHP_EOL; + */ + + //echo "URL: " . $url . PHP_EOL; + $client = new Zend\Http\Client(); + $client->setMethod(Request::METHOD_POST); + $client->setUri($url); + + $client->setHeaders( + array( + 'accept' => 'application/json', + ) + ); + if (isset($this->dspaceRestCookie)) { + $client->addCookie('JSESSIONID', $this->dspaceRestCookie); + } + //echo "File: " . $file . PHP_EOL; + $client->setFileUpload($file, basename($file)); + $response = $client->send(); + if ($response->getStatusCode() != 200) { + throw new Exception("Error Sending POST UPLOAD rest request at " . $url . " Error message: " . $response->getContent()); + } + return $response; + } + + private function restDelete($url) + { + $request = new Request(); + $request->setMethod(Request::METHOD_DELETE); + $request->setUri($url); + $client = new Zend\Http\Client(); + if (isset($this->dspaceRestCookie)) { + $request->getHeaders()->addHeader(new Cookie(array('JSESSIONID' => $this->dspaceRestCookie))); + } + $response = $client->send($request); + if ($response->getStatusCode() != 200) { + throw new Exception("Error Sending DELETE rest request at " . $url . " Error message: " . $response->getContent()); + } + return $response; + } + + private function getCollections() + { + $url = $this->dspaceURL . "/rest/communities/" . $this->baseCommunity . "/collections"; + return $this->restGet($url); + } + + private function getCollection($name) + { + if (!isset($this->collections)) { + $this->collections = $this->getCollections(); + } + + foreach ($this->collections as $collection) { + if (trim($collection['name']) == trim($name)) { + return $collection['uuid']; + } + } + + return ""; + } + + private function getBitstreams($itemUUID) + { + $url = $this->dspaceURL . "/rest/items/" . $itemUUID . "/bitstreams"; + $response = $this->restGet($url); + return $response; + } + + private function getItemByField($field, $value) + { + $this->notify("Finding item '" . $value . "' by field '" . $field . "'"); + $url = $this->dspaceURL . "/rest/items/find-by-metadata-field"; + $data = '{"key":"' . $field . '", "value":"' . $value . '"}'; + $response = $this->restPost($url, $data); + $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); + return $response; + } + + private function deleteItem($itemUUID) + { + $url = $this->dspaceURL . "/rest/items/" . $itemUUID; + $response = $this->restDelete($url); + return $response; + } + + private function createItem($collectionUUID, $data) + { + $url = $this->dspaceURL . "/rest/collections/" . $collectionUUID . "/items"; + $response = $this->restPost($url, $data); + $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); + return $response['uuid']; + } + + private function addItemMetadata($itemUUID, $newMetadata) + { + $url = $this->dspaceURL . "/rest/items/" . $itemUUID . "/metadata"; + $response = $this->restPost($url, $newMetadata); + $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); + return $response['uuid']; + } + + private function addItemBitstream($itemUUID, $file) + { + //Create bitstream + $url = $this->dspaceURL . "/rest/items/" . $itemUUID . '/bitstreams?name=' . basename($file); + $tempDir = "./tmp"; + if (!file_exists($tempDir)) { + mkdir($tempDir); + } + $tempFile = $tempDir . "/" . basename($file); + $filePDF = fopen($tempFile, "w+"); + fwrite($filePDF, file_get_contents($file)); + fclose($filePDF); + + $response = $this->restUpload($url, $tempFile); + $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); + $bitstreamUUID = $response['uuid']; + unlink($tempFile); + return $bitstreamUUID; + } + + private function createCollection($name) + { + $url = $this->dspaceURL . "/rest/communities/" . $this->baseCommunity . "/collections"; + $data = '{"name":"' . $name . '"}'; + $response = $this->restPost($url, $data); + + //Invalidate collections cache + unset($this->collections); + $response = Zend\Json\Json::decode($response->getContent(), Zend\Json\Json::TYPE_ARRAY); + $this->notify("Collection created:" . $response['uuid']); + return $response['uuid']; + } + + private function generateItemJson($i) + { + $output = "{\"metadata\":["; + $first = 0; + foreach ($i->getMedatataFields() as $field) { + foreach ($i->getMetadata($field) as $value) { + if ($first == 0) { + $first = 1; + } else { + $output = $output . ','; + } + //Check if field has language component + $language = ""; + if (substr_count($field, '.') > 2) { + $pos = strrpos($field, '.'); + $language = substr($field, $pos + 1); + $field = substr($field, 0, $pos); + } + + $output = $output . '{"key":"' . $field . '","value":"' . str_replace("\t", '\t', str_replace("\n", '\n', str_replace('"', '\"', $value))) . '"'; + if ($language != "") { + $output = $output . ',"language":"' . $language . '"'; + } + $output = $output . '}'; + } + } + $output = $output . "]}"; + return $output; + } + + private function saveUsingRestApi($t) + { + if (!isset($this->dspaceURL)) { + throw new Exception("Variable dspaceURL is not set."); + } + + //Auth + $this->restAuth(); + + //Check target collection + $collectionUUID = $this->getCollection($t->getCollection()); + if ($collectionUUID == "") { + $collectionUUID = $this->createCollection($t->getCollection()); + } + + //Check if item exists + $items = $this->getItemByField(self::ITEM_KEY_FIELD, $t->getId()); + if (count($items) == 0) { + $this->notify("Creating item:" . $t->getId()); + $itemUUID = $this->createItem($collectionUUID, $this->generateItemJson($t)); + $this->notify("Item created:" . $itemUUID . " on collection " . $collectionUUID); + + //add bitstreams + foreach ($t->getFiles() as $file) { + $bitstreamUUID = $this->addItemBitstream($itemUUID, $file); + $this->notify("Item bitstream added:" . $bitstreamUUID); + } + } else { + $item = array_shift($items); + $itemUUID = $item['uuid']; + //TODO: update item metadata + //$this->addItemMetadata($itemUUID, $t->metadataToString()); + + if (count($this->getBitstreams($itemUUID)) == 0) { + //add bitstreams + foreach ($t->getFiles() as $file) { + $bitstreamUUID = $this->addItemBitstream($itemUUID, $file); + $this->notify("Item bitstream added:" . $bitstreamUUID); + } + } + } + } + + //save a Thesis + public function saveItem($t) + { + if (!isset($this->baseCommunity)) { + throw new Exception("Variable baseCommunity is not set. Use setBaseCommunity"); + } + try { + $this->saveUsingRestApi($t); + } catch (Exception $e) { + $this->notify("Error saving item: " . $e->getMessage()); + } + } } -?> diff --git a/src/classes/GenericItemSynchronizer.php b/src/classes/GenericItemSynchronizer.php index ca60fa3..ea8c3fb 100644 --- a/src/classes/GenericItemSynchronizer.php +++ b/src/classes/GenericItemSynchronizer.php @@ -2,69 +2,69 @@ require 'ItemSynchronizer.php'; class GenericItemSynchronizer implements ItemSynchronizer { - private $origin; - private $target; - private $metadataConverter; - //geters and seters for the repository from where the item will be extracted - public function getOriginRepository() - { - return $this->origin; - } - public function setOriginRepository($o) - { - $this->origin = $o; - } + private $origin; + private $target; + private $metadataConverter; + //geters and seters for the repository from where the item will be extracted + public function getOriginRepository() + { + return $this->origin; + } + public function setOriginRepository($o) + { + $this->origin = $o; + } - //geters and seters for the repository to where the item will be imported - public function getTargetRepository() - { - return $this->target; - } - public function setTargetRepository($t) - { - $this->target = $t; - } + //geters and seters for the repository to where the item will be imported + public function getTargetRepository() + { + return $this->target; + } + public function setTargetRepository($t) + { + $this->target = $t; + } - //geters and seters for the metadata converter - public function getMetadataConverter() - { - return $this->metadataConverter; - } - public function setMetadataConverter($m) - { - $this->metadataConverter = $m; - } + //geters and seters for the metadata converter + public function getMetadataConverter() + { + return $this->metadataConverter; + } + public function setMetadataConverter($m) + { + $this->metadataConverter = $m; + } - //synchronize repositories of a specific year - public function syncReposByYear($year) - { - $itemList = $this->origin->getItemsByYear($year); - $this->__syncRepos($itemList); - } + //synchronize repositories of a specific year + public function syncReposByYear($year) + { + $itemList = $this->origin->getItemsByYear($year); + $this->__syncRepos($itemList); + } - //synchronize entire repositories - public function syncRepos() - { - $itemList = $this->origin->getAllItems(); - $this->__syncRepos($itemList); - } + //synchronize entire repositories + public function syncRepos() + { + $itemList = $this->origin->getAllItems(); + $this->__syncRepos($itemList); + } - //synchronize entire repositories - private function __syncRepos($itemList) - { - if (!isset($this->metadataConverter)) { - throw new Exception("Metadata converter is not set. Use setMetadataConverter before calling this method"); - } - echo "Syncing " . count($itemList) . " items" . PHP_EOL; - while (count($itemList) > 0) { - $itemOrigin = array_shift($itemList); - $itemTarget = $this->metadataConverter->convert($itemOrigin); - $itemTarget->setId($itemOrigin->getId()); - $itemTarget->setCollection($itemOrigin->getCollection()); - foreach ($itemOrigin->getFiles() as $file) { - $itemTarget->addFile($file); - } - $this->target->saveItem($itemTarget); - } - } + //synchronize entire repositories + private function __syncRepos($itemList) + { + if (!isset($this->metadataConverter)) { + throw new Exception("Metadata converter is not set. Use setMetadataConverter before calling this method"); + } + echo "Syncing " . count($itemList) . " items" . PHP_EOL; + while (count($itemList) > 0) { + $itemOrigin = array_shift($itemList); + $itemTarget = $this->metadataConverter->convert($itemOrigin); + $itemTarget->setId($itemOrigin->getId()); + $itemTarget->setCollection($itemOrigin->getCollection()); + foreach ($itemOrigin->getFiles() as $file) { + $itemTarget->addFile($file); + } + $this->target->saveItem($itemTarget); + } + } } diff --git a/src/classes/Item.php b/src/classes/Item.php index 1c764b7..c72a52f 100644 --- a/src/classes/Item.php +++ b/src/classes/Item.php @@ -1,27 +1,28 @@ \ No newline at end of file diff --git a/src/classes/ItemImpl.php b/src/classes/ItemImpl.php index ad62163..3df8150 100644 --- a/src/classes/ItemImpl.php +++ b/src/classes/ItemImpl.php @@ -1,95 +1,109 @@ fields = Array(); - $this->metadata = Array(); - $this->files = Array(); - } - //Returns a metadata field - function getMetadata($metadataField) { - if (in_array($metadataField, $this->fields)) { - if (!is_array($this->metadata[$metadataField])) { - throw new Exception("Field '" . $metadataField . "' is not an array"); + function __construct() + { + $this->fields = Array(); + $this->metadata = Array(); + $this->files = Array(); + } + //Returns a metadata field + function getMetadata($metadataField) + { + if (in_array($metadataField, $this->fields)) { + if (!is_array($this->metadata[$metadataField])) { + throw new Exception("Field '" . $metadataField . "' is not an array"); - } - return $this->metadata[$metadataField]; - } else { - throw new Exception("Field '" . $metadataField . "' not found"); - } + } + return $this->metadata[$metadataField]; + } else { + throw new Exception("Field '" . $metadataField . "' not found"); + } - } - //Set a metadata field - function setMetadata($metadataField, $array) { - if (!in_array($metadataField, $this->fields)) { - array_push($this->fields, $metadataField); - } - $this->metadata[$metadataField] = $array; - } + } + //Set a metadata field + function setMetadata($metadataField, $array) + { + if (!in_array($metadataField, $this->fields)) { + array_push($this->fields, $metadataField); + } + $this->metadata[$metadataField] = $array; + } - function addMetadata($metadataField, $value) { - if (!in_array($metadataField, $this->fields)) { - array_push($this->fields, $metadataField); - $this->metadata[$metadataField] = Array(); - } - array_push($this->metadata[$metadataField], $value); - } + function addMetadata($metadataField, $value) + { + if (!in_array($metadataField, $this->fields)) { + array_push($this->fields, $metadataField); + $this->metadata[$metadataField] = Array(); + } + array_push($this->metadata[$metadataField], $value); + } - //Get all metadata fields - function getMedatataFields() { - return $this->fields; - } + //Get all metadata fields + function getMedatataFields() + { + return $this->fields; + } - function hasMetadataField($field) { - return in_array($field, $this->fields); - } + function hasMetadataField($field) + { + return in_array($field, $this->fields); + } - function getCollection() { - return $this->collection; - } - function setCollection($collection) { - $this->collection = $collection; - } + function getCollection() + { + return $this->collection; + } + function setCollection($collection) + { + $this->collection = $collection; + } - //set and get for id - function getId() { - return $this->id; - } + //set and get for id + function getId() + { + return $this->id; + } - function setId($id) { - $this->id = $id; - } + function setId($id) + { + $this->id = $id; + } - //set and get for files - function getFiles() { - return $this->files; - } - function addFile($path) { - array_push($this->files, $path); - } + //set and get for files + function getFiles() + { + return $this->files; + } + function addFile($path) + { + array_push($this->files, $path); + } - function metadataToString() { - $output = "{\"metadata\":["; - $first = 0; - foreach ($this->getMedatataFields() as $field) { - foreach ($this->getMetadata($field) as $value) { - if ($first == 0) { - $first = 1; - } else { - $output = $output . ','; - } - $output = $output . '{"key":"' . $field . '","value":"' . str_replace('"', '\"', $value) . '"}'; - } - } - $output = $output . "]}"; - return $output; - } + function metadataToString() + { + $output = "{\"metadata\":["; + $first = 0; + foreach ($this->getMedatataFields() as $field) { + foreach ($this->getMetadata($field) as $value) { + if ($first == 0) { + $first = 1; + } else { + $output = $output . ','; + } + $output = $output . '{"key":"' . $field . '","value":"' . str_replace('"', '\"', $value) . '"}'; + } + } + $output = $output . "]}"; + return $output; + } } ?> \ No newline at end of file diff --git a/src/classes/ItemMetadataConverter.php b/src/classes/ItemMetadataConverter.php index 056538d..d1acf4a 100644 --- a/src/classes/ItemMetadataConverter.php +++ b/src/classes/ItemMetadataConverter.php @@ -4,40 +4,44 @@ require 'ItemImpl.php'; use Zend\Json; -class ItemMetadataConverter implements MetadataConverter { +class ItemMetadataConverter implements MetadataConverter +{ - private $map; + private $map; - function __construct($mapfile) { - $reader = new Zend\Config\Reader\Json(); - $this->map = $reader->fromFile($mapfile); - } + function __construct($mapfile) + { + $reader = new Zend\Config\Reader\Json(); + $this->map = $reader->fromFile($mapfile); + } - private function getConversion($field) { - if (array_key_exists($field, $this->map)) { - return $this->map[$field]; - } else { - return ""; - } - } + private function getConversion($field) + { + if (array_key_exists($field, $this->map)) { + return $this->map[$field]; + } else { + return ""; + } + } - //returns all items available of a given year - public function convert($object) { - $r = new ItemImpl(); - if ($object instanceof ItemImpl) { - foreach ($object->getMedatataFields() as $originField) { - $targetField = $this->getConversion($originField); - if ($targetField != "") { - foreach ($object->getMetadata($originField) as $value) { - $r->addMetadata($targetField, $value); - } - } - } - return $r; + //returns all items available of a given year + public function convert($object) + { + $r = new ItemImpl(); + if ($object instanceof ItemImpl) { + foreach ($object->getMedatataFields() as $originField) { + $targetField = $this->getConversion($originField); + if ($targetField != "") { + foreach ($object->getMetadata($originField) as $value) { + $r->addMetadata($targetField, $value); + } + } + } + return $r; - } else { - throw new Exception("ItemMetadataConverter can convert only Item objects"); - } - } + } else { + throw new Exception("ItemMetadataConverter can convert only Item objects"); + } + } } ?> \ No newline at end of file diff --git a/src/classes/ItemSynchronizer.php b/src/classes/ItemSynchronizer.php index bbe6b7f..5222294 100644 --- a/src/classes/ItemSynchronizer.php +++ b/src/classes/ItemSynchronizer.php @@ -1,17 +1,18 @@ \ No newline at end of file diff --git a/src/classes/MetadataConverter.php b/src/classes/MetadataConverter.php index b9ffc4d..86dc103 100644 --- a/src/classes/MetadataConverter.php +++ b/src/classes/MetadataConverter.php @@ -1,7 +1,8 @@ \ No newline at end of file diff --git a/src/classes/Observer.php b/src/classes/Observer.php index c3e5d65..3a68b69 100644 --- a/src/classes/Observer.php +++ b/src/classes/Observer.php @@ -1,7 +1,6 @@ getItemsFromWS(); - } - - public function getItemsByYear($year) - { - return $this->getItemsFromWS("ano=" . $year); - } - - function setDefaultCollection($collection) - { - $this->defaultCollection = $collection; - } - - function __construct($wsURL) - { - $this->wsURL = $wsURL; - } - - private function getItemsFromWS($args = "") - { - $url = $this->wsURL; - if ($args != "") { - $url = $url . "teses.php?" . $args; - } - echo "Calling " . $url . PHP_EOL; - $html = implode('', file($url)); // Return a String. - $phpNative = Zend\Json\Encoder::encodeUnicodeString($html); // Encodes the String $html - $teses = Zend\Json\Json::decode($phpNative, Zend\Json\Json::TYPE_ARRAY); - $r = array(); - for ($i = 0; $i <= sizeof($teses); $i++) { - if (array_key_exists($i, $teses)) { - $t = new ItemImpl(); - - //id - $acervo = $teses[$i]["cod_acervo"]; - $t->setId($acervo); - - //outros metadados no registro principal - foreach ($teses[$i] as $key => $value) { - $t->addMetadata($key, html_entity_decode($value)); - } - - //arquivos - if (array_key_exists("links", $teses[$i]) && $teses[$i]["links"] != "") { - $t->addFile($teses[$i]["links"]); - } - - //metadados MARC - $html = implode('', file($this->wsURL . "marc.php?cod_acervo=" . $acervo)); // Returns a String. - $phpNative = Zend\Json\Encoder::encodeUnicodeString($html); - $camposMarc = Zend\Json\Json::decode($phpNative, Zend\Json\Json::TYPE_ARRAY); - - for ($j = 0; $j <= sizeof($camposMarc) - 1; $j++) { - $field = $camposMarc[$j]["paragrafo"]; - if ($camposMarc[$j]["secao"] != '') { - $field = $field . "-" . $camposMarc[$j]["secao"]; - } - if ($camposMarc[$j]["seq_paragrafo"] != '') { - $field = $field . "-" . $camposMarc[$j]["seq_paragrafo"]; - } - //Abstract comes from another database field - if ($camposMarc[$j]["paragrafo"] == self::MARC_PARAGRAFO_ABSTRACT) { - $value = html_entity_decode($camposMarc[$j]["texto_descricao"]); - //remove control characters from abstract - $value = preg_replace('/[[:cntrl:]]/', ' ', $value); - } else { - $value = html_entity_decode($camposMarc[$j]["descricao"]); - } - $t->addMetadata($field, $value); - } - - //coleção - if ($t->hasMetadataField(self::MARC_COLLECTION_FIELD)) { - $t->setCollection(array_values($t->getMetadata(self::MARC_COLLECTION_FIELD))[0]); - } else if ($this->defaultCollection != "") { - $t->setCollection($this->defaultCollection); - } else { - throw new Exception("Item does not have default collection field '" . self::MARC_COLLECTION_FIELD . "' and there is no default collection configured. Use setDefaultCollection"); - } - - array_push($r, $t); - } - } - return $r; - } - - //returns a Thesis with a given id - public function getItem($id) - { - return $this->getItemFromWS("acervo=" . $id); - } - - //save a Thesis - public function saveItem($t) - { - //TODO: implement this - throw new Exception("Saving items on Pergamum not available yet"); - } + const MARC_COLLECTION_FIELD = '710-b-1'; + const MARC_PARAGRAFO_ABSTRACT = "520"; + + private $wsURL; + private $defaultCollection; + + + public function getAllItems() + { + return $this->getItemsFromWS(); + } + + public function getItemsByYear($year) + { + return $this->getItemsFromWS("ano=" . $year); + } + + function setDefaultCollection($collection) + { + $this->defaultCollection = $collection; + } + + function __construct($wsURL) + { + $this->wsURL = $wsURL; + } + + private function getItemsFromWS($args = "") + { + $url = $this->wsURL; + if ($args != "") { + $url = $url . "teses.php?" . $args; + } + echo "Calling " . $url . PHP_EOL; + $html = implode('', file($url)); // Return a String. + $phpNative = Zend\Json\Encoder::encodeUnicodeString($html); // Encodes the String $html + $teses = Zend\Json\Json::decode($phpNative, Zend\Json\Json::TYPE_ARRAY); + $r = array(); + for ($i = 0; $i <= sizeof($teses); $i++) { + if (array_key_exists($i, $teses)) { + $t = new ItemImpl(); + + //id + $acervo = $teses[$i]["cod_acervo"]; + $t->setId($acervo); + + //outros metadados no registro principal + foreach ($teses[$i] as $key => $value) { + $t->addMetadata($key, html_entity_decode($value)); + } + + //arquivos + if (array_key_exists("links", $teses[$i]) && $teses[$i]["links"] != "") { + $t->addFile($teses[$i]["links"]); + } + + //metadados MARC + $html = implode('', file($this->wsURL . "marc.php?cod_acervo=" . $acervo)); // Returns a String. + $phpNative = Zend\Json\Encoder::encodeUnicodeString($html); + $camposMarc = Zend\Json\Json::decode($phpNative, Zend\Json\Json::TYPE_ARRAY); + + for ($j = 0; $j <= sizeof($camposMarc) - 1; $j++) { + $field = $camposMarc[$j]["paragrafo"]; + if ($camposMarc[$j]["secao"] != '') { + $field = $field . "-" . $camposMarc[$j]["secao"]; + } + if ($camposMarc[$j]["seq_paragrafo"] != '') { + $field = $field . "-" . $camposMarc[$j]["seq_paragrafo"]; + } + //Abstract comes from another database field + if ($camposMarc[$j]["paragrafo"] == self::MARC_PARAGRAFO_ABSTRACT) { + $value = html_entity_decode($camposMarc[$j]["texto_descricao"]); + //remove control characters from abstract + $value = preg_replace('/[[:cntrl:]]/', ' ', $value); + } else { + $value = html_entity_decode($camposMarc[$j]["descricao"]); + } + $t->addMetadata($field, $value); + } + + //coleção + if ($t->hasMetadataField(self::MARC_COLLECTION_FIELD)) { + $t->setCollection(array_values($t->getMetadata(self::MARC_COLLECTION_FIELD))[0]); + } else if ($this->defaultCollection != "") { + $t->setCollection($this->defaultCollection); + } else { + throw new Exception("Item does not have default collection field '" . self::MARC_COLLECTION_FIELD . "' and there is no default collection configured. Use setDefaultCollection"); + } + + array_push($r, $t); + } + } + return $r; + } + + //returns a Thesis with a given id + public function getItem($id) + { + return $this->getItemFromWS("acervo=" . $id); + } + + //save a Thesis + public function saveItem($t) + { + //TODO: implement this + throw new Exception("Saving items on Pergamum not available yet"); + } } diff --git a/src/classes/Repository.php b/src/classes/Repository.php index 2304dce..e1d7c8a 100644 --- a/src/classes/Repository.php +++ b/src/classes/Repository.php @@ -1,16 +1,17 @@ \ No newline at end of file diff --git a/src/classes/Subject.php b/src/classes/Subject.php index 01751b6..f77a655 100644 --- a/src/classes/Subject.php +++ b/src/classes/Subject.php @@ -1,7 +1,8 @@ getItemsFromXLS("ano=" . $year); - } - - public function getAllItems() { - return $this->getItemsFromXLS(); - } - - function setDefaultCollection($collection) { - $this->defaultCollection = $collection; - } - - function __construct($path) { - if (file_exists($path)) { - $this->xlsPath = $path; - } else { - throw new Exception("XLS File does not exist: " . $path); - } - - } - - private function formatName($name) { - $names = explode(" ", $name); - $result = end($names) . ","; - foreach ($names as $value) { - if ($value != end($names)) { - $result = $result . " " . $value; - } - } - return $result; - } - - private function getItemsFromXLS($filter_field = null, $filter_value = null) { - $objPHPExcel = PHPExcel_IOFactory::load($this->xlsPath); - $r = Array(); - foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { - //echo 'Worksheet - ', $worksheet->getTitle() . PHP_EOL; - //skip empty wordksheet - if ($worksheet->getCell('A1')->getCalculatedValue() == "") { - continue; - } - foreach ($worksheet->getRowIterator() as $row) { - //echo ' Row number - ', $row->getRowIndex() . PHP_EOL; - //skip header - if ($row->getRowIndex() == 1) { - continue; - } - $t = new ItemImpl(); - $cellIterator = $row->getCellIterator(); - $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set - foreach ($cellIterator as $cell) { - if (!is_null($cell)) { - $value = $cell->getCalculatedValue(); - if ($value != "") { - if (in_array(substr($cell->getCoordinate(), 0, 1), self::XLS_NAME_COLUMNS) || (substr($cell->getCoordinate(), 0, 1) == self::XLS_SUBJECT_COLUMN)) { - if (substr($cell->getCoordinate(), 0, 1) == self::XLS_SUBJECT_COLUMN) { - //subjects separated by comma - foreach (explode(',', $value) as $subject) { - $t->addMetadata(substr($cell->getCoordinate(), 0, 1), trim($subject)); - } - } else { - //names separated by semicolon - foreach (explode(';', $value) as $name) { - $t->addMetadata(substr($cell->getCoordinate(), 0, 1), trim($this->formatName($name))); - } - } - } else { - //atomic values - $t->addMetadata(substr($cell->getCoordinate(), 0, 1), trim($value)); - } - } - //echo ' Cell - ', $cell->getCoordinate(), ' - ', $cell->getCalculatedValue(), EOL; - } - } - $dir = $worksheet->getCell(self::XLS_DIR_COLUMN . $row->getRowIndex())->getCalculatedValue(); - $file = $worksheet->getCell(self::XLS_FILE_COLUMN . $row->getRowIndex())->getCalculatedValue(); - $t->addFile($dir . '/' . rawurlencode($file)); - - $t->setCollection("TCC " . $worksheet->getCell(self::XLS_COLLECTION_COLUMN . $row->getRowIndex())->getCalculatedValue()); - - $t->addMetadata("descricao", "TCC (graduação) - Universidade Federal de Santa Catarina. Centro Tecnológico. Curso de " . $worksheet->getCell(self::XLS_COLLECTION_COLUMN . $row->getRowIndex())->getCalculatedValue() . "."); - $t->addMetadata("tipo", "TCCgrad"); - - $t->setId($worksheet->getCell(self::XLS_ID_COLUMN . $row->getRowIndex())->getCalculatedValue()); - array_push($r, $t); - } - } - - return $r; - } - - //returns a Thesis with a given id - public function getItem($id) { - return $this->getItemsFromXLS("acervo=" . $id); - } - - //save a Thesis - public function saveItem($t) { - throw new Exception("Saving items on XLS is not available"); - } +class XLS implements Repository +{ + + const XLS_COLLECTION_COLUMN = 'A'; + const XLS_ID_COLUMN = 'B'; + const XLS_DIR_COLUMN = 'H'; + const XLS_FILE_COLUMN = 'I'; + const XLS_NAME_COLUMNS = array('J', 'K', 'L'); + const XLS_SUBJECT_COLUMN = 'G'; + + private $defaultCollection; + + public function getItemsByYear($year) + { + return $this->getItemsFromXLS("ano=" . $year); + } + + public function getAllItems() + { + return $this->getItemsFromXLS(); + } + + function setDefaultCollection($collection) + { + $this->defaultCollection = $collection; + } + + function __construct($path) + { + if (file_exists($path)) { + $this->xlsPath = $path; + } else { + throw new Exception("XLS File does not exist: " . $path); + } + + } + + private function formatName($name) + { + $names = explode(" ", $name); + $result = end($names) . ","; + foreach ($names as $value) { + if ($value != end($names)) { + $result = $result . " " . $value; + } + } + return $result; + } + + private function getItemsFromXLS($filter_field = null, $filter_value = null) + { + $objPHPExcel = PHPExcel_IOFactory::load($this->xlsPath); + $r = Array(); + foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { + //echo 'Worksheet - ', $worksheet->getTitle() . PHP_EOL; + //skip empty wordksheet + if ($worksheet->getCell('A1')->getCalculatedValue() == "") { + continue; + } + foreach ($worksheet->getRowIterator() as $row) { + //echo ' Row number - ', $row->getRowIndex() . PHP_EOL; + //skip header + if ($row->getRowIndex() == 1) { + continue; + } + $t = new ItemImpl(); + $cellIterator = $row->getCellIterator(); + $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set + foreach ($cellIterator as $cell) { + if (!is_null($cell)) { + $value = $cell->getCalculatedValue(); + if ($value != "") { + if (in_array(substr($cell->getCoordinate(), 0, 1), self::XLS_NAME_COLUMNS) || (substr($cell->getCoordinate(), 0, 1) == self::XLS_SUBJECT_COLUMN)) { + if (substr($cell->getCoordinate(), 0, 1) == self::XLS_SUBJECT_COLUMN) { + //subjects separated by comma + foreach (explode(',', $value) as $subject) { + $t->addMetadata(substr($cell->getCoordinate(), 0, 1), trim($subject)); + } + } else { + //names separated by semicolon + foreach (explode(';', $value) as $name) { + $t->addMetadata(substr($cell->getCoordinate(), 0, 1), trim($this->formatName($name))); + } + } + } else { + //atomic values + $t->addMetadata(substr($cell->getCoordinate(), 0, 1), trim($value)); + } + } + //echo ' Cell - ', $cell->getCoordinate(), ' - ', $cell->getCalculatedValue(), EOL; + } + } + $dir = $worksheet->getCell(self::XLS_DIR_COLUMN . $row->getRowIndex())->getCalculatedValue(); + $file = $worksheet->getCell(self::XLS_FILE_COLUMN . $row->getRowIndex())->getCalculatedValue(); + $t->addFile($dir . '/' . rawurlencode($file)); + + $t->setCollection("TCC " . $worksheet->getCell(self::XLS_COLLECTION_COLUMN . $row->getRowIndex())->getCalculatedValue()); + + $t->addMetadata("descricao", "TCC (graduação) - Universidade Federal de Santa Catarina. Centro Tecnológico. Curso de " . $worksheet->getCell(self::XLS_COLLECTION_COLUMN . $row->getRowIndex())->getCalculatedValue() . "."); + $t->addMetadata("tipo", "TCCgrad"); + + $t->setId($worksheet->getCell(self::XLS_ID_COLUMN . $row->getRowIndex())->getCalculatedValue()); + array_push($r, $t); + } + } + + return $r; + } + + //returns a Thesis with a given id + public function getItem($id) + { + return $this->getItemsFromXLS("acervo=" . $id); + } + + //save a Thesis + public function saveItem($t) + { + throw new Exception("Saving items on XLS is not available"); + } } ?> \ No newline at end of file diff --git a/tests/PergamumTest.php b/tests/PergamumTest.php index 7e696b8..7264eb1 100644 --- a/tests/PergamumTest.php +++ b/tests/PergamumTest.php @@ -21,11 +21,13 @@ public function testThesisTonini() $p = new Pergamum(); $t = $p->getAllThesis(2014); $count=0; - $results = array_filter($t, function($tese) { - if ($tese['autor'] == 'TONINI, Gustavo Alexssandro.'){ - $count++; + $results = array_filter( + $t, function ($tese) { + if ($tese['autor'] == 'TONINI, Gustavo Alexssandro.') { + $count++; + } } - }); + ); $this->assertEquals(1, $count); } }