fix errors and create verb ListIdentifiers (ref. #181)

This commit is contained in:
Eduardo Humberto 2019-02-05 20:43:18 -02:00
parent d546486e68
commit 0677283b0f
5 changed files with 246 additions and 2 deletions

View File

@ -26,6 +26,7 @@ class REST_Oaipmh_Expose_Controller extends REST_Controller {
$this->list_records = new \Tainacan\OAIPMHExpose\OAIPMH_List_Records();
$this->get_record = new \Tainacan\OAIPMHExpose\OAIPMH_Get_Record();
$this->identify = new \Tainacan\OAIPMHExpose\OAIPMH_Identify();
$this->identifiers = new \Tainacan\OAIPMHExpose\OAIPMH_List_Identifiers();
}
public function register_routes() {
@ -79,6 +80,15 @@ class REST_Oaipmh_Expose_Controller extends REST_Controller {
$this->list_records->list_records($request);
break;
case 'ListIdentifiers':
if ( !isset($request['metadataPrefix']) && !isset($request['resumptionToken']) ) {
$this->identifiers->config();
$this->identifiers->errors[] = $this->list_records->oai_error('missingArgument','metadataPrefix');
$this->identifiers->oai_exit($request, $this->list_records->errors);
}
$this->identifiers->list_identifiers($request);
break;
case 'GetRecord':
if ( !isset($request['metadataPrefix']) ) {

View File

@ -182,6 +182,11 @@ class OAIPMH_Expose {
$logs_folder = $upload_dir . 'tainacan/tokens/';
if (!is_dir($logs_folder)) {
if( !is_dir($upload_dir . 'tainacan') && !mkdir($upload_dir . 'tainacan')){
return false;
}
if (!mkdir($logs_folder)) {
return false;
}

View File

@ -130,7 +130,7 @@ class OAIPMH_Get_Record extends OAIPMH_Expose {
try{
if ($maps) {
foreach ($maps as $key => $val) {
$this->xml_creater->addChild($this->working_node, $key, html_entity_decode($val));
$this->xml_creater->addChild($this->working_node, $key, html_entity_decode($val->get_value()));
}
}
}catch(Exception $e){

View File

@ -0,0 +1,229 @@
<?php
namespace Tainacan\OAIPMHExpose;
use Tainacan\Repositories;
use Tainacan\Entities;
class OAIPMH_List_Identifiers extends OAIPMH_Expose {
protected $working_node;
public $errors;
public $xml_creater;
public $restoken = '-';
public $expirationdatetime;
public $num_rows;
public $cursor;
public $deliveredrecords;
public $from;
public $until;
public $sets;
public $metadataPrefix;
/**
* @signature CONSTRUTOR
*
* getting the collection repository
* @author: Eduardo
*/
function __construct() {
$this->collection_repository = Repositories\Collections::get_instance();
$this->item_repository = Repositories\Items::get_instance();
}
/**
* @return array
* @throws \Exception
*/
public function list_collections() {
$collections = $this->collection_repository->fetch([]);
$response = [];
if($collections->have_posts()){
while ($collections->have_posts()){
$collections->the_post();
$collection = new Entities\Collection($collections->post);
array_push($response, $collection);
}
wp_reset_postdata();
}
return $response;
}
/**
* main method return the items, filtered or not filtered
*
* @return array
*/
public function get_items() {
$items = array();
$args = [
'posts_per_page' => $this->MAXRECORDS,
'paged' => $this->deliveredrecords == 0 ? 1 : ( $this->deliveredrecords / 100 ) + 1,
'order' => 'DESC',
'orderby' => 'ID',
'post_status' => array( 'trash', 'publish' )
];
if( !empty($this->sets) ){
$collections = $this->list_collections();
$collections_list = [];
foreach ( $collections as $collection ) {
if( !empty($this->sets) && !in_array($collection->get_id(), $this->sets)){
continue;
}
$collections_list[] = $collection;
}
$result = $this->item_repository->fetch($args, $collections_list, 'OBJECT');
} else {
$result = $this->item_repository->fetch($args, [], 'OBJECT');
}
if($result){
foreach ($result as $item) {
$item->collection = $item->get_collection();
$items[] = $item;
}
}
return $items;
}
/**
* @signature - list_identifiers
* @param array $param Os argumentos vindos da url (verb,until,from,set,metadataprefix,resumptioToken)
* @return mostra o xml do list record desejado
* @description - Metodo responsavel em mostrar o xml do list records, o metodo executado no controller
* ele chama os demais metodos que fazem as verificacoes de erros
* @author: Eduardo
*/
public function list_identifiers($data) {
session_write_close();
$this->config();
$this->initiate_variables($data);
$items = $this->get_items();
$numRows = count($items);
if($numRows == 0){
$this->errors[] = $this->oai_error('noRecordsMatch');
$this->oai_exit($data,$this->errors);
}
$this->verify_resumption_token($numRows);
$this->xml_creater = new Xml_Response($data);
foreach ( $items as $item ) {
$collection = $item->collection;
$identifier = 'oai:'.$this->repositoryIdentifier.':'. $item->get_id();
$datestamp = $this->formatDatestamp($item->get_creation_date());
$setspec = $collection->get_id();
$cur_record = $this->xml_creater->create_record();
$cur_header = $this->xml_creater->create_header($identifier, $datestamp, $setspec,$cur_record, ( $item->get_status() === 'trash' ) ? true : false );
}
//resumptionToken
$this->add_resumption_token_xml($numRows);
ob_start('ob_gzhandler');
header($this->CONTENT_TYPE);
if (isset($this->xml_creater)) {
$this->xml_creater->display();
} else {
exit("There is a bug in codes");
}
ob_end_flush();
}
/**
* @param $data
*/
public function initiate_variables( $data ) {
if ( isset($data['resumptionToken']) ) {
if ( !file_exists(TOKEN_PREFIX . $data['resumptionToken']) ) {
$this->errors[] = $this->oai_error('badResumptionToken', '', $data['resumptionToken']);
} else {
$readings = $this->readResumToken(TOKEN_PREFIX . $data['resumptionToken']);
if ($readings == false) {
$this->errors[] = $this->oai_error('badResumptionToken', '', $data['resumptionToken']);
} else {
list($this->deliveredrecords, $this->from, $this->until, $sets, $this->metadataPrefix) = $readings;
if($sets=='-'){
$this->sets = array();
}else{
$this->sets = explode(',', $sets);
}
}
}
} else {
$this->deliveredrecords = 0;
if (isset($data['set'])) {
if (is_array($data['set'])) {
$this->sets = $data['set'];
} else {
$this->sets = array($data['set']);
}
} else {
$this->sets = array();
}
if (isset($data['from'])) {
$this->from = $data['from'];
} else {
$this->from = '-';
}
if (isset($data['until'])) {
$this->until = $data['until'];
} else {
$this->until = '-';
}
$this->metadataPrefix = $data['metadataPrefix'];
}
if(is_array($this->errors)&&count($this->errors)>0){
$this->oai_exit($data,$this->errors);
}
}
public function verify_resumption_token($numRows) {
if ( $numRows === $this->MAXRECORDS ) {
if( implode(',',$this->sets) == ''){
$this->sets = '-';
}else{
$this->sets = implode(',',$this->sets);
}
$this->cursor = (int) $this->deliveredrecords + $this->MAXRECORDS;
$this->restoken = $this->createResumToken($this->cursor, $this->from,$this->until,$this->sets, $this->metadataPrefix);
$this->expirationdatetime = date("Y-m-d\TH:i:s\Z", time() * TOKEN_VALID);
}
}
public function add_resumption_token_xml($numRows) {
// ResumptionToken
if ( $this->restoken != '-') {
if ($this->expirationdatetime) {
$this->xml_creater->create_resumpToken($this->restoken, $this->expirationdatetime, $numRows, $this->cursor);
} else {
$this->xml_creater->create_resumpToken('', null, $numRows, $this->deliveredrecords);
}
}
}
}

View File

@ -179,7 +179,7 @@ class OAIPMH_List_Records extends OAIPMH_Expose {
try{
if ($maps) {
foreach ($maps as $key => $val) {
$this->xml_creater->addChild($this->working_node, $key, html_entity_decode($val));
$this->xml_creater->addChild($this->working_node, $key, html_entity_decode($val->get_value()));
}
}
}catch(Exception $e){