From bda87251c8913e61d01d45cfd9e8a0888902832b Mon Sep 17 00:00:00 2001 From: pbarroca <pbarroca@git-test.afi-sa.fr> Date: Mon, 23 Apr 2012 17:11:35 +0000 Subject: [PATCH] =?UTF-8?q?OAI:=20ajout=20r=C3=A9ponse=20GetRecord?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 2 + library/Class/Notice.php | 1 + library/Class/Notice/DublinCoreVisitor.php | 26 +++++- .../WebService/OAI/Response/GetRecord.php | 59 +++++++++++++ .../Class/Notice/DublinCoreVisitorTest.php | 8 +- .../WebService/OAI/Response/GetRecordTest.php | 83 +++++++++++++++++++ 6 files changed, 173 insertions(+), 6 deletions(-) create mode 100644 library/Class/WebService/OAI/Response/GetRecord.php create mode 100644 tests/library/Class/WebService/OAI/Response/GetRecordTest.php diff --git a/.gitattributes b/.gitattributes index 452a19c604e..36f5523c9f4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1763,6 +1763,7 @@ library/Class/WebService/LibraryThing.php -text library/Class/WebService/MappedSoapClient.php -text library/Class/WebService/OAI.php -text library/Class/WebService/OAI/CatalogueVisitor.php -text +library/Class/WebService/OAI/Response/GetRecord.php -text library/Class/WebService/OAI/Response/Identify.php -text library/Class/WebService/OAI/Response/ListSets.php -text library/Class/WebService/OAI/Response/Null.php -text @@ -3488,6 +3489,7 @@ tests/library/Class/UserGroupTest.php -text tests/library/Class/UsersTest.php -text tests/library/Class/VodeclicLinkTest.php -text tests/library/Class/WebService/BabelioTest.php -text +tests/library/Class/WebService/OAI/Response/GetRecordTest.php -text tests/library/Class/WebService/OAI/Response/IdentifyTest.php -text tests/library/Class/WebService/OAI/Response/ListSetsTest.php -text tests/library/Class/WebService/OAIIdentify.xml -text diff --git a/library/Class/Notice.php b/library/Class/Notice.php index b0c2dce8fb6..9d59662cee5 100644 --- a/library/Class/Notice.php +++ b/library/Class/Notice.php @@ -989,6 +989,7 @@ class Class_Notice extends Storm_Model_Abstract public function acceptVisitor($visitor) { $visitor->visitClefAlpha($this->getClefAlpha()); $visitor->visitTitre($this->getTitrePrincipal()); + $visitor->visitDateMaj($this->getDateMaj()); } } diff --git a/library/Class/Notice/DublinCoreVisitor.php b/library/Class/Notice/DublinCoreVisitor.php index deeb9a02610..f0c8cadeb87 100644 --- a/library/Class/Notice/DublinCoreVisitor.php +++ b/library/Class/Notice/DublinCoreVisitor.php @@ -22,6 +22,9 @@ class Class_Notice_DublinCoreVisitor { protected $_xml; protected $_builder; + protected $_identifier; + protected $_date; + public function __construct() { $this->_builder = new Class_Xml_Oai_DublinCoreBuilder(); @@ -39,16 +42,31 @@ class Class_Notice_DublinCoreVisitor { public function visitClefAlpha($clef) { - $this->_xml .= $this->_builder->identifier(sprintf('http://%s%s/recherche/notice/%s', - $_SERVER['SERVER_NAME'], - BASE_URL, - $clef)); + $this->_identifier = sprintf('http://%s%s/recherche/notice/%s', + $_SERVER['SERVER_NAME'], BASE_URL, $clef); + $this->_xml .= $this->_builder->identifier($this->_identifier); } public function visitTitre($titre) { $this->_xml .= $this->_builder->title($titre); } + + + public function visitDateMaj($dateMaj) { + $this->_date = substr($dateMaj, 0, 10); + $this->_xml .= $this->_builder->date($this->_date); + } + + + public function getIdentifier() { + return $this->_identifier; + } + + + public function getDate() { + return $this->_date; + } } ?> \ No newline at end of file diff --git a/library/Class/WebService/OAI/Response/GetRecord.php b/library/Class/WebService/OAI/Response/GetRecord.php new file mode 100644 index 00000000000..cd525e3c68a --- /dev/null +++ b/library/Class/WebService/OAI/Response/GetRecord.php @@ -0,0 +1,59 @@ +<?php +/** + * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved. + * + * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * AFI-OPAC 2.0 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with AFI-OPAC 2.0; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +class Class_WebService_OAI_Response_GetRecord extends Class_WebService_OAI_Response_Null { + protected $_notice; + protected $_dublin_core_visitor; + + public function buildXmlOn($builder) { + if (null === $this->_notice) + return ''; + + $this->_dublin_core_visitor = new Class_Notice_DublinCoreVisitor(); + $this->_dublin_core_visitor->visit($this->_notice); + + return + $builder->request(array('verb' => 'GetRecord', + 'metadataPrefix' => 'oai_dc'), + $this->_baseUrl) + . $builder->GetRecord($builder->record($this->_buildHeaders($builder) + . $this->_buildMetadata($builder))); + } + + + protected function _buildHeaders($builder) { + return + $builder->header($builder->identifier($this->_dublin_core_visitor->getIdentifier())) + . $builder->header($builder->datestamp($this->_dublin_core_visitor->getDate())); + } + + + protected function _buildMetadata($builder) { + return $builder->metadata($this->_dublin_core_visitor->xml()); + } + + + public function setNotice($notice) { + $this->_notice = $notice; + } +} + + +?> \ No newline at end of file diff --git a/tests/library/Class/Notice/DublinCoreVisitorTest.php b/tests/library/Class/Notice/DublinCoreVisitorTest.php index 0ea00c13f89..1aeccc259e9 100644 --- a/tests/library/Class/Notice/DublinCoreVisitorTest.php +++ b/tests/library/Class/Notice/DublinCoreVisitorTest.php @@ -37,7 +37,8 @@ class DublinCoreVisitorPotterTest extends DublinCoreVisitorTestCase { $potter = Class_Notice::getLoader() ->newInstanceWithId(4) ->setClefAlpha('harrypotter-sorciers') - ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers'); + ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers') + ->setDateMaj('2012-04-23'); $this->_dublin_core_visitor->visit($potter); } @@ -74,9 +75,12 @@ class DublinCoreVisitorSouvignyTest extends DublinCoreVisitorTestCase { parent::setUp(); $souvigny = Class_Notice::getLoader() ->newInstanceWithId(5) - ->setClefAlpha('souvigny-bible-11eme'); + ->setClefAlpha('souvigny-bible-11eme') + ->setDateMaj('2012-04-23'); + $oldServerName = $_SERVER['SERVER_NAME']; $_SERVER['SERVER_NAME'] = 'moulins.fr'; $this->_dublin_core_visitor->visit($souvigny); + $_SERVER['SERVER_NAME'] = $oldServerName; } diff --git a/tests/library/Class/WebService/OAI/Response/GetRecordTest.php b/tests/library/Class/WebService/OAI/Response/GetRecordTest.php new file mode 100644 index 00000000000..544ab4b39b6 --- /dev/null +++ b/tests/library/Class/WebService/OAI/Response/GetRecordTest.php @@ -0,0 +1,83 @@ +<?php +/** + * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved. + * + * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * AFI-OPAC 2.0 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with AFI-OPAC 2.0; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class OAIGetRecordTest extends Storm_Test_ModelTestCase { + const OAI_RECORD_PATH = '//oai:GetRecord/oai:record/'; + const OAI_HEADER_PATH = 'oai:header/'; + + protected $_xpath; + protected $_response; + + public function setUp() { + parent::setUp(); + $this->_xpath = TestXPathFactory::newOai(); + $this->_xpath->registerNameSpace('oai_dc', 'http://www.openarchives.org/OAI/2.0/oai_dc/'); + $this->_response = new Class_WebService_OAI_Response_GetRecord('http://afi-sa.fr/oai/do'); + + $potter = Class_Notice::getLoader() + ->newInstanceWithId(4) + ->setClefAlpha('harrypotter-sorciers') + ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers') + ->setDateMaj('2001-12-14 11:39:44'); + + $this->_response->setNotice($potter); + } + + + /** @test */ + public function requestVerbShouldBeGetRecord() { + $this->_xpath->assertXpath($this->_response->xml(), + '//oai:request[@verb="GetRecord"]'); + } + + + /** @test */ + public function requestMetadataPrefixShouldBeOaiDc() { + $this->_xpath->assertXpath($this->_response->xml(), + '//oai:request[@metadataPrefix="oai_dc"]'); + } + + + /** @test */ + public function headerShouldContainRecordIdentifier() { + $this->_xpath->assertXPathContentContains($this->_response->xml(), + self::OAI_RECORD_PATH . self::OAI_HEADER_PATH . 'oai:identifier', + sprintf('http://localhost%s/recherche/notice/harrypotter-sorciers', + BASE_URL)); + } + + + /** @test */ + public function recordHeaderDateStampShouldBe2001DecemberFourteen() { + $this->_xpath->assertXPathContentContains($this->_response->xml(), + self::OAI_RECORD_PATH . self::OAI_HEADER_PATH . 'oai:datestamp', + '2001-12-14'); + } + + + /** @test */ + public function metadataShouldContainOaiDublinCore() { + $this->_xpath->assertXPath($this->_response->xml(), + self::OAI_RECORD_PATH . 'oai:metadata/oai_dc:dc'); + } + +} \ No newline at end of file -- GitLab