From 1172f8dcef2c8198ddcdaffcdf65a735811d20a3 Mon Sep 17 00:00:00 2001
From: Adrian Lang <dokuwiki@adrianlang.de>
Date: Sat, 13 Nov 2010 18:20:51 +0100
Subject: [PATCH] Introduce metadata write wrapper p_save_metadata

p_purge_metadata now updates the metadata cache and the INFO array like the other metadata writing functions
---
 inc/parserutils.php | 40 ++++++++++++++++++++++++----------------
 lib/exe/indexer.php |  4 ++--
 2 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/inc/parserutils.php b/inc/parserutils.php
index 847b0382f..b8b063fc3 100644
--- a/inc/parserutils.php
+++ b/inc/parserutils.php
@@ -223,7 +223,7 @@ function p_get_instructions($text){
  * @author Esther Brunner <esther@kaffeehaus.ch>
  */
 function p_get_metadata($id, $key='', $render=false){
-    global $ID, $INFO, $cache_metadata;
+    global $ID;
 
     // cache the current page
     // Benchmarking shows the current page's metadata is generally the only page metadata
@@ -234,11 +234,7 @@ function p_get_metadata($id, $key='', $render=false){
     // metadata has never been rendered before - do it! (but not for non-existent pages)
     if ($render && !isset($meta['current']['description']['abstract']) && page_exists($id)){
         $meta = p_render_metadata($id, $meta);
-        io_saveFile(metaFN($id, '.meta'), serialize($meta));
-
-        // sync cached copies, including $INFO metadata
-        if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
-        if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
+        p_save_metadata($id, $meta);
     }
 
     $val = $meta['current'];
@@ -305,13 +301,7 @@ function p_set_metadata($id, $data, $render=false, $persistent=true){
     // save only if metadata changed
     if ($meta == $orig) return true;
 
-    // sync cached copies, including $INFO metadata
-    global $cache_metadata, $INFO;
-
-    if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
-    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
-
-    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
+    return p_save_metadata($id, $meta);
 }
 
 /**
@@ -321,16 +311,16 @@ function p_set_metadata($id, $data, $render=false, $persistent=true){
  * @author Michael Klier <chi@chimeric.de>
  */
 function p_purge_metadata($id) {
-    $metafn = metaFN('id', '.meta');
-    $meta   = p_read_metadata($id);
+    $meta = p_read_metadata($id);
     foreach($meta['current'] as $key => $value) {
         if(is_array($meta[$key])) {
             $meta['current'][$key] = array();
         } else {
             $meta['current'][$key] = '';
         }
+
     }
-    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
+    return p_save_metadata($id, $meta);
 }
 
 /**
@@ -360,6 +350,24 @@ function p_read_metadata($id,$cache=false) {
     return $meta;
 }
 
+/**
+ * This is the backend function to save a metadata array to a file
+ *
+ * @param    string   $id      absolute wiki page id
+ * @param    array    $meta    metadata
+ *
+ * @return   bool              success / fail
+ */
+function p_save_metadata($id, $meta) {
+    // sync cached copies, including $INFO metadata
+    global $cache_metadata, $INFO;
+
+    if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta;
+    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
+
+    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
+}
+
 /**
  * renders the metadata of a page
  *
diff --git a/lib/exe/indexer.php b/lib/exe/indexer.php
index f35f9ed72..3fa81715b 100644
--- a/lib/exe/indexer.php
+++ b/lib/exe/indexer.php
@@ -190,7 +190,7 @@ function metaUpdate(){
 
     // rendering needed?
     if (@file_exists($file)) return false;
-    if (!@file_exists(wikiFN($ID))) return false;
+    if (!page_exists($ID)) return false;
 
     global $conf;
 
@@ -213,7 +213,7 @@ function metaUpdate(){
     }
 
     $meta = p_render_metadata($ID, $meta);
-    io_saveFile($file, serialize($meta));
+    p_save_metadata($ID, $meta);
 
     echo "metaUpdate(): finished".NL;
     return true;
-- 
GitLab