From cc3152d52940c0d78da389459ccbfcba584faec0 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@git-test.afi-sa.fr>
Date: Fri, 4 May 2012 14:26:09 +0000
Subject: [PATCH] =?UTF-8?q?OAI:=20Refacto=20GetRecord=20est=20trait=C3=A9?=
 =?UTF-8?q?=20via=20MVC=20+=20apparition=20request=20GetRecord?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                                |  4 +-
 .../opac/controllers/OaiController.php        | 24 +++++-
 .../views/scripts/oai/get-record.xml.phtml    | 13 ++++
 .../WebService/OAI/Request/GetRecord.php      | 73 +++++++++++++++++++
 .../OAIControllerGetRecordTest.php}           | 36 +++++----
 5 files changed, 129 insertions(+), 21 deletions(-)
 create mode 100644 application/modules/opac/views/scripts/oai/get-record.xml.phtml
 create mode 100644 library/Class/WebService/OAI/Request/GetRecord.php
 rename tests/{library/Class/WebService/OAI/Response/GetRecordTest.php => application/modules/opac/controllers/OAIControllerGetRecordTest.php} (80%)

diff --git a/.gitattributes b/.gitattributes
index ead39383d32..413e07fa6d0 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -582,6 +582,7 @@ application/modules/opac/views/scripts/java/protoflow.phtml -text
 application/modules/opac/views/scripts/java/slide_show.phtml -text
 application/modules/opac/views/scripts/module.phtml -text
 application/modules/opac/views/scripts/noticeajax/notice.phtml -text
+application/modules/opac/views/scripts/oai/get-record.xml.phtml -text
 application/modules/opac/views/scripts/oai/identify.xml.phtml -text svneol=unset#text/plain
 application/modules/opac/views/scripts/oai/list-identifiers.xml.phtml -text
 application/modules/opac/views/scripts/oai/list-metadata-formats.xml.phtml -text svneol=unset#application/xml
@@ -1773,6 +1774,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/Request/GetRecord.php -text
 library/Class/WebService/OAI/Request/Identify.php -text
 library/Class/WebService/OAI/Request/ListIdentifiers.php -text
 library/Class/WebService/OAI/Request/ListMetadataFormats.php -text
@@ -3441,6 +3443,7 @@ tests/application/modules/opac/controllers/CmsControllerTest.php -text
 tests/application/modules/opac/controllers/FormulaireContactTest.php -text
 tests/application/modules/opac/controllers/IndexControllerTranslationTest.php -text
 tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php -text
+tests/application/modules/opac/controllers/OAIControllerGetRecordTest.php -text
 tests/application/modules/opac/controllers/OAIControllerIdentifyTest.php -text
 tests/application/modules/opac/controllers/OAIControllerListIdentifiersTest.php -text
 tests/application/modules/opac/controllers/OAIControllerListMetadataFormatsTest.php -text
@@ -3522,7 +3525,6 @@ 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/ResumptionTokenTest.php -text
 tests/library/Class/WebService/OAIIdentify.xml -text
 tests/library/Class/WebService/OAIListIdentifiers.xml -text
diff --git a/application/modules/opac/controllers/OaiController.php b/application/modules/opac/controllers/OaiController.php
index 60022f584f6..160d92ca007 100644
--- a/application/modules/opac/controllers/OaiController.php
+++ b/application/modules/opac/controllers/OaiController.php
@@ -26,6 +26,7 @@ class OaiController extends Zend_Controller_Action {
 			->addActionContext('list-metadata-formats', 'xml')
 			->addActionContext('list-records', 'xml')
 			->addActionContext('list-sets', 'xml')
+			->addActionContext('get-record', 'xml')
 			->initContext();
 	}
 
@@ -38,7 +39,8 @@ class OaiController extends Zend_Controller_Action {
 													'Identify' => 'identify',
 													'ListMetadataFormats' => 'list-metadata-formats',
 													'ListRecords' => 'list-records',
-													'ListSets' => 'list-sets');
+													'ListSets' => 'list-sets',
+													'GetRecord' => 'get-record');
 
 		if (array_key_exists($this->_getParam('verb'), $verbsMapping)) {
 			$this->_forward($verbsMapping[$this->_getParam('verb')], null, null, 
@@ -149,6 +151,26 @@ class OaiController extends Zend_Controller_Action {
 		}
 		$this->view->builder = $builder;
 	}
+
+
+	public function getRecordAction() {
+		$this->getHelper('ViewRenderer')->setLayoutScript('empty.phtml');
+		$baseUrl = $this->buildBaseUrl();
+		$request = new Class_WebService_OAI_Request_GetRecord($this->_request->getParams(), 
+																													$baseUrl);
+		$this->view->request = $request;
+		$builder = new Class_Xml_Builder();
+
+		$this->view->error = $request->getErrorOn($builder);
+
+		if ($notice = $request->getNotice()) {
+			$visitor = new Class_Notice_DublinCoreVisitor();
+			$visitor->visit($notice);
+			$recordBuilder = new Class_WebService_OAI_Response_RecordBuilder();
+			$this->view->record = $recordBuilder->xml($builder, $visitor);
+		}
+		$this->view->builder = $builder;
+	}
 }
 
 ?>
\ No newline at end of file
diff --git a/application/modules/opac/views/scripts/oai/get-record.xml.phtml b/application/modules/opac/views/scripts/oai/get-record.xml.phtml
new file mode 100644
index 00000000000..23bfbf777cb
--- /dev/null
+++ b/application/modules/opac/views/scripts/oai/get-record.xml.phtml
@@ -0,0 +1,13 @@
+<?php echo '<?xml version="1.0" encoding="UTF-8"?>';?>
+<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
+	<responseDate><?php echo date('c');?></responseDate>
+	<?php echo $this->request->renderOn($this->builder); ?>
+	<?php echo ($this->error) ? $this->error : '';?>
+	<?php if ($this->record) { ?>
+	<GetRecord>
+		<record><?php echo $this->record; ?></record>
+	</GetRecord>
+	<?php } ?>
+</OAI-PMH>
diff --git a/library/Class/WebService/OAI/Request/GetRecord.php b/library/Class/WebService/OAI/Request/GetRecord.php
new file mode 100644
index 00000000000..0da755b6ad9
--- /dev/null
+++ b/library/Class/WebService/OAI/Request/GetRecord.php
@@ -0,0 +1,73 @@
+<?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_Request_GetRecord {
+	protected $_baseUrl;
+	protected $_params;
+	protected $_metadataPrefix;
+	protected $_notice;
+
+
+	public function __construct($params, $baseUrl) {
+		$this->_baseUrl = $baseUrl;
+		$this->_params = array_merge(array('metadataPrefix' => null,
+																			 'identifier' => null),
+																 $params);
+
+		$this->_metadataPrefix = $this->_params['metadataPrefix'];
+		$this->_identifier = $this->_params['identifier'];
+	}
+
+
+	public function getErrorOn($builder) {
+		if (!$this->_metadataPrefix) 
+			return $builder->error(array('code' => 'badArgument'), 'Missing metadataPrefix');
+
+		if ('oai_dc' != $this->_metadataPrefix) 
+			return $builder->error(array('code' => 'cannotDisseminateFormat'));
+
+		if (!$this->_identifier)
+			return $builder->error(array('code' => 'badArgument'), 'Missing identifier');
+		
+		$parts = explode('/', $this->_identifier);
+		$this->_notice = Class_Notice::getLoader()->getNoticeByClefAlpha(end($parts));
+
+		if (!$this->_notice) 
+			return $builder->error(array('code' => 'idDoesNotExist'));
+	}
+
+
+	public function renderOn($builder) {
+		$attributes = array('verb' => 'GetRecord');
+		if ($this->_metadataPrefix)
+			$attributes['metadataPrefix'] = $this->_metadataPrefix;
+
+		if ($this->_identifier)
+			$attributes['identifier'] = $this->_identifier;
+
+		return $builder->request($attributes, $this->_baseUrl);
+	}
+
+
+	public function getNotice() {
+		return $this->_notice;
+	}
+}
+?>
\ No newline at end of file
diff --git a/tests/library/Class/WebService/OAI/Response/GetRecordTest.php b/tests/application/modules/opac/controllers/OAIControllerGetRecordTest.php
similarity index 80%
rename from tests/library/Class/WebService/OAI/Response/GetRecordTest.php
rename to tests/application/modules/opac/controllers/OAIControllerGetRecordTest.php
index 8115ec7e34a..006b3545711 100644
--- a/tests/library/Class/WebService/OAI/Response/GetRecordTest.php
+++ b/tests/application/modules/opac/controllers/OAIControllerGetRecordTest.php
@@ -18,27 +18,27 @@
  * 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 
  */
+require_once 'AbstractControllerTestCase.php';
 
-abstract class OAIGetRecordTestCase extends Storm_Test_ModelTestCase {
+abstract class OAIControllerGetRecordTestCase extends AbstractControllerTestCase {
 	const OAI_RECORD_PATH = '//oai:GetRecord/oai:record/';
 	const OAI_HEADER_PATH = 'oai:header/';
 
 	protected $_xpath;
-	protected $_response;
 	protected $_xml;
 
 	public function setUp() {
 		parent::setUp();
 		$this->_xpath = TestXPathFactory::newOaiDc();
-		$this->_response = new Class_WebService_OAI_Response_GetRecord('http://afi-sa.fr/oai/do');
 	}
 }
 
 
-class OAIGetRecordNoIdentifierTest extends OAIGetRecordTestCase {
+class OAIControllerGetRecordNoIdentifierTest extends OAIControllerGetRecordTestCase {
 	public function setUp() {
 		parent::setUp();
-		$this->_xml = $this->_response->xml(array('metadataPrefix' => 'oai_dc'));
+		$this->dispatch('/opac/oai/request?verb=GetRecord&metadataPrefix=oai_dc');
+		$this->_xml = $this->_response->getBody();
 	}
 
 
@@ -57,10 +57,11 @@ class OAIGetRecordNoIdentifierTest extends OAIGetRecordTestCase {
 }
 
 
-class OAIGetRecordNoMetadataPrefixTest extends OAIGetRecordTestCase {
+class OAIControllerGetRecordNoMetadataPrefixTest extends OAIControllerGetRecordTestCase {
 	public function setUp() {
 		parent::setUp();
-		$this->_xml = $this->_response->xml(array('identifier' => 'toto'));
+		$this->dispatch('/opac/oai/request?verb=GetRecord&identifier=toto');
+		$this->_xml = $this->_response->getBody();
 	}
 
 
@@ -80,7 +81,7 @@ class OAIGetRecordNoMetadataPrefixTest extends OAIGetRecordTestCase {
 
 
 
-class OAIGetRecordNotFoundParamsTest extends OAIGetRecordTestCase {
+class OAIControllerGetRecordNotFoundParamsTest extends OAIControllerGetRecordTestCase {
 	public function setUp() {
 		parent::setUp();
 
@@ -89,9 +90,8 @@ class OAIGetRecordNotFoundParamsTest extends OAIGetRecordTestCase {
 			->with('harrypotter-sorciers')
 			->answers(null);
 
-		$this->_xml = $this->_response->xml(array('identifier' => 'harrypotter-sorciers',
-																							'metadataPrefix' => 'oai_dc'));
-		
+		$this->dispatch('/opac/oai/request?verb=GetRecord&metadataPrefix=oai_dc&identifier=harrypotter-sorciers');
+		$this->_xml = $this->_response->getBody();
 	}
 
 
@@ -110,7 +110,7 @@ class OAIGetRecordNotFoundParamsTest extends OAIGetRecordTestCase {
 }
 
 
-class OAIGetRecordNotSupportedPrefixTest extends OAIGetRecordTestCase {
+class OAIControllerGetRecordNotSupportedPrefixTest extends OAIControllerGetRecordTestCase {
 	public function setUp() {
 		parent::setUp();
 
@@ -123,9 +123,8 @@ class OAIGetRecordNotSupportedPrefixTest extends OAIGetRecordTestCase {
 								  ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers')
 								  ->setDateMaj('2001-12-14 11:39:44'));
 
-		$this->_xml = $this->_response->xml(array('identifier' => 'harrypotter-sorciers',
-																							'metadataPrefix' => 'not_supported'));
-		
+		$this->dispatch('/opac/oai/request?verb=GetRecord&metadataPrefix=not_supported&identifier=harrypotter-sorciers');		
+		$this->_xml = $this->_response->getBody();
 	}
 
 
@@ -144,7 +143,7 @@ class OAIGetRecordNotSupportedPrefixTest extends OAIGetRecordTestCase {
 }
 
 
-class OAIGetRecordValidParamsTest extends OAIGetRecordTestCase {
+class OAIControllerGetRecordValidParamsTest extends OAIControllerGetRecordTestCase {
 	public function setUp() {
 		parent::setUp();
 
@@ -157,9 +156,8 @@ class OAIGetRecordValidParamsTest extends OAIGetRecordTestCase {
 								  ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers')
 								  ->setDateMaj('2001-12-14 11:39:44'));
 
-		$this->_xml = $this->_response->xml(array('identifier' => 'harrypotter-sorciers',
-																							'metadataPrefix' => 'oai_dc'));
-		
+		$this->dispatch('/opac/oai/request?verb=GetRecord&metadataPrefix=oai_dc&identifier=harrypotter-sorciers');
+		$this->_xml = $this->_response->getBody();
 	}
 
 
-- 
GitLab