Commits

Michael Haschke committed c5b6309

[fix] this patch should fix the post-output-process/caching/content-negotiation problem
[mod] change loading order of plugins, debuglog plugin comes first now
[mod] add some more debug log messages

  • Participants
  • Parent commits 951e0aa

Comments (0)

Files changed (2)

foafpressapp/core/Foafpress.php

 
     protected function init()
     {
+        $this->addLogMessage('Init Foafpress plugin');
+        
         $this->LoadConfiguration();
 
         // add foafpress templates to template configuration
 
     protected function LoadConfiguration()
     {
+        $this->addLogMessage('Load Foafpress configuration');
+
         // check user configuration of namespaces
         if (!isset($this->config['ns']))
         {
     // Foafpress event handlers for SPCMS
     protected function SubscribeEventHandlers()
     {
-        
+        $this->addLogMessage('Subscribe Foafpress event handlers');
+
+        $this->pm->subscribe('sandbox_parse_failed', $this, 'FindResource');
         if (isset($this->config['enableOutputPostprocessing']) && $this->config['enableOutputPostprocessing'] === true)
         {
             $this->pm->subscribe('sandbox_parse_start', $this, 'CheckCache');
         }
-        $this->pm->subscribe('sandbox_parse_failed', $this, 'FindResource');
         $this->pm->subscribe('sandbox_parse_end', $this, 'LoadResourceFromFile');
 
         return;
 
     protected function LoadLibrariesAndIncludes()
     {
+        $this->addLogMessage('Load Arc2, RDFTO, Foafpress includes/adapters');
+
         // load ARC2
         $this->pm->need('./arc2/ARC2');
 
     // event listener for "sandbox_parse_start"
     public function CheckCache($filename)
     {
+        $this->addLogMessage('Check cache for '.$filename);
+
         // get preferenced laguage stack from LanguageChecker plugin
         if ($this->pm->isActive('LanguageChecker'))
         {
         if ($validCachedOutput = $this->cache->getVar($filename.serialize($this->languageStackPreferences)))
         {
             $cachedOutput = $validCachedOutput;
+            $this->addLogMessage('Found valid cache.');
             $this->pm->subscribe('sandbox_flush_start', $this, 'PreventDoubleOutput'); // only to be safe not to echo two times "the same"
         }
         elseif (defined('IS_PRODUCTION_INSTANCE') && IS_PRODUCTION_INSTANCE === true)
         {
             $cachedOutput = $this->cache->getVar($filename.serialize($this->languageStackPreferences), null, -1);
+            if ($cachedOutput)
+            {
+                $this->addLogMessage('Use invalid cache.');
+            }
         }
 
         if ($cachedOutput)
             ob_end_clean();
 
             //do post output processing here
+            $this->addLogMessage('Start post output processing.');
         }
         
         return;
     
     public function FindResource($file)
     {
+        $this->addLogMessage('Try to find something for '.$file);
+
+        $extensions = $this->config['types'];
+
+        $file = $this->FindFileByDirectoryIndexCheck($file);
+        
+        $this->URI_Request = 'http://'.$_SERVER['SERVER_NAME'].str_replace(BASEDIR, BASEURL, $file);
+        $this->addLogMessage('Update var URI_Request='.$this->URI_Request);
+
+        $file = $this->CheckForApplicationTypeRequestByFileExtension($file);
+        $this->addLogMessage('Found request for: '.($this->extensiontype?$this->extensiontype:'unresolved!'));
+
+        $this->RedirectToUrlByContentNegotiation();
+
+        if ($file_rdf = $this->GetAppropriateRdfFile($file))
+        {
+                $this->addLogMessage('Found appropriate RDF file: '.$file_rdf);
+                $this->set_URI_Document($file_rdf);
+                $this->sandbox->parse($file_rdf);
+                // LoadResourceFromFile method will be triggered automatically by event dispatcher
+                return;
+        }
+
+        // TODO: send 404 if no file/resource is available
+        die($file.' is not found!');
+        
+        return;
+    }
+
+    public function FindFileByDirectoryIndexCheck($file)
+    {
         $extensions = $this->config['types'];
 
         if (is_dir($file) && isset($this->config['DirectoryIndex']) && $this->config['DirectoryIndex'])
                 }
             }
         }
-        
-        $this->URI_Request = 'http://'.$_SERVER['SERVER_NAME'].str_replace(BASEDIR, BASEURL, $file);
-        
-        // check for request type by file extension
-        
-        //*
+
+        return $file;
+
+    }
+
+    public function CheckForApplicationTypeRequestByFileExtension($file)
+    {
+        $this->addLogMessage('Check for requested application type by file extension.');
+
+        $extensions = $this->config['types'];
+
         foreach ($extensions as $type=>$ext)
         {
             if (substr($file, -1*strlen($ext)) == $ext)
                 break;
             }
         }
-        //*/
-        
-        //die($this->URI_Request);
-        
-        // check for existing files
-        
+
+        return $file;
+    }
+
+    public function GetAppropriateRdfFile($file)
+    {
+        $this->addLogMessage('Check for an existing RDF file.');
+
+        $extensions = $this->config['types'];
+
         foreach ($extensions as $ext)
         {
             if (is_readable($file.$ext))
             {
-                $this->sandbox->parse($file.$ext);
-                // LoadResourceFromFile method will be triggered automatically by event dispatcher
-                
-                return;
+                return $file.$ext;
             }
         }
-    
-        // TODO: send 404 if no file/resource is available
-        die($file.' is not found!');
-        
-        return;
+
+        return false;
     }
     
     public function LoadResourceFromFile($file)
     {
-        // get url of rdf document
-        $this->URI_Document = 'http://'.$_SERVER['SERVER_NAME'].str_replace(BASEDIR, BASEURL, $file);
-        
-        if ($this->URI_Request && $this->extensiontype)
-            $this->URI_Document = $this->URI_Request;
-        
-        //die($this->URI_Document);
+        $this->set_URI_Document($file);
         
         if (false === ($index = $this->cache->getVar($this->URI_Document, 'Foafpress', time()-filectime($file), 0)))
         {
         
         return;
     }
+
+    public function set_URI_Document($file)
+    {
+        // get url of rdf document
+        $this->URI_Document = 'http://'.$_SERVER['SERVER_NAME'].str_replace(BASEDIR, BASEURL, $file);
+
+        if ($this->URI_Request && $this->extensiontype)
+        {
+            $this->URI_Document = $this->URI_Request;
+        }
+
+        $this->addLogMessage('Set URI for document to: '.$this->URI_Document);
+
+        return;
+    }
     
     /**
      * Resolve best resource from request
     
     protected function isExportRequest()
     {
-        // if one of the RDF types is requested with q=1 then forward location to file
-        if ($this->URI_Request && !$this->extensiontype && $type = $this->isRequestType(array_keys($this->config['types'])))
-        {
-            //die($this->URI_Request.' '.$type.' '.$this->URI_Request.$this->config['types'][$type]);
-            //die('Location: '.$this->URI_Request.$this->config['types'][$type]);
-            //header('Location: '.$this->URI_Document, true, 301); exit();
-            header('Location: '.$this->URI_Request.$this->config['types'][$type], true, 301); exit();
-        }
-        
+        // if one of the application types is requested with q=1 then forward location to file
+        $this->RedirectToUrlByContentNegotiation();
+
         // if RDF file is requested directly and the client can work with the
         // application type (or file extensions imply export, see Foafpress
         // config) then set export=true
         return false;
        
     }
+
+    public function RedirectToUrlByContentNegotiation()
+    {
+        // if one of the application types is requested with q=1 then forward location to file
+        if ($this->URI_Request && !$this->extensiontype && $type = $this->isRequestType(array_keys($this->config['types'])))
+        {
+            header('Location: '.$this->URI_Request.$this->config['types'][$type], true, 301); exit();
+        }
+
+        return;
+    }
     
     protected function isRequestType(Array $types, $soft = false)
     {
 
     }
 
+    public function addLogMessage($msg)
+    {
+        $msg = $msg.' -- memory '.intval(memory_get_usage(true)/1024).'kb';
+        $this->sandbox->pm->publish('sandbox_add_log_message', $msg);
+        return;
+    }
+
     public function print_r($array)
     {
         echo '<pre>';
 $c['plugin']['folder'][] = './../../../core/';
 
 // load plugins at start
+$c['plugin']['load'][] = './i18n/LanguageChecker';
+$c['plugin']['load'][] = './debug/DebugLog';
 $c['plugin']['load'][] = './Foafpress';
 $c['plugin']['load'][] = './hooks/ActivityFlickr';
 $c['plugin']['load'][] = './hooks/ActivityIdentica';
 $c['plugin']['load'][] = './hooks/ActivityTwitter';
 $c['plugin']['load'][] = './hooks/ActivityGoogle';
-$c['plugin']['load'][] = './i18n/LanguageChecker';
-$c['plugin']['load'][] = './debug/DebugLog';
 
 // caching
 if ($production) if (!@$c['cache']['age']) $c['cache']['age'] = 60 * 60; // cache time in seconds