[Mapbender-commits] r4639 - trunk/mapbender/http/classes
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Wed Sep 16 11:53:18 EDT 2009
Author: christoph
Date: 2009-09-16 11:53:16 -0400 (Wed, 16 Sep 2009)
New Revision: 4639
Added:
trunk/mapbender/http/classes/class_georss.php
trunk/mapbender/http/classes/class_georss_factory.php
trunk/mapbender/http/classes/class_georss_item.php
trunk/mapbender/http/classes/class_rss.php
trunk/mapbender/http/classes/class_rss_factory.php
trunk/mapbender/http/classes/class_rss_item.php
Log:
RSS classes
Added: trunk/mapbender/http/classes/class_georss.php
===================================================================
--- trunk/mapbender/http/classes/class_georss.php (rev 0)
+++ trunk/mapbender/http/classes/class_georss.php 2009-09-16 15:53:16 UTC (rev 4639)
@@ -0,0 +1,22 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License
+# and Simplified BSD license.
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once(dirname(__FILE__)."/../../core/globalSettings.php");
+require_once(dirname(__FILE__)."/../classes/class_rss.php");
+require_once(dirname(__FILE__)."/../classes/class_georss_item.php");
+
+/**
+ * Creates an RSS Feed.
+ */
+class GeoRss extends Rss {
+
+ protected function getNamespaceString () {
+ return parent::getNamespaceString() .
+ ' xmlns:georss="http://www.georss.org/georss"';
+ }
+}
+?>
\ No newline at end of file
Added: trunk/mapbender/http/classes/class_georss_factory.php
===================================================================
--- trunk/mapbender/http/classes/class_georss_factory.php (rev 0)
+++ trunk/mapbender/http/classes/class_georss_factory.php 2009-09-16 15:53:16 UTC (rev 4639)
@@ -0,0 +1,64 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License
+# and Simplified BSD license.
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once dirname(__FILE__) . "/../../core/globalSettings.php";
+require_once dirname(__FILE__) . "/../classes/class_rss_factory.php";
+require_once dirname(__FILE__) . "/../classes/class_georss.php";
+
+class GeoRssFactory extends RssFactory {
+
+ public function createFromUrl ($url) {
+ $rss = $this->parseDocument($url, new GeoRss());
+ if (is_null($rss)) {
+ return null;
+ }
+ return $this->parseItems($url, $rss);
+ }
+
+ protected function parseItems ($url, $rss) {
+ $domxpath = $this->createDomXpathFromUrl($url, $rss);
+ if (is_null($domxpath)) {
+ return null;
+ }
+
+ $nodeList = $domxpath->query("/rss/channel/item");
+ foreach ($nodeList as $node) {
+ $item = new GeoRssItem();
+ $item = $this->parseItem($node, $item);
+ $rss->append($item);
+ }
+ return $rss;
+ }
+
+ protected function parseItem ($node, $item) {
+
+ $item = parent::parseItem($node, $item);
+
+ foreach ($node->childNodes as $childNode) {
+ switch ($childNode->tagName) {
+ case "georss:box":
+ $coordinateString = trim($childNode->nodeValue);
+ $coordinateArray = explode(" ", $coordinateString);
+
+ if (count($coordinateArray) === 4) {
+ $bbox = new Mapbender_bbox(
+ floatval($coordinateArray[0]),
+ floatval($coordinateArray[1]),
+ floatval($coordinateArray[2]),
+ floatval($coordinateArray[3]),
+ "EPSG:4326"
+ );
+ $item->setBbox($bbox);
+ }
+ break;
+ }
+ }
+ return $item;
+ }
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/classes/class_georss_item.php
===================================================================
--- trunk/mapbender/http/classes/class_georss_item.php (rev 0)
+++ trunk/mapbender/http/classes/class_georss_item.php 2009-09-16 15:53:16 UTC (rev 4639)
@@ -0,0 +1,42 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License
+# and Simplified BSD license.
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once dirname(__FILE__) . "/../../core/globalSettings.php";
+require_once dirname(__FILE__) . "/../classes/class_georss.php";
+require_once dirname(__FILE__) . "/../classes/class_bbox.php";
+
+class GeoRssItem extends RssItem {
+ private $bbox;
+
+ public function setBbox ($bbox) {
+ if (is_a($bbox, "Mapbender_bbox")) {
+ $this->bbox = $bbox;
+ return true;
+ }
+ new mb_exception(__FILE__ .
+ ": setBbox(): parameter not a Mapbender_bbox!");
+ return false;
+ }
+
+ public function getBbox () {
+ return $this->bbox;
+ }
+
+ protected function getItemString () {
+ $str = parent::getItemString();
+ if (is_a($this->bbox, "Mapbender_bbox")) {
+ $str .= "<georss:box>" .
+ $this->bbox->min->x . " " .
+ $this->bbox->min->y . " " .
+ $this->bbox->max->x . " " .
+ $this->bbox->max->y .
+ "</georss:box>\n";
+ }
+ return $str;
+ }
+}
+?>
\ No newline at end of file
Added: trunk/mapbender/http/classes/class_rss.php
===================================================================
--- trunk/mapbender/http/classes/class_rss.php (rev 0)
+++ trunk/mapbender/http/classes/class_rss.php 2009-09-16 15:53:16 UTC (rev 4639)
@@ -0,0 +1,140 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License
+# and Simplified BSD license.
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once dirname(__FILE__) . "/../../core/globalSettings.php";
+require_once dirname(__FILE__) . "/../classes/class_rss_item.php";
+
+/**
+ * Creates an RSS Feed.
+ */
+class Rss {
+ var $channel_url;
+ var $channel_title;
+ var $channel_description;
+ var $channel_lang;
+ var $channel_copyright;
+ var $channel_date;
+ var $channel_creator;
+ var $channel_subject;
+ var $image_url;
+ protected $items = array();
+ protected $nritems;
+
+ const MAX_ENTRIES = 10;
+
+ public function append ($item) {
+ $this->items[]= $item;
+ $this->nritems++;
+ }
+
+
+
+ public function __construct() {
+ $this->nritems=0;
+ $this->channel_url='';
+ $this->channel_title='';
+ $this->channel_description='';
+ $this->channel_lang='';
+ $this->channel_copyright='';
+ $this->channel_date='';
+ $this->channel_creator='';
+ $this->channel_subject='';
+ $this->image_url='';
+ }
+
+ public function setChannel($url, $title, $description, $lang, $copyright, $creator, $subject) {
+ $this->channel_url=$url;
+ $this->channel_title=$title;
+ $this->channel_description=$description;
+ $this->channel_lang=$lang;
+ $this->channel_copyright=$copyright;
+ $this->channel_date=date("Y-m-d").'T'.date("H:i:s").'+01:00';
+ $this->channel_creator=$creator;
+ $this->channel_subject=$subject;
+ }
+
+ public function setImage($url) {
+ $this->image_url=$url;
+ }
+
+ public function setItem($rssItem) {
+ if (is_a($rssItem, "RssItem")) {
+ $this->items[]= $rssItem;
+ $this->nritems++;
+ return true;
+ }
+ new mb_exception(__FILE__ .
+ ": setItem(): parameter is not an RSS item!");
+ }
+
+ public function getItem ($index) {
+ if (!is_numeric($index)) {
+ return null;
+ }
+ $i = intval($index);
+ if ($i >= 0 && $i < count($this->items)) {
+ return $this->items[$i];
+ }
+ return null;
+ }
+
+ public function __toString () {
+ $output = '<?xml version="1.0" encoding="' . CHARSET . '"?>'."\n";
+ $output .= '<rss ' . $this->getNamespaceString() . ' version="2.0">'."\n";
+// $output .= '<rdf:RDF ' . $this->getNamespaceString() . '>'."\n";
+// $output .= '<channel rdf:about="'.htmlentities($this->channel_url, ENT_QUOTES, CHARSET) .'">'."\n";
+ $output .= '<channel>'."\n";
+ $output .= '<title>'.$this->channel_title.'</title>'."\n";
+ $output .= '<link>'.htmlentities(
+ $this->channel_url,
+ ENT_QUOTES,
+ CHARSET
+ ).'</link>'."\n";
+ $output .= '<description>'.$this->channel_description.'</description>'."\n";
+# $output .= '<dc:language>'.$this->channel_lang.'</dc:language>'."\n";
+# $output .= '<dc:rights>'.$this->channel_copyright.'</dc:rights>'."\n";
+# $output .= '<dc:date>'.$this->channel_date.'</dc:date>'."\n";
+# $output .= '<dc:creator>'.$this->channel_creator.'</dc:creator>'."\n";
+# $output .= '<dc:subject>'.$this->channel_subject.'</dc:subject>'."\n";
+
+# $output .= '<items>'."\n";
+# $output .= '<rdf:Seq>';
+# for($k=0; $k<$this->nritems; $k++) {
+# $output .= '<rdf:li rdf:resource="'.
+# htmlentities(
+# $this->items[$k]->getUrl(),
+# ENT_QUOTES,
+# CHARSET
+# )
+# .'"/>'."\n";
+# };
+# $output .= '</rdf:Seq>'."\n";
+# $output .= '</items>'."\n";
+# $output .= '<image rdf:resource="'.$this->image_url.'"/>'."\n";
+ $output .= '</channel>'."\n";
+ for($k=0; $k<$this->nritems; $k++) {
+ $output .= $this->items[$k];
+ };
+// $output .= '</rdf:RDF>'."\n";
+ $output .= '</rss>'."\n";
+ return $output;
+ }
+
+ protected function getNamespaceString () {
+ return "";
+# return 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ' .
+# 'xmlns="http://purl.org/rss/1.0/" ' .
+# 'xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ' .
+# 'xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" ' .
+# 'xmlns:dc="http://purl.org/dc/elements/1.1/" ' .
+# 'xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" ' .
+# 'xmlns:admin="http://webns.net/mvcb/" ' .
+# 'xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"';
+ }
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/classes/class_rss_factory.php
===================================================================
--- trunk/mapbender/http/classes/class_rss_factory.php (rev 0)
+++ trunk/mapbender/http/classes/class_rss_factory.php 2009-09-16 15:53:16 UTC (rev 4639)
@@ -0,0 +1,100 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License
+# and Simplified BSD license.
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once dirname(__FILE__) . "/../../core/globalSettings.php";
+require_once dirname(__FILE__) . "/../classes/class_rss.php";
+
+class RssFactory {
+
+ public function createFromUrl ($url) {
+ $rss = $this->parseDocument($url, new Rss());
+ if (is_null($rss)) {
+ return null;
+ }
+ return $this->parseItems($url, $rss);
+ }
+
+ protected function parseDocument ($url, $rss) {
+ $domxpath = $this->createDomXpathFromUrl($url, $rss);
+ if (is_null($domxpath)) {
+ return null;
+ }
+
+ $nodeList = $domxpath->query("/rss/channel/title");
+ if ($nodeList->length === 1) {
+ $rss->channel_title = trim($nodeList->item(0)->nodeValue);
+ }
+ else {
+ new mb_warning(__FILE__ . ": load(): Could not find title in RSS feed.");
+ }
+
+ $nodeList = $domxpath->query("/rss/channel/description");
+ if ($nodeList->length === 1) {
+ $rss->channel_description = trim($nodeList->item(0)->nodeValue);
+ }
+ else {
+ new mb_warning(__FILE__ . ": load(): Could not find description in RSS feed.");
+ }
+
+ $nodeList = $domxpath->query("/rss/channel/link");
+ if ($nodeList->length === 1) {
+ $rss->channel_url = trim($nodeList->item(0)->nodeValue);
+ }
+ else {
+ new mb_warning(__FILE__ . ": load(): Could not find url in RSS feed.");
+ }
+ return $rss;
+ }
+
+ protected function createDomXpathFromUrl ($url) {
+ $dom = new DOMDocument();
+ $dom->preserveWhitespace = false;
+ $success = $dom->load($url);
+ if (!$success) {
+ new mb_exception(__FILE__ . ": load(): Could not load " . $url);
+ return null;
+ }
+
+ return new DOMXPath($dom);
+ }
+
+ protected function parseItems ($url, $rss) {
+ $domxpath = $this->createDomXpathFromUrl($url, $rss);
+ if (is_null($domxpath)) {
+ return null;
+ }
+
+ $nodeList = $domxpath->query("/rss/channel/item");
+ foreach ($nodeList as $node) {
+ $item = new RssItem();
+ $item = $this->parseItem($node, $item);
+ $rss->append($item);
+ }
+ return $rss;
+ }
+
+ protected function parseItem ($node, $item) {
+ foreach ($node->childNodes as $childNode) {
+ switch ($childNode->tagName) {
+ case "title":
+ $item->setTitle(trim($childNode->nodeValue));
+ break;
+
+ case "description":
+ $item->setDescription(trim($childNode->nodeValue));
+ break;
+
+ case "link":
+ $item->setUrl(trim($childNode->nodeValue));
+ break;
+ }
+ }
+ return $item;
+ }
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/classes/class_rss_item.php
===================================================================
--- trunk/mapbender/http/classes/class_rss_item.php (rev 0)
+++ trunk/mapbender/http/classes/class_rss_item.php 2009-09-16 15:53:16 UTC (rev 4639)
@@ -0,0 +1,50 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License
+# and Simplified BSD license.
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once(dirname(__FILE__)."/../../core/globalSettings.php");
+require_once(dirname(__FILE__)."/../classes/class_rss.php");
+
+class RssItem {
+ protected $title;
+ protected $description;
+ protected $url;
+
+ public function __construct () {
+ }
+
+ public function setUrl ($url) {
+ $this->url = $url;
+ }
+
+ public function getUrl () {
+ return $this->url;
+ }
+
+ public function setTitle ($title) {
+ $this->title = $title;
+ }
+
+ public function setDescription ($description) {
+ $this->description = $description;
+ }
+
+ public function __toString () {
+ return '<item>'."\n" .
+ $this->getItemString() . '</item>'."\n";
+# return '<item rdf:about="' . $this->url . '">'."\n" .
+# $this->getItemString() . '</item>'."\n";
+ }
+
+ protected function getItemString () {
+ return '<title>' . $this->title . '</title>' . "\n" .
+ '<link>' . htmlentities($this->url, ENT_QUOTES, CHARSET) . '</link>' . "\n" .
+ '<description>' . $this->description . '</description>' . "\n" .
+// '<feedburner:origLink>' . $this->url . '</feedburner:origLink>' .
+ "\n";
+ }
+}
+?>
\ No newline at end of file
More information about the Mapbender_commits
mailing list