2013-10-12 09:26:10 +00:00
|
|
|
|
<?php
|
|
|
|
|
namespace Reference;
|
|
|
|
|
|
|
|
|
|
use \Helpers\View as view;
|
|
|
|
|
use \Helpers\dbh;
|
2013-10-15 01:36:49 +00:00
|
|
|
|
use \Reference\Listener\RisListener;
|
2013-10-12 09:26:10 +00:00
|
|
|
|
use \Files\Files;
|
|
|
|
|
use \LibRIS\RISReader;
|
|
|
|
|
use \LibRIS\RISTags;
|
|
|
|
|
use \LibRIS\RISWriter;
|
|
|
|
|
|
|
|
|
|
class Ris
|
|
|
|
|
{
|
|
|
|
|
private $db; //传入PDO对象.
|
|
|
|
|
private $config; //站点设置
|
2013-10-15 01:36:49 +00:00
|
|
|
|
private $dbh;
|
2013-10-12 09:26:10 +00:00
|
|
|
|
|
|
|
|
|
protected $events = NULL;
|
|
|
|
|
public $table;
|
2013-10-15 01:36:49 +00:00
|
|
|
|
public $ris_records = NULL;
|
2013-10-12 09:26:10 +00:00
|
|
|
|
|
|
|
|
|
function __construct($db = NULL,$mail = NULL)
|
|
|
|
|
{
|
|
|
|
|
if(empty($db))
|
|
|
|
|
{
|
|
|
|
|
$this->db = \Zend_Registry::get('db');
|
|
|
|
|
}else{
|
|
|
|
|
$this->db = $db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->config = \Zend_Registry::get('config');
|
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$Listener = new RisListener();
|
|
|
|
|
@$this->events()->attachAggregate($Listener);
|
2013-10-12 09:26:10 +00:00
|
|
|
|
|
|
|
|
|
$this->table = new \Helpers\Table();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function events(\Zend_EventManager_EventCollection $events = NULL)
|
|
|
|
|
{
|
|
|
|
|
if ($events !== NULL) {
|
|
|
|
|
$this->events = $events;
|
|
|
|
|
} elseif ($this->events === NULL) {
|
|
|
|
|
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
|
|
|
|
|
}
|
|
|
|
|
return $this->events;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ris导入
|
|
|
|
|
public function loadout()
|
|
|
|
|
{
|
|
|
|
|
$file = $this->uploadRisFile();
|
|
|
|
|
$text = $this->loadRisText();
|
|
|
|
|
|
|
|
|
|
if(empty($text) && $file === false)
|
|
|
|
|
{
|
|
|
|
|
return "导入失败,请选择要导入的文件或直接使用ris文本";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$records = array();
|
|
|
|
|
|
|
|
|
|
if($file !== false)
|
|
|
|
|
{
|
|
|
|
|
$records = array_merge($records,$this->processRis($file,NULL));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!empty($text))
|
|
|
|
|
{
|
|
|
|
|
$records = array_merge($records,$this->processRis(NULL,$text));
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$this->ris_records = $records;
|
|
|
|
|
|
2013-10-12 09:26:10 +00:00
|
|
|
|
$data = $this->reBuildRisArray($records);
|
2013-10-15 01:36:49 +00:00
|
|
|
|
return $data;
|
2013-10-12 09:26:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//上传RIS文件
|
|
|
|
|
public function uploadRisFile()
|
|
|
|
|
{
|
2013-10-28 03:26:44 +00:00
|
|
|
|
if(!isset($_FILES['Filedata']))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-12 09:26:10 +00:00
|
|
|
|
$file = $_FILES['Filedata'];
|
|
|
|
|
|
|
|
|
|
if (@is_uploaded_file($file['tmp_name']) === false) {
|
2013-10-28 03:26:44 +00:00
|
|
|
|
return;
|
2013-10-12 09:26:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//文本直接导入
|
|
|
|
|
public function loadRisText()
|
|
|
|
|
{
|
2013-10-28 03:26:44 +00:00
|
|
|
|
if(!isset($_REQUEST['ristext']))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$text = $_REQUEST['ristext'];
|
2013-10-12 09:26:10 +00:00
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//处理ris文件
|
|
|
|
|
public function processRis($file = NULL,$text = NULL)
|
|
|
|
|
{
|
|
|
|
|
$ris = new RISReader();
|
|
|
|
|
if(!empty($file) && empty($text))
|
|
|
|
|
{
|
|
|
|
|
$ris->parseFile($file['tmp_name']);
|
|
|
|
|
}else{
|
|
|
|
|
$ris->parseString($text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$records = $ris->getRecords();
|
|
|
|
|
|
|
|
|
|
return $records;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//对解析过的数据进行编排
|
|
|
|
|
public function reBuildRisArray($records)
|
|
|
|
|
{
|
|
|
|
|
$data = array();
|
|
|
|
|
foreach($records as $k=>$ref)
|
|
|
|
|
{
|
|
|
|
|
$data[$k] = array();
|
|
|
|
|
foreach($ref as $index=>$value)
|
|
|
|
|
{
|
|
|
|
|
if(isset($this->attr[$index]))
|
|
|
|
|
{
|
|
|
|
|
$index_name = $this->attr[$index];
|
|
|
|
|
if(count($value) > 1)
|
|
|
|
|
{
|
|
|
|
|
$data[$k][$index_name] = array();
|
|
|
|
|
foreach($value as $item)
|
|
|
|
|
{
|
|
|
|
|
$data[$k][$index_name][] = $item;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
$data[$k][$index_name] = $value[0];
|
|
|
|
|
}
|
2013-10-15 01:36:49 +00:00
|
|
|
|
}
|
2013-10-12 09:26:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset($records);
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//将解析好的ris数据写入数据库
|
2013-10-24 09:54:24 +00:00
|
|
|
|
public function pushToDataTable($data){
|
2013-10-12 09:26:10 +00:00
|
|
|
|
|
|
|
|
|
if(!is_array($data) || count($data) < 1)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$dbh = $this->dbh = new dbh();
|
2013-10-12 09:26:10 +00:00
|
|
|
|
|
|
|
|
|
foreach($data as $k=>$ref)
|
|
|
|
|
{
|
2013-10-15 01:36:49 +00:00
|
|
|
|
if (is_null($ref['title']))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 14:41:35 +00:00
|
|
|
|
@$tags = $ref['tags'];
|
|
|
|
|
@$author = $ref['author'];
|
2013-10-12 09:26:10 +00:00
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$ref['ris'] = $this->makeRisData(array(0=>$this->ris_records[$k]));
|
2013-10-28 03:26:44 +00:00
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$results = $this->events()->trigger('checkLoad', $this, compact('ref'));
|
|
|
|
|
$id = $results->bottom();
|
|
|
|
|
|
|
|
|
|
if ($id > 0)
|
2013-11-14 02:49:15 +00:00
|
|
|
|
{
|
2013-11-01 09:13:35 +00:00
|
|
|
|
$this->unsetVar($ref);
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$this->events()->trigger('deleteAuthor', $this, compact('id'));
|
|
|
|
|
$this->events()->trigger('deleteTag', $this, compact('id'));
|
|
|
|
|
|
2013-10-24 09:54:24 +00:00
|
|
|
|
unset($ref['reference']);
|
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$dbh->update($this->table->reference,$ref," id=$id ");
|
2013-11-14 02:49:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
$ref['reference'] = $this->makeReferenceFlag($ref);
|
2013-11-01 09:13:35 +00:00
|
|
|
|
$this->unsetVar($ref);
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$id = $dbh->insert($this->table->reference,$ref,true);
|
2013-10-12 09:26:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
$this->insertTags($id,$tags);
|
|
|
|
|
$this->insertAuthor($id,$author);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 03:26:44 +00:00
|
|
|
|
//更新单个reference的RIS
|
|
|
|
|
public function updateWithRis($id,$ref)
|
|
|
|
|
{
|
|
|
|
|
if(empty($id) || !is_numeric($id))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_null($ref['title']))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@$tags = $ref['tags'];
|
|
|
|
|
@$author = $ref['author'];
|
|
|
|
|
|
|
|
|
|
$ref['ris'] = $this->makeRisData(array(0=>$this->ris_records[0]));
|
|
|
|
|
|
|
|
|
|
$this->events()->trigger('deleteAuthor', $this, compact('id'));
|
|
|
|
|
$this->events()->trigger('deleteTag', $this, compact('id'));
|
|
|
|
|
|
|
|
|
|
$this->unsetVar($ref);
|
|
|
|
|
if(isset($ref['reference'])) unset($ref['reference']);
|
|
|
|
|
|
|
|
|
|
$this->dbh = $dbh = new dbh();
|
|
|
|
|
|
|
|
|
|
if($dbh->update($this->table->reference,$ref," id=$id "))
|
|
|
|
|
{
|
|
|
|
|
$this->insertTags($id,$tags);
|
|
|
|
|
$this->insertAuthor($id,$author);
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
//写入标签
|
|
|
|
|
public function insertTags($id,$tags){
|
|
|
|
|
if(is_array($tags) && count($tags) > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach($tags as $v)
|
|
|
|
|
{
|
|
|
|
|
$this->dbh->insert($this->table->reference_tag,array('id'=>$id,'tag'=>$v));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//写入作者
|
|
|
|
|
public function insertAuthor($id,$author)
|
|
|
|
|
{
|
|
|
|
|
$index = 0;
|
|
|
|
|
if(is_array($author) && count($author) > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach($author as $v)
|
|
|
|
|
{
|
|
|
|
|
$index ++ ;
|
|
|
|
|
$author_splited = $this->splitAuthor($v);
|
|
|
|
|
$this->dbh->insert($this->table->reference_author,array('id'=>$id , 'lastname'=>$author_splited['lastname'] , 'firstname'=>$author_splited['firstname'] , 'place'=>$index ));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
2013-11-14 02:49:15 +00:00
|
|
|
|
if(is_string($author))
|
|
|
|
|
{
|
|
|
|
|
$author_splited = $this->splitAuthor($author);
|
|
|
|
|
$this->dbh->insert($this->table->reference_author,array('id'=>$id , 'lastname'=>$author_splited['lastname'] , 'firstname'=>$author_splited['firstname'] , 'place'=>0 ));
|
|
|
|
|
}
|
2013-10-15 01:36:49 +00:00
|
|
|
|
return false;
|
2013-10-12 09:26:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 01:36:49 +00:00
|
|
|
|
//创建ris格式的数据
|
|
|
|
|
public function makeRisData($ref)
|
|
|
|
|
{
|
|
|
|
|
$ris_writer = new RISWriter();
|
|
|
|
|
return $ris_writer->writeRecords($ref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//创建reference 字段
|
|
|
|
|
public function makeReferenceFlag($ref){
|
2013-11-01 09:13:35 +00:00
|
|
|
|
$str = join(', ',$ref['author']).'. ';
|
2013-11-14 02:49:15 +00:00
|
|
|
|
$str .= $ref['title'].'. ';
|
2013-11-01 09:13:35 +00:00
|
|
|
|
$str .= $ref['publisher'].', ';
|
|
|
|
|
isset($ref['year']) ? $str .= $ref['year'].', ':"";
|
|
|
|
|
isset($ref['volume']) ? $str .= $ref['volume']:"";
|
|
|
|
|
isset($ref['issue']) ? $str .= '('.$ref['issue'].')':"";
|
|
|
|
|
isset($ref['pages']) ? $str .= ':'.$ref['pages']:"";
|
|
|
|
|
isset($ref['endpage'])? $str .= '-'.$ref['endpage']:"";
|
|
|
|
|
isset($ref['doi']) ? $str .= '. doi:'.$ref['doi'] : "";
|
2013-10-15 01:36:49 +00:00
|
|
|
|
|
2013-11-01 09:13:35 +00:00
|
|
|
|
return $str;
|
2013-10-15 01:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//卸载不需要的变量
|
|
|
|
|
public function unsetVar(&$ref)
|
|
|
|
|
{
|
|
|
|
|
unset($ref['pages']);
|
|
|
|
|
unset($ref['endpage']);
|
|
|
|
|
unset($ref['issue']);
|
|
|
|
|
unset($ref['volume']);
|
|
|
|
|
unset($ref['tags']);
|
|
|
|
|
unset($ref['author']);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-12 09:26:10 +00:00
|
|
|
|
//将作者名字分割为数组
|
|
|
|
|
public function splitAuthor($author){
|
|
|
|
|
if(preg_match("/\,/",$author))
|
|
|
|
|
{
|
|
|
|
|
$arr = explode(",",$author);
|
|
|
|
|
return array(
|
2013-10-13 14:41:35 +00:00
|
|
|
|
'lastname' => trim($arr[0]),
|
|
|
|
|
'firstname' => trim($arr[1])
|
2013-10-12 09:26:10 +00:00
|
|
|
|
);
|
|
|
|
|
}else{
|
|
|
|
|
return array(
|
|
|
|
|
'firstname' => '',
|
|
|
|
|
'lastname' => trim($author)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public $attr = array(
|
2013-10-13 14:41:35 +00:00
|
|
|
|
'TY' => 'type',
|
2013-10-12 09:26:10 +00:00
|
|
|
|
'TI' => 'title',
|
|
|
|
|
'AU' => 'author',
|
|
|
|
|
'PY' => 'year',
|
|
|
|
|
'LA' => 'language',
|
|
|
|
|
'KW' => 'tags',
|
|
|
|
|
'AB' => 'abstract',
|
|
|
|
|
'DO' => 'doi',
|
2013-10-15 01:36:49 +00:00
|
|
|
|
'T2' => 'publisher',
|
|
|
|
|
'VL' => 'volume',
|
|
|
|
|
'IS' => 'issue',
|
|
|
|
|
'SP' => 'pages',
|
2013-10-13 14:41:35 +00:00
|
|
|
|
'EP' => 'endpage'
|
2013-10-12 09:26:10 +00:00
|
|
|
|
);
|
|
|
|
|
}
|