westdc-core/Westdc/Metadata/Dataset.php

171 lines
4.1 KiB
PHP
Raw Normal View History

2014-12-21 12:19:17 +00:00
<?php
/**
* Created by PhpStorm.
* User: liujin834
* Date: 14/12/21
* Time: 下午4:46
*/
namespace Westdc\Metadata;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Westdc\EventModel\AbstractEventManager;
class Dataset extends AbstractEventManager implements ServiceManagerAwareInterface{
protected $serviceManager;
private $db;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
$this->init();
return $this;
}
private function init()
{
$dbService = $this->serviceManager->get('Db');
$this->db = $dbService->getPdo();
}
/**
* 查询某个元数据的数据集信息(数据存储位置)
2014-12-21 12:19:17 +00:00
* @param $uuid
* @return mixed
*/
public function fetch($uuid)
{
$sql = "SELECT * FROM dataset WHERE uuid=?";
$sth = $this->db->prepare($sql);
$sth ->execute(array($uuid));
return $sth->fetch();
}
/**
*
* @param $uuid
* @param $host
* @param $path
* @return bool|string
*/
public function record($uuid,$host,$path)
{
$tools = $this->serviceManager->get('Tools');
if( false == $tools->isUUID($uuid) )
return "Invalid UUID";
$data = $this->fetch($uuid);
if(isset($data['id']) && $data['id'] > 0)
return $this->update($uuid,$host,$path);
else
return $this->insert($uuid,$host,$path);
}
/**
* @param $uuid
* @param $host
* @param $path
* @return bool
*/
public function update($uuid,$host,$path)
{
$sql = "UPDATE dataset SET host='$host',path='$path' WHERE uuid='$uuid'";
if($this->db->exec($sql) > 0)
{
$this->proftpUpload($uuid,$host);
$this->getEventManager()->trigger('dataset.update.success', $this, compact('uuid','host','path'));
return true;
}else{
return false;
}
}
/**
* @param $uuid
* @param $host
* @param $path
* @return bool
*/
public function insert($uuid,$host,$path)
{
$dbService = $this->serviceManager->get('Db');
$dbh = $dbService->getDbh();
$id = $dbh->insert('dataset',[
'uuid' => $uuid,
'host' => $host,
'path' => $path,
],true);
2014-12-21 12:19:17 +00:00
if(is_numeric($id) && $id>0)
{
$this->proftpUpload($uuid,$host);
$this->getEventManager()->trigger('dataset.insert.success', $this, compact('id','uuid','host','path'));
return true;
}else{
return false;
}
2014-12-21 12:19:17 +00:00
}
/**
* @param $uuid
* @return bool
*/
public function delete($uuid)
{
$sql = "DELETE FROM dataset WHERE uuid='$uuid'";
if($this->db->exec($sql) > 0)
{
$this->getEventManager()->trigger('dataset.delete.success', $this, compact('uuid'));
return true;
}else{
return false;
}
}
public function reload($uuid)
{
$data = $this->fetch($uuid);
if(!isset($data['id']) || empty($data['id']))
{
return "未找到对应的记录,无法完成更新";
}
try {
$this->proftpUpload($uuid, $data['host']);
}catch(\Exception $e){
return $e->getMessage();
}
return true;
}
/**
* @param $uuid
* @param $host
*/
public function proftpUpload($uuid,$host)
{
if ($host=='ftp1.westgis.ac.cn')
{
//var_dump("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1");
file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1");
} else if ($host=='ftp2.westgis.ac.cn') {
//var_dump("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1");
file_get_contents("http://ftp2.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1");
}
}
2014-12-21 12:19:17 +00:00
}