add service controller, which offer thumb, xml and search service.

This commit is contained in:
wlx 2010-11-18 07:24:32 +00:00
parent 1bd10aa0d9
commit 7b1759d39b
1 changed files with 429 additions and 0 deletions

View File

@ -0,0 +1,429 @@
<?php
class ServiceController extends Zend_Controller_Action
{
private $limit=10;
function __call($id, $arguments)
{
Zend_Debug::dump($arguments,'argu');
Zend_Debug::dump($id,'id');
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
}
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();
}
/*
* 返回XML源文件
*/
function xmlAction()
{
$uuid=$this->_request->getParam('uuid');
$row=$this->db->fetchRow("select xml.data from xml left join metadata on xml.id=metadata.id where metadata.uuid=?",array($uuid));
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->setHeader('Content-Type', 'text/xml')
->setBody($row['data']);
}
/*
* 查看数据缩略图
*/
function thumbAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$uuid = $this->_request->getParam('uuid');
$thumb=$this->db->fetchRow("select t.data,t.filetype from thumbnail t left join metadata m on t.id=m.id where m.uuid=?",array($uuid));
if (empty($thumb)) {
header("Content-Type:image/png");
header("Content-Length: " . filesize("images/nothumb.png"));
$file=fopen("images/nothumb.png",'r');
fpassthru($file);
} else {
header("Content-Type:image/".$thumb['filetype']);
print base64_decode($thumb['data']);
}
}
/*
* 高级搜索, no page, json output
*/
function searchAction()
{
$this->view->addHelperPath('helper','Zend_View_Helper_');
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
//$page=(int)$this->_request->getParam('page');
//if (empty($page)) $page=1;
//$offset=$this->limit*($page-1);
$key=$this->_request->getParam('q');
$n=(int)$this->_request->getParam('n');
$s=(int)$this->_request->getParam('s');
$e=(int)$this->_request->getParam('e');
$w=(int)$this->_request->getParam('w');
if (!(empty($n) && empty($key))) {
if (empty($n)) $n=90;
if (empty($s)) $s=-90;
if (empty($e)) $e=180;
if (empty($w)) $w=-180;
//$start=$this->_request->getParam('start');
//$end=$this->_request->getParam('end');
$sql="select id,uuid,description,filesize,fileformat from metadata where east<? and west>? and north<? and south>?";
if (!empty($key)) {
$sql.= " and (title ilike ? or description ilike ?)";
$sql.= " order by title";
$rows=$this->db->fetchAll($sql,array($e,$w,$n,$s,'%'.$key.'%','%'.$key.'%'));
} else {
$sql.= " order by title";
$rows=$this->db->fetchAll($sql,array($e,$w,$n,$s));
}
$this->_helper->json($rows);
}
}
/*
* 数据浏览
*/
function browseAction()
{
$md=new MetadataTable();
$db=$md->getAdapter();
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$limit=10;
$offset=$limit*($page-1);
$state=$db->query('select count(*) from metadata');
$row=$state->fetchAll();
$sum=$row[0]['count'];
$select=$db->select();
$select->from('metadata','*')->order('title')->limitPage($page,$limit);
$this->view->metadata = $db->fetchAll($select);
$this->view->page=new Pagination($sum,$page,$limit);
}
/*
* 产生geojson数据
*/
function jsonAction()
{
$md=new MetadataTable();
$db=$md->getAdapter();
$id = (int)$this->_request->getParam('id');
$where='';
if (!empty($id)) { $where=' where id='.$id; }
$sql='select id,uuid,west,south,north,east,title from metadata'.$where;
$state=$db->query($sql);
$rows=$state->fetchAll();
$geomd=new GeoMetaData();
foreach($rows as $row){
if ($id)
$box=new GeoBox($row['west'],$row['south'],$row['east'],$row['north']);
else
$box=new GeoBoxLine($row['west'],$row['south'],$row['east'],$row['north']);
$feature=new Geofeature();
$feature->id=$row['id'];
$feature->addProperties('title',$row['title']);
$feature->addProperties('uuid',$row['uuid']);
$feature->addProperties('id',$row['id']);
$feature->geometry=$box;
$geomd->addFeature($feature);
}
$this->_helper->json($geomd);
}
/*
* 时空动态浏览
*/
function timemapAction()
{
$sql='select id,uuid,west,south,north,east,title,timebegin,timeend from metadata where timebegin is not null';
$this->view->rows=$this->db->fetchAll($sql);
}
function detailAction()
{
$id=(int)$this->_request->getParam('id');
$xml=new XmlTable();
$db=$xml->getAdapter();
$where=$db->quoteInto('id=?',$id);
$order='id desc';
$row=$xml->fetchRow($where,$order);
// Load the XML source
$xml = new DOMDocument;
$xml->loadXML($row->data);
$xsl = new DOMDocument;
$xsl->load($this->view->config->westdc->xsl);
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
$this->view->xml=$proc->transformToXML($xml);
//$this->_helper->layout->disableLayout();
//$this->_helper->viewRenderer->setNoRender();
//$this->getResponse()->setHeader('Content-Type', 'text/html')
// ->setBody($proc->transformToXML($xml));
}
function feedAction()
{
$feedArray = array(
'title' => '中国西部环境与生态科学数据中心',
'link' => 'http://'.$_SERVER['SERVER_NAME'].'/data/feed',
'description' => '共享西部计划产生的数据',
'language' => 'zh-CN',
'charset' => 'utf-8',
'published' => time(),
//'generator' => 'Zend Framework Zend_Feed',
'entries' => array()
);
$sql="select * from metadata order by ts_created desc";
$rs=$this->db->fetchAll($sql);
$feedArray['published']=strtotime($rs[0]['ts_created']);
foreach($rs as $r)
{
$feedArray['entries'][] = array(
'title' => $r['title'],
'link' => 'http://'.$_SERVER['SERVER_NAME'].'/data/' . $r['uuid'],
'guid' => 'http://'.$_SERVER['SERVER_NAME'].'/data/' . $r['uuid'],
//'content'=>$r['description'],
'description' => $r['description'],
'lastUpdate' => strtotime($r['ts_created'])
);
}
$feed = Zend_Feed::importArray($feedArray,'rss');
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$feed->send();
}
public function pingbackAction()
{
$this->_helper->ViewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$server = new Zend_XmlRpc_Server();
$server->setClass('PingbackRpc', 'pingback');
echo $server->handle();
}
public function pingtestAction()
{
$this->_helper->ViewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$client = new Zend_XmlRpc_Client('http://test.westgis.ac.cn/data/pingback');
$arg1 = 'http://wlx.westgis.ac.cn/567/';
$arg2 = 'http://test.westgis.ac.cn/data/487591d0-d437-4114-b810-cbef7c4ee4b2';
//$result = $client->call('pingback.ping', array($arg1, $arg2));
$test = $client->getProxy('pingback');
$test->ping($arg1,$arg2);
//var_dump($result);
}
public function outputAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$sql="select title,datatype,filesize,id from metadata";
$md=$this->db->fetchAll($sql);
foreach($md as $m)
{
print $m["title"].",";
print ($m["datatype"]?"内部":"公开").",";
print ($m["filesize"]==1?" ":$m["filesize"]).",";
$sql1="select keyword from keyword where id=".$m["id"]." and keytype='place'";
$kd=$this->db->fetchAll($sql1);
foreach ($kd as $p) print $p["keyword"]." ";
print ",";
$sql1="select keyword from keyword where id=".$m["id"]." and keytype='temporal'";
$kd=$this->db->fetchAll($sql1);
foreach ($kd as $p) print $p["keyword"]." ";
print ",";
print "<br>";
}
}
/**
* parse the wiki syntax used to render tables, code modified from mediawiki
*
* @private
*/
function parseTable ( $text ) {
$lines = explode ( "\n" , $text );
$td_history = array (); // Is currently a td tag open?
$last_tag_history = array (); // Save history of last lag activated (td, th or caption)
$tr_history = array (); // Is currently a tr tag open?
$tr_attributes = array (); // history of tr attributes
$has_opened_tr = array(); // Did this table open a <tr> element?
$indent_level = 0; // indent level of the table
foreach ( $lines as $key => $line )
{
$line = trim ( $line );
if( $line == '' ) { // empty line, go to next line
continue;
}
$first_character = $line{0};
$matches = array();
if ( preg_match( '/^(:*)\{\|(.*)$/' , $line , $matches ) ) {
// First check if we are starting a new table
$indent_level = strlen( $matches[1] );
$lines[$key] = str_repeat( '<dl><dd>' , $indent_level ) . "<table{$attributes}>";
array_push ( $td_history , false );
array_push ( $last_tag_history , '' );
array_push ( $tr_history , false );
array_push ( $tr_attributes , '' );
array_push ( $has_opened_tr , false );
} else if ( count ( $td_history ) == 0 ) {
// Don't do any of the following
continue;
} else if ( substr ( $line , 0 , 2 ) == '|}' ) {
// We are ending a table
$line = '</table>' . substr ( $line , 2 );
$last_tag = array_pop ( $last_tag_history );
if ( !array_pop ( $has_opened_tr ) ) {
$line = "<tr><td></td></tr>{$line}";
}
if ( array_pop ( $tr_history ) ) {
$line = "</tr>{$line}";
}
if ( array_pop ( $td_history ) ) {
$line = "</{$last_tag}>{$line}";
}
array_pop ( $tr_attributes );
$lines[$key] = $line . str_repeat( '</dd></dl>' , $indent_level );
} else if ( substr ( $line , 0 , 2 ) == '|-' ) {
// Now we have a table row
$line = preg_replace( '#^\|-+#', '', $line );
$line = '';
$last_tag = array_pop ( $last_tag_history );
array_pop ( $has_opened_tr );
array_push ( $has_opened_tr , true );
if ( array_pop ( $tr_history ) ) {
$line = '</tr>';
}
if ( array_pop ( $td_history ) ) {
$line = "</{$last_tag}>{$line}";
}
$lines[$key] = $line;
array_push ( $tr_history , false );
array_push ( $td_history , false );
array_push ( $last_tag_history , '' );
}
else if ( $first_character == '|' || $first_character == '!' || substr ( $line , 0 , 2 ) == '|+' ) {
// This might be cell elements, td, th or captions
if ( substr ( $line , 0 , 2 ) == '|+' ) {
$first_character = '+';
$line = substr ( $line , 1 );
}
$line = substr ( $line , 1 );
if ( $first_character == '!' ) {
$line = str_replace ( '!!' , '||' , $line );
}
// Split up multiple cells on the same line.
// FIXME : This can result in improper nesting of tags processed
// by earlier parser steps, but should avoid splitting up eg
// attribute values containing literal "||".
$cells = explode( '||' , $line );
$lines[$key] = '';
// Loop through each table cell
foreach ( $cells as $cell )
{
$previous = '';
if ( $first_character != '+' )
{
$tr_after = array_pop ( $tr_attributes );
if ( !array_pop ( $tr_history ) ) {
$previous = "<tr{$tr_after}>\n";
}
array_push ( $tr_history , true );
array_push ( $tr_attributes , '' );
array_pop ( $has_opened_tr );
array_push ( $has_opened_tr , true );
}
$last_tag = array_pop ( $last_tag_history );
if ( array_pop ( $td_history ) ) {
$previous = "</{$last_tag}>{$previous}";
}
if ( $first_character == '|' ) {
$last_tag = 'td';
} else if ( $first_character == '!' ) {
$last_tag = 'th';
} else if ( $first_character == '+' ) {
$last_tag = 'caption';
} else {
$last_tag = '';
}
array_push ( $last_tag_history , $last_tag );
// A cell could contain both parameters and data
$cell_data = explode ( '|' , $cell , 2 );
// Bug 553: Note that a '|' inside an invalid link should not
// be mistaken as delimiting cell parameters
if ( strpos( $cell_data[0], '[[' ) !== false ) {
$cell = "{$previous}<{$last_tag}>{$cell}";
} else if ( count ( $cell_data ) == 1 )
$cell = "{$previous}<{$last_tag}>{$cell_data[0]}";
else {
$cell = "{$previous}<{$last_tag}>{$cell_data[1]}";
}
$lines[$key] .= $cell;
array_push ( $td_history , true );
}
}
}
// Closing open td, tr && table
while ( count ( $td_history ) > 0 )
{
if ( array_pop ( $td_history ) ) {
$lines[] = '</td>' ;
}
if ( array_pop ( $tr_history ) ) {
$lines[] = '</tr>' ;
}
if ( !array_pop ( $has_opened_tr ) ) {
$lines[] = "<tr><td></td></tr>" ;
}
$lines[] = '</table>' ;
}
$output = implode ( "\n" , $lines ) ;
//@todo:移除<table></table>中所有的换行符
//可能存在问题
$output=preg_replace('/([a-zA-Z])>\n/m','$1>',$output);
$output=preg_replace('/\n<\//m','</',$output);
// special case: don't return empty table
if( $output == "<table>\n<tr><td></td></tr>\n</table>" ) {
$output = '';
}
return $output;
}
}