1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
| /****************************************************
* 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.="\r\n";
}
$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; |