Skip to content

Instantly share code, notes, and snippets.

@yconst
Created October 1, 2011 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yconst/1256287 to your computer and use it in GitHub Desktop.
Save yconst/1256287 to your computer and use it in GitHub Desktop.
Blogger feed retrieval plugin with MySQL caching for the Indexhibit CMS.
<?php if (!defined('SITE')) exit('No direct script access allowed');
/**
* Cached Blogger Feed Retriever for Indexhibit
* Copyright (c) 2010, Yiannis Chatzikonstantinou (yconst)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* README
*
* Cached Blogger Plugin for Indexhibit
* written by Yiannis Chatzikonstantinou (yconst)
* http://yconst.com
*
* This is a simple plugin for indexhibit that allows you to present the feed from your blogger(blogspot) blog inside an indexhibit site. The plugin produces HTML *that is fully style-able.
* Currently titles, dates, content, content images and comments with comment authors are supported. Judging from my results the full HTML inside each post is retrieved. This can cause sometimes problems with the CSS added by this plugin. But this is an issue of the blog being imported.
*
* Because of the significant overhead of retrieving all post and comment feeds from Blogger the plugin implements a cache mechanism using indexhibit's mysql database. So, everytime the blog is loaded only the basic feed is retrieved from the blogger server, in order to compare dates with the cache. If the cache is outdated, then the whole feed is retrieved again.
*
* PLEASE NOTE:
* This plugin will WRITE DATA inside your DATABASE. While the utmost caution has been exercised to prevent it from blowing everything up, I certainly CANNOT GUARANTEE that it won't!
*
* YOU ARE USING THIS PLUGIN AT YOUR OWN RISK.
* I AM NOT RESPONSIBLE FOR ANY EFFECTS WHATSOEVER CAUSED BY IT'S INSTALLATION/USE.
* BACKUP YOUR SITE AND DATABASE BEFORE PROCEEDING.
*
* Usage
*
* 1. Upload 'plugin.ndxz_blogger.php' to '/ndxz-studio/site/plugins/' on your web-server using ftp.
*
* 2. Create a new exhibit and place the following code in the editor:
* <plug:ndxz_bloggerc 'your_blogger_blog_id', 'number_of_posts_to_retrieve'/>
* The above values should be without the ''s.
*
* You can easily determine you blogger ID by logging into your Blogger Dashboard and then clicking e.g. 'Edit Posts', your ID will appear in the browser address bar.
*
* 3. Tadaa! Enjoy your new super-integrated feed!
*
* (optional) You can customize how the feed appears by adding the following styles into your stylesheet:
*
* .bloggerPosts { your_style_here }
* .bloggerTitle { your_style_here }
* .bloggerDate { your_style_here }
* .bloggerContent { your_style_here }
* .bloggerComments { your_style_here }
* .bloggerCommentsTitle { your_style_here }
* .bloggerComment { your_style_here }
* .bloggerCommentAuthor { your_style_here }
*
* And the structure is:
* BloggerPosts
* BloggerPost
* BloggerTitle
* BloggerDate
* BloggerContent
* BloggerComments
* BloggerCommentsTitle
* BloggerComment
* BloggerCommentAuthor
*
*/
function ndxz_bloggerc($blogid, $nEntries)
{
/** Read feed using SimpleXML */
$xml_doc = simplexml_load_file("http://www.blogger.com/feeds/$blogid/posts/default");
/** Retrieve and Store Modification Date from the Feed.*/
$blogDate = $xml_doc->updated;
/** Open the Database for Access*/
openMySQL();
/** Retrieve date record from database*/
$result = mysql_query("SELECT date FROM blogger_cache");
$rows = mysql_fetch_array($result);
$dbDate = $rows[0];
/**Compare the two dates. If different, retrieve the blog posts again,
update the database with the new date and posts and return.*/
$content="";
if ($blogDate != $dbDate) {
/** Retrieve Blog Posts from Blogger..*/
$content = retrieveBlogPosts($xml_doc,$blogid,$nEntries);
/** Escape data. */
$insertDate = mysql_escape_string ( $blogDate );
$insertContent = mysql_escape_string ( $content );
/** Perform queries to erase old and store new data in the database..*/
mysql_query("DELETE FROM blogger_cache");
$query="INSERT INTO blogger_cache (date, content) VALUES ('$insertDate', '$insertContent')";
mysql_query($query);
}
/** Else, get the stored output from the database and dump it.*/
else {
$result = mysql_query("SELECT content FROM blogger_cache");
$row = mysql_fetch_row($result);
$content = $row[0];
}
/** Close database and return*/
mysql_close();
return($content);
}
/** Open the MySQL Database associated with indexhibit */
function openMysql() {
global $indx;
mysql_connect($indx['host'],$indx['user'],$indx['pass']) or die( "Unable to connect");
@mysql_select_db($indx['db']) or die( "Unable to select database");
@mysql_query("CREATE TABLE IF NOT EXISTS blogger_cache (date text, content mediumtext)");
}
/** Retrieve Blog Feed and Comments and create a HTML output of it */
function retrieveBlogPosts($xml_document,$id,$nE) {
$output="<div class=\"bloggerPosts\">";
$counter=0;
foreach ($xml_document->entry as $entry) {
$counter += 1;
if ($counter > $nE) {
break;
}
$output .=
"<div class=\"bloggerPost\">" .
"<div class=\"bloggerTitle\">" . $entry->title .
"</div><div class=\"bloggerDate\">" . $entry->published .
"</div><div class=\"bloggerContent\">" . $entry->content .
"</div>";
/** Retrieve string containing Post-ID */
$postid = $entry->id;
/** Extract the Post-ID from the return string */
$postid = stristr($postid, "post-");
$postid = substr($postid, 5);
$rep = stristr($postid, "/comments");
$postid = str_ireplace($rep, "", $postid);
/** Retrieve Comments for this Post */
$comments_url = "http://www.blogger.com/feeds/$id/$postid/comments/default";
$xml_comments = simplexml_load_file($comments_url);
$output .= "<div class=\"bloggerComments\">";
$entries=$xml_comments->entry;
if (count($entries) > 0 ) {
$output .= "<div class=\"bloggerCommentsTitle\">Comments</div>";
$commentsString = "";
foreach ($entries as $centry) {
/** Comments are returned starting from the most recent. Therefore
we create a separate comment string with the comments in reverse
and attach it to the output afterwards */
$commentsString =
"<div class=\"bloggerComment\">" . $centry->content .
"<div class=\"bloggerCommentAuthor\">" . $centry->author->name . "</div></div>"
. $commentsString;
}
$output .= $commentsString;
$output .= "</div>";
}
$output .= "</div></div>";
}
$output .= "</div>";
$output = str_ireplace("width=\"640\"", "width=\"350\"", $output);
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment