2015-06-18 13:52:24 +00:00
< ? php
class Version
{
private $db ;
private $changelog ;
private $err ;
private $userid ;
2015-06-25 03:46:21 +00:00
public $zend_db ;
2015-06-18 13:52:24 +00:00
//初始化
function __construct ( $config )
{
//$this->db=$config->db;
$this -> versionTable = $config -> versionTable ;
2015-06-25 04:45:51 +00:00
$this -> statusTable = $config -> statusTable ;
2015-06-29 08:35:56 +00:00
$this -> metadataTable = $config -> metadataTable ;
$this -> xmlTable = $config -> xmlTable ;
2015-06-18 13:52:24 +00:00
$this -> changelog = $config -> changelog ;
2015-06-25 03:46:21 +00:00
$this -> db = pg_connect ( http_build_query ( $config -> db , '' , ' ' ));
2015-06-18 13:52:24 +00:00
$this -> err = array ();
$this -> userid = $config -> userid ;
}
function __destruct ()
{
2015-06-25 03:46:21 +00:00
pg_close ( $this -> db );
2015-06-18 13:52:24 +00:00
}
//版本更新: 指定versionid
2015-06-25 03:46:21 +00:00
function update_by_version ( $uuid , $versionid , $changelog = " " )
2015-06-18 13:52:24 +00:00
{
if ( ! $changelog ) $changelog = $this -> changelog ;
// 1. 保存变化记录 save changelog & userid
$sql = " UPDATE { $this -> versionTable } SET changelog=' $changelog ',userid= $this->userid WHERE id= $versionid and uuid=' $uuid ' " ;
pg_query ( $this -> db , $sql );
// 2. 同步元数据
$sql = " select xml from { $this -> versionTable } where id= $versionid and uuid=' $uuid ' " ;
$result = pg_query ( $this -> db , $sql );
$row = pg_fetch_row ( $result );
$iso = new ISO19115 ();
@ $iso -> loadXML ( $row [ 0 ]);
if ( $iso -> validate ())
{
$this -> err [] = " 元数据: " . $uuid . " 导入过程中发现错误。 \n " . implode ( " \n " , $iso -> error );
}
2015-06-25 03:46:21 +00:00
@ $iso -> saveDB ( $this -> zend_db );
2015-06-18 13:52:24 +00:00
// 3. 移除中间版本
$sql = " delete from { $this -> versionTable } where uuid=' $uuid ' and changelog is null " ;
pg_query ( $this -> db , $sql );
pg_free_result ( $result );
}
//版本更新:以最新的一个版本进行更新
2015-06-25 03:46:21 +00:00
function update_by_uuid ( $uuid , $changelog = " " )
2015-06-18 13:52:24 +00:00
{
if ( ! $changelog ) $changelog = $this -> changelog ;
// 1. 保存变化记录 save changelog & userid
$sql = " UPDATE { $this -> versionTable } SET changelog=' $changelog ',userid= $this->userid WHERE id in (select id from { $this -> versionTable } where uuid=' $uuid ' order by ts_created desc limit 1) " ;
pg_query ( $this -> db , $sql );
// 2. 同步元数据
$sql = " select xml from { $this -> versionTable } where uuid=' $uuid ' order by ts_created desc limit 1 " ;
$result = pg_query ( $this -> db , $sql );
$row = pg_fetch_row ( $result );
$iso = new ISO19115 ();
@ $iso -> loadXML ( $row [ 0 ]);
if ( $iso -> validate ())
{
$this -> err [] = " 元数据: " . $uuid . " 导入过程中发现错误。 \n " . implode ( " \n " , $iso -> error );
}
2015-06-25 03:46:21 +00:00
@ $iso -> saveDB ( $this -> zend_db );
2015-06-18 13:52:24 +00:00
// 3. 移除中间版本
$sql = " delete from { $this -> versionTable } where uuid=' $uuid ' and changelog is null " ;
pg_query ( $this -> db , $sql );
pg_free_result ( $result );
}
//专题更新:将指定专题的数据,全部按照最新版本进行更新
function update_by_source ( $sourceid , $changelog = " " )
{
if ( ! $changelog ) $changelog = $this -> changelog ;
// 1. 保存变化记录 save changelog & userid
$sql = " UPDATE { $this -> versionTable } SET changelog=' $changelog ',userid= $this->userid WHERE id in (
2015-06-25 04:45:51 +00:00
select distinct on ( uuid ) id from { $this -> versionTable } where uuid in ( select uuid from datasource where sourceid = $sourceid )
and uuid in ( select distinct uuid from { $this -> statusTable }) order by uuid , ts_created desc
2015-06-29 08:35:56 +00:00
) and changelog is null " ;
2015-06-18 13:52:24 +00:00
pg_query ( $this -> db , $sql );
// 2. 同步元数据
$sql = " select xml from { $this -> versionTable } WHERE id in (
2015-06-29 08:35:56 +00:00
select distinct on ( uuid ) id from { $this -> versionTable } where uuid in ( select uuid from datasource where sourceid = $sourceid )
and uuid in ( select distinct uuid from { $this -> statusTable }) order by uuid , ts_created desc
) and changelog is not null " ;
$result = pg_query ( $this -> db , $sql );
while ( $row = pg_fetch_row ( $result ))
{
$iso = new ISO19115 ();
@ $iso -> loadXML ( $row [ 0 ]);
if ( $iso -> validate ())
{
$this -> err [] = " 元数据: " . $uuid . " 导入过程中发现错误。 \n " . implode ( " \n " , $iso -> error );
}
@ $iso -> saveDB ( $this -> zend_db );
}
// 3. 移除中间版本
$sql = " delete from { $this -> versionTable } where uuid in (select distinct uuid from datasource where sourceid= $sourceid )
and uuid in ( select distinct uuid from { $this -> statusTable }) and changelog is null " ;
pg_query ( $this -> db , $sql );
pg_free_result ( $result );
}
// 更新符合SQL条件查询的所有数据的最新版本
function update_by_sql ( $like , $changelog = " " )
{
if ( ! $changelog ) $changelog = $this -> changelog ;
// 1. 保存变化记录 save changelog & userid
$sql = " UPDATE { $this -> versionTable } SET changelog=' $changelog ',userid= $this->userid WHERE id in (
select distinct on ( uuid ) id from { $this -> versionTable } where uuid in ( select distinct m . uuid from { $this -> metadataTable } m left join { $this -> xmlTable } x on m . id = x . id where x . xml like '%$like%' )
and uuid in ( select distinct uuid from { $this -> statusTable }) order by uuid , ts_created desc
) and changelog is null " ;
pg_query ( $this -> db , $sql );
// 2. 同步元数据
$sql = " select xml from { $this -> versionTable } WHERE id in (
select distinct on ( uuid ) id from { $this -> versionTable } where uuid in ( select distinct m . uuid from { $this -> metadataTable } m left join { $this -> xmlTable } x on m . id = x . id where x . xml like '%$like%' )
and uuid in ( select distinct uuid from { $this -> statusTable }) order by uuid , ts_created desc
) and changelog is not null " ;
2015-06-18 13:52:24 +00:00
$result = pg_query ( $this -> db , $sql );
while ( $row = pg_fetch_row ( $result ))
{
$iso = new ISO19115 ();
@ $iso -> loadXML ( $row [ 0 ]);
if ( $iso -> validate ())
{
$this -> err [] = " 元数据: " . $uuid . " 导入过程中发现错误。 \n " . implode ( " \n " , $iso -> error );
}
2015-06-25 03:46:21 +00:00
@ $iso -> saveDB ( $this -> zend_db );
2015-06-18 13:52:24 +00:00
}
// 3. 移除中间版本
2015-06-29 08:35:56 +00:00
$sql = " delete from { $this -> versionTable } where uuid in (select distinct m.uuid from { $this -> metadataTable } m left join { $this -> xmlTable } x on m.id=x.id where x.xml like '% $like %')
and uuid in ( select distinct uuid from { $this -> statusTable }) and changelog is null " ;
2015-06-18 13:52:24 +00:00
pg_query ( $this -> db , $sql );
pg_free_result ( $result );
}
//版本删除
function delete ( $uuid , $versionid )
{
$sql = " DELETE FROM { $this -> versionTable } WHERE uuid=' $uuid ' and id= $versionid " ;
return pg_query ( $this -> db , $sql );
}
//todo: 版本直接发布(仅发布之前为评审状态的数据)
2015-06-25 03:46:21 +00:00
function release_by_version ( $uuid , $versionid )
2015-06-18 13:52:24 +00:00
{
//todo
$sql = " SELECT s.*,v.xml,m.title FROM en.mdstatus s left join en.mdversion v on s.uuid=v.uuid
left join en . metadata m on s . uuid = m . uuid WHERE v . id = ? " ;
return pg_query ( $this -> db , $sql );
}
2015-06-25 03:46:21 +00:00
function release_by_uuid ( $uuid )
2015-06-18 13:52:24 +00:00
{
//todo
}
function release_by_source ( $sourceid )
{
//todo
}
//错误输出
function error ()
{
if ( ! empty ( $this -> err ))
{
foreach ( $this -> err as $e ) print $e ;
}
}
}