Permalink
Browse files

First new version of item classes.

  • Loading branch information...
1 parent 9a4feb6 commit 1d13c3336dc92f7160a1bf25ae1762454ee06ebb @rmccue rmccue committed Dec 10, 2011
@@ -0,0 +1,10 @@
+<?php
+
+class Lilina_Author extends Lilina_Object {
+ public $name;
+ public $url;
+
+ public static function &from_obj($obj) {
+ return parent::_from_obj(__CLASS__, $obj);
+ }
+}
@@ -0,0 +1,11 @@
+<?php
+
+class Lilina_Enclosure extends Lilina_Object {
+ public $url;
+ public $type;
+ public $length;
+
+ public static function &from_obj($obj) {
+ return parent::_from_obj(__CLASS__, $obj);
+ }
+}
@@ -0,0 +1,132 @@
+<?php
+
+class Lilina_Item extends Lilina_Object {
+ public $hash;
+ public $timestamp;
+ public $title;
+ public $content;
+ public $summary;
+ public $permalink;
+ public $metadata;
+ public $feed = false;
+ public $feed_id;
+
+ public $author = false;
+ public $enclosure = false;
+
+ protected $data;
+
+ public function __get($name) {
+ if ($name === 'data') {
+ return $this->get_data();
+ }
+ }
+
+ /*public function __set($name, $value) {
+ if ($name === 'data') {
+ throw new Exception();
+ }
+ }*/
+
+ /**
+ * Check whether the current item has an enclosure or not
+ *
+ * Checks to make sure an item has an enclosure and that that enclosure
+ * has a link to use.
+ *
+ * @since 1.0
+ *
+ * @return bool
+ */
+ public function has_enclosure() {
+ return (!empty($this->metadata->enclosure) || !empty($this->enclosure));
+ }
+
+ /**
+ * Return the enclosure for the current item
+ *
+ * @since 1.0
+ *
+ * @return string Absolute URL to the enclosure
+ */
+ public function get_enclosure() {
+ if (!empty($this->metadata->enclosure)) {
+ return $this->metadata->enclosure;
+ }
+ if (!empty($this->enclosure)) {
+ return $this->enclosure;
+ }
+ return false;
+ }
+
+ public function get_data($name) {
+ // Lilina_Item_Data::get($this->hash)
+ }
+
+ public function set_data($name, $value) {
+ // Lilina_Item_Data::set($this->hash, $this->data)
+ }
+
+ public function get_feed() {
+ return Feeds::get_instance()->get($this->feed_id);
+ }
+
+ // -- Static Methods -- \\
+
+ public static function &from_obj($obj) {
+ return parent::_from_obj(__CLASS__, $obj);
+ }
+
+ public static function from_sp($item, $feed) {
+ $new_item = new Lilina_Item();
+ $new_item->hash = sha1($item->get_id());
+ $new_item->title = $item->get_title();
+ $new_item->content = $item->get_content();
+ $new_item->summary = $item->get_description();
+ $new_item->permalink = $item->get_permalink();
+
+ $date = $item->get_date('U');
+ if ($date === 0 || $date === false || $date === null) {
+ $date = self::default_date($item);
+ }
+ $new_item->timestamp = $date;
+
+ if ($enclosure = $item->get_enclosure()) {
+ $new_item->enclosure = new Lilina_Enclosure();
+ $new_item->enclosure->url = $enclosure->get_link();
+ $new_item->enclosure->type = $enclosure->get_real_type();
+ $new_item->enclosure->length = $enclosure->get_length();
+ }
+
+ if ($author = $item->get_author()) {
+ $new_item->author = new Lilina_Author();
+ $new_item->author->name = $item->get_author()->get_name();
+ $new_item->author->url = $item->get_author()->get_link();
+ }
+ else {
+ }
+
+ if(!empty($feed))
+ $new_item->feed_id = $feed;
+
+ return $new_item;
+ }
+
+ protected static function default_date(&$item) {
+ $date = $item->get_feed()->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'pubDate');
+ $date = strtotime($date[0]['data']);
+
+ if ($date !== 0 && $date !== false && $date !== null) {
+ return $date;
+ }
+
+ $date = $item->get_feed()->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'lastBuildDate');
+ $date = strtotime($date[0]['data']);
+
+ if ($date !== 0 && $date !== false && $date !== null) {
+ return $date;
+ }
+
+ return 0;
+ }
+}
@@ -0,0 +1,181 @@
+<?php
+/**
+ * The Lilina items class
+ * @package Lilina
+ * @subpackage Classes
+ */
+
+/**
+ * Item manager
+ *
+ */
+class Lilina_Items implements Countable, IteratorAggregate {
+ protected $data = array();
+ protected $iterator;
+
+ /**
+ * Object constructor
+ *
+ * Sets our used properties with user input
+ */
+ protected function __construct() {
+ $handler = new DataHandler();
+ $current = $handler->load('items.data');
+ if ($current !== null) {
+ $this->data = json_decode($current);
+ if ($this->data === $current || $this->data === null) {
+ $this->data = unserialize($current);
+ }
+
+ $this->data = (array) $this->data;
+ }
+
+ foreach ($this->data as $key => $value) {
+ $this->data[$key] = Lilina_Item::from_obj($value);
+ }
+ $this->sort_all();
+ }
+
+ /**
+ * Get the current item
+ *
+ * Convienience method, if you're trying to loop through the
+ * items, ensure you use the Iterator
+ * @return Lilina_Item
+ */
+ public function &current() {
+ return $this->getIterator()->current();
+ }
+
+ /**
+ * Get the previous item
+ *
+ * @since 1.0
+ *
+ * @return bool|Lilina_Item False if item doesn't exist, otherwise returns the specified item
+ */
+ public function &previous() {
+ $previous = $this->getIterator()->previous();
+ $this->getIterator()->next();
+
+ return $previous;
+ }
+
+ /**
+ * Get the items iterator
+ *
+ * @return Lilina_Items_Iterator
+ */
+ public function getIterator() {
+ if (!empty($this->iterator)) {
+ return $this->iterator;
+ }
+
+ $this->iterator = new Lilina_Items_Iterator($this->data);
+ return $this->iterator;
+ }
+
+ /**
+ * Get the number of items
+ *
+ * @return int
+ */
+ public function count() {
+ return count($this->data);
+ }
+
+ /**
+ * Get an item
+ *
+ * Get an item by ID
+ *
+ * @param int $hash Item index to retrieve
+ * @return bool|Lilina_Item False if item doesn't exist, otherwise returns the specified item
+ */
+ public function get($id) {
+ if (!isset($this->items[$id])) {
+ return false;
+ }
+
+ return $this->items[$id];
+ }
+
+ public function get_items() {
+ return $this->data;
+ }
+
+ /**
+ * Check the current item against the cached items
+ *
+ * Checks the item against the cached database. If the item does not
+ * exist, calls insert_item(). If the item is out-of-date, calls
+ * update_item().
+ *
+ * @param Lilina_Item $item Item to check
+ * @return bool
+ */
+ public function check($item) {
+ if (!isset($this->data[$item->hash])) {
+ $this->update($item);
+ do_action('insert_item', $item);
+ return true;
+ }
+
+ $existing = $this->data[$item->hash];
+ if ($existing->timestamp !== $item->timestamp) {
+ $this->update($item);
+ do_action('update_item', $item, $existing);
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Update the cached version of the current item
+ *
+ * Updates the item into the database with the information from the
+ * current item.
+ *
+ * @param Lilina_Item $item Item to update
+ */
+ protected function insert($item) {
+ $this->data[$item->hash] = $item;
+ }
+
+ /**
+ * Update the cached version of the current item
+ *
+ * Updates the item into the database with the information from the
+ * current item.
+ *
+ * @param Lilina_Item $item Item to update
+ */
+ protected function update($item) {
+ $this->data[$item->hash] = $item;
+ }
+
+ /**
+ * Save items
+ *
+ * Stores current items back into cache.
+ *
+ * @since 1.0
+ */
+ public function save() {
+ $this->data = apply_filters('save_items', $this->data, $this);
+ $handler = new DataHandler();
+ $handler->save('items.data', json_encode($this->data));
+ }
+
+ /**
+ * usort callback for items
+ *
+ * @param stdObject $a First item
+ * @param stdObject $b Second item
+ * @param bool
+ */
+ public static function sort_items($a, $b) {
+ return $b->timestamp - $a->timestamp;
+ }
+}
@@ -0,0 +1,5 @@
+<?php
+
+class Lilina_Items_Iterator extends ArrayIterator {
+
+}
@@ -0,0 +1,13 @@
+<?php
+
+abstract class Lilina_Object {
+ protected static function &_from_obj($class, $obj) {
+ $vars = get_object_vars($obj);
+ $real = new $class();
+ foreach ($vars as $name => $value) {
+ $real->$name = $value;
+ }
+
+ return $real;
+ }
+}

0 comments on commit 1d13c33

Please sign in to comment.