cvs: pearweb /include pear-config.php /include/Damblan RSS.php /include/Damblan/RSS Cache.php

From: Date: Tue, 09 Sep 2003 16:00:46 +0000
Subject: cvs: pearweb /include pear-config.php /include/Damblan RSS.php /include/Damblan/RSS Cache.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-12581@lists.php.net to get a copy of this message
mj Tue Sep 9 12:00:46 2003 EDT Added files: /pearweb/include/Damblan/RSS Cache.php Modified files: /pearweb/include/Damblan RSS.php /pearweb/include pear-config.php Log: * Implementing a poor-man's RSS caching system Index: pearweb/include/Damblan/RSS.php diff -u pearweb/include/Damblan/RSS.php:1.1 pearweb/include/Damblan/RSS.php:1.2 --- pearweb/include/Damblan/RSS.php:1.1 Thu Sep 4 15:06:02 2003 +++ pearweb/include/Damblan/RSS.php Tue Sep 9 12:00:45 2003 @@ -15,7 +15,7 @@ +----------------------------------------------------------------------+ | Author: Martin Jansen <mj@php.net> | +----------------------------------------------------------------------+ - $Id: RSS.php,v 1.1 2003/09/04 19:06:02 mj Exp $ + $Id: RSS.php,v 1.2 2003/09/09 16:00:45 mj Exp $ */ require_once "PEAR.php"; @@ -26,7 +26,7 @@ * * @author Martin Jansen <mj@php.net> * @package Damblan - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ class Damblan_RSS { @@ -38,21 +38,38 @@ * @return object Instance of Damblan_RSS_* or PEAR_Error */ function getFeed($type) { + if ($type != "latest") { + $cache = $type; + $value = substr($type, 3); + $type = substr($type, 0, 3); + } else { + $value = $cache = $type; + } + + require_once "Damblan/RSS/Cache.php"; + if (Damblan_RSS_Cache::isCached($cache) == true) { + return Damblan_RSS_Cache::get($cache); + } + $site = &Damblan_Site::factory(); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array(&$site, "error404")); if ($type == "latest") { require_once "Damblan/RSS/Latest.php"; $rss_obj = new Damblan_RSS_Latest(); - } else if (substr($type, 0, 4) == "cat_") { + } else if ($type == "cat") { require_once "Damblan/RSS/Category.php"; - $rss_obj = new Damblan_RSS_Category(substr($type, 4)); - } else if (substr($type, 0, 4) == "pkg_") { + $rss_obj = new Damblan_RSS_Category($value); + } else if ($type == "pkg") { require_once "Damblan/RSS/Package.php"; - $rss_obj = new Damblan_RSS_Package(substr($type, 4)); + $rss_obj = new Damblan_RSS_Package($value); } if (isset($rss_obj)) { + if (!PEAR::isError($rss_obj)) { + Damblan_RSS_Cache::write($cache, $rss_obj->toString()); + } + return $rss_obj; } else { PEAR::raiseError("The requested URL " . $_SERVER['REQUEST_URI'] . " was not found on this server."); @@ -74,7 +91,12 @@ } else { header("Content-Type: text/xml"); print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"; - print $ret->toString(); + + if (is_object($ret)) { + print $ret->toString(); + } else { + print $ret; + } } } } Index: pearweb/include/pear-config.php diff -u pearweb/include/pear-config.php:1.13 pearweb/include/pear-config.php:1.14 --- pearweb/include/pear-config.php:1.13 Sat Jul 26 14:46:14 2003 +++ pearweb/include/pear-config.php Tue Sep 9 12:00:46 2003 @@ -15,7 +15,7 @@ +----------------------------------------------------------------------+ | Authors: | +----------------------------------------------------------------------+ - $Id: pear-config.php,v 1.13 2003/07/26 18:46:14 jon Exp $ + $Id: pear-config.php,v 1.14 2003/09/09 16:00:46 mj Exp $ */ if (isset($_SERVER['PEAR_TMPDIR'])) { @@ -60,4 +60,7 @@ } define('PEAR_COMMON_USER_NAME_REGEX', '/^[a-z][a-z0-9]+$/i'); + +define('DAMBLAN_RSS_CACHE_DIR', PEAR_TMPDIR . '/rss_cache'); +define('DAMBLAN_RSS_CACHE_TIME', 1800); ?> Index: pearweb/include/Damblan/RSS/Cache.php +++ pearweb/include/Damblan/RSS/Cache.php <?php /* +----------------------------------------------------------------------+ | PEAR Web site version 1.0 | +----------------------------------------------------------------------+ | Copyright (c) 2003 The PEAR Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Martin Jansen <mj@php.net> | +----------------------------------------------------------------------+ $Id: Cache.php,v 1.1 2003/09/09 16:00:46 mj Exp $ */ /** * Generates a RSS feed for the latest releases in a given category * * @author Martin Jansen <mj@php.net> * @package Damblan * @category RSS * @version $Revision: 1.1 $ */ class Damblan_RSS_Cache { /** * Determines if there is a valid cache file for the given feed * * @access public * @param string Name of the feed * @return boolean */ function isCached($file) { $file = DAMBLAN_RSS_CACHE_DIR . "/" . $file; if (file_exists($file)) { $ts = filemtime($file); // If the chache file is too old, don't use it $diff = time() - $ts; if ($diff <= DAMBLAN_RSS_CACHE_TIME) { return true; } } return false; } /** * Returns the content of the cache file for the given feed * * @access public * @param string Name of the feed * @return string */ function get($file) { return @file_get_contents(DAMBLAN_RSS_CACHE_DIR . "/" . $file); } /** * Writes the content of a cache file * * @todo Implement locking * @access public * @param string Name of the feed * @param string Content of the cache file * @return boolean */ function write($file, $data) { $fp = @fopen(DAMBLAN_RSS_CACHE_DIR . "/" . $file, "w"); if (!$fp) { return false; } if (!@fputs($fp, $data)) { return false; } fclose($fp); return true; } /** * Flushes the cache for the given feed * * @access public * @param string Name of the feed * @return boolean */ function flush($file) { return @unlink(DAMBLAN_RSS_CACHE_DIR . "/" . $file); } } ?>

« previous php.pear.cvs (#12581) next »