From 95dbfe5721704a800c0f108c048ab3cb5e13de3b Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sun, 30 Apr 2006 20:17:40 +0200
Subject: [PATCH] metadata enhancements

This adds meta data rendering to the indexer process to build missing meta data
in the background.

p_get_first_heading was adjusted to make use of the new meta data mechanisms

A problem with unitialized arrays in p_set_metadata and PHP5 was fixed (I think)

darcs-hash:20060430181740-7ad00-8952fa6beb4fadf6b4321627998442d34febfc8d.gz
---
 inc/parser/metadata.php | 46 ++++++++++++++++++++---------------------
 inc/parserutils.php     | 38 ++++++++++++++++------------------
 lib/exe/indexer.php     | 28 ++++++++++++++++++++++++-
 3 files changed, 68 insertions(+), 44 deletions(-)

diff --git a/inc/parser/metadata.php b/inc/parser/metadata.php
index 0ba59c65e..e7173dda6 100644
--- a/inc/parser/metadata.php
+++ b/inc/parser/metadata.php
@@ -26,7 +26,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
 
   var $doc  = '';
   var $meta = array();
-  
+
   var $headers = array();
   var $capture = true;
   var $store   = '';
@@ -73,7 +73,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
         'level' => $level-$conf['toptoclevel']+1
       );
     }
-    
+
     // add to summary
     if ($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF;
   }
@@ -88,7 +88,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
   function p_open(){
     if ($this->capture) $this->doc .= DOKU_LF;
   }
-  
+
   function p_close(){
     if ($this->capture){
       if (strlen($this->doc) > 250) $this->capture = false;
@@ -112,7 +112,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
 
   function emphasis_open(){}
   function emphasis_close(){}
-  
+
   function underline_open(){}
   function underline_close(){}
 
@@ -144,7 +144,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
       $this->doc   = '';
     }
   }
-  
+
   /**
    * Callback for footnote end syntax
    *
@@ -160,11 +160,11 @@ class Doku_Renderer_metadata extends Doku_Renderer {
       $this->store = '';
     }
   }
-    
+
   function listu_open(){
     if ($this->capture) $this->doc .= DOKU_LF;
   }
-  
+
   function listu_close(){
     if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
   }
@@ -172,7 +172,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
   function listo_open(){
     if ($this->capture) $this->doc .= DOKU_LF;
   }
-  
+
   function listo_close(){
     if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
   }
@@ -180,7 +180,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
   function listitem_open($level){
     if ($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* ';
   }
-  
+
   function listitem_close(){
     if ($this->capture) $this->doc .= DOKU_LF;
   }
@@ -211,7 +211,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
   function quote_open(){
     if ($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'“';
   }
-  
+
   function quote_close(){
     if ($this->capture){
       $this->doc .= '”';
@@ -247,7 +247,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
   function singlequoteopening(){
     if ($this->capture) $this->doc .= '‘';
   }
-  
+
   function singlequoteclosing(){
     if ($this->capture) $this->doc .= '’';
   }
@@ -255,7 +255,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
   function doublequoteopening(){
     if ($this->capture) $this->doc .= '“';
   }
-  
+
   function doublequoteclosing(){
     if ($this->capture) $this->doc .= '”';
   }
@@ -271,30 +271,30 @@ class Doku_Renderer_metadata extends Doku_Renderer {
    */
   function internallink($id, $name = NULL){
     global $ID;
-    
+
     $default = $this->_simpleTitle($id);
-    
+
     // first resolve and clean up the $id
     resolve_pageid(getNS($ID), $id, $exists);
     list($id, $hash) = split('#', $id, 2);
-    
+
     // set metadata
     $this->meta['relation']['references'][$id] = $exists;
     // $data = array('relation' => array('isreferencedby' => array($ID => true)));
     // p_set_metadata($id, $data);
-    
+
     // add link title to summary
     if ($this->capture){
       $name = $this->_getLinkTitle($name, $default, $id);
       $this->doc .= $name;
-    } 
+    }
   }
 
   function externallink($url, $name = NULL){
     if ($this->capture){
       if ($name) $this->doc .= $name;
       else $this->doc .= '<'.$url.'>';
-    } 
+    }
   }
 
   function interwikilink($match, $name = NULL, $wikiName, $wikiUri){
@@ -302,21 +302,21 @@ class Doku_Renderer_metadata extends Doku_Renderer {
       list($wikiUri, $hash) = explode('#', $wikiUri, 2);
       $name = $this->_getLinkTitle($name, $wikiName.'>'.$wikiUri);
       $this->doc .= $name;
-    } 
+    }
   }
 
   function windowssharelink($url, $name = NULL){
     if ($this->capture){
       if ($name) $this->doc .= $name;
       else $this->doc .= '<'.$url.'>';
-    } 
+    }
   }
 
   function emaillink($address, $name = NULL){
     if ($this->capture){
       if ($name) $this->doc .= $name;
       else $this->doc .= '<'.$address.'>';
-    } 
+    }
   }
 
   function internalmedia($src, $title=NULL, $align=NULL, $width=NULL,
@@ -412,7 +412,7 @@ class Doku_Renderer_metadata extends Doku_Renderer {
       return '['.$title.']';
     }
   }
-    
+
 }
 
-//Setup VIM: ex: et ts=4 enc=utf-8 :
\ No newline at end of file
+//Setup VIM: ex: et ts=4 enc=utf-8 :
diff --git a/inc/parserutils.php b/inc/parserutils.php
index 981f33cc1..93c2b3604 100644
--- a/inc/parserutils.php
+++ b/inc/parserutils.php
@@ -239,23 +239,23 @@ function p_get_instructions($text){
  */
 function p_get_metadata($id, $key=false, $render=false){
   $file = metaFN($id, '.meta');
-  
+
   if (@file_exists($file)) $meta = unserialize(io_readFile($file, false));
   else $meta = array();
-  
+
   // metadata has never been rendered before - do it!
   if ($render && !$meta['description']['abstract']){
     $meta = p_render_metadata($id, $meta);
     io_saveFile($file, serialize($meta));
   }
-  
+
   // filter by $key
   if ($key){
     list($key, $subkey) = explode(' ', $key, 2);
     if (trim($subkey)) return $meta[$key][$subkey];
     else return $meta[$key];
   }
-  
+
   return $meta;
 }
 
@@ -266,35 +266,37 @@ function p_get_metadata($id, $key=false, $render=false){
  */
 function p_set_metadata($id, $data, $render=false){
   if (!is_array($data)) return false;
-  
+
   $orig = p_get_metadata($id);
-  
+
   // render metadata first?
   if ($render) $meta = p_render_metadata($id, $orig);
   else $meta = $orig;
-    
+
   // now add the passed metadata
   $protected = array('description', 'date', 'contributor');
   foreach ($data as $key => $value){
-    
+
     // be careful with sub-arrays of $meta['relation']
     if ($key == 'relation'){
       foreach ($value as $subkey => $subvalue){
         $meta[$key][$subkey] = array_merge($meta[$key][$subkey], $subvalue);
       }
-      
+
     // be careful with some senisitive arrays of $meta
     } elseif (in_array($key, $protected)){
       if (is_array($value)){
+        #FIXME not sure if this is the intended thing:
+        if(!is_array($meta[$key])) $meta[$key] = array($meta[$key]);
         $meta[$key] = array_merge($meta[$key], $value);
       }
-    
+
     // no special treatment for the rest
     } else {
       $meta[$key] = $value;
     }
   }
-  
+
   // save only if metadata changed
   if ($meta == $orig) return true;
   return io_saveFile(metaFN($id, '.meta'), serialize($meta));
@@ -314,13 +316,13 @@ function p_render_metadata($id, $orig){
   // set up the renderer
   $renderer = & new Doku_Renderer_metadata();
   $renderer->meta = $orig;
-  
+
   // loop through the instructions
   foreach ($instructions as $instruction){
     // execute the callback against the renderer
     call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]);
   }
-  
+
   return $renderer->meta;
 }
 
@@ -476,15 +478,11 @@ function p_render($mode,$instructions,& $info){
 /**
  * Gets the first heading from a file
  *
- * @author Jan Decaluwe <jan@jandecaluwe.com>
+ * @author Andreas Gohr <andi@splitbrain.org>
  */
 function p_get_first_heading($id){
-  $file = wikiFN($id);
-  if (@file_exists($file)) {
-    $instructions = p_cached_instructions($file,true);
-		$meta = $instructions[0][1];
-		return isset($meta[0]['first_heading']) ? trim($meta[0]['first_heading']) : NULL;
-  }
+  $meta = p_get_metadata($id);
+  if($meta['title']) return $meta['title'];
   return NULL;
 }
 
diff --git a/lib/exe/indexer.php b/lib/exe/indexer.php
index 28eaa8284..d37fb9c9a 100644
--- a/lib/exe/indexer.php
+++ b/lib/exe/indexer.php
@@ -21,7 +21,7 @@ sendGIF();
 if(!$_REQUEST['debug']) ob_start();
 
 // run one of the jobs
-runIndexer() or runSitemapper();
+runIndexer() or metaUpdate() or runSitemapper();
 
 if(!$_REQUEST['debug']) ob_end_clean();
 exit;
@@ -74,6 +74,32 @@ function runIndexer(){
     return true;
 }
 
+/**
+ * Will render the metadata for the page if not exists yet
+ *
+ * This makes sure pages which are created from outside DokuWiki will
+ * gain their data when viewed for the first time.
+ */
+function metaUpdate(){
+    print "metaUpdate(): started".NL;
+
+    $ID = cleanID($_REQUEST['id']);
+    if(!$ID) return false;
+    $file = metaFN($ID, '.meta');
+
+    // rendering needed?
+    if (@file_exists($file)) return false;
+
+    require_once(DOKU_INC.'inc/parserutils.php');
+
+    $meta = array();
+    $meta = p_render_metadata($ID, $meta);
+    io_saveFile($file, serialize($meta));
+
+    print "metaUpdate(): finished".NL;
+    return true;
+}
+
 /**
  * Builds a Google Sitemap of all public pages known to the indexer
  *
-- 
GitLab