This code is not completely finished and can only be used at your own risk!
This snippet use PhpDelicious to post and get data from your del.icio.us account. Some data is sent to chunks that includes all the html design code. Also includes tagcloud generator, backup, restore and random link features.
/****************************************************
* All usage att own risk! Bogge not responsible.
* Name: phpdelicious
* Version: 0.1
* Created by Bogge, bogge.com
*****************************************************
Thanks to:
Ed Eliot for:
PhpDelicious, http://www.ejeliot.com/pages/php-delicious
Delicious Stumble, http://www.ejeliot.com/blog/78
Backup & Restore scripts, http://www.ejeliot.com/blog/69
Usage: This snippet use PhpDelicious to post and get data from your del.icio.us account. Some data is sent to chunks that includes all the html design code. Also includes tagcloud generator, backup, restore and random link features. Se examples.
Installing: Download PhpDelicious @ http://www.ejeliot.com/pages/php-delicious. This script is tested with PhpDelicious last updated 13th January 2006. Upload PhpDelicious to webserver. Change first line at config to point to php-delicious.inc.php.
Examples:
[ [phpdelicious?action=getallposts]] Returns all posts.
[ [phpdelicious?action=getallposts&tag=etomite]] Returns all posts with tag etomite.
[ [phpdelicious?action=getrecentposts]] Returns 15 most recent posts.
[ [phpdelicious?action=getrecentposts&tag=etomite]] Returns 15 most recent posts with tag etomite.
[ [phpdelicious?action=getrecentposts&count=20]] Returns 20 most recent posts. Min 15, Max 100.
[ [phpdelicious?action=getrecentposts&tag=etomite&count=30]] Returns 30 most recent posts with tag etomite.
[ [phpdelicious?action=getposts]] Returns all post made today.
[ [phpdelicious?action=getposts&tag=etomite]] Returns latest post with tag etomite
[ [phpdelicious?action=getposts&tag=etomite&date=2007-02-09]] Returns all posts with tag etomite that was posted 2007-02-09.
[ [phpdelicious?action=getposts&tag=etomite&date=2007-02-09&url=http://www.etomite.org/]]
[ [phpdelicious?action=getposts&tag=etomite&url=http://www.etomite.org/]] Returns all post with etomite tag and http://www.etomite.org/ as url.
[ [phpdelicious?action=getposts&url=http://www.etomite.org/]] Returns all post with http://www.etomite.org/ as url.
[ [phpdelicious?action=tagcloud]] Returns a tagcloud.
[ [phpdelicious?action=getlastupdate]] Returns last time the user updated their del.icio.us account.
[ [phpdelicious?action=getdates]] Returns list with number of posts for each date.
[ [phpdelicious?action=getdates&tag=etomite]] Returns list with number of posts for each date for etomite tag.
[ [phpdelicious?action=stumble]] Redirect you to a random site within your posts.
[ [phpdelicious?action=stumble&tag=etomite]] Redirect you to a random site thats tagged with etomite.
[ [phpdelicious?action=getalltags]] Returns all tags in format "tag (count)".
[ [phpdelicious?action=backup]] Backup alla posts to database.
[ [phpdelicious?action=restore]] Restore all posts to del.icio.us.
Example chunk { {deliciouspost}}:
<h3><a href="{url}">{desc}</a></h3>
{notes}
<p>
<b>Last updated:</b> {updated}<br />
<b>Tags:</b> {alltags}
</p>
Format:
date - YYYY-MM-DD HH:MM:SS
url - Must be an perfect match, http://www.etomite.org/ and http://www.etomite.org is note the same url.
Public Methods not included in this snippet:
AddBundle()
AddPost()
DeleteBundle()
DeletePost()
GetAllBundles()
LastErrorNo()
LastErrorString()
RenameTag()
SQL Backup Table:
create table if not exists del_posts (
url varchar(250),
description varchar(250),
notes text,
hash varchar(50),
updated datetime,
primary key(hash)
);
create table if not exists del_tags (
hash varchar(50),
tag varchar(50)
);
*/
/*****Config*****/
/* Include php-delicious, http://www.ejeliot.com/pages/php-delicious */
require("phpdelicious/php-delicious.inc.php");
//del.icio.us
$username = 'username'; // your del.icio.us username
$password = 'password'; // your del.icio.us password
//Tags
$landing = isset($landing)? $landing : "[*id*]"; //Where to go when you clicked on a tag.
//TagCloud
$min = isset($min)? $min : "2"; //Minimum occurrences of a word to be displayed
$font_max = isset($font_max)? $font_max : "400"; //Maximum font-size % (play with that to fit your site)
$font_min = isset($font_min)? $font_min : "80"; //Minimum font-size %
$exclude = array('',' ',' ',' ','and','a','-','—','–','the','—','to','.',':',',',' '); //exclude list
//Backup and Restore
$host = 'localhost'; //Name of mysql server
$dbase = 'db'; //Database name
$user = 'user'; //Mysql user
$pass = 'password'; //Mysql password
$posts_table = 'del_posts'; //Table of posts
$tags_table = 'del_tags'; //Table of tags
/*****************/
//Functions
function output_deliciouspost($funcEtomite, $aPosts, $landing) {
foreach ($aPosts as $aPost) { //Loop posts.
foreach ($aPost["tags"] as $aPost["tag"]) { //Create "alltags" string.
$aPost["alltags"] .= '<a href="[~'.$landing.'~]?listtag='.$aPost["tag"].'">'.$aPost["tag"].'</a> ';
}
$output .= $funcEtomite->parseChunk(deliciouspost, $aPost); //Output to chunk.
/*
$aPost includes:
["url"]=> string like "http://www.etomite.com/"
["desc"]=> string like "Etomite Content Management System"
["notes"]=> string like "Etomite is a Content Management System."
["hash"]=> string, hash of url
["tags"]=> array like array(2) { [0]=> string(7) "etomite" [1]=> string(6) "cms" }
["updated"]=> string like "2007-02-07 09:47:09"
["tag"]=> string like "cms", last tag in tags array.
["alltags"]=> string like "etomite cms", all tags in one line.
*/
}
return $output;
}
//GetAllPosts
If ($action == "getallposts") {
$oPhpDelicious = new PhpDelicious($username, $password);
$aPosts = $oPhpDelicious->GetAllPosts($tag); //Call php-delicious function.
$output = output_deliciouspost($etomite, $aPosts, $landing); //Calls output_deliciouspost function.
}
//GetDates
elseif ($action == "getdates") {
$oPhpDelicious = new PhpDelicious($username, $password);
$dates = $oPhpDelicious->GetDates($tag);
if(isset($tag)) {
$output .= '<h2>Number of posts for '.$tag.' tag</h2>';
}
else {
$output .= '<h2>Number of posts for each date</h2>';
}
foreach ($dates as $date) {
$output .= '<b>'.$date['date'].':</b> '.$date['count'].'<br />';
}
}
//GetLastUpdate
elseif ($action == "getlastupdate") {
$oPhpDelicious = new PhpDelicious($username, $password);
$output = $oPhpDelicious->GetLastUpdate();
}
//GetPosts
elseif ($action == "getposts") {
$oPhpDelicious = new PhpDelicious($username, $password);
$aPosts = $oPhpDelicious->GetPosts($tag, $date, $url);
$output = output_deliciouspost($etomite, $aPosts, $landing);
}
//GetRecentPosts
elseif ($action == "getrecentposts") {
$oPhpDelicious = new PhpDelicious($username, $password);
$aPosts = $oPhpDelicious->GetRecentPosts($tag, $count);
$output = output_deliciouspost($etomite, $aPosts, $landing);
}
//GetAllTags
elseif ($action == "getalltags") {
$oPhpDelicious = new PhpDelicious($username, $password);
$tags = $oPhpDelicious->GetAllTags();
foreach($tags as $tag) {
$output .= $tag['tag']." (".$tag['count'].") ";
}
}
//TagCloud
elseif ($action == "tagcloud") {
$oPhpDelicious = new PhpDelicious($username, $password);
$tags = $oPhpDelicious->GetAllTags();
foreach($tags as $tag) {
if($tag['count']>=$min && (!in_array($tag['tag'],$exclude))) {
$words[$tag['tag']] = $tag['count'];
}
}
$max_size=max($words); //word with most hits
ksort($words, SORT_STRING); //sort them alphabetically (just comment that out, then they will be unsortet
$multiplier=$font_max/$max_size; //define the multiplier for the size
$min_size=$font_min; //minimum size
$output='<div class="tagcloud">';
foreach($words as $key => $worth) {
$font_size=$multiplier*$worth;
//$countvalues='('.$worth.')'; //uncomment this for displaying the hits next to the links
$output.='<span><a style="font-size:'.max($font_size,$min_size).'%;" href="[~'.$landing.'~]?listtag='.$key.'">'.$key.'</a>'.$countvalues.'</span> ';
$output.="rn";
}
$output.='</div><br />';
if(isset($_GET['listtag'])) {
$listtag = $_GET['listtag'];
$output .= '<h2>Links tagged with '.$listtag.'</h2>';
$oPhpDelicious2 = new PhpDelicious($username, $password);
$aPosts = $oPhpDelicious2->GetAllPosts($listtag);
$output .= output_deliciouspost($etomite, $aPosts, $landing);
}
}
//Stumble
elseif ($action == "stumble") {
$oPhpDelicious = new PhpDelicious($username, $password);
$aPosts = $oPhpDelicious->GetAllPosts($tag);
$iPost = array_rand($aPosts);
header('Location: '.$aPosts[$iPost]['url']);
exit;
}
//BackUp
elseif ($action == "backup") {
$oDelicious = new PhpDelicious($username, $password);
if ($aPosts = $oDelicious->GetAllPosts()) {
//Clean tables
$query = 'delete from '.$posts_table;
$etomite->dbExtQuery($host, $user, $pass, $dbase, $query);
$query = 'delete from '.$tags_table;
$etomite->dbExtQuery($host, $user, $pass, $dbase, $query);
$sInsertPosts = 'insert into '.$posts_table.' (url, description, notes, hash, updated) values';
$sInsertTags = 'insert into '.$tags_table.' (hash, tag) values';
foreach ($aPosts as $aPost) {
$sInsertPosts .= sprintf(" ('%s', '%s', '%s', '%s', '%s'),",
mysql_real_escape_string($aPost['url']),
mysql_real_escape_string($aPost['desc']),
mysql_real_escape_string($aPost['notes']),
mysql_real_escape_string($aPost['hash']),
mysql_real_escape_string($aPost['updated'])
);
foreach ($aPost['tags'] as $sTag) {
$sInsertTags .= sprintf(" ('%s', '%s'),",
mysql_real_escape_string($aPost['hash']),
mysql_real_escape_string($sTag)
);
}
}
$query = rtrim($sInsertPosts, ',');
$etomite->dbExtQuery($host, $user, $pass, $dbase, $query);
$query = rtrim($sInsertTags, ',');
$etomite->dbExtQuery($host, $user, $pass, $dbase, $query);
$output .= 'Backup done! (I hope)';
}
else {
$output .= $oDelicious->LastErrorString();
}
}
//Restore
elseif ($action == "restore") {
$oDelicious = new PhpDelicious($username, $password);
$query = 'select url, description, notes, hash, updated from '.$posts_table.' order by updated asc';
if ($oPosts = $etomite->dbExtQuery($host, $user, $pass, $dbase, $query)) {
while ($aPost = mysql_fetch_assoc($oPosts)) {
$aTags = array();
$query = sprintf("select tag from ".$tags_table." where hash = '%s' order by tag asc", $aPost['hash']);
if ($oTags = $etomite->dbExtQuery($host, $user, $pass, $dbase, $query)) {
while ($aTag = mysql_fetch_assoc($oTags)) {
$aTags[] = $aTag['tag'];
}
}
else {
$output .= 'Error getting tags!';
}
$oDelicious->AddPost($aPost['url'], $aPost['description'], $aPost['notes'], $aTags, $aPost['updated'], true);
}
$output .= 'Restore done! (I hope)';
}
else {
$output .= 'Error getting posts!';
}
}
else {
$oPhpDelicious = new PhpDelicious($username, $password);
return $oPhpDelicious->LastErrorString();
}
return $output;