Changeset 2214:6d7d4c3f60d9 for inc/core/class.dc.repository.reader.php
- Timestamp:
- 10/02/13 01:42:40 (12 years ago)
- Branch:
- dcRepo
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.repository.reader.php
r2146 r2214 14 14 /** 15 15 @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 18 Provides an object to parse XML feed of modules from repository. 19 This class extends clearbricks netHttp class. 19 20 */ 20 21 class dcRepositoryReader extends netHttp 21 22 { 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 */ 28 36 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 */ 31 45 public function __construct() 32 46 { … … 35 49 } 36 50 51 /** 52 * Parse modules feed. 53 * 54 * @param string $url XML feed URL 55 * @return object dcRepository instance 56 */ 37 57 public function parse($url) 38 58 { 39 59 $this->validators = array(); 40 if ($this->cache_dir) 41 {60 61 if ($this->cache_dir) { 42 62 return $this->withCache($url); 43 63 } 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 */ 58 79 public static function quickParse($url, $cache_dir=null, $force=false) 59 80 { … … 69 90 } 70 91 92 /** 93 * Set cache directory. 94 * 95 * @param string $dir Cache directory 96 * @return boolean True if cache dierctory is useable 97 */ 71 98 public function setCacheDir($dir) 72 99 { 73 100 $this->cache_dir = null; 74 101 75 if (!empty($dir) && is_dir($dir) && is_writeable($dir)) 76 { 102 if (!empty($dir) && is_dir($dir) && is_writeable($dir)) { 77 103 $this->cache_dir = $dir; 78 104 return true; … … 82 108 } 83 109 110 /** 111 * Set cache TTL. 112 * 113 * @param string $str Cache TTL 114 */ 84 115 public function setCacheTTL($str) 85 116 { 86 117 $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 */ 96 129 public function setForce($force) 97 130 { … … 99 132 } 100 133 134 /** 135 * Get repository XML feed URL content. 136 * 137 * @param string $url XML feed URL 138 * @return string Feed content 139 */ 101 140 protected function getModulesXML($url) 102 141 { … … 111 150 } 112 151 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 */ 113 158 protected function withCache($url) 114 159 { … … 124 169 $may_use_cached = false; 125 170 126 if (@file_exists($cached_file) && !$this->force)127 {171 # Use cache file ? 172 if (@file_exists($cached_file) && !$this->force) { 128 173 $may_use_cached = true; 129 174 $ts = @filemtime($cached_file); 130 if ($ts > strtotime($this->cache_ttl)) 131 { 175 if ($ts > strtotime($this->cache_ttl)) { 132 176 # Direct cache 133 177 return unserialize(file_get_contents($cached_file)); … … 136 180 } 137 181 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 ? 142 186 if ($this->cache_touch_on_fail) { 143 187 @files::touch($cached_file); 144 188 } 145 # connection failed - fetched from cache189 # Connection failed - fetched from cache 146 190 return unserialize(file_get_contents($cached_file)); 147 191 } … … 149 193 } 150 194 195 # Parse response 151 196 switch ($this->getStatus()) 152 197 { 198 # Not modified, use cache 153 199 case '304': 154 200 @files::touch($cached_file); 155 201 return unserialize(file_get_contents($cached_file)); 202 # Ok, parse feed 156 203 case '200': 157 if ($modules = new dcRepositoryParser($this->getContent())) 158 { 204 if ($modules = new dcRepositoryParser($this->getContent())) { 159 205 try { 160 206 files::makeDir(dirname($cached_file), true); 161 } catch (Exception $e) { 207 } 208 catch (Exception $e) { 162 209 return $modules; 163 210 } 164 211 165 if (($fp = @fopen($cached_file, 'wb'))) 166 { 212 if (($fp = @fopen($cached_file, 'wb'))) { 167 213 fwrite($fp, serialize($modules)); 168 214 fclose($fp); … … 176 222 } 177 223 224 /** 225 * Prepare query. 226 * 227 * @return array Query headers 228 */ 178 229 protected function buildRequest() 179 230 { … … 181 232 182 233 # Cache validators 183 if (!empty($this->validators)) 184 { 234 if (!empty($this->validators)) { 185 235 if (isset($this->validators['IfModifiedSince'])) { 186 236 $headers[] = 'If-Modified-Since: '.$this->validators['IfModifiedSince']; … … 189 239 if (is_array($this->validators['IfNoneMatch'])) { 190 240 $etags = implode(',', $this->validators['IfNoneMatch']); 191 } else { 241 } 242 else { 192 243 $etags = $this->validators['IfNoneMatch']; 193 244 } … … 199 250 } 200 251 252 /** 253 * Tweak query cache validator. 254 * 255 * @param string $key Validator key 256 * @param string $value Validator value 257 */ 201 258 private function setValidator($key, $value) 202 259 {
Note: See TracChangeset
for help on using the changeset viewer.