From 8c1b780ff11e329bfb6f9d1f59f64d9d2c633750 Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Thu, 11 Apr 2013 07:32:32 +1200 Subject: [PATCH 1/6] Remove unecessary debug message on normal behaviour. --- inc/check_UTF8.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/check_UTF8.php b/inc/check_UTF8.php index 80fa01d1..e3c93dbf 100644 --- a/inc/check_UTF8.php +++ b/inc/check_UTF8.php @@ -221,8 +221,8 @@ function check_string($ics){ return false; } } else { - dbg_error_log( "LOG check_string","the string is UTF8 compliant"); +// dbg_error_log( "LOG check_string","the string is UTF8 compliant"); return true; } } -?> \ No newline at end of file +?> From 0453c9a902d80138e38d98746c1dae69a91b488f Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Thu, 11 Apr 2013 07:33:14 +1200 Subject: [PATCH 2/6] A script for archiving non-repeating events into an archive calendar. --- scripts/archive-old-events.php | 170 +++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100755 scripts/archive-old-events.php diff --git a/scripts/archive-old-events.php b/scripts/archive-old-events.php new file mode 100755 index 00000000..d781dc91 --- /dev/null +++ b/scripts/archive-old-events.php @@ -0,0 +1,170 @@ +#!/usr/bin/env php + +* @copyright Morphoss Ltd +* @license http://gnu.org/copyleft/gpl.html GNU GPL v3 or later +*/ +$script_file = __FILE__; + +chdir(str_replace('/scripts/archive-old-events.php','/htdocs',$script_file)); +$_SERVER['SERVER_NAME'] = 'localhost'; + +/** +* Call with something like e.g.: +* +* scripts/archive-old-events.php -a archive -p karora -o P93D +* +*/ + +$args = (object) array(); +$args->debug = false; +$args->principal = false; +$args->collection = false; + +$args->older = 'P190D'; +$args->delete = false; +$args->archive_suffix = 'archive'; +$debugging = null; + + +function parse_arguments() { + global $args; + + $opts = getopt( 'a:c:o:p:s:dh' ); + foreach( $opts AS $k => $v ) { + switch( $k ) { + case 'a': $args->archive_suffix = $v; break; + case 'c': $args->collection = $v; break; + case 'o': $args->older = $v; break; + case 'p': $args->principal = $v; break; + case 's': $_SERVER['SERVER_NAME'] = $v; break; + case 'd': $args->debug = true; $debugging = explode(',',$v); break; + case 'h': usage(); break; + default: $args->{$k} = $v; + } + } + $bad = false; + if ( $args->principal === false ) { + echo "You must supply a principal.\n"; + $bad = true; + } + if ( $args->collection === false ) { + echo "You must supply a collection.\n"; + $bad = true; + } + if ( $bad ) { + usage(); + } +} + +function usage() { + + echo << Appendeded (after a '-') to the name of the original calendar to give + the archive calendar name. Default 'archive'. + -o Archive events completed this much prior to the current + date. Default 'P-190D' + -p The name of the principal to do the archiving for (required). + -c The name of the collection to do the archiving for (required). + -s The servername to be used to identify the DAViCal configuration file. + + -d xxx Enable debugging where 'xxx' is a comma-separated list of debug subsystems + +USAGE; + exit(0); +} + +parse_arguments(); + +if ( $args->debug && is_array($debugging )) { + foreach( $debugging AS $v ) { + $c->dbg[$v] = 1; + } +} + +require_once("./always.php"); +require_once('AwlQuery.php'); +require_once('RRule-v2.php'); +require_once('vCalendar.php'); + + +/** +* Essentially what we are doing is: +* +DELETE FROM caldav_data JOIN calendar_item USING(dav_id) + WHERE dav_name LIKE '/someprincipal/%' + AND ( + (rrule IS NULL AND dtend < archive_before_date) + OR (last_instance_end < archive_before_date) + ) + +*/ +$recent = new RepeatRuleDateTime(gmdate('Ymd\THis\Z')); +$recent->modify('P-6D'); +$archive_before_date = new RepeatRuleDateTime(gmdate('Ymd\THis\Z')); +$archive_before_date->modify( $args->older ); +if ( $archive_before_date > $recent ) { + echo "Cowardly refusing to archive events before "+$archive_before_date->format('Y-m-d H:i:s') + "\n"; +} + + +if ( $args->debug ) printf( "Archiving event instances finished before '%s'\n", $archive_before_date->UTC() ); + +// SQL to create an archive collection but only if it doesn't exist already. +$archive_collection_sql = <<principal, $args->collection ); +$collection_archive = sprintf( '/%s/%s-%s/', $args->principal, $args->collection, $args->archive_suffix ); + +$sqlargs = array( + ':collection_dav_name' => $collection_dav_name, + ':archive_dav_name' => $collection_archive + ); +$qry = new AwlQuery($archive_collection_sql, $sqlargs); +if ( $qry->Exec(__CLASS__, __LINE__, __FILE__) ) { + $qry->QDo('SELECT collection_id FROM collection WHERE dav_name = ?', $collection_dav_name ); + $row = $qry->Fetch(); + $source_collection_id = $row->collection_id; + + $qry->QDo('SELECT collection_id FROM collection WHERE dav_name = ?', $collection_archive ); + $row = $qry->Fetch(); + $archive_collection_id = $row->collection_id; + + $archive_sql = <<debug ) printf( "%s\n", $archive_sql ); + + $sqlargs[':archive_before_date'] = $archive_before_date->FloatOrUTC(); + $sqlargs[':source_collection_id'] = $source_collection_id; + $sqlargs[':archive_collection_id'] = $archive_collection_id; + $qry->QDo($archive_sql, $sqlargs); +} \ No newline at end of file From 8a5d6412978fc9320d83638c6846ed0395bb685c Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Mon, 15 Apr 2013 14:49:33 +1200 Subject: [PATCH 3/6] Once we do archive the events we have to update various things so they realise it too. --- scripts/archive-old-events.php | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/scripts/archive-old-events.php b/scripts/archive-old-events.php index d781dc91..662b1931 100755 --- a/scripts/archive-old-events.php +++ b/scripts/archive-old-events.php @@ -92,6 +92,7 @@ if ( $args->debug && is_array($debugging )) { require_once("./always.php"); require_once('AwlQuery.php'); +require_once('AwlCache.php'); require_once('RRule-v2.php'); require_once('vCalendar.php'); @@ -142,10 +143,18 @@ $sqlargs = array( $qry = new AwlQuery($archive_collection_sql, $sqlargs); if ( $qry->Exec(__CLASS__, __LINE__, __FILE__) ) { $qry->QDo('SELECT collection_id FROM collection WHERE dav_name = ?', $collection_dav_name ); + if ( $qry->rows() != 1 ) { + printf( "Could not find source collection '%s'\n", $collection_dav_name); + exit(1); + } $row = $qry->Fetch(); $source_collection_id = $row->collection_id; $qry->QDo('SELECT collection_id FROM collection WHERE dav_name = ?', $collection_archive ); + if ( $qry->rows() != 1 ) { + printf( "Could not create archive collection '%s'!\n", $collection_archive); + exit(2); + } $row = $qry->Fetch(); $archive_collection_id = $row->collection_id; @@ -167,4 +176,32 @@ EOSQL; $sqlargs[':source_collection_id'] = $source_collection_id; $sqlargs[':archive_collection_id'] = $archive_collection_id; $qry->QDo($archive_sql, $sqlargs); + + /** + * At this point we've done all the work, we just have to inform the rest of the world that + * everything has changed underneath it. + */ + + // Now ensure the collection tag changes... + $sql = 'UPDATE collection SET dav_etag = random(), modified = current_timestamp WHERE collection_id IN (:source_id, :archive_id)'; + $sqlargs = array( + ':source_id' => $source_collection_id, + ':archive_id' => $archive_collection_id + ); + $qry->QDo($sql, $sqlargs); + + // Delete the sync tokens... + $sql = 'DELETE FROM sync_token WHERE collection_id IN (:source_id, :archive_id)'; + $qry->QDo($sql, $sqlargs); + + // Delete the sync_changes... + $sql = 'DELETE FROM sync_changes WHERE collection_id IN (:source_id, :archive_id)'; + $qry->QDo($sql, $sqlargs); + + // Uncache anything to do with the collection, or the archive + $cache = getCacheInstance(); + $cache_ns = 'collection-'.preg_replace( '{/[^/]*$}', '/', $collection_dav_name); + $cache->delete( $cache_ns, null ); + $cache_ns = 'collection-'.preg_replace( '{/[^/]*$}', '/', $collection_archive); + $cache->delete( $cache_ns, null ); } \ No newline at end of file From f67780c461f4260db898151cb3bf7dac1e6da527 Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Tue, 16 Apr 2013 09:57:38 +1200 Subject: [PATCH 4/6] Fix typo. --- scripts/archive-old-events.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/archive-old-events.php b/scripts/archive-old-events.php index 662b1931..b51b7f8b 100755 --- a/scripts/archive-old-events.php +++ b/scripts/archive-old-events.php @@ -191,7 +191,7 @@ EOSQL; $qry->QDo($sql, $sqlargs); // Delete the sync tokens... - $sql = 'DELETE FROM sync_token WHERE collection_id IN (:source_id, :archive_id)'; + $sql = 'DELETE FROM sync_tokens WHERE collection_id IN (:source_id, :archive_id)'; $qry->QDo($sql, $sqlargs); // Delete the sync_changes... @@ -204,4 +204,4 @@ EOSQL; $cache->delete( $cache_ns, null ); $cache_ns = 'collection-'.preg_replace( '{/[^/]*$}', '/', $collection_archive); $cache->delete( $cache_ns, null ); -} \ No newline at end of file +} From 5065e595965b2d5b26f3856b1cb5b2a328e86feb Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Tue, 16 Apr 2013 10:20:03 +1200 Subject: [PATCH 5/6] Also need to change collection_id... --- scripts/archive-old-events.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/archive-old-events.php b/scripts/archive-old-events.php index b51b7f8b..ab19c413 100755 --- a/scripts/archive-old-events.php +++ b/scripts/archive-old-events.php @@ -159,7 +159,9 @@ if ( $qry->Exec(__CLASS__, __LINE__, __FILE__) ) { $archive_collection_id = $row->collection_id; $archive_sql = << Date: Wed, 17 Apr 2013 12:25:16 +1200 Subject: [PATCH 6/6] Don't warn on slow queries since this is a batch process. --- scripts/archive-old-events.php | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/archive-old-events.php b/scripts/archive-old-events.php index ab19c413..ba542278 100755 --- a/scripts/archive-old-events.php +++ b/scripts/archive-old-events.php @@ -141,6 +141,7 @@ $sqlargs = array( ':archive_dav_name' => $collection_archive ); $qry = new AwlQuery($archive_collection_sql, $sqlargs); +$qry->query_time_warning = 5; // Don't warn on queries unless they take more than 5 seconds. if ( $qry->Exec(__CLASS__, __LINE__, __FILE__) ) { $qry->QDo('SELECT collection_id FROM collection WHERE dav_name = ?', $collection_dav_name ); if ( $qry->rows() != 1 ) {