]> Tony Duckles's Git Repositories (git.nynim.org) - delicious-utils.git/blob - delicious-dump/php-delicious/examples/mysql-backup/backup-to-mysql.php
Initial commit
[delicious-utils.git] / delicious-dump / php-delicious / examples / mysql-backup / backup-to-mysql.php
1 <?php
2 // Export from del.icio.us to MySql
3 // Written by Ed Eliot (www.ejeliot.com) - November 2006
4
5 require('../../php-delicious.inc.php'); // see www.ejeliot.com/pages/php-delicious for more details on library
6
7 define('DELICIOUS_USER', 'YOUR_USER');
8 define('DELICIOUS_PASS', 'YOUR_PASS');
9
10 define('MYSQL_SERVER', 'YOUR_HOST');
11 define('MYSQL_DB', 'delicious');
12 define('MYSQL_USER', 'YOUR_USER');
13 define('MYSQL_PASS', 'YOUR_PASS');
14
15 define('POSTS_TABLE', 'posts');
16 define('TAGS_TABLE', 'tags');
17
18 $oDelicious = new PhpDelicious(DELICIOUS_USER, DELICIOUS_PASS);
19
20 if ($aPosts = $oDelicious->GetAllPosts()) {
21 if ($oDb = mysql_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS)) {
22 if (mysql_select_db(MYSQL_DB, $oDb)) {
23 mysql_query('delete from '.POSTS_TABLE, $oDb);
24 mysql_query('delete from '.TAGS_TABLE, $oDb);
25 $sInsertPosts = 'insert into '.POSTS_TABLE.' (url, description, notes, hash, updated) values';
26 $sInsertTags = 'insert into '.TAGS_TABLE.' (hash, tag) values';
27 foreach ($aPosts as $aPost) {
28 $sInsertPosts .= sprintf(" ('%s', '%s', '%s', '%s', '%s'),",
29 mysql_real_escape_string($aPost['url'], $oDb),
30 mysql_real_escape_string($aPost['desc'], $oDb),
31 mysql_real_escape_string($aPost['notes'], $oDb),
32 mysql_real_escape_string($aPost['hash'], $oDb),
33 mysql_real_escape_string($aPost['updated'], $oDb)
34 );
35 foreach ($aPost['tags'] as $sTag) {
36 $sInsertTags .= sprintf(" ('%s', '%s'),",
37 mysql_real_escape_string($aPost['hash'], $oDb),
38 mysql_real_escape_string($sTag, $oDb)
39 );
40 }
41 }
42 mysql_query(rtrim($sInsertPosts, ','), $oDb);
43 mysql_query(rtrim($sInsertTags, ','), $oDb);
44 } else {
45 echo mysql_error($oDb);
46 }
47 mysql_close($oDb);
48 } else {
49 echo "Could not connect to MySql server.";
50 }
51 } else {
52 echo $oDelicious->LastErrorString();
53 }
54 ?>