Location: PHPKode > projects > Buzzword > buzzword-0.8.12/planet/subscription.inc
<?php

/*
 * buzzword
 * Copyright (c) 2003 Jon Tai
 *
 * $Id: subscription.inc 22 2004-04-28 19:20:58Z brad $
 *
 * This file is part of buzzword.
 *
 * buzzword is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * buzzword is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with buzzword; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * returns all planet subscriptions
 *
 * subscriptions are ordered by created DESC
 */
function get_planet_subscriptions() {
    $subscriptions = array();

    $sql = 'SELECT subscription_key ';
    $sql .= 'FROM '.DB_PREFIX.'planet_subscriptions ';
    $sql .= 'ORDER BY title';

    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)) {
        $subscription = new planet_subscription($row['subscription_key']);
        if ($subscription->exists())
            $subscriptions[] = $subscription;
    }

    return $subscriptions;
}

class planet_subscription {
    function planet_subscription($subscription_key) {
        $this->subscription_key = (int) $subscription_key;

        $row = get_record('planet_subscription', $this->subscription_key);
        if (!$row) {
            $sql = 'SELECT UNIX_TIMESTAMP(last_retrieval) as last_retrieval, ';
            $sql .= 'title, url, feed_url, icon ';
            $sql .= 'FROM '.DB_PREFIX.'planet_subscriptions ';
            $sql .= "WHERE subscription_key = {$this->subscription_key} ";
            $result = mysql_query($sql);
            $row = mysql_fetch_assoc($result);

            mysql_free_result($result);

            if ($row)
                cache_record('planet_subscription', $this->subscription_key, $row);
        }

        $this->exists = is_array($row);
        $this->last_retrieval = ($row) ? $row['last_retrieval'] : 0;
        $this->title = ($row) ? $row['title'] : 'subscription';
        $this->url = ($row) ? $row['url'] : '';
        $this->feed_url = ($row) ? $row['feed_url'] : '';
        $this->icon = ($row) ? $row['icon'] : 0;

        $this->is_accessible = TRUE;	// needed for relevance
        $this->is_visible = TRUE;	// needed for relevance

        if (class_exists('gallery_image'))
            $this->icon = new gallery_image($this->icon);
    }

    function create() {
        $subscription_key = $this->subscription_key;
        $last_retrieval = mysql_quote_string(date('Y-m-d H:i:s', $this->last_retrieval));
        $title = mysql_quote_string($this->title);
        $url = mysql_quote_string($this->format_url());
        $feed_url = mysql_quote_string($this->format_feed_url());
        $icon = (class_exists('gallery_image')) ? $this->icon->image_key : $this->icon;

        $sql = 'INSERT INTO '.DB_PREFIX.'planet_subscriptions ';
        $sql .= '(subscription_key, last_retrieval, title, url, feed_url, icon) VALUES ';
        $sql .= "($subscription_key, $last_retrieval, $title, $url, $feed_url, $icon)";
        mysql_query($sql);

        $this->exists = TRUE;

        cache_relevance_keywords('planet_subscription', $this->subscription_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title));
        flush_record('planet_subscription', $this->subscription_key);
    }

    function update() {
        $subscription_key = $this->subscription_key;
        $last_retrieval = mysql_quote_string(date('Y-m-d H:i:s', $this->last_retrieval));
        $title = mysql_quote_string($this->title);
        $url = mysql_quote_string($this->format_url());
        $feed_url = mysql_quote_string($this->format_feed_url());
        $icon = (class_exists('gallery_image')) ? $this->icon->image_key : $this->icon;

        $sql = 'UPDATE '.DB_PREFIX.'planet_subscriptions SET ';
        $sql .= "title = $title, ";
        $sql .= "url = $url, ";
        $sql .= "feed_url = $feed_url, ";
        $sql .= "last_retrieval = $last_retrieval, ";
        $sql .= "icon = $icon ";
        $sql .= "WHERE subscription_key = $subscription_key";
        mysql_query($sql);

        cache_relevance_keywords('planet_subscription', $this->subscription_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title));
        flush_record('planet_subscription', $this->subscription_key);
    }

    function destroy() {
        $sql = 'DELETE FROM '.DB_PREFIX.'planet_subscriptions ';
        $sql .= "WHERE subscription_key = {$this->subscription_key} ";
        mysql_query($sql);

        $this->exists = FALSE;

        $entries = get_planet_entries($this->subscription_key);
        foreach ($entries as $entry)
            $entry->destroy();

        flush_relevance_keywords('planet_subscription', $this->subscription_key);
        flush_record('planet_subscription', $this->subscription_key);
    }

    function exists() {
        return $this->exists;
    }

    function get_display_date_last_retrieved() {
        return date(get_pref('buzzword_date_format'), $this->last_retrieval);
    }

    function get_display_time_last_retrieved() {
        return date(get_pref('buzzword_time_format'), $this->last_retrieval);
    }

    function get_display_title() {
        return $this->title;
    }

    function get_display_short_title($length = FALSE) {
        return ($length === FALSE) ?
            str_trunc($this->title, get_pref('buzzword_short_title_length'), TRUE):
            str_trunc($this->title, $length, TRUE);
    }

    function get_display_url() {
        return '<a href="'.$this->url.'" target="subscription_'.$this->subscription_key.'">'.$this->url.'</a>';
    }

    function get_display_short_url() {
        $url = parse_url($this->url);
        return $url['host'];
    }

    function get_display_feed_url() {
        return '<a href="'.$this->feed_url.'" target="subscription_'.$this->subscription_key.'">(feed)</a>';
    }

    function get_display_short_feed_url() {
        $feed_url = parse_url($this->feed_url);
        return $feed_url['host'];
    }

    function get_display_relevance() {
        $relevance = "<p>\n";
        $relevance .= $this->get_display_terse_relevance();
        $relevance .= $this->get_display_short_url()."<br>\n";
        $relevance .= "</p>\n";
        return $relevance;
    }

    function get_display_terse_relevance() {
        return "<a href=\"../planet/subscription.php?s={$this->subscription_key}\">".$this->get_display_short_title()."</a><br>\n";
    }

    function format_url() {
        $url = $this->url;

        // if no protocol is specified, use HTTP
        if (!preg_match('/^[a-z]+:/i', $url))
            $url = 'http://'.$url;

        // if url is a domain without a trailing /, add one
        if (preg_match('/^[a-z]+:\/\/[^\/]+$/i', $url))
            $url .= '/';

        return $url;
    }

    function format_feed_url() {
        $feed_url = $this->feed_url;

        // if no protocol is specified, use HTTP
        if (!preg_match('/^[a-z]+:/i', $feed_url))
            $feed_url = 'http://'.$feed_url;

        // if url is a domain without a trailing /, add one
        if (preg_match('/^[a-z]+:\/\/[^\/]+$/i', $feed_url))
            $feed_url .= '/';

        return $feed_url;
    }

    function fetch_feed($force_refetch = false) {
        $cache_stale = ((time() - $this->last_retrieval) > get_pref('planet_fetch_interval'));
        if ( ($cache_stale) || ($force_refetch) ) {
            $snoopy = new planet_snoopy();
            $snoopy->agent = 'MagpieRSS/0.51 (+http://magpierss.sf.net; No cache)';
            $snoopy->read_timeout = 5;
            $snoopy->use_gzip = true;
            @$snoopy->fetch($this->feed_url);

            if ( ($snoopy->status < 200) || ($snoopy->status >= 300) )
                return;

            $magpierss = new planet_magpierss($snoopy->results);
            if (!is_object($magpierss))
                return;

            // flush the old entries from the cache
            $entries = get_planet_entries($this->subscription_key);
            foreach ($entries as $entry)
                $entry->destroy();

            // read the new (or re-fetched) entries into the cache
            foreach ($magpierss->items as $item) {
                $entry = new planet_entry(mysql_unique_key(DB_PREFIX.'planet_entries', 'entry_key'));

                // parse out the date
                if (!empty($item['dc']['date'])) {
                    $entry->created = planet_parse_w3c_dtf($item['dc']['date']);
                } else if ( (!empty($item['pubdate'])) && (strtotime($item['pubdate']) != -1) ) {
                    $entry->created = strtotime($item['pubdate']);
                } else if ( (!empty($item['title'])) && (strtotime($item['title']) != -1) ) {
                    $entry->created = strtotime($item['title']);
                } else {
                    $entry->created = time();
                }

                // get the other fields
                $entry->trackback_url = (!empty($item['link'])) ? $item['link'] : $this->url;
                $entry->title = (!empty($item['title'])) ? $item['title'] : '';
                $entry->body = (!empty($item['description'])) ? $item['description'] : '';
                $entry->parent = $this;
                $entry->create();
            }

            // record the time for caching purposes
            $this->last_retrieval = time();
            $this->update();
        } 
    }
}

function planet_parse_w3c_dtf($date_str) {
    # regex to match wc3dtf
    $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";

    if (preg_match($pat, $date_str, $match)) {
        list($year, $month, $day, $hours, $minutes, $seconds) = 
            array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
        
        # calc epoch for current date assuming GMT
        $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
        
        $offset = 0;
        if ($match[10] == 'Z') {
            # zulu time, aka GMT
        } else {
            list($tz_mod, $tz_hour, $tz_min) =
                array($match[8], $match[9], $match[10]);
            
            # zero out the variables
            if (!$tz_hour) $tz_hour = 0;
            if (!$tz_min) $tz_min = 0;
        
            $offset_secs = (($tz_hour*60)+$tz_min)*60;
            
            # is timezone ahead of GMT?  then subtract offset
            if ($tz_mod == '+')
                $offset_secs = $offset_secs * -1;

            $offset = $offset_secs;    
        }
        $epoch = $epoch + $offset;
        return $epoch;
    } else {
        return -1;
    }
}

?>
Return current item: Buzzword