From 88196e8b9b2c35230bee297e32c82b74c7597b34 Mon Sep 17 00:00:00 2001 From: Hans-Emil Skogh Date: Tue, 10 Jan 2012 19:53:23 +0100 Subject: [PATCH] Human readable permission data will now be cached in serialized form for faster page generation. --- wp-fdroid/android-permissions.php | 35 ++++++++++++++++++++++++++++++- wp-fdroid/wp-fdroid.php | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/wp-fdroid/android-permissions.php b/wp-fdroid/android-permissions.php index 17a2b31f..53e5c0c4 100644 --- a/wp-fdroid/android-permissions.php +++ b/wp-fdroid/android-permissions.php @@ -4,9 +4,37 @@ $android_manifest_file_path = 'AndroidManifest.xml'; // Path to the strings.xml-file from the Android source. Get it from https://raw.github.com/android/platform_frameworks_base/master/core/res/res/values/strings.xml for example. $android_strings_file_path = 'strings.xml'; +$cache_file_path = 'android-permissions.cache'; + // Returns an associative array with android permissions and data about them -function get_android_permissions_array($android_manifest_file_path, $android_strings_file_path) { +function get_android_permissions_array($android_manifest_file_path, $android_strings_file_path, $cache_file_path) { + // Check status of cache + $android_manifest_file_stat = stat($android_manifest_file_path); + $android_manifest_file_mtime = $android_manifest_file_stat['mtime']; + $android_strings_file_stat = stat($android_strings_file_path); + $android_strings_file_mtime = $android_strings_file_stat['mtime']; + $cache_file_mtime = 0; + if(file_exists($cache_file_path)) { + $cache_file_stat = stat($cache_file_path); + $cache_file_mtime = $cache_file_stat['mtime']; + } + + // If the cache is fresh, use it instead + if($android_manifest_file_mtime < $cache_file_mtime && $android_strings_file_mtime < $cache_file_mtime ) { + $cache_file_handle = fopen($cache_file_path, 'r'); + $cache_file_content = fread($cache_file_handle, filesize($cache_file_path)); + fclose($cache_file_handle); + + $permissions = unserialize($cache_file_content); + + return $permissions; + } + + // We are updating the cache, touch the file (note: race condition possible between stating the cache file above and this line...) + touch($cache_file_path); + + // Get permission raw data from XML $manifestDoc = new DOMDocument; $manifestDoc->load($android_manifest_file_path); $manifestXpath = new DOMXPath($manifestDoc); @@ -64,6 +92,11 @@ function get_android_permissions_array($android_manifest_file_path, $android_str $comment = ''; } } + + // Update cache with serialized permissions + $cache_file_handle = fopen($cache_file_path, 'w'); + fwrite($cache_file_handle, serialize($permissions)); + fclose($cache_file_handle); return $permissions; } diff --git a/wp-fdroid/wp-fdroid.php b/wp-fdroid/wp-fdroid.php index 6ac7d306..34bc730a 100644 --- a/wp-fdroid/wp-fdroid.php +++ b/wp-fdroid/wp-fdroid.php @@ -103,7 +103,7 @@ class FDroid function get_app($query_vars) { - $permissions_data = get_android_permissions_array($this->site_path.'/repo/AndroidManifest.xml', $this->site_path.'/repo/strings.xml'); + $permissions_data = get_android_permissions_array($this->site_path.'/repo/AndroidManifest.xml', $this->site_path.'/repo/strings.xml', $this->site_path.'/repo/android-permissions.cache'); $xml = simplexml_load_file($this->site_path.'/repo/index.xml'); foreach($xml->children() as $app) { -- 2.30.2