文献管理中添加了ris格式导入功能

This commit is contained in:
Li Jianxuan 2013-10-12 09:26:10 +00:00
parent 3d90704b51
commit c0a3ee9e35
6 changed files with 300 additions and 2 deletions

View File

@ -1307,6 +1307,25 @@ class Admin_DataController extends Zend_Controller_Action
return true; return true;
} }
//ris
if($ac == "ris")
{
$this->_helper->viewRenderer('ref-ris');
$submit = $this->_getParam('submit');
if(!empty($submit))
{
$ris = new \Reference\Ris();
$this->view->data = $ris->loadout();
try{
$ris->pushToDataTable($this->view->data);
}catch(Exception $e)
{
view::Dump($e->getMessage(),false);
}
}
}
}//文献管理 refAction() }//文献管理 refAction()
/* /*

View File

@ -4,4 +4,5 @@
<li <?= $this->ac=="add" ? 'class="active"':"" ?>><a href="/admin/data/ref/ac/add">单篇添加</a></li> <li <?= $this->ac=="add" ? 'class="active"':"" ?>><a href="/admin/data/ref/ac/add">单篇添加</a></li>
<li <?= $this->ac=="multiupload" ? 'class="active"':"" ?>><a href="/admin/data/ref/ac/multiupload">批量上传</a></li> <li <?= $this->ac=="multiupload" ? 'class="active"':"" ?>><a href="/admin/data/ref/ac/multiupload">批量上传</a></li>
<li <?= $this->ac=="files" ? 'class="active"':"" ?>><a href="/admin/data/ref/ac/files">文件管理</a></li> <li <?= $this->ac=="files" ? 'class="active"':"" ?>><a href="/admin/data/ref/ac/files">文件管理</a></li>
<li <?= $this->ac=="ris" ? 'class="active"':"" ?>><a href="/admin/data/ref/ac/ris">RIS导入</a></li>
</ul> </ul>

View File

@ -0,0 +1,67 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('后台管理');
$this->headTitle()->setSeparator(' - ');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/admin/data">数据管理</a>');
$this->breadcrumb('文献管理');
$this->breadcrumb()->setSeparator(' > ');
$this->theme->AppendPlus($this,'colorbox');
$this->theme->AppendPlus($this,"uploadify");
$this->theme->AppendPlus($this,'admin_plugin');
?>
<style>
table thead tr th {background:#EBF2F6;}
</style>
<div class="row-fluid">
<div class="span2">
<?= $this->partial('data/left.phtml'); ?>
</div>
<div class="span10">
<div>
<?= $this->partial('data/ref-nav.phtml',array('ac'=>$this->ac)); ?>
</div>
<?php if(!empty($this->error)) { ?>
<?= $this->error ?>
<?php } ?>
<?php if(!empty($this->msg)) { ?>
<?= $this->msg ?>
<?php } else{ ?>
<?php if(empty($this->data)) { ?>
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="inputPassword">RIS文件</label>
<div class="controls">
<input type="file" name="Filedata" id="file_upload" />
<div id="uploadedFile"></div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">RIS文本</label>
<div class="controls">
<textarea class="input-block-level" name="ristest" rows="5"></textarea>
</div>
</div>
<div class="form-actions">
<input type="hidden" name="submit" value="1" />
<button type="submit" class="btn btn-primary">导入</button>
</div>
</form>
<?php }else{ ?>
<?php
foreach($this->data as $k=>$ref)
{
echo '<ul>';
foreach($ref as $index=>$value)
{
$v = is_array($value) ? join("|",$value) : $value;
echo '<li>'.$index .":". $v .'</li>';
}
echo "</ul>";
}
?>
<?php } ?>
<?php } ?>
</div>
</div>
<!-- //页面内容 -->

View File

@ -9,6 +9,8 @@ class Table
//文献 //文献
public $reference = "reference"; public $reference = "reference";
public $metadata_reference = "mdref"; public $metadata_reference = "mdref";
public $reference_author = "ref_author";
public $reference_tag = "ref_tag";
//数据申请 //数据申请
public $offlineapp = "offlineapp"; public $offlineapp = "offlineapp";

View File

@ -423,7 +423,6 @@ class Reference
}else{ }else{
return "删除失败"; return "删除失败";
} }
} }
} }

View File

@ -0,0 +1,210 @@
<?php
namespace Reference;
use \Helpers\View as view;
use \Helpers\dbh;
//use \Reference\Listener\RisListener;
use \Files\Files;
use \LibRIS\RISReader;
use \LibRIS\RISTags;
use \LibRIS\RISWriter;
class Ris
{
private $db; //传入PDO对象.
private $config; //站点设置
protected $events = NULL;
public $table;
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');
//$Listener = new RisListener();
//@$this->events()->attachAggregate($Listener);
$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));
}
$data = $this->reBuildRisArray($records);
return $data;
//view::Dump($records,false);
}
//上传RIS文件
public function uploadRisFile()
{
$file = $_FILES['Filedata'];
if (@is_uploaded_file($file['tmp_name']) === false) {
return false;
}
return $file;
}
//文本直接导入
public function loadRisText()
{
$text = $_REQUEST['ristest'];
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];
}
}
}
}
unset($records);
return $data;
}
//将解析好的ris数据写入数据库
public function pushToDataTable($data){
if(!is_array($data) || count($data) < 1)
{
return false;
}
$dbh = new dbh();
foreach($data as $k=>$ref)
{
$tags = $ref['tags'];
$author = $ref['author'];
unset($ref['tags']);
unset($ref['author']);
$ref['reference'] = $ref['title'].'---'.date("Y-m-d H:i:s").".".microtime().rand();
$id = $dbh->insert($this->table->reference,$ref,true);
if(is_array($tags) && count($tags) > 0)
{
foreach($tags as $v)
{
$dbh->insert($this->table->reference_tag,array('id'=>$id,'tag'=>$v));
}
}
$index = 0;
if(is_array($author) && count($author) > 0)
{
foreach($author as $v)
{
$index ++ ;
$author_splited = $this->splitAuthor($v);
$dbh->insert($this->table->reference_author,array('id'=>$id , 'lastname'=>$author_splited['lastname'] , 'firstname'=>$author_splited['firstname'] , 'place'=>$index ));
}
}
}
}
//将作者名字分割为数组
public function splitAuthor($author){
if(preg_match("/\,/",$author))
{
$arr = explode(",",$author);
return array(
'firstname' => trim($arr[0]),
'lastname' => trim($arr[1])
);
}else{
return array(
'firstname' => '',
'lastname' => trim($author)
);
}
}
public $attr = array(
'TP' => 'type',
'TI' => 'title',
'AU' => 'author',
'PY' => 'year',
'LA' => 'language',
'KW' => 'tags',
'AB' => 'abstract',
'DO' => 'doi',
'PB' => 'publisher'
);
}