chiark / gitweb /
Human readable permission data will now be cached in serialized form for faster page...
authorHans-Emil Skogh <hansemil@gmail.com>
Tue, 10 Jan 2012 18:53:23 +0000 (19:53 +0100)
committerHans-Emil Skogh <hansemil@gmail.com>
Tue, 10 Jan 2012 18:53:23 +0000 (19:53 +0100)
wp-fdroid/android-permissions.php
wp-fdroid/wp-fdroid.php

index 17a2b31fcad3148b668303bad5bcd1ba4009a2cb..53e5c0c4e9a98912ce3e22ad4021e555d728bdcf 100644 (file)
@@ -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.\r
 $android_strings_file_path = 'strings.xml';\r
 \r
+$cache_file_path = 'android-permissions.cache';\r
+\r
 // Returns an associative array with android permissions and data about them\r
-function get_android_permissions_array($android_manifest_file_path, $android_strings_file_path) {\r
+function get_android_permissions_array($android_manifest_file_path, $android_strings_file_path, $cache_file_path) {\r
 \r
+       // Check status of cache\r
+       $android_manifest_file_stat = stat($android_manifest_file_path);\r
+       $android_manifest_file_mtime = $android_manifest_file_stat['mtime'];\r
+       $android_strings_file_stat = stat($android_strings_file_path);\r
+       $android_strings_file_mtime = $android_strings_file_stat['mtime'];\r
+       $cache_file_mtime = 0;\r
+       if(file_exists($cache_file_path)) {\r
+               $cache_file_stat = stat($cache_file_path);\r
+               $cache_file_mtime = $cache_file_stat['mtime'];\r
+       }\r
+       \r
+       // If the cache is fresh, use it instead\r
+       if($android_manifest_file_mtime < $cache_file_mtime && $android_strings_file_mtime < $cache_file_mtime ) {\r
+               $cache_file_handle = fopen($cache_file_path, 'r');\r
+               $cache_file_content = fread($cache_file_handle, filesize($cache_file_path));\r
+               fclose($cache_file_handle);\r
+               \r
+               $permissions = unserialize($cache_file_content);\r
+               \r
+               return $permissions;\r
+       }\r
+\r
+       // We are updating the cache, touch the file (note: race condition possible between stating the cache file above and this line...)\r
+       touch($cache_file_path);\r
+       \r
+       // Get permission raw data from XML\r
        $manifestDoc = new DOMDocument;\r
        $manifestDoc->load($android_manifest_file_path);\r
        $manifestXpath = new DOMXPath($manifestDoc);\r
@@ -64,6 +92,11 @@ function get_android_permissions_array($android_manifest_file_path, $android_str
                        $comment = '';\r
                }\r
        }\r
+\r
+       // Update cache with serialized permissions\r
+       $cache_file_handle = fopen($cache_file_path, 'w');\r
+       fwrite($cache_file_handle, serialize($permissions));\r
+       fclose($cache_file_handle);\r
        \r
        return $permissions;\r
 }\r
index 6ac7d306703f0a77fb8df9014ed0826d57fcbc70..34bc730a1f13c830906a711502e1e113030aff05 100644 (file)
@@ -103,7 +103,7 @@ class FDroid
 \r
 \r
     function get_app($query_vars) {\r
-               $permissions_data = get_android_permissions_array($this->site_path.'/repo/AndroidManifest.xml', $this->site_path.'/repo/strings.xml');\r
+               $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');\r
 \r
         $xml = simplexml_load_file($this->site_path.'/repo/index.xml');\r
         foreach($xml->children() as $app) {\r