From 40afc7e9877855272caa5c65fb3575e4420b2966 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 4 Dec 2009 01:57:14 -0500 Subject: Add 4 new events: StartCssLinkElement, EndCssLinkElement, StartScriptElement, EndScriptElement --- EVENTS.txt | 21 +++++++++++++++++++++ lib/htmloutputter.php | 42 ++++++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/EVENTS.txt b/EVENTS.txt index 34a222e8f..f4ec62033 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -574,3 +574,24 @@ EndShortenUrl: After a URL has been shortened - $shortenerName: name of the requested shortener - $shortenedUrl: short version of the url +StartCssLinkElement: Before a element is written +- $action +- &$src +- &$theme +- &$media + +EndCssLinkElement: After a element is written +- $action +- $src +- $theme +- $media + +StartScriptElement: Before a element is written +- $action +- &$src +- &$type + +EndScriptElement: After a element is written +- $action +- $src +- $type diff --git a/lib/htmloutputter.php b/lib/htmloutputter.php index d267526c8..a7c596917 100644 --- a/lib/htmloutputter.php +++ b/lib/htmloutputter.php @@ -350,14 +350,17 @@ class HTMLOutputter extends XMLOutputter */ function script($src, $type='text/javascript') { - $url = parse_url($src); - if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment)) - { - $src = common_path($src) . '?version=' . STATUSNET_VERSION; + if(Event::handle('StartScriptElement', array($this,&$src,&$type))) { + $url = parse_url($src); + if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment)) + { + $src = common_path($src) . '?version=' . STATUSNET_VERSION; + } + $this->element('script', array('type' => $type, + 'src' => $src), + ' '); + Event::handle('EndScriptElement', array($this,$src,$type)); } - $this->element('script', array('type' => $type, - 'src' => $src), - ' '); } /** @@ -371,19 +374,22 @@ class HTMLOutputter extends XMLOutputter */ function cssLink($src,$theme=null,$media=null) { - $url = parse_url($src); - if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment)) - { - if(file_exists(Theme::file($src,$theme))){ - $src = Theme::path($src, $theme) . '?version=' . STATUSNET_VERSION; - }else{ - $src = common_path($src); + if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) { + $url = parse_url($src); + if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment)) + { + if(file_exists(Theme::file($src,$theme))){ + $src = Theme::path($src, $theme) . '?version=' . STATUSNET_VERSION; + }else{ + $src = common_path($src); + } } + $this->element('link', array('rel' => 'stylesheet', + 'type' => 'text/css', + 'href' => $src, + 'media' => $media)); + Event::handle('EndCssLinkElement', array($this,$src,$theme,$media)); } - $this->element('link', array('rel' => 'stylesheet', - 'type' => 'text/css', - 'href' => $src, - 'media' => $media)); } /** -- cgit v1.2.3-54-g00ecf From 99b23782ef871d774e0c4abffc3632ea70ee149a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 15:30:43 -0500 Subject: let Geonames clients use commercial Web service --- plugins/GeonamesPlugin.php | 59 +++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index 340a6f0bf..fdbf79e41 100644 --- a/plugins/GeonamesPlugin.php +++ b/plugins/GeonamesPlugin.php @@ -51,6 +51,10 @@ class GeonamesPlugin extends Plugin { const LOCATION_NS = 1; + public $host = 'ws.geonames.org'; + public $username = null; + public $token = null; + /** * convert a name into a Location object * @@ -75,12 +79,11 @@ class GeonamesPlugin extends Plugin // XXX: break down a name by commas, narrow by each - $str = http_build_query(array('maxRows' => 1, - 'q' => $name, - 'lang' => $language, - 'type' => 'json')); - - $result = $client->get('http://ws.geonames.org/search?'.$str); + $result = $client->get($this->wsUrl('search', + array('maxRows' => 1, + 'q' => $name, + 'lang' => $language, + 'type' => 'json'))); if ($result->isOk()) { $rj = json_decode($result->getBody()); @@ -135,10 +138,9 @@ class GeonamesPlugin extends Plugin $client = HTTPClient::start(); - $str = http_build_query(array('geonameId' => $id, - 'lang' => $language)); - - $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); + $result = $client->get($this->wsUrl('hierarchyJSON', + array('geonameId' => $id, + 'lang' => $language))); if ($result->isOk()) { @@ -205,12 +207,11 @@ class GeonamesPlugin extends Plugin $client = HTTPClient::start(); - $str = http_build_query(array('lat' => $lat, - 'lng' => $lon, - 'lang' => $language)); - $result = - $client->get('http://ws.geonames.org/findNearbyPlaceNameJSON?'.$str); + $client->get($this->wsUrl('findNearbyPlaceNameJSON', + array('lat' => $lat, + 'lng' => $lon, + 'lang' => $language))); if ($result->isOk()) { @@ -286,10 +287,9 @@ class GeonamesPlugin extends Plugin $client = HTTPClient::start(); - $str = http_build_query(array('geonameId' => $location->location_id, - 'lang' => $language)); - - $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); + $result = $client->get($this->wsUrl('hierarchyJSON', + array('geonameId' => $location->location_id, + 'lang' => $language))); if ($result->isOk()) { @@ -376,7 +376,7 @@ class GeonamesPlugin extends Plugin { $c = common_memcache(); - if (!$c) { + if (empty($c)) { return null; } @@ -387,7 +387,7 @@ class GeonamesPlugin extends Plugin { $c = common_memcache(); - if (!$c) { + if (empty($c)) { return null; } @@ -398,7 +398,7 @@ class GeonamesPlugin extends Plugin { $c = common_memcache(); - if (!$c) { + if (empty($c)) { return null; } @@ -411,4 +411,19 @@ class GeonamesPlugin extends Plugin implode(',', array_keys($attrs)) . ':'. common_keyize(implode(',', array_values($attrs)))); } + + function wsUrl($method, $params) + { + if (!empty($this->username)) { + $params['username'] = $this->username; + } + + if (!empty($this->token)) { + $params['token'] = $this->token; + } + + $str = http_build_query($params); + + return 'http://'.$this->host.'/'.$method.'?'.$str; + } } -- cgit v1.2.3-54-g00ecf From e074fe659903cb6cbd3dace96889f34a8c8796c9 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 15:34:25 -0500 Subject: add logging utilities to Plugin class --- lib/plugin.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/plugin.php b/lib/plugin.php index 87d7be5a7..2c77c3e12 100644 --- a/lib/plugin.php +++ b/lib/plugin.php @@ -76,4 +76,14 @@ class Plugin { return true; } + + protected function log($level, $msg) + { + common_log($level, get_class($this) . ': '.$msg); + } + + protected function debug($msg) + { + $this->log(LOG_DEBUG, $msg); + } } -- cgit v1.2.3-54-g00ecf From 41f10b742c4c3c21a8783050c6bb55dad17f050d Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 15:34:41 -0500 Subject: remove duplicate log() function from RealtimePlugin --- plugins/Realtime/RealtimePlugin.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/plugins/Realtime/RealtimePlugin.php b/plugins/Realtime/RealtimePlugin.php index cbfa6bae0..030df405b 100644 --- a/plugins/Realtime/RealtimePlugin.php +++ b/plugins/Realtime/RealtimePlugin.php @@ -120,7 +120,7 @@ class RealtimePlugin extends Plugin function onEndShowStatusNetStyles($action) { - $action->cssLink(common_path('plugins/Realtime/realtimeupdate.css'), + $action->cssLink(common_path('plugins/Realtime/realtimeupdate.css'), null, 'screen, projection, tv'); return true; } @@ -293,13 +293,6 @@ class RealtimePlugin extends Plugin return $tags; } - // Push this up to Plugin - - function log($level, $msg) - { - common_log($level, get_class($this) . ': '.$msg); - } - function _getScripts() { return array('plugins/Realtime/realtimeupdate.js', -- cgit v1.2.3-54-g00ecf From 098426cee901d264332c0ad0dd2155d30487d35f Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 16:25:05 -0500 Subject: configurable expiry for cached Geonames data defaults to 90 days --- plugins/GeonamesPlugin.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index fdbf79e41..a750f1242 100644 --- a/plugins/GeonamesPlugin.php +++ b/plugins/GeonamesPlugin.php @@ -54,6 +54,7 @@ class GeonamesPlugin extends Plugin public $host = 'ws.geonames.org'; public $username = null; public $token = null; + public $expiry = 7776000; // 90-day expiry /** * convert a name into a Location object @@ -197,6 +198,9 @@ class GeonamesPlugin extends Plugin function onLocationFromLatLon($lat, $lon, $language, &$location) { + $lat = rtrim($lat, "0"); + $lon = rtrim($lon, "0"); + $loc = $this->getCache(array('lat' => $lat, 'lon' => $lon)); @@ -380,7 +384,11 @@ class GeonamesPlugin extends Plugin return null; } - return $c->get($this->cacheKey($attrs)); + $key = $this->cacheKey($attrs); + + $value = $c->get($key); + + return $value; } function setCache($attrs, $loc) @@ -391,18 +399,11 @@ class GeonamesPlugin extends Plugin return null; } - $c->set($this->cacheKey($attrs), $loc); - } - - function clearCache($attrs) - { - $c = common_memcache(); + $key = $this->cacheKey($attrs); - if (empty($c)) { - return null; - } + $result = $c->set($key, $loc, 0, time() + $this->expiry); - $c->delete($this->cacheKey($attrs)); + return $result; } function cacheKey($attrs) -- cgit v1.2.3-54-g00ecf