<commit_before><?php

	loadlib("flickr_users");
	loadlib("flickr_contacts_import");
	loadlib("flickr_geobookmarks_import");
	loadlib("flickr_photos_import");
	loadlib("flickr_faves_import");
	loadlib("flickr_push");
	loadlib("flickr_push_subscriptions");

	# TO DO: add an optional flag that lets you offset the last mindate
	# by (n) seconds in case you need to backfill but not all the way back
	# to the start of time (20120105/straup)

	#################################################################

	function flickr_backups_type_map($string_keys=0){

		$map = array(
			0 => 'photos',
			1 => 'faves',
			2 => 'contacts',
			3 => 'geobookmarks'
		);

		if ($string_keys){
			$map = array_flip($map);
		}

		return $map;
	}

	#################################################################

	function flickr_backups_create(&$user, $type_id){

		$now = time();

		$backup = array(
			'user_id' => $user['id'],
			'type_id' => $type_id,
			'date_created' => $now,
		);

		$hash = array();

		foreach ($backup as $k => $v){
			$hash[$k] = AddSlashes($v);
		}

		$rsp = db_insert('FlickrBackups', $hash);

		if ($rsp['ok']){
			$rsp['backup'] = $backup;
		}

		else if ($rsp['error_code'] == 1062){

			$map = flickr_backups_type_map();
			$backups = flickr_backups_for_user($user, $type_id);
				
			$rsp = okay(array(
				'backup' => $backups[$map[$type_id]],
			));	
		}

		else {}

		if ($rsp['ok']){
			$push_rsp = flickr_backups_manage_push_subscription($rsp['backup']);
		}

		return $rsp;
	}

	#################################################################

	function flickr_backups_update(&$backup, $update){

		$hash = array();

		foreach ($update as $k => $v){
			$hash[$k] = AddSlashes($v);
		}

		$enc_user = AddSlashes($backup['user_id']);
		$enc_type = AddSlashes($backup['type_id']);

		$where = "user_id='{$enc_user}' AND type_id='{$enc_type}'";

		$rsp = db_update('FlickrBackups', $hash, $where);

		if ($rsp['ok']){

			$backup = array_merge($backup, $update);
			$push_rsp = flickr_backups_manage_push_subscriptions($backup);
		}

		return $rsp;
	}

	#################################################################

	function flickr_backups_manage_push_subscription(&$backup){

		$push_features = array("flickr_push", "flickr_push_backups");

		if (! features_is_enabled($push_features)){
			return okay();
		}

		$sub = array(
			'user_id' => $backup['user_id'],
			'topic_id' => $backup['type_id'],
		);

		if (! flickr_backups_is_registered_push_subscription($sub)){
			return okay();
		}

		if ($backup['disabled']){
			# $push_rsp = flickr_push_subscriptions_remove_subscription($sub);
		}

		else {
			# $push_rsp = flickr_push_subscriptions_register_subscription($sub);
		}

		return $push_rsp;
	}

	#################################################################

	function flickr_backups_users($ensure_enabled=1){

		$sql = "SELECT DISTINCT(user_id) FROM FlickrBackups";
		$rsp = db_fetch($sql);

		$users = array();

		foreach ($rsp['rows'] as $row){

			if (($ensure_enabled) && ($row['disabled'])){
				continue;
			}

			$users[] = users_get_by_id($row['user_id']);
		}

		return $users;
	}

	#################################################################

	function flickr_backups_is_registered_user(&$user, $ensure_enabled=0){

		$enc_user = AddSlashes($user['id']);
		$sql = "SELECT * FROM FlickrBackups WHERE user_id='{$enc_user}'";

		$rsp = db_fetch($sql);
		$row = db_single($rsp);

		if (! $row){
			return 0;
		}

		if (($ensure_enabled) && ($row['disabled'])){
			return 0;
		}

		return 1;
	}

	#################################################################

	function flickr_backups_is_registered_subscription(&$sub){
		return flickr_backups_is_registered_push_subscription($sub);
	}

	function flickr_backups_is_registered_push_subscription(&$sub){

		loadlib("flickr_push");
		$map = flickr_push_topic_map("string keys");

		# for now, anyway...

		$valid = array(
			$map['my_photos'],
			$map['my_faves'],
			$map['commons'],
		);

		return (in_array($sub['topic_id'], $valid)) ? 1 : 0;
	}

	#################################################################

	function flickr_backups_for_user(&$user, $type_id=null){

		$enc_user = AddSlashes($user['id']);
		$sql = "SELECT * FROM FlickrBackups WHERE user_id='{$enc_user}'";

		if (isset($type_id)){

			$enc_type = AddSlashes($type_id);
			$sql .= " AND type_id='{$enc_type}'";
		}

		$rsp = db_fetch($sql);
		$backups = array();

		$map = flickr_backups_type_map();

		foreach($rsp['rows'] as $row){
			$type = $map[$row['type_id']];
			$backups[$type] = $row;
		}

		return $backups;
	}

	#################################################################

	function flickr_backups_get_photos(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['photos'])){

			return not_okay("backups not registered");
		}

		#

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$backup = $backups['photos'];
		$update = array();

		$start_time = time();

		if (! $backup['date_firstupdate']){

			$rsp = flickr_photos_import_for_nsid($flickr_user['nsid']);
		}

		else {

			$more = array(
				'min_date' => $backup['date_lastupdate'],
			);

			$rsp = flickr_photos_import_get_recent($flickr_user['nsid'], $more);
		}

		#

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}

		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

	function flickr_backups_get_faves(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['faves'])){

			return not_okay("backups for faves not registered");
		}

		#

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$backup = $backups['faves'];
		$update = array();

		$start_time = time();
		$more = array();

		if ($backup['date_firstupdate']){
			$more['min_fave_date'] = $backup['date_lastupdate'];
		}

		$rsp = flickr_faves_import_for_nsid($flickr_user['nsid'], $more);

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}
		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

	function flickr_backups_get_contacts(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['contacts'])){

			return not_okay("backups not registered");
		}

		$backup = $backups['contacts'];
		$update = array();

		$start_time = time();

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$more = array(
			'purge_existing_contacts' => 1
		);

		$rsp = flickr_contacts_import_for_nsid($flickr_user['nsid'], $more);

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}
		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

	function flickr_backups_get_geobookmarks(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['geobookmarks'])){

			# return not_okay("backups not registered");
		}

		$backup = $backups['geobookmarks'];
		$update = array();

		$start_time = time();

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$rsp = flickr_geobookmarks_import_for_nsid($flickr_user['nsid'], $more);

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}
		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

?>
<commit_msg>fix function; skip doing push stuff in lib_flickr_backups for right now<commit_after><?php

	loadlib("flickr_users");
	loadlib("flickr_contacts_import");
	loadlib("flickr_geobookmarks_import");
	loadlib("flickr_photos_import");
	loadlib("flickr_faves_import");
	loadlib("flickr_push");
	loadlib("flickr_push_subscriptions");

	# TO DO: add an optional flag that lets you offset the last mindate
	# by (n) seconds in case you need to backfill but not all the way back
	# to the start of time (20120105/straup)

	#################################################################

	function flickr_backups_type_map($string_keys=0){

		$map = array(
			0 => 'photos',
			1 => 'faves',
			2 => 'contacts',
			3 => 'geobookmarks'
		);

		if ($string_keys){
			$map = array_flip($map);
		}

		return $map;
	}

	#################################################################

	function flickr_backups_create(&$user, $type_id){

		$now = time();

		$backup = array(
			'user_id' => $user['id'],
			'type_id' => $type_id,
			'date_created' => $now,
		);

		$hash = array();

		foreach ($backup as $k => $v){
			$hash[$k] = AddSlashes($v);
		}

		$rsp = db_insert('FlickrBackups', $hash);

		if ($rsp['ok']){
			$rsp['backup'] = $backup;
		}

		else if ($rsp['error_code'] == 1062){

			$map = flickr_backups_type_map();
			$backups = flickr_backups_for_user($user, $type_id);
				
			$rsp = okay(array(
				'backup' => $backups[$map[$type_id]],
			));	
		}

		else {}

		if ($rsp['ok']){
			$push_rsp = flickr_backups_manage_push_subscription($rsp['backup']);
		}

		return $rsp;
	}

	#################################################################

	function flickr_backups_update(&$backup, $update){

		$hash = array();

		foreach ($update as $k => $v){
			$hash[$k] = AddSlashes($v);
		}

		$enc_user = AddSlashes($backup['user_id']);
		$enc_type = AddSlashes($backup['type_id']);

		$where = "user_id='{$enc_user}' AND type_id='{$enc_type}'";

		$rsp = db_update('FlickrBackups', $hash, $where);

		if (($rsp['ok']) && (isset($update['disabled']))){

			$backup = array_merge($backup, $update);
			$push_rsp = flickr_backups_manage_push_subscription($backup);
		}

		return $rsp;
	}

	#################################################################

	function flickr_backups_manage_push_subscription(&$backup){

		log_info("skipping push stuff for now...");
		return;

		$push_features = array("flickr_push", "flickr_push_backups");

		if (! features_is_enabled($push_features)){
			return okay();
		}

		$sub = array(
			'user_id' => $backup['user_id'],
			'topic_id' => $backup['type_id'],
		);

		if (! flickr_backups_is_registered_push_subscription($sub)){
			return okay();
		}

		if ($backup['disabled']){
			# $push_rsp = flickr_push_subscriptions_remove_subscription($sub);
		}

		else {
			# $push_rsp = flickr_push_subscriptions_register_subscription($sub);
		}

		return $push_rsp;
	}

	#################################################################

	function flickr_backups_users($ensure_enabled=1){

		$sql = "SELECT DISTINCT(user_id) FROM FlickrBackups";
		$rsp = db_fetch($sql);

		$users = array();

		foreach ($rsp['rows'] as $row){

			if (($ensure_enabled) && ($row['disabled'])){
				continue;
			}

			$users[] = users_get_by_id($row['user_id']);
		}

		return $users;
	}

	#################################################################

	function flickr_backups_is_registered_user(&$user, $ensure_enabled=0){

		$enc_user = AddSlashes($user['id']);
		$sql = "SELECT * FROM FlickrBackups WHERE user_id='{$enc_user}'";

		$rsp = db_fetch($sql);
		$row = db_single($rsp);

		if (! $row){
			return 0;
		}

		if (($ensure_enabled) && ($row['disabled'])){
			return 0;
		}

		return 1;
	}

	#################################################################

	function flickr_backups_is_registered_subscription(&$sub){
		return flickr_backups_is_registered_push_subscription($sub);
	}

	function flickr_backups_is_registered_push_subscription(&$sub){

		loadlib("flickr_push");
		$map = flickr_push_topic_map("string keys");

		# for now, anyway...

		$valid = array(
			$map['my_photos'],
			$map['my_faves'],
			$map['commons'],
		);

		return (in_array($sub['topic_id'], $valid)) ? 1 : 0;
	}

	#################################################################

	function flickr_backups_for_user(&$user, $type_id=null){

		$enc_user = AddSlashes($user['id']);
		$sql = "SELECT * FROM FlickrBackups WHERE user_id='{$enc_user}'";

		if (isset($type_id)){

			$enc_type = AddSlashes($type_id);
			$sql .= " AND type_id='{$enc_type}'";
		}

		$rsp = db_fetch($sql);
		$backups = array();

		$map = flickr_backups_type_map();

		foreach($rsp['rows'] as $row){
			$type = $map[$row['type_id']];
			$backups[$type] = $row;
		}

		return $backups;
	}

	#################################################################

	function flickr_backups_get_photos(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['photos'])){

			return not_okay("backups not registered");
		}

		#

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$backup = $backups['photos'];
		$update = array();

		$start_time = time();

		if (! $backup['date_firstupdate']){

			$rsp = flickr_photos_import_for_nsid($flickr_user['nsid']);
		}

		else {

			$more = array(
				'min_date' => $backup['date_lastupdate'],
			);

			$rsp = flickr_photos_import_get_recent($flickr_user['nsid'], $more);
		}

		#

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}

		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

	function flickr_backups_get_faves(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['faves'])){

			return not_okay("backups for faves not registered");
		}

		#

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$backup = $backups['faves'];
		$update = array();

		$start_time = time();
		$more = array();

		if ($backup['date_firstupdate']){
			$more['min_fave_date'] = $backup['date_lastupdate'];
		}

		$rsp = flickr_faves_import_for_nsid($flickr_user['nsid'], $more);

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}
		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

	function flickr_backups_get_contacts(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['contacts'])){

			return not_okay("backups not registered");
		}

		$backup = $backups['contacts'];
		$update = array();

		$start_time = time();

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$more = array(
			'purge_existing_contacts' => 1
		);

		$rsp = flickr_contacts_import_for_nsid($flickr_user['nsid'], $more);

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}
		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

	function flickr_backups_get_geobookmarks(&$user){

		$backups = flickr_backups_for_user($user);

		if (! isset($backups['geobookmarks'])){

			# return not_okay("backups not registered");
		}

		$backup = $backups['geobookmarks'];
		$update = array();

		$start_time = time();

		$flickr_user = flickr_users_get_by_user_id($user['id']);

		$rsp = flickr_geobookmarks_import_for_nsid($flickr_user['nsid'], $more);

		if ($rsp['ok']){
			$update['date_lastupdate'] = $start_time;
			$update['details'] = "count: {$rsp['count_imported']}";

			if (! $backup['date_firstupdate']){
				$update['date_firstupdate'] = $update['date_lastupdate'];
			}
		}

		else {
			$update['details'] = "update failed ($start_time) : {$rsp['error']}";
		}

		flickr_backups_update($backup, $update);

		return $rsp;
	}

	#################################################################

?>

{'fraction_non_alphanumeric': 0.2687813996962262, 'fraction_numerical': 0.0029209019745297347, 'mean_word_length': 5.129251700680272, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2213258', 'n_tokens_mistral': 6874, 'n_tokens_neox': 5952, 'n_words': 945}