fluxcms /inc/popoon/components/cache.php

Language PHP Lines 566
MD5 Hash 8dbd91c85cda395fefde4162ee675350
Repository https://github.com/chregu/fluxcms.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
// +----------------------------------------------------------------------+
// | popoon                                                               |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001-2006 Bitflux GmbH                                 |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Hannes Gassert <hannes.gassert@unifr.ch>                     |
// +----------------------------------------------------------------------+
//
// $Id$
 

require_once('Cache.php'); 

/**
 * Component Caching for Popoon
 *
 * This classy class should mimic the caching mechanims Cocoon uses.
 * Have a look at http://bugzilla.bitflux.ch/show_bug.cgi?id=23 and the Cocoon documentation for more on this topic.
 *
 * @package  popoon
 * @author Hannes Gassert <hannes.gassert@unifr.ch>
 * @version $Id
 * @TODO Take care of sending HTTP Cache headers, implement and test other containers than the filesystem, DO COMPONENTS
 */
class componentCache{

    /**
     * "Definition" of the 'cachable' interface (methods only)
     *
     * @var array    $cachableInterface    Methods required by the 'cachable' interface
     * @access private
     */
    var $cachableInterface = array('generateValidity', 'checkValidity', 'generateKey');

    /**
     * Array of PEAR::Cache container objects, one entry per container.
     *
     * @var  array   $cacheHandlers
     * @access private
     */
    var $cacheHandlers      = array();

    /**
     * Name of default PEAR::Cache container
     *
     * @var   string
     * @access private
     */
    var $defaultContainer   = 'file';
    
    /**
     * Name of PEAR::Cache container currently in use (currently a container is chosen per pipeline)
     *
     * @var   string
     * @access private
     */    
    var $currentContainer   = 'file';
    
    /**
     * Filename suffix for cache-metafiles (for validityObjects)
     *
     * @var   string
     * @access private
     */    
    var $metafileSuffix     = '.meta';

    /**
     * Filename suffix for cache-globalfiles (for globals)
     *
     * @var   string
     * @access private
     */    
    var $globalSuffix     = '.global';
	
    /**
     * Cache-key generated by the last component in the pipeline
     *
     * @var   string   MD5 hash, probably
     * @access private
     */    
    var $lastCacheKey       = null;



  /**
   * Indicates a cache miss.
   *
   * All components must recalculate their results if this is set to true.
   *
   * @var  $miss             
   * @access        private    
   * @_load()            
   */ 
    var $miss  = false;
    
    /**
     * Type of the last component in the pipeline
     *
     * Used by loadLast(), updated by init().
     *
     * @see init(), loadLast()
     * @access private
     */
    var $componentTypes  = array();
    
    /**
     * Indicates wether the current component is cachable at all.
     * True by default, is set to false if the component doesn't implement the
     * 'cachable' interface, for example.
     *
     * @var bool
     */
    var $iscachable        = true;
    
    /**
     * Cache-key generated by the current component
     *
     * @var    string
     * @see    $currentComponent
     * @access private
     */    
    var $currentCacheKey    = null;
    
    var $currentMetaObject = array();


    /**
     * Constructor
     *
     * If necessary creates a new PEAR::Cache object of the desired type and adds it to $cacheHandlers
     * Also sets $currentContainer to the name of the currenct container. Currenty you should only use the default
     * container (Container/file.php), I haven't tested anything else yet.
     *
     * @param  array  map:pipeline attributes
     * @see $cacheHandlers
     * @access public
     */
    function componentCache($pipelineAttribs, &$sitemap){ 
        $this->sitemap = &$sitemap;
        //determine cache container       
        if(isset($pipelineAttribs['container'])) {
            $containerType = $pipelineAttribs['container'];
        }
        else {
            $containerType = $this->defaultContainer; 
        }

        if(!isset($this->cacheHandlers[$containerType]) || !is_object($this->cacheHandlers[$containerType])){
            $this->cacheHandlers[$containerType] =& new Cache($containerType, $this->getContainerOptions($containerType, $pipelineAttribs));
        }

        $this->currentContainer = $containerType;
    }
    
    /**
     * Initalizer
     *
     * This is some kind of pseudo constructor. Hands the current component to this class.
     * This means that init() _has_ to be called for every component that should be cached.
     *
     * @param   object  $component  Component to be cached
     * @access public
     */
    function init(&$component){
        
        //only work with components implementing the 'cachable' interface
        if(!$this->implementscachable($component)) {
            $this->iscachable = false;
            popoon::raiseError("You're trying to use a non-cachable component inside of a pipeline marked cachable.\n".
                               "This should be supported somehow but isn't implemented yet. Sorry.",
                               POPOON_ERROR_WARNING,
                               __FILE__, __LINE__, null);
            return(false);
        }
        else{
            $this->currentComponent =& $component;
            $this->lastCacheKey = $this->currentCacheKey;
            $this->componentTypes[] = $this->currentComponent->attribs['type'];
            $this->currentCacheKey = $component->generateKey($component->attribs, $this->lastCacheKey);
            $this->setCacheGroup();
            return(true);
        }
    }


    
    /**
     * Try to get cached component output
     *
     * Tries to get a validityObject for the current request. If there's one and if the component does verify it's validity,
     * the cached content is fetched and returned. Works only with Components implementing the 'cachable' interface, of course.
     * In any other case than success returns null.
     *
     * @return   string   cached content
     * @access   private
     * @see implementscachable(), loadValidityObject(), checkValidity(), loadCachedContent()
     */
    function _load($what = 'content'){

        $component =& $this->currentComponent;
                   
        if(!$this->miss && !is_null($validityObject = $this->loadValidityObject())){  //there's a candidate in the cache!
            //test if this candidate is a valid one    
            if($component->checkValidity($validityObject)){
                //if so, we do really have a valid cache hit!
                if($what == 'content') {
					 return($this->loadCachedContent());
				}
                else {
					return($this->currentCacheKey);
				}
            }
        }
        //in any other case
        $this->miss = true;
        return(null);
    }

    /**
     * Load content associated with last cache key
     *
     * When we come to a point in the pipeline where we need the output of the last component, we call loadLast(). 
     * It resets the cache object to the state it was when handling the last component, fetches the content wanted and
     * sets back the state of this object.
     *
     * @param  $target  reference to the variable you wan't to have the content you're looking for put into.
     */
    function loadLast(&$target){         

        //get last componentType
        $pos = count($this->componentTypes) - 2; if($pos < 0) $pos = 0; $lastComponentType = $this->componentTypes[$pos];

        //backup currentCacheKey
        $t = $this->currentCacheKey;

        //last is now current
        $this->currentCacheKey = $this->lastCacheKey;

        //also set back currentCacheGroup
        $this->setCacheGroup($lastComponentType, $this->lastCacheKey);

        /*load it
           but just, if target is_null, if it's not null, we already have something in 
           $this->xml. This is mostly set, when a component before also had to regenerate
           its data and can be mostly seen when we create a whole pipeline (eg. the generator
            loads xml-data, then passes that to a transformer. There is no need to read the
            content from the cache, if something like that happens. Moreover it leads to
            problems with libxslt2/libxml internal handling, as it looses some info when 
            we serialize and deserialize xml-data (this has to be tested more thoroughly)
            *by chregu*
        */
        
        if (is_null($target)) {
            $target = $this->loadCachedContent();
        }
       
        //reset all
        $this->currentCacheKey = $t;
        $this->setCacheGroup();
    }

    /**
     * Load the content associated to the current cache Key
     *
     * You'd use this rather seldom, cause most of the time you'll wan't to use loadLast()
     * @param bool $printsDirectly if header and result from cache should be printed directly (needed for serializers)
     * @see loadLast, $currentCacheKey
     */
    function load($printsDirectly = false){        return($this->loadCachedContent($printsDirectly));}

    /**
     * Check if there's something in the cache for the current cache key
     *
     * Behind this hides all the validity checking between the cache system and the components, 
     * so you should call this alway. Perhaps I could in some way call this from init() to have
     * one "must-do" call less..  
     *
     * @return bool   true if the content associated with there's a valid cache entry for $currentCacheKey.
     * @see $currentCacheKey
     */
    function isCached(){    return(!is_null($this->_load('id')));}

    /**
     * Returns the current cache key.
     *
     * Just a getter-method. I don't remember where I wanted to use this.
     *
     * @return string
     * @see $currentCacheKey
     */
    function getCacheKey(){ return($this->currentCacheKey);}

    
    /**
     * Store component generated data 
     *
     * Tells the current component to produce the data and call the methods to store that content and the according validityObject.
     *
     * @param   string   $launchMethod     Method to call to make the component generate the content
     * @param   bool     $printsDirectly  Indicates wether $launchMethod prints the content directly (as e.g. the readers do) or wether we have to get the content from $content
     * @access  public
     * @see storeContent(), $storeValidityObhect()
     */
    function store($content = null, $launchMethod = null, $param = null, $printsDirectly = false){
        
        if(!$this->iscachable) return(false); //we could also support a mix of cachable and non-cachable compos.,
        
        if($printsDirectly) {
            ob_start();
        } 

        if(!is_null($launchMethod) && method_exists($this->currentComponent, $launchMethod)) {
              $retVal = $this->currentComponent->$launchMethod($param);
        }
        else { /*add error handling here?*/ }
        
        if($printsDirectly){  // if $printsDirectly, then it must be some kind of serializer, so we need the headers on output
            $this->sitemap->printHeader();
            $content = ob_get_contents();
            ob_end_flush();
        }
        // else{ popoon_sitemap::var2XMLString($content); }
        if ( strtolower(get_class($content)) == "domdocument")
        {
            $content = $content->dumpmem();
        }
        
        $done = $this->storeContent($content) &&  $this->storeValidityObject()  ;   //only store validityObject after content was saved successfully.
        if(!$done)  popoon::raiseError('Could not write to cache! Is cache_dir writable? Is your component outputting?',
                                       POPOON_ERROR_WARNING,
                                       __FILE__, __LINE__, null);
        return($content);
        
    }
    
    /**
     * Checks wether a component implements the cachable interface
     *
     * Tests if all methods in $interface_methods are implemented by $component.
     *
     * @access  public
     * @param   object  poopon component object
     * @returns bool
     * @access public
     */
    function implementscachable(&$component){
        foreach($this->cachableInterface as $mustHaveMethod)
            if(!method_exists($component, $mustHaveMethod)) return(false);
        return(true);
    }

         
    /**
     * Try to load  validityObject from $cacheKey.meta
     *
     * @return   object  validityObject
     */
    function loadValidityObject(){
        $metafileKey   = $this->currentCacheKey . $this->metafileSuffix;
        $metafileGroup = $this->currentCacheGroup;
        $this->currentValidityObject = $this->cacheHandlers[$this->currentContainer]->get($metafileKey, $metafileGroup);        
       
        return($this->currentValidityObject["validityObject"] );
    }

    /**
     * Try to load  globals 
     *
     * @return   bool
     */
    function loadGlobals(){
		if (isset($this->currentMetaObject["globals"] ) && $globals = $this->currentMetaObject["globals"] ) {
			popoon_sitemap::setGlobalOptionsAll($globals);
		}
        return(true);
    }
    

    /**
     * Try to load header
     *
     * @return   bool
     */
    function loadHeader(){
		if (isset($this->currentMetaObject["header"] ) ) {
            $this->sitemap->header = array_merge($this->currentMetaObject["header"],$this->sitemap->header);
		}
        return(true);
    }
    
    
    
    /**
     * Stores validityObject in Cache
     *
     * Tinkers file and group name and calls save() method of current PEAR::Cache container.
     *
     * @return  bool  
     */
    function storeValidityObject(){
        $metafileKey   = $this->currentCacheKey . $this->metafileSuffix;
        $metafileGroup = $this->currentCacheGroup;
        $this->currentValidityObject["validityObject"] = $this->currentComponent->generateValidity();
        return($this->cacheHandlers[$this->currentContainer]->save($metafileKey,
                                                                   $this->currentValidityObject,
                                                                   '',   //hm.. perhaps we'll have to talk aboout expiration once..  
                                                                   $metafileGroup
                                                                   ));
                                                                
    }

    /**
     * Stores Globals in currentMetaObject
     *
     * @return  bool  
     * @see storeContent()
     */
    function storeGlobals() {
		if (is_array($globals = popoon_sitemap::getGlobalOptionsAll())) {
		    $this->currentMetaObject["globals"] = $globals;
        } 
		return true;
    }

    /**
     * Stores Header in currentMetaObject
     *
     * @return  bool  
     * @see storeContent()
     */
    function storeHeader() {
        if (isset($this->sitemap->header) && count($this->sitemap->header) > 0 ) {
	    	$this->currentMetaObject["header"] = $this->sitemap->header;
        }
	    return true;
    }

    /**
     * Stores component data in cache
     *
     * @param  $content  string representation of the output of the current component 
     * @return bool
     * @see storeGlobals()
     */
    function storeContent($content = '') {
        // store the globals in the currentMetaObject
        $this->storeGlobals();
        $this->storeHeader();        
        if(!empty($content)){ 
            return($this->cacheHandlers[$this->currentContainer]->extSave($this->currentCacheKey, $content, serialize($this->currentMetaObject), null, $this->setCacheGroup()));
        }
        else { /* else we should do popoon::error, I guess..*/ }
    }
    
    /**
     * Tries to load string content belonging to the current cache key 
     *
     * @param bool $printsDirectly if header and result from cache should be printed directly (needed for serializers)
     * @return string
     * @see $currentCacheKey, loadGlobals()
     */
    function loadCachedContent($printsDirectly = false){
        $this->currentMetaObject = unserialize($this->cacheHandlers[$this->currentContainer]->getUserData($this->currentCacheKey,$this->currentCacheGroup));
        $this->loadGlobals();
        $this->loadHeader();
        if ($printsDirectly) {
            $this->sitemap->printHeader();
            print $this->cacheHandlers[$this->currentContainer]->get($this->currentCacheKey, $this->currentCacheGroup);
            return true;
        } else {
            return($this->cacheHandlers[$this->currentContainer]->get($this->currentCacheKey, $this->currentCacheGroup));
        }
    }


    /**
     * Determine group of current component
     *
     * Cache objects are grouped primarily to avoid too large directories, which would mean too long a lookup time to get
     * a particular file. 
     *
     * @return   string   Group name
     * @access private
     */
    function setCacheGroup($type = null, $cacheKey = null){
        if(is_null($type))     $type = $this->currentComponent->attribs['type'];
        if(is_null($cacheKey)) $cacheKey = $this->currentCacheKey;
        $this->currentCacheGroup = $type . $cacheKey[0];
        return($this->currentCacheGroup);
    }
    

    /**
     * Determines the option to use for container $containerType
     *
     * This is quite a bad method. It relies halfway on the bitfluxcms environment, which it shouldn't. 
     * I did not know where to put popoon-specific configuration, so I added
     * my stuff to the bitfluxcms $BX_config hash.. dunno if that's good.
     * If you don't use bitfluxcms but standalone popoon, you can put container options such as
     * 'cachedir' into your sitemap.xml as attributes of a pipeline. You'd better take absolute path names
     * there, which is something I should add to the popoon documentation, if there's such a thing :)
     *
     * @return   array   PEAR::Cache container options
     * @param    string  $containerType
     * @param    array   $pipelineAttribs
     */
    function getContainerOptions($containerType = 'file', $pipelineAttribs = array()){
  
        // SETTINGS FOR PEAR::Cache/Container/File
        if(strtolower($containerType) == 'file'){
            if(isset($pipelineAttribs['cachedir'])) {
                $pipelineAttribs['cache_dir'] = $pipelineAttribs['cachedir']; unset($pipelineAttribs['cachedir']);
            }
            else if(isset($GLOBALS['BX_config']['componentCaching']['params']['cache_dir'])){
                $pipelineAttribs['cache_dir'] = $GLOBALS['BX_config']['componentCaching']['params']['cache_dir']; 
            }
            else if (isset($GLOBALS['BX_config']['popoon']['cacheParams'])) {
                $pipelineAttribs['cache_dir']  = $GLOBALS['BX_config']['popoon']['cacheParams']['cache_dir'];
                $pipelineAttribs['encoding_mode'] = $GLOBALS['BX_config']['popoon']['cacheParams']['encoding_mode']; 				
            } 
            else if (isset($GLOBALS['BX_config']['caching']['params'])) {
                $pipelineAttribs['cache_dir']  = $GLOBALS['BX_config']['caching']['params']['cache_dir'];
                $pipelineAttribs['encoding_mode'] = $GLOBALS['BX_config']['caching']['params']['encoding_mode']; 				
            } 
            else if (defined("BX_PROJECT_DIR")) {
                $pipelineAttribs['cache_dir']  = BX_PROJECT_DIR."/tmp/cache/";
            }
            else {
                $pipelineAttribs['cache_dir'] = "./tmp/cache/";
            }
            $pipelineAttribs['max_userdata_linelength'] = 0;

            //don't bother PEAR::Cache with our own attributes
            if(isset($pipelineAttribs['cachable'])) unset($pipelineAttribs['cachable']);

            //componentCaching gets a directory for itself
            $pipelineAttribs['cache_dir'] = str_replace('//', '/', $pipelineAttribs['cache_dir'] . '/components/');


        }              


        else{
            //no other containers looked at yet..
        }


        return($pipelineAttribs); 
    }
}



?>
Back to Top