Skip to content
This repository
Browse code

add hooks to backup geobookmarks

  • Loading branch information...
commit d953d4b3570d4ba5bfa148c4beabb78a08dd97e0 1 parent 992eb1e
straup authored
23 bin/backup_geobookmarks.php
... ... @@ -0,0 +1,23 @@
  1 +<?php
  2 +
  3 + $root = dirname(dirname(__FILE__));
  4 + ini_set("include_path", "{$root}/www:{$root}/www/include");
  5 +
  6 + #
  7 +
  8 + include("include/init.php");
  9 + loadlib("flickr_backups");
  10 +
  11 + if (! $GLOBALS['cfg']['enable_feature_backups']){
  12 + echo "backups are currently disabled\n";
  13 + exit();
  14 + }
  15 +
  16 + foreach (flickr_backups_users() as $user){
  17 + echo "backup contacts for {$user['username']}\n";
  18 + $rsp = flickr_backups_get_geobookmarks($user);
  19 + dumper($rsp);
  20 + }
  21 +
  22 + exit();
  23 +?>
36 bin/backup_register_all.php
... ... @@ -0,0 +1,36 @@
  1 +<?php
  2 +
  3 + $root = dirname(dirname(__FILE__));
  4 + ini_set("include_path", "{$root}/www:{$root}/www/include");
  5 +
  6 + set_time_limit(0);
  7 +
  8 + #
  9 +
  10 + include("include/init.php");
  11 + loadlib("flickr_backups");
  12 +
  13 + if (! $GLOBALS['cfg']['enable_feature_backups']){
  14 + echo "backups are currently disabled\n";
  15 + exit();
  16 + }
  17 +
  18 + $map = flickr_backups_type_map("string keys");
  19 +
  20 + foreach (flickr_backups_users() as $user){
  21 +
  22 + $backups = flickr_backups_for_user($user);
  23 +
  24 + foreach ($map as $label => $type_id){
  25 +
  26 + if (isset($backups[$label])){
  27 + continue;
  28 + }
  29 +
  30 + echo "backup", "register '{$user['username']}' for {$label} backups\n";
  31 +
  32 + $rsp = flickr_backups_create($user, $type_id);
  33 + }
  34 + }
  35 +
  36 +?>
11 schema/alters/20120217.db_users.schema
... ... @@ -0,0 +1,11 @@
  1 +CREATE TABLE `FlickrGeoBookmarks` (
  2 + `id` BIGINT(20) UNSIGNED NOT NULL,
  3 + `user_id` INT(11) UNSIGNED NOT NULL,
  4 + `name` VARCHAR(255),
  5 + `latitude` DECIMAL(9,6) DEFAULT NULL,
  6 + `longitude` DECIMAL(9,6) DEFAULT NULL,
  7 + `accuracy` TINYINT(3) UNSIGNED NOT NULL,
  8 + `woeid` INT(11) UNSIGNED NOT NULL,
  9 + `geoperms` TINYINT(3) UNSIGNED NOT NULL,
  10 + `geocontext` TINYINT(3) UNSIGNED NOT NULL
  11 +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
14 schema/db_users.schema
@@ -97,3 +97,17 @@ CREATE TABLE `FlickrPhotosGeoCorrections` (
97 97 KEY `by_user_old` (`user_id`, `old_woeid`),
98 98 KEY `by_user_new` (`user_id`, `new_woeid`)
99 99 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  100 +
  101 +DROP TABLE IF EXISTS `FlickrGeoBookmarks`;
  102 +
  103 +CREATE TABLE `FlickrGeoBookmarks` (
  104 + `id` BIGINT(20) UNSIGNED NOT NULL,
  105 + `user_id` INT(11) UNSIGNED NOT NULL,
  106 + `name` VARCHAR(255),
  107 + `latitude` DECIMAL(9,6) DEFAULT NULL,
  108 + `longitude` DECIMAL(9,6) DEFAULT NULL,
  109 + `accuracy` TINYINT(3) UNSIGNED NOT NULL,
  110 + `woeid` INT(11) UNSIGNED NOT NULL,
  111 + `geoperms` TINYINT(3) UNSIGNED NOT NULL,
  112 + `geocontext` TINYINT(3) UNSIGNED NOT NULL
  113 +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
41 www/include/lib_flickr_backups.php
@@ -2,6 +2,7 @@
2 2
3 3 loadlib("flickr_users");
4 4 loadlib("flickr_contacts_import");
  5 + loadlib("flickr_geobookmarks_import");
5 6 loadlib("flickr_photos_import");
6 7 loadlib("flickr_faves_import");
7 8
@@ -17,6 +18,7 @@ function flickr_backups_type_map($string_keys=0){
17 18 0 => 'photos',
18 19 1 => 'faves',
19 20 2 => 'contacts',
  21 + 3 => 'geobookmarks'
20 22 );
21 23
22 24 if ($string_keys){
@@ -281,4 +283,43 @@ function flickr_backups_get_contacts(&$user){
281 283 }
282 284
283 285 #################################################################
  286 +
  287 + function flickr_backups_get_geobookmarks(&$user){
  288 +
  289 + $backups = flickr_backups_for_user($user);
  290 +
  291 + if (! isset($backups['geobookmarks'])){
  292 +
  293 + # return not_okay("backups not registered");
  294 + }
  295 +
  296 + $backup = $backups['geobookmarks'];
  297 + $update = array();
  298 +
  299 + $start_time = time();
  300 +
  301 + $flickr_user = flickr_users_get_by_user_id($user['id']);
  302 +
  303 + $rsp = flickr_geobookmarks_import_for_nsid($flickr_user['nsid'], $more);
  304 +
  305 + if ($rsp['ok']){
  306 + $update['date_lastupdate'] = $start_time;
  307 + $update['details'] = "count: {$rsp['count_imported']}";
  308 +
  309 + if (! $backup['date_firstupdate']){
  310 + $update['date_firstupdate'] = $update['date_lastupdate'];
  311 + }
  312 + }
  313 +
  314 + else {
  315 + $update['details'] = "update failed ($start_time) : {$rsp['error']}";
  316 + }
  317 +
  318 + flickr_backups_update($backup, $update);
  319 +
  320 + return $rsp;
  321 + }
  322 +
  323 + #################################################################
  324 +
284 325 ?>
39 www/include/lib_flickr_geobookmarks.php
... ... @@ -0,0 +1,39 @@
  1 +<?php
  2 +
  3 + #################################################################
  4 +
  5 + function flickr_geobookmarks_add($bookmark){
  6 +
  7 + $user = users_get_by_id($bookmark['user_id']);
  8 + $cluster_id = $user['cluster_id'];
  9 +
  10 + $insert = array();
  11 +
  12 + foreach ($bookmark as $k => $v){
  13 + $insert[$k] = AddSlashes($v);
  14 + }
  15 +
  16 + $rsp = db_insert_users($cluster_id, 'FlickrGeoBookmarks', $insert);
  17 +
  18 + if ($rsp['ok']){
  19 + $rsp['bookmark'] = $bookmark;
  20 + }
  21 +
  22 + return $rsp;
  23 + }
  24 +
  25 + #################################################################
  26 +
  27 + function flickr_geobookmarks_purge_for_user(&$user){
  28 +
  29 + $cluster_id = $user['cluster_id'];
  30 + $enc_id = AddSlashes($user['id']);
  31 +
  32 + $sql = "DELETE FROM FlickrGeoBookmarks WHERE user_id='{$enc_id}'";
  33 + $rsp = db_write_users($cluster_id, $sql);
  34 +
  35 + return $rsp;
  36 + }
  37 +
  38 + #################################################################
  39 +?>
93 www/include/lib_flickr_geobookmarks_import.php
... ... @@ -0,0 +1,93 @@
  1 +<?php
  2 +
  3 + loadlib("flickr_api");
  4 + loadlib("flickr_users");
  5 + loadlib("flickr_geobookmarks");
  6 + loadlib("flickr_geo_permissions");
  7 +
  8 + #################################################################
  9 +
  10 + function flickr_geobookmarks_import_for_nsid($nsid, $more=array()){
  11 +
  12 + $flickr_user = flickr_users_get_by_nsid($nsid);
  13 + $user = users_get_by_id($flickr_user['user_id']);
  14 +
  15 + if (! $user){
  16 + return not_okay("Not a valid user");
  17 + }
  18 +
  19 + $flickr_user = flickr_users_get_by_user_id($user['id']);
  20 +
  21 + $method = 'flickr.people.geoBookmarks.getList';
  22 +
  23 + $args = array(
  24 + 'auth_token' => $flickr_user['auth_token'],
  25 + );
  26 +
  27 + $rsp = flickr_api_call($method, $args);
  28 +
  29 + if (! $rsp['ok']){
  30 + return $rsp;
  31 + }
  32 +
  33 + if (! $rsp['rsp']['bookmarks']['count']){
  34 + return okay();
  35 + }
  36 +
  37 + $bookmarks = array();
  38 +
  39 + # mark everything as private for now since none of that stuff
  40 + # got turned on before I left, sad face... (20120217/straup)
  41 +
  42 + $geo_perms = flickr_geo_permissions_map("string keys");
  43 + $geo_private = $geo_perms['private'];
  44 +
  45 + foreach ($rsp['rsp']['bookmarks']['bookmark'] as $bm){
  46 +
  47 + $bm['user_id'] = $user['id'];
  48 + $bm['name'] = $bm['label'];
  49 + $bm['geocontext'] = $bm['context'];
  50 + $bm['geoperms'] = $geo_private;
  51 + $bm['woeid'] = 0;
  52 +
  53 + unset($bm['label']);
  54 + unset($bm['pretty_name']);
  55 + unset($bm['context']);
  56 +
  57 + $geo_method = 'flickr.places.findByLatLon';
  58 +
  59 + $geo_args = array(
  60 + 'lat' => $bm['latitude'],
  61 + 'lon' => $bm['longitude'],
  62 + 'accuracy' => $bm['accuracy'],
  63 + );
  64 +
  65 + $geo_rsp = flickr_api_call($geo_method, $geo_args);
  66 +
  67 + if ($geo_rsp['ok']){
  68 + # I still miss xpath...
  69 + $bm['woeid'] = $geo_rsp['rsp']['places']['place'][0]['woeid'];
  70 + }
  71 +
  72 + $bookmarks[] = $bm;
  73 + }
  74 +
  75 + $rsp = flickr_geobookmarks_purge_for_user($user);
  76 +
  77 + if (! $rsp['ok']){
  78 + return $rsp;
  79 + }
  80 +
  81 + $count = 0;
  82 +
  83 + foreach ($bookmarks as $bm){
  84 + $rsp = flickr_geobookmarks_add($bm);
  85 + $count += $rsp['ok'];
  86 + }
  87 +
  88 + return okay(array('count_imported' => $count));
  89 + }
  90 +
  91 + #################################################################
  92 +
  93 +?>

0 comments on commit d953d4b

Please sign in to comment.
Something went wrong with that request. Please try again.