Changeset 2214:6d7d4c3f60d9
- Timestamp:
- 10/02/13 01:42:40 (12 years ago)
- Branch:
- dcRepo
- Location:
- inc/core
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.repository.parser.php
r2146 r2214 14 14 /** 15 15 @ingroup DC_CORE 16 @brief R peository modulesfeed parser16 @brief Repository modules XML feed parser 17 17 18 Provides an object to parse feed of modules fromrepository.18 Provides an object to parse XML feed of modules from a repository. 19 19 */ 20 20 class dcRepositoryParser 21 21 { 22 /** @var object XML object of feed contents */ 22 23 protected $xml; 24 /** @var array Array of feed contents */ 23 25 protected $items; 26 /** @var string XML bloc tag */ 24 27 protected static $bloc = 'http://dotaddict.org/da/'; 25 28 29 /** 30 * Constructor. 31 * 32 * @param string Feed content 33 */ 26 34 public function __construct($data) 27 35 { … … 43 51 } 44 52 53 /** 54 * Parse XML into array 55 */ 45 56 protected function _parse() 46 57 { … … 49 60 } 50 61 51 foreach ($this->xml->module as $i) 52 { 62 foreach ($this->xml->module as $i) { 53 63 $attrs = $i->attributes(); 54 64 … … 70 80 $item['support'] = (string) $i->children(self::$bloc)->support; 71 81 $item['sshot'] = (string) $i->children(self::$bloc)->sshot; 72 82 73 83 $tags = array(); 74 foreach($i->children(self::$bloc)->tags as $t) 75 { 84 foreach($i->children(self::$bloc)->tags as $t) { 76 85 $tags[] = (string) $t->tag; 77 86 } 78 87 $item['tags'] = implode(', ',$tags); 79 88 80 # First filter right now 89 # First filter right now. If DC_DEV is set all modules are parse 81 90 if (defined('DC_DEV') && DC_DEV === true || version_compare(DC_VERSION,$item['dc_min'],'>=')) { 82 91 $this->items[$item['id']] = $item; … … 85 94 } 86 95 96 /** 97 * Get modules. 98 * 99 * @return array Modules list 100 */ 87 101 public function getModules() 88 102 { -
inc/core/class.dc.repository.php
r2156 r2214 1 1 <?php 2 2 # -- BEGIN LICENSE BLOCK --------------------------------------- 3 # 4 # This file is part of Dotclear 2. 5 # 6 # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear 7 # Licensed under the GPL version 2.0 license. 8 # See LICENSE file or 9 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 10 # 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return; } 13 14 /** 15 @ingroup DC_CORE 16 @brief Repository modules XML feed reader 17 18 Provides an object to parse XML feed of modules from repository. 19 */ 3 20 class dcRepository 4 21 { 22 /** @var object dcCore instance */ 5 23 public $core; 24 /** @var object dcModules instance */ 6 25 public $modules; 7 26 27 /** @var string User agent used to query repository */ 28 protected $user_agent = 'DotClear.org RepoBrowser/0.1'; 29 /** @var string XML feed URL */ 8 30 protected $xml_url; 31 /** @var array Array of new/update modules from repository */ 9 32 protected $data; 10 33 34 /** 35 * Constructor. 36 * 37 * @param object $modules dcModules instance 38 * @param string $xml_url XML feed URL 39 */ 11 40 public function __construct(dcModules $modules, $xml_url) 12 41 { … … 14 43 $this->modules = $modules; 15 44 $this->xml_url = $xml_url; 16 } 17 45 $this->user_agent = sprintf('Dotclear/%s)', DC_VERSION); 46 } 47 48 /** 49 * Check repository. 50 * 51 * @param boolean $force Force query repository 52 * @return boolean True if get feed or cache 53 */ 18 54 public function check($force=false) 19 55 { … … 58 94 } 59 95 96 /** 97 * Get a list of modules. 98 * 99 * @param boolean $update True to get update modules, false for new ones 100 * @return array List of update/new modules 101 */ 60 102 public function get($update=false) 61 103 { … … 63 105 } 64 106 107 /** 108 * Search a module. 109 * 110 * Search string is cleaned, split and compare to split: 111 * - module id and clean id, 112 * - module name, clean name, 113 * - module desccription. 114 * 115 * Every time a part of query is find on module, 116 * result accuracy grow. Result is sorted by acuracy. 117 * 118 * @param string $pattern String to search 119 * @return array Match modules 120 */ 65 121 public function search($pattern) 66 122 { … … 100 156 } 101 157 158 /** 159 * Quick download and install module. 160 * 161 * @param string $url Module package URL 162 * @param string $dest Path to install module 163 * @return integer 1 = installed, 2 = update 164 */ 102 165 public function process($url, $dest) 103 166 { … … 106 169 } 107 170 171 /** 172 * Download a module. 173 * 174 * @param string $url Module package URL 175 * @param string $dest Path to put module package 176 */ 108 177 public function download($url, $dest) 109 178 { … … 123 192 } 124 193 194 /** 195 * Install a previously downloaded module. 196 * 197 * @param string $path Module package URL 198 * @param string $path Path to module package 199 * @return integer 1 = installed, 2 = update 200 */ 125 201 public function install($path) 126 202 { … … 128 204 } 129 205 130 public static function agent() 131 { 132 return sprintf('Dotclear/%s)', DC_VERSION); 133 } 134 206 /** 207 * User Agent String. 208 * 209 * @param string $str User agent string 210 */ 211 public function agent($str) 212 { 213 $this->user_agent = $str; 214 } 215 216 /** 217 * Sanitize string. 218 * 219 * @param string $str String to sanitize 220 * @param null $_ Unused param 221 */ 135 222 public static function sanitize(&$str, $_) 136 223 { … … 138 225 } 139 226 140 private static function compare($v1,$v2,$op) 141 { 142 $v1 = preg_replace('!-r(\d+)$!','-p$1',$v1); 143 $v2 = preg_replace('!-r(\d+)$!','-p$1',$v2); 144 return version_compare($v1,$v2,$op); 145 } 146 227 /** 228 * Compare version. 229 * 230 * @param string $v1 Version 231 * @param string $v2 Version 232 * @param string $op Comparison operator 233 * @return boolean True is comparison is true, dude! 234 */ 235 private static function compare($v1, $v2, $op) 236 { 237 return version_compare( 238 preg_replace('!-r(\d+)$!', '-p$1', $v1), 239 preg_replace('!-r(\d+)$!', '-p$1', $v2), 240 $op 241 ); 242 } 243 244 /** 245 * Sort modules list. 246 * 247 * @param array $a A module 248 * @param array $b A module 249 * @return integer 250 */ 147 251 private static function sort($a,$b) 148 252 { -
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.