添加后台元数据处理工具,支持正则替换、文件大小、标题引用处理及使用声明处理

This commit is contained in:
wlx 2010-07-03 07:27:51 +00:00
parent ded86c30e9
commit 525ad50235
9 changed files with 545 additions and 1 deletions

View File

@ -0,0 +1,292 @@
<?php
class Admin_WatermdController extends Zend_Controller_Action
{
function preDispatch()
{
$this->db=Zend_Registry::get('db');
$this->view->config = Zend_Registry::get('config');
$this->messenger=$this->_helper->getHelper('FlashMessenger');
$this->view->messages = $this->messenger->getMessages();
$this->wdb=Zend_Db::factory('Pdo_Pgsql', array('host'=> '210.77.68.252','username' => 'wlz','password' => 'glacier','dbname'=> 'new_geonetwork'));
}
function postDispatch()
{
$this->view->messages = $this->messenger->getMessages();
}
function indexAction()
{
//其他连接
}
function replaceAction()
{
//use preg_replace to replace the string
$form=new ReplaceForm();
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
$pattern=$formdata["pattern"];
$form->populate($formdata);
if (substr($pattern,0,1)!='/')
{
$pattern='/'.$pattern.'/m';
}
$replace=$formdata["replace"];
if (isset($formdata["test"]))
{
$this->view->test=preg_replace($pattern,$replace,$formdata["source"]);
} elseif (isset($formdata["submit"]))
{
$sql="select uuid,data from metadata where istemplate = 'n'";
$rows=$this->wdb->fetchAll($sql);
foreach($rows as $k=>$row)
{
//do the replace
$new_data=preg_replace($pattern,$replace,$row['data']);
//防止正则错误
if (!empty($new_data))
{
$sql="update metadata set data=? where uuid=?";
$this->wdb->query($sql,array($new_data,$uuid));
$dom = new DOMDocument();
$dom->loadXML($formdata['source']);
$title=$dom->getElementsByTagName('resTitle')->item(0)->nodeValue;
$deal['uuid']=$uuid;
$deal['title']=$title;
$this->view->deal[]=$deal;
}
}
}
}
$this->view->form=$form;
}
//替换标题和英文标题到数据引用中,以标题为准
function citetitleAction()
{
$form=new ReplaceForm();
$form->setElements(array($form->source,$form->test,$form->submit));
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
$form->populate($formdata);
if (isset($formdata["test"]))
{
$dom = new DOMDocument();
$dom->loadXML($formdata['source']);
$root = $dom->getElementsByTagName('Metadata');
$xpath = new DOMXpath($dom);
$title_key=$xpath->query("//Metadata/dataIdInfo/idCitation/resTitle");
$title=$title_key->item(0)->nodeValue;
$title_en_path=$xpath->query("//Metadata/dataIdInfo/idCitation/resAltTitle");
$title_en=$title_en_path->item(0)->nodeValue;
$otherCitDet=$xpath->query("//Metadata/dataIdInfo/idCitation/otherCitDet");
$other=$otherCitDet->item(0)->nodeValue;
$other_cn=mb_strstr($other,'[',true);
$title_o=mb_strstr($other_cn,'.',false);
$title_ol=substr($title_o,1,-4);
$title_old=mb_strstr($title_ol,'.',true);
$other_eng=mb_strstr($other,'[',false);
$titlen_o=mb_strstr($other_eng,'.',false);
$titlen_ol=substr($titlen_o,1,-4);
$titlen_old=mb_strstr($titlen_ol,',',true);
$new_other=str_replace(array($title_old,$titlen_old),array($title,$title_en),$other);
$otherCitDet->item(0)->nodeValue=$new_other;
$this->view->test= $dom->saveXML($root->item(0));
} elseif (isset($formdata["submit"]))
{
$sql="select uuid,data from metadata where istemplate = 'n'";
$rows=$this->wdb->fetchAll($sql);
foreach($rows as $k=>$row)
{
//do the replace
$dom = new DOMDocument();
$dom->loadXML($row['data']);
$uuid = $row['uuid'];
$root = $dom->getElementsByTagName('Metadata');
$xpath = new DOMXpath($dom);
$title_key=$xpath->query("//Metadata/dataIdInfo/idCitation/resTitle");
$title=$title_key->item(0)->nodeValue;
$title_en_path=$xpath->query("//Metadata/dataIdInfo/idCitation/resAltTitle");
$title_en=$title_en_path->item(0)->nodeValue;
$otherCitDet=$xpath->query("//Metadata/dataIdInfo/idCitation/otherCitDet");
$other=$otherCitDet->item(0)->nodeValue;
$other_cn=mb_strstr($other,'[',true);
$title_o=mb_strstr($other_cn,'.',false);
$title_ol=substr($title_o,1,-4);
$title_old=mb_strstr($title_ol,'.',true);
$other_eng=mb_strstr($other,'[',false);
$titlen_o=mb_strstr($other_eng,'.',false);
$titlen_ol=substr($titlen_o,1,-4);
$titlen_old=mb_strstr($titlen_ol,',',true);
$new_other=str_replace(array($title_old,$titlen_old),array($title,$title_en),$other);
$otherCitDet->item(0)->nodeValue=$new_other;
$new_data = $dom->saveXML($root->item(0));
//防止错误
if (!empty($new_data))
{
$sql="update metadata set data=? where uuid=?";
$this->wdb->query($sql,array($new_data,$uuid));
$deal['uuid']=$uuid;
$deal['title']=$title;
$this->view->deal[]=$deal;
}
}
}
}
$this->view->form=$form;
}
//文件大小处理
function filesizeAction()
{
$form=new ReplaceForm();
$form->pattern->setLabel('文件大小');
$form->setElements(array($form->pattern,$form->source,$form->test,$form->submit));
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
$form->populate($formdata);
if (isset($formdata["test"]))
{
$dom = new DOMDocument();
$dom->loadXML($formdata['source']);
$root = $dom->getElementsByTagName('Metadata');
$xpath = new DOMXpath($dom);
$title_key=$xpath->query("//Metadata/dataIdInfo/idCitation/resTitle");
$title=$title_key->item(0)->nodeValue;
$transize_path=$xpath->query("//Metadata/distInfo/distTranOps/transSize");
$transize_path->item(0)->nodeValue=$formdata['pattern'];
$this->view->test= $dom->saveXML($root->item(0));
} elseif (isset($formdata["submit"]))
{
$sql="select uuid,data from metadata where istemplate = 'n'";
$rows=$this->wdb->fetchAll($sql);
foreach($rows as $k=>$row)
{
//do the replace
$dom = new DOMDocument();
$dom->loadXML($row['data']);
$root = $dom->getElementsByTagName('Metadata');
$uuid = $row['uuid'];
$xpath = new DOMXpath($dom);
$title_key=$xpath->query("//Metadata/dataIdInfo/idCitation/resTitle");
$title=$title_key->item(0)->nodeValue;
$transize_path=$xpath->query("//Metadata/distInfo/distTranOps/transSize");
$sql="select sum(file_size) as fsize from westdc_fileinfo where meta_uuid = ?";
$this->db->setFetchMode(Zend_Db::FETCH_OBJ);
$row=$this->db->fetchRow($sql,array($uuid));
$transize_path->item(0)->nodeValue=$row->fsize;
$new_data = $dom->saveXML($root->item(0));
//防止错误
if (!empty($new_data))
{
$sql="update metadata set data=? where uuid=?";
$this->wdb->query($sql,array($new_data,$uuid));
$deal['uuid']=$uuid;
$deal['title']=$title;
$this->view->deal[]=$deal;
}
}
}
}
$this->view->form=$form;
}
//修改限制使用说明
//useLimit可以是多项的
function uselimitAction()
{
$form=new ReplaceForm();
$form->pattern->setLabel('使用声明');
$form->setElements(array($form->pattern,$form->source,$form->test,$form->submit));
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
$form->populate($formdata);
$limit_text=$formdata['pattern'];
if (isset($formdata["test"]))
{
$dom = new DOMDocument();
$dom->loadXML($formdata['source']);
$root = $dom->getElementsByTagName('Metadata');
$xpath = new DOMXpath($dom);
$title_key=$xpath->query("//Metadata/dataIdInfo/idCitation/resTitle");
$title=$title_key->item(0)->nodeValue;
$uselimits=$xpath->query("//Metadata/dataIdInfo/resConst/Consts/useLimit");
$do_uselimit=1;
foreach($uselimits as $limit)
{
//已经有声明,则不需要处理
if ($limit->nodeValue==$limit_text || (strlen($limit->nodeValue)>3))
$do_uselimit=0;
}
if ($do_uselimit==1)
{
$new_uselimit=$dom->createElement('useLimit',$limit_text);
$new_resconst=$dom->createElement('resConst');
$new_const=$dom->createElement('Consts');
$new_const->appendChild($new_uselimit);
$new_resconst->appendChild($new_const);
$desckey=$xpath->query("//Metadata/dataIdInfo/descKeys")->item(0);
$resconst=$xpath->query("//Metadata/dataIdInfo/resConst")->item(0);
if ($resconst)
$resconst->parentNode->insertBefore($new_resconst,$resconst);
else
$desckey->parentNode->insertBefore($new_resconst,$desckey);
$new_data=$dom->saveXML();
}
$this->view->test= $new_data;
} elseif (isset($formdata["submit"]))
{
$sql="select uuid,data from metadata where istemplate = 'n'";
$rows=$this->wdb->fetchAll($sql);
foreach($rows as $k=>$row)
{
//do the replace
$dom = new DOMDocument();
$dom->loadXML($row['data']);
$root = $dom->getElementsByTagName('Metadata');
$uuid = $row['uuid'];
$xpath = new DOMXpath($dom);
$title_key=$xpath->query("//Metadata/dataIdInfo/idCitation/resTitle");
$title=$title_key->item(0)->nodeValue;
$uselimits=$xpath->query("//Metadata/dataIdInfo/resConst/Consts/useLimit");
$do_uselimit=1;
foreach($uselimits as $limit)
{
//已经有声明,则不需要处理
if ($limit->nodeValue==$limit_text || (strlen($limit->nodeValue)>3))
$do_uselimit=0;
}
if ($do_uselimit==1)
{
$new_uselimit=$dom->createElement('useLimit',$limit_text);
$new_resconst=$dom->createElement('resConst');
$new_const=$dom->createElement('Consts');
$new_const->appendChild($new_uselimit);
$new_resconst->appendChild($new_const);
$desckey=$xpath->query("//Metadata/dataIdInfo/descKeys")->item(0);
$resconst=$xpath->query("//Metadata/dataIdInfo/resConst")->item(0);
if ($resconst)
$resconst->parentNode->insertBefore($new_resconst,$resconst);
else
$desckey->parentNode->insertBefore($new_resconst,$desckey);
$new_data=$dom->saveXML();
//防止错误
if (!empty($new_data))
{
$sql="update metadata set data=? where uuid=?";
$this->wdb->query($sql,array($new_data,$uuid));
$deal['uuid']=$uuid;
$deal['title']=$title;
$this->view->deal[]=$deal;
}
}
}
}
}
$this->view->form=$form;
}
}

View File

@ -0,0 +1,43 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('后台管理');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/admin.css');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/admin">后台首页</a>');
$this->breadcrumb('<a href="/admin/watermd">WATER元数据工具</a>');
$this->breadcrumb('引用中的标题替换');
$this->breadcrumb()->setSeparator(' > ');
?>
<div id="leftPanel">
<?= $this->partial('watermd/left.phtml'); ?>
</div>
<div id="rightPanel">
<?php if ($this->msg or $this->messages) :?>
<div id="message">
<?php if ($this->msg) : ?>
<p><?php echo $this->msg; ?></p>
<?php endif; if ($this->messages): foreach($this->messages as $msg): ?>
<p><?php echo $msg; ?></p>
<?php endforeach;endif; ?>
</div>
<?php endif; ?>
<?php if ($this->deal) : ?>
<div id="mdlist">
已处理数据:
<?php foreach ($this->deal as $deal): ?>
<ul>
<li>UUID<?php echo $deal['uuid']; ?></li>
<li><a href="/water/<?= $deal['uuid']; ?>"><?= $deal['title']; ?></a></li>
</ul>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php echo $this->form; ?>
<?php if ($this->test) : ?>
<div>测试结果:<br />
<?= $this->escape($this->test) ?>
</div>
<?php endif; ?>

View File

@ -0,0 +1,43 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('后台管理');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/admin.css');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/admin">后台首页</a>');
$this->breadcrumb('<a href="/admin/watermd">WATER元数据工具</a>');
$this->breadcrumb('文件大小处理');
$this->breadcrumb()->setSeparator(' > ');
?>
<div id="leftPanel">
<?= $this->partial('watermd/left.phtml'); ?>
</div>
<div id="rightPanel">
<?php if ($this->msg or $this->messages) :?>
<div id="message">
<?php if ($this->msg) : ?>
<p><?php echo $this->msg; ?></p>
<?php endif; if ($this->messages): foreach($this->messages as $msg): ?>
<p><?php echo $msg; ?></p>
<?php endforeach;endif; ?>
</div>
<?php endif; ?>
<?php if ($this->deal) : ?>
<div id="mdlist">
已处理数据:
<?php foreach ($this->deal as $deal): ?>
<ul>
<li>UUID<?php echo $deal['uuid']; ?></li>
<li><a href="/water/<?= $deal['uuid']; ?>"><?= $deal['title']; ?></a></li>
</ul>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php echo $this->form; ?>
<?php if ($this->test) : ?>
<div>测试结果:<br />
<?= $this->escape($this->test) ?>
</div>
<?php endif; ?>

View File

@ -0,0 +1,20 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('后台管理');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/admin.css');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/admin">后台首页</a>');
$this->breadcrumb('WATER元数据工具');
$this->breadcrumb()->setSeparator(' > ');
?>
<div id="divContent">
<div id="leftPanel">
<?= $this->partial('watermd/left.phtml'); ?>
</div>
<div id="rightPanel">
<p>此页面的所有功能都是对元数据进行直接处理!</p>
<p>请小心使用!</p>
<p>不要多次刷新!</p>
</div>
</div>

View File

@ -0,0 +1,6 @@
<ul>
<li><a href="/admin/watermd/replace">正则替换工具</a></li>
<li><a href="/admin/watermd/citetitle">引用的标题替换</a></li>
<li><a href="/admin/watermd/filesize">文件大小处理</a></li>
<li><a href="/admin/watermd/uselimit">使用声明处理</a></li>
</ul>

View File

@ -0,0 +1,44 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('后台管理');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/admin.css');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/admin">后台首页</a>');
$this->breadcrumb('<a href="/admin/watermd">WATER元数据工具</a>');
$this->breadcrumb('正则替换');
$this->breadcrumb()->setSeparator(' > ');
?>
<div id="leftPanel">
<?= $this->partial('watermd/left.phtml'); ?>
</div>
<div id="rightPanel">
<?php if ($this->msg or $this->messages) :?>
<div id="message">
<?php if ($this->msg) : ?>
<p><?php echo $this->msg; ?></p>
<?php endif; if ($this->messages): foreach($this->messages as $msg): ?>
<p><?php echo $msg; ?></p>
<?php endforeach;endif; ?>
</div>
<?php endif; ?>
<?php if ($this->deal) : ?>
<div id="mdlist">
已处理数据:
<?php foreach ($this->deal as $deal): ?>
<ul>
<li>UUID<?php echo $deal['uuid']; ?></li>
<li><a href="/water/<?= $deal['uuid']; ?>"><?= $deal['title']; ?></a></li>
</ul>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php echo $this->form; ?>
<?php if ($this->test) : ?>
<div>测试结果:<br />
<?= $this->escape($this->test) ?>
</div>
<?php endif; ?>

View File

@ -0,0 +1,43 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('后台管理');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/admin.css');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/admin">后台首页</a>');
$this->breadcrumb('<a href="/admin/watermd">WATER元数据工具</a>');
$this->breadcrumb('数据使用声明处理');
$this->breadcrumb()->setSeparator(' > ');
?>
<div id="leftPanel">
<?= $this->partial('watermd/left.phtml'); ?>
</div>
<div id="rightPanel">
<?php if ($this->msg or $this->messages) :?>
<div id="message">
<?php if ($this->msg) : ?>
<p><?php echo $this->msg; ?></p>
<?php endif; if ($this->messages): foreach($this->messages as $msg): ?>
<p><?php echo $msg; ?></p>
<?php endforeach;endif; ?>
</div>
<?php endif; ?>
<?php if ($this->deal) : ?>
<div id="mdlist">
已处理数据:
<?php foreach ($this->deal as $deal): ?>
<ul>
<li>UUID<?php echo $deal['uuid']; ?></li>
<li><a href="/water/<?= $deal['uuid']; ?>"><?= $deal['title']; ?></a></li>
</ul>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php echo $this->form; ?>
<?php if ($this->test) : ?>
<div>测试结果:<br />
<?= $this->escape($this->test) ?>
</div>
<?php endif; ?>

View File

@ -1076,7 +1076,7 @@ class DataController extends Zend_Controller_Action
}
/**
* parse the wiki syntax used to render tables, code from mediawiki
* parse the wiki syntax used to render tables, code modified from mediawiki
*
* @private
*/

View File

@ -0,0 +1,53 @@
<?php
class ReplaceForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('Replace');
$pattern = new Zend_Form_Element_Text('pattern');
$pattern->setLabel('正则表达式PATTERN')->setRequired(true)
->addFilter('StripTags') ->addFilter('StringTrim');
$replace=new Zend_Form_Element_Text('replace');
$replace->setLabel('替换表达式')
->setRequired(true)
->addFilter('StringTrim')
->addValidator('NotEmpty');
$source=new Zend_Form_Element_Textarea('source');
$source->setLabel('测试XML数据')->setRequired(true)->setAttrib('rows',10);
$test = new Zend_Form_Element_Submit('test');
$test->setLabel('测试');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('全部运行!(危险,请先进行测试)');
$this->addElements(array($pattern,$replace,$source,$test,$submit));
/*
$this->clearDecorators();
$this->addDecorator('FormElements')
->addDecorator('HtmlTag', array('tag' => '<ul>','class'=>'commentform'))
->addDecorator('Form');
$this->setElementDecorators(array(
array('ViewHelper'),
array('Errors'),
array('Description'),
array('Label', array('separator'=>' ')),
array('HtmlTag', array('tag' => 'li', 'class'=>'element-group')),
));
// buttons do not need labels
$submit->setDecorators(array(
array('ViewHelper'),
array('Description'),
array('HtmlTag', array('tag' => 'li', 'class'=>'submit-group')),
));
*/
}
}