PHP Classes

File: SlashHeadlines.php

Recommend this page to a friend!
  Classes of Sandip Bhattacharya   SlashHeadlines   SlashHeadlines.php   Download  
File: SlashHeadlines.php
Role: ???
Content type: text/plain
Description: The main class file. Include this in your code.
Class: SlashHeadlines
Author: By
Last change:
Date: 21 years ago
Size: 6,476 bytes
 

Contents

Class file image Download
<?php // -*-php-*- if (!defined("SLASHHEAD")) { define("SLASHHEAD",1); /** * Classes SlashStory and SlashHeadlines * * Copyright (c) 2001 Sandip Bhattacharya http://www.sandipb.net * * $Id: SlashHeadlines.php,v 1.2 2001/03/09 23:13:48 sandipb Exp $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the credit for the * specific area of functionality facilitated by this code is not claimed * by the user. * * I am really interested in feedbacks. Please send * comments/patches/criticism/flames to <sandipb@bigfoot.com>. * You will get due credits. ;) * */ //////////////////////////////// // A Slashdot Story ////////////////////////////// class SlashStory{ var $title; var $url; var $time; var $author; var $department; var $topic; var $comments; var $section; var $image; var $storyptr; var $lastchars; function suck($o){ $this->title = $o->title; $this->url = $o->url; $this->time = $o->time; $this->author = $o->author; $this->department = $o->department; $this->topic = $o->topic; $this->comments = $o->comments; $this->section = $o->section; $this->image = $o->image; }//function suck } //class SlashStory //////////////////////////////// /// A bunch of Slashdot stories //////////////////////////////// class SlashHeadlines { var $last_update; //time() of last update var $stories = Array(); var $file_name = "slashdot.dat"; var $url = "http://www.slashdot.org/slashdot.xml"; var $do_cache = 1; var $slash_image_prefix = "http://images.slashdot.org/topics/"; var $interval = 1800; //half an hour function SlashHeadlines(){ } //function SlashHeadlines function set_local_file($f){ $this->file_name = $f; }//function set_local_file function set_cache($docache){ $this->do_cache = $docache; } // function set_cache function set_update_interval($intervalsecs){ $this->interval = $intervalsecs; } // function set_update_interval function set_url($u){ $this->url = $u; } //function set_url function set_image_prefix($prefix){ $this->slash_image_prefix = $prefix; } //function get_image_url function remote_update(){ $this->storyptr=0; $stuff = @file($this->url); if(!$stuff) return 0; $xmldata=""; while (list($i,$s) = each($stuff)) $xmldata.=$s; $this->strip_news($xmldata); if ($this->do_cache){ if (!$this->write_file()) return 0; } return 1; } //function remote_update function update(){ if ($this->do_cache){ //if no cache file try once if(!file_exists($this->file_name)) { if(!$this->remote_update()) { return 0; //bummer!! } } //try to read the cache file if (!$this->read_file()) { return 0; } //check cached time if ((time() - $this->last_update) < ($this->interval)) { return 1; //cache is sufficient } } if (!$this->remote_update()) { return 0; } return 1; } //function update function get_date($t){ return date("l j F Y, g:i a T", $t); } //function get_date function get_last_update(){ return $this->get_date($this->last_update); } //function get_last_update function read_file(){ if(!($f = @fopen($this->file_name,"r"))) return 0; $stuff = fread($f,filesize($this->file_name)); fclose($f); $fakeHeadlines = unserialize($stuff); $this->suck($fakeHeadlines); return 1; } //function read_file function write_file(){ if(!($f = @fopen($this->file_name,"w"))) return 0; $this->last_update = time(); $fakeHeadlines = serialize($this); fwrite($f,$fakeHeadlines); return 1; }//function write_file function suck($o){ $this->last_update = $o->last_update; for ($x = 0; $x < count($o->stories);$x++){ $this->stories[$x] = new SlashStory(); $this->stories[$x]->suck($o->stories[$x]); } }//function suck function strip_news($content){ $parser = xml_parser_create(); xml_set_object($parser,&$this); xml_set_element_handler($parser,"begin_element","end_element"); xml_set_character_data_handler($parser,"characters"); if (!xml_parse($parser,$content)) die("XML parse error: ". xml_error_string(xml_get_error_code($parser))." at line ". xml_get_current_line_number($parser)); } //function strip_news function begin_element($parser,$tagname){ switch($tagname){ case "STORY": $this->stories[$this->storyptr] = new SlashStory(); break; } } //function begin_element function end_element($parser,$tagname){ switch($tagname){ case "STORY": // print "Added story ".$this->storyptr.") ".$this->stories[$this->storyptr]->title."<br />"; $this->storyptr++; break; case "TITLE": $this->stories[$this->storyptr]->title = $this->lastchars; break; case "URL": $this->stories[$this->storyptr]->url = $this->lastchars; break; case "TIME": $this->stories[$this->storyptr]->time = $this->lastchars; break; case "AUTHOR": $this->stories[$this->storyptr]->author = $this->lastchars; break; case "DEPARTMENT": $this->stories[$this->storyptr]->department = $this->lastchars; break; case "TOPIC": $this->stories[$this->storyptr]->topic = $this->lastchars; break; case "COMMENTS": $this->stories[$this->storyptr]->comments = $this->lastchars; break; case "SECTION": $this->stories[$this->storyptr]->section = $this->lastchars; break; case "IMAGE": $this->stories[$this->storyptr]->image = $this->slash_image_prefix.$this->lastchars; break; } }//function end_elements function characters($parser,$data){ if (strlen(trim($data))) $this->lastchars = $data; }//function characters function get_num_stories(){ return count($this->stories); }//function get_num_stories }//class SlashHeadlines } // if !defined ?>