Dotclear

Changeset 2214:6d7d4c3f60d9 for inc


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

Location:
inc/core
Files:
3 edited

Legend:

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

    r2146 r2214  
    1414/** 
    1515@ingroup DC_CORE 
    16 @brief Rpeository modules feed parser 
     16@brief Repository modules XML feed parser 
    1717 
    18 Provides an object to parse feed of modules from repository. 
     18Provides an object to parse XML feed of modules from a repository. 
    1919*/ 
    2020class dcRepositoryParser 
    2121{ 
     22     /** @var  object    XML object of feed contents */ 
    2223     protected $xml; 
     24     /** @var  array     Array of feed contents */ 
    2325     protected $items; 
     26     /** @var  string    XML bloc tag */ 
    2427     protected static $bloc = 'http://dotaddict.org/da/'; 
    2528 
     29     /** 
     30      * Constructor. 
     31      * 
     32      * @param string         Feed content 
     33      */ 
    2634     public function __construct($data) 
    2735     { 
     
    4351     } 
    4452 
     53     /** 
     54      * Parse XML into array 
     55      */ 
    4556     protected function _parse() 
    4657     { 
     
    4960          } 
    5061 
    51           foreach ($this->xml->module as $i) 
    52           { 
     62          foreach ($this->xml->module as $i) { 
    5363               $attrs = $i->attributes(); 
    5464 
     
    7080               $item['support']    = (string) $i->children(self::$bloc)->support; 
    7181               $item['sshot']      = (string) $i->children(self::$bloc)->sshot; 
    72                 
     82 
    7383               $tags = array(); 
    74                foreach($i->children(self::$bloc)->tags as $t) 
    75                { 
     84               foreach($i->children(self::$bloc)->tags as $t) { 
    7685                    $tags[] = (string) $t->tag; 
    7786               } 
    7887               $item['tags']       = implode(', ',$tags); 
    7988                
    80                # First filter right now 
     89               # First filter right now. If DC_DEV is set all modules are parse 
    8190               if (defined('DC_DEV') && DC_DEV === true || version_compare(DC_VERSION,$item['dc_min'],'>=')) { 
    8291                    $this->items[$item['id']] = $item; 
     
    8594     } 
    8695 
     96     /** 
     97      * Get modules. 
     98      * 
     99      * @return     array          Modules list 
     100      */ 
    87101     public function getModules() 
    88102     { 
  • inc/core/class.dc.repository.php

    r2156 r2214  
    11<?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 ----------------------------------------- 
     12if (!defined('DC_RC_PATH')) { return; } 
     13 
     14/** 
     15@ingroup DC_CORE 
     16@brief Repository modules XML feed reader 
     17 
     18Provides an object to parse XML feed of modules from repository. 
     19*/ 
    320class dcRepository 
    421{ 
     22     /** @var  object    dcCore instance */ 
    523     public $core; 
     24     /** @var  object    dcModules instance */ 
    625     public $modules; 
    726 
     27     /** @var  string    User agent used to query repository */ 
     28     protected $user_agent = 'DotClear.org RepoBrowser/0.1'; 
     29     /** @var  string    XML feed URL */ 
    830     protected $xml_url; 
     31     /** @var  array     Array of new/update modules from repository */ 
    932     protected $data; 
    1033 
     34     /** 
     35      * Constructor. 
     36      * 
     37      * @param object    $modules       dcModules instance 
     38      * @param string    $xml_url       XML feed URL 
     39      */ 
    1140     public function __construct(dcModules $modules, $xml_url) 
    1241     { 
     
    1443          $this->modules = $modules; 
    1544          $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      */ 
    1854     public function check($force=false) 
    1955     { 
     
    5894     } 
    5995 
     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      */ 
    60102     public function get($update=false) 
    61103     { 
     
    63105     } 
    64106 
     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      */ 
    65121     public function search($pattern) 
    66122     { 
     
    100156     } 
    101157 
     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      */ 
    102165     public function process($url, $dest) 
    103166     { 
     
    106169     } 
    107170 
     171     /** 
     172      * Download a module. 
     173      * 
     174      * @param string    $url Module package URL 
     175      * @param string    $dest     Path to put module package 
     176      */ 
    108177     public function download($url, $dest) 
    109178     { 
     
    123192     } 
    124193 
     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      */ 
    125201     public function install($path) 
    126202     { 
     
    128204     } 
    129205 
    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      */ 
    135222     public static function sanitize(&$str, $_) 
    136223     { 
     
    138225     } 
    139226 
    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      */ 
    147251     private static function sort($a,$b) 
    148252     { 
  • 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