westdc-core/Westdc/Reference/RisExport.php

140 lines
3.1 KiB
PHP

<?php
namespace Westdc\Reference;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use LibRIS\RISReader;
use LibRIS\RISTags;
use LibRIS\RISWriter;
class RisExport implements ServiceManagerAwareInterface
{
protected $serviceManager;
private $table;
private $db;
public $ris_records = NULL;
protected $ris;
public $attr;
public $attr_flip;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
$this->init();
return $this;
}
public function init()
{
$this->table = new \stdClass;
$this->table->reference = "reference";
$dbService = $this->serviceManager->get('Db');
$this->db = $dbService->getPdo();
}
//读取数据
public function preRead($mode = "all")
{
if($mode == "all")
{
$sql = "SELECT * FROM {$this->table->reference} where length(ris)<10 or ris is null ORDER BY year DESC,title ASC,id ASC";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
}//preRead
//将数据组成RIS数组格式
public function processArrayDataToRisData($arrayData,$risPrior = true,$mixAuthor = true,$mixTags = true)
{
if(!is_array($arrayData))
{
return false;
}
$risData = array();
if($risPrior === true)
{
$risReader = new RISReader();
}
$this->ris = $this->serviceManager->get('Reference/Ris');
$this->attr = $this->ris->attr;
$this->attr_flip = array_flip($this->ris->attr);
unset($this->ris);
$this->reference = $this->serviceManager->get('Reference');
foreach($arrayData as $k=>$v)
{
$risData[$k] = $this->transformToRis($v);
if($mixAuthor === true || $mixTags === true)
{
if($mixAuthor === true)
{
$author = $this->reference->getAuthorByReference($v['id'],true);
if(is_array($author) && count($author)>0)
{
$risData[$k] = array_merge($risData[$k],array("AU"=>$author));
}
unset($author);
}//mixAuthor
if($mixTags === true)
{
$tags = $this->reference->getTagsByReference($v['id'],true);
if(is_array($tags) && count($tags) > 0)
{
$risData[$k] = array_merge($risData[$k],array("KW"=>$tags));
}
unset($tags);
}
}
if(!is_array($risData[$k]) || count($risData[$k]) < 1)
{
unset($risData[$k]);
}
unset($arrayData[$k]);
}
return $risData;
}//processArrayDataToRisData
//单条记录的整编
public function transformToRis($record)
{
$arr = array();
foreach($record as $k=>$v)
{
if(!empty($v))
{
if(isset($this->attr_flip[$k]))
{
//echo $k ."-". $this->attr_flip[$k] . '-' .$v;
//echo "<br />";
$arr[$this->attr_flip[$k]] = array(0=>$v);
}
}
}
//echo "<br />";
return $arr;
}//transformToRis
//输出成文件
public function output($risData)
{
$risWirte = new RISWriter();
return @$risWirte->writeRecords($risData);
}//output
}