Dotclear


Ignore:
Timestamp:
10/02/13 01:42:40 (12 years ago)
Author:
Denis Jean-Chirstian <contact@…>
Branch:
dcRepo
Message:

Document and clean up dcRepository classes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.repository.reader.php

    r2146 r2214  
    1414/** 
    1515@ingroup DC_CORE 
    16 @brief Repository modules feed reader 
    17  
    18 Provides an object to parse feed of modules from repository. 
     16@brief Repository modules XML feed reader 
     17 
     18Provides an object to parse XML feed of modules from repository. 
     19This class extends clearbricks netHttp class. 
    1920*/ 
    2021class dcRepositoryReader extends netHttp 
    2122{ 
    22      protected $user_agent         = 'DotClear.org RepoBrowser/0.1'; 
    23      protected $timeout            = 5; 
    24      protected $validators         = null;                  ///< <b>array</b>   HTTP Cache validators 
    25      protected $cache_dir          = null;                  ///< <b>string</b>  Cache temporary directory 
    26      protected $cache_file_prefix  = 'dcrepo';              ///< <b>string</b>  Cache file prefix 
    27      protected $cache_ttl          = '-30 minutes';         ///< <b>string</b>  Cache TTL 
     23     /** @var  string    User agent used to query repository */ 
     24     protected $user_agent = 'DotClear.org RepoBrowser/0.1'; 
     25     /** @var  integer   User agent used to query repository */ 
     26     protected $timeout = 5; 
     27     /** @var  array      HTTP Cache validators */ 
     28     protected $validators = null; 
     29     /** @var  string    Cache temporary directory */ 
     30     protected $cache_dir = null; 
     31     /** @var  string    Cache file prefix */ 
     32     protected $cache_file_prefix = 'dcrepo'; 
     33     /** @var  integer   Cache TTL */ 
     34     protected $cache_ttl = '-30 minutes'; 
     35     /** @var  boolean   'Cache' TTL on server failed */ 
    2836     protected $cache_touch_on_fail = true; 
    29      protected $force              = false; 
    30  
     37     /** @var  boolean   Force query server */ 
     38     protected $force = false; 
     39 
     40     /** 
     41      * Constructor. 
     42      * 
     43      * Bypass first argument of clearbricks netHttp constructor. 
     44      */ 
    3145     public function __construct() 
    3246     { 
     
    3549     } 
    3650 
     51     /** 
     52      * Parse modules feed. 
     53      * 
     54      * @param string    $url      XML feed URL 
     55      * @return     object    dcRepository instance 
     56      */ 
    3757     public function parse($url) 
    3858     { 
    3959          $this->validators = array(); 
    40           if ($this->cache_dir) 
    41           { 
     60 
     61          if ($this->cache_dir) { 
    4262               return $this->withCache($url); 
    4363          } 
    44           else 
    45           { 
    46                if (!$this->getModulesXML($url)) { 
    47                     return false; 
    48                } 
    49  
    50                if ($this->getStatus() != '200') { 
    51                     return false; 
    52                } 
    53  
    54                return new dcRepositoryParser($this->getContent()); 
    55           } 
    56      } 
    57  
     64          elseif (!$this->getModulesXML($url) || $this->getStatus() != '200') { 
     65               return false; 
     66          } 
     67 
     68          return new dcRepositoryParser($this->getContent()); 
     69     } 
     70 
     71     /** 
     72      * Quick parse modules feed. 
     73      * 
     74      * @param string    $url      XML feed URL 
     75      * @param string    $cache_dir     Cache directoy or null for no cache 
     76      * @param boolean   $force         Force query repository 
     77      * @return     object    Self instance 
     78      */ 
    5879     public static function quickParse($url, $cache_dir=null, $force=false) 
    5980     { 
     
    6990     } 
    7091 
     92     /** 
     93      * Set cache directory. 
     94      * 
     95      * @param string    $dir      Cache directory 
     96      * @return     boolean   True if cache dierctory is useable 
     97      */ 
    7198     public function setCacheDir($dir) 
    7299     { 
    73100          $this->cache_dir = null; 
    74101 
    75           if (!empty($dir) && is_dir($dir) && is_writeable($dir)) 
    76           { 
     102          if (!empty($dir) && is_dir($dir) && is_writeable($dir)) { 
    77103               $this->cache_dir = $dir; 
    78104               return true; 
     
    82108     } 
    83109 
     110     /** 
     111      * Set cache TTL. 
     112      * 
     113      * @param string    $str      Cache TTL 
     114      */ 
    84115     public function setCacheTTL($str) 
    85116     { 
    86117          $str = trim($str); 
    87           if (!empty($str)) 
    88           { 
    89                if (substr($str, 0, 1) != '-') { 
    90                     $str = '-'.$str; 
    91                } 
    92                $this->cache_ttl = $str; 
    93           } 
    94      } 
    95  
     118 
     119          if (!empty($str)) { 
     120               $this->cache_ttl = substr($str, 0, 1) == '-' ? $str : '-'.$str; 
     121          } 
     122     } 
     123 
     124     /** 
     125      * Set force query reposiory. 
     126      * 
     127      * @param boolean   $force    True to force query 
     128      */ 
    96129     public function setForce($force) 
    97130     { 
     
    99132     } 
    100133 
     134     /** 
     135      * Get repository XML feed URL content. 
     136      * 
     137      * @param string    $url      XML feed URL 
     138      * @return     string    Feed content 
     139      */ 
    101140     protected function getModulesXML($url) 
    102141     { 
     
    111150     } 
    112151 
     152     /** 
     153      * Get repository modules list using cache. 
     154      * 
     155      * @param string    $url      XML feed URL 
     156      * @return     array     Feed content or False on fail 
     157      */ 
    113158     protected function withCache($url) 
    114159     { 
     
    124169          $may_use_cached = false; 
    125170 
    126           if (@file_exists($cached_file) && !$this->force) 
    127           { 
     171          # Use cache file ? 
     172          if (@file_exists($cached_file) && !$this->force) { 
    128173               $may_use_cached = true; 
    129174               $ts = @filemtime($cached_file); 
    130                if ($ts > strtotime($this->cache_ttl)) 
    131                { 
     175               if ($ts > strtotime($this->cache_ttl)) { 
    132176                    # Direct cache 
    133177                    return unserialize(file_get_contents($cached_file)); 
     
    136180          } 
    137181 
    138           if (!$this->getModulesXML($url)) 
    139           { 
    140                if ($may_use_cached) 
    141                { 
     182          # Query repository 
     183          if (!$this->getModulesXML($url)) { 
     184               if ($may_use_cached) { 
     185                    # Touch cache TTL even if query failed ? 
    142186                    if ($this->cache_touch_on_fail) { 
    143187                         @files::touch($cached_file); 
    144188                    } 
    145                     # connection failed - fetched from cache 
     189                    # Connection failed - fetched from cache 
    146190                    return unserialize(file_get_contents($cached_file)); 
    147191               } 
     
    149193          } 
    150194 
     195          # Parse response 
    151196          switch ($this->getStatus()) 
    152197          { 
     198               # Not modified, use cache 
    153199               case '304': 
    154200                    @files::touch($cached_file); 
    155201                    return unserialize(file_get_contents($cached_file)); 
     202               # Ok, parse feed 
    156203               case '200': 
    157                     if ($modules = new dcRepositoryParser($this->getContent())) 
    158                     { 
     204                    if ($modules = new dcRepositoryParser($this->getContent())) { 
    159205                         try { 
    160206                              files::makeDir(dirname($cached_file), true); 
    161                          } catch (Exception $e) { 
     207                         } 
     208                         catch (Exception $e) { 
    162209                              return $modules; 
    163210                         } 
    164211 
    165                          if (($fp = @fopen($cached_file, 'wb'))) 
    166                          { 
     212                         if (($fp = @fopen($cached_file, 'wb'))) { 
    167213                              fwrite($fp, serialize($modules)); 
    168214                              fclose($fp); 
     
    176222     } 
    177223 
     224     /** 
     225      * Prepare query. 
     226      * 
     227      * @return     array     Query headers 
     228      */ 
    178229     protected function buildRequest() 
    179230     { 
     
    181232 
    182233          # Cache validators 
    183           if (!empty($this->validators)) 
    184           { 
     234          if (!empty($this->validators)) { 
    185235               if (isset($this->validators['IfModifiedSince'])) { 
    186236                    $headers[] = 'If-Modified-Since: '.$this->validators['IfModifiedSince']; 
     
    189239                    if (is_array($this->validators['IfNoneMatch'])) { 
    190240                         $etags = implode(',', $this->validators['IfNoneMatch']); 
    191                     } else { 
     241                    } 
     242                    else { 
    192243                         $etags = $this->validators['IfNoneMatch']; 
    193244                    } 
     
    199250     } 
    200251 
     252     /** 
     253      * Tweak query cache validator. 
     254      * 
     255      * @param string    $key      Validator key 
     256      * @param string    $value         Validator value 
     257      */ 
    201258     private function setValidator($key, $value) 
    202259     { 
Note: See TracChangeset for help on using the changeset viewer.

Sites map