[2146] | 1 | <?php |
---|
[2214] | 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; } |
---|
[2146] | 13 | |
---|
[2214] | 14 | /** |
---|
| 15 | @ingroup DC_CORE |
---|
| 16 | @brief Repository modules XML feed reader |
---|
[2222] | 17 | @since 2.6 |
---|
[2214] | 18 | |
---|
| 19 | Provides an object to parse XML feed of modules from repository. |
---|
| 20 | */ |
---|
[2216] | 21 | class dcStore |
---|
[2146] | 22 | { |
---|
[2214] | 23 | /** @var object dcCore instance */ |
---|
[2146] | 24 | public $core; |
---|
[2214] | 25 | /** @var object dcModules instance */ |
---|
[2146] | 26 | public $modules; |
---|
| 27 | |
---|
[2222] | 28 | /** @var array Modules fields to search on and their weighting */ |
---|
| 29 | public static $weighting = array('id' => 10, 'name' => 8, 'author' => 6, 'tags' => 4, 'desc' => 2); |
---|
| 30 | |
---|
[2214] | 31 | /** @var string User agent used to query repository */ |
---|
| 32 | protected $user_agent = 'DotClear.org RepoBrowser/0.1'; |
---|
| 33 | /** @var string XML feed URL */ |
---|
[2146] | 34 | protected $xml_url; |
---|
[2214] | 35 | /** @var array Array of new/update modules from repository */ |
---|
[2146] | 36 | protected $data; |
---|
| 37 | |
---|
[2214] | 38 | /** |
---|
| 39 | * Constructor. |
---|
| 40 | * |
---|
| 41 | * @param object $modules dcModules instance |
---|
| 42 | * @param string $xml_url XML feed URL |
---|
| 43 | */ |
---|
[2146] | 44 | public function __construct(dcModules $modules, $xml_url) |
---|
| 45 | { |
---|
| 46 | $this->core = $modules->core; |
---|
[2147] | 47 | $this->modules = $modules; |
---|
| 48 | $this->xml_url = $xml_url; |
---|
[2214] | 49 | $this->user_agent = sprintf('Dotclear/%s)', DC_VERSION); |
---|
[2215] | 50 | |
---|
| 51 | $this->check(); |
---|
[2146] | 52 | } |
---|
| 53 | |
---|
[2214] | 54 | /** |
---|
| 55 | * Check repository. |
---|
| 56 | * |
---|
| 57 | * @param boolean $force Force query repository |
---|
| 58 | * @return boolean True if get feed or cache |
---|
| 59 | */ |
---|
[2147] | 60 | public function check($force=false) |
---|
[2146] | 61 | { |
---|
| 62 | if (!$this->xml_url) { |
---|
| 63 | return false; |
---|
| 64 | } |
---|
[2216] | 65 | if (($parser = dcStoreReader::quickParse($this->xml_url, DC_TPL_CACHE, $force)) === false) { |
---|
[2146] | 66 | return false; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | $raw_datas = $parser->getModules(); |
---|
| 70 | |
---|
| 71 | uasort($raw_datas, array('self','sort')); |
---|
| 72 | |
---|
| 73 | $skipped = array_keys($this->modules->getDisabledModules()); |
---|
| 74 | foreach ($skipped as $p_id) { |
---|
| 75 | if (isset($raw_datas[$p_id])) { |
---|
| 76 | unset($raw_datas[$p_id]); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | $updates = array(); |
---|
| 81 | $current = $this->modules->getModules(); |
---|
| 82 | foreach ($current as $p_id => $p_infos) { |
---|
| 83 | if (isset($raw_datas[$p_id])) { |
---|
[2388] | 84 | if (dcUtils::versionsCompare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) { |
---|
[2146] | 85 | $updates[$p_id] = $raw_datas[$p_id]; |
---|
| 86 | $updates[$p_id]['root'] = $p_infos['root']; |
---|
| 87 | $updates[$p_id]['root_writable'] = $p_infos['root_writable']; |
---|
| 88 | $updates[$p_id]['current_version'] = $p_infos['version']; |
---|
| 89 | } |
---|
| 90 | unset($raw_datas[$p_id]); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | $this->data = array( |
---|
| 95 | 'new' => $raw_datas, |
---|
| 96 | 'update' => $updates |
---|
| 97 | ); |
---|
| 98 | |
---|
| 99 | return true; |
---|
| 100 | } |
---|
| 101 | |
---|
[2214] | 102 | /** |
---|
| 103 | * Get a list of modules. |
---|
| 104 | * |
---|
| 105 | * @param boolean $update True to get update modules, false for new ones |
---|
| 106 | * @return array List of update/new modules |
---|
| 107 | */ |
---|
[2146] | 108 | public function get($update=false) |
---|
| 109 | { |
---|
| 110 | return $this->data[$update ? 'update' : 'new']; |
---|
| 111 | } |
---|
| 112 | |
---|
[2214] | 113 | /** |
---|
| 114 | * Search a module. |
---|
| 115 | * |
---|
| 116 | * Search string is cleaned, split and compare to split: |
---|
| 117 | * - module id and clean id, |
---|
| 118 | * - module name, clean name, |
---|
| 119 | * - module desccription. |
---|
| 120 | * |
---|
| 121 | * Every time a part of query is find on module, |
---|
[2216] | 122 | * result accuracy grow. Result is sorted by accuracy. |
---|
[2214] | 123 | * |
---|
| 124 | * @param string $pattern String to search |
---|
| 125 | * @return array Match modules |
---|
| 126 | */ |
---|
[2148] | 127 | public function search($pattern) |
---|
[2146] | 128 | { |
---|
| 129 | $result = array(); |
---|
| 130 | |
---|
[2148] | 131 | # Split query into small clean words |
---|
[2222] | 132 | if (!($patterns = self::patternize($pattern))) { |
---|
| 133 | return $result; |
---|
| 134 | } |
---|
[2148] | 135 | |
---|
| 136 | # For each modules |
---|
| 137 | foreach ($this->data['new'] as $id => $module) { |
---|
[2222] | 138 | $module['id'] = $id; |
---|
[2148] | 139 | |
---|
[2222] | 140 | # Loop through required module fields |
---|
| 141 | foreach(self::$weighting as $field => $weight) { |
---|
[2148] | 142 | |
---|
[2222] | 143 | # Skip fields which not exsist on module |
---|
| 144 | if (empty($module[$field])) { |
---|
| 145 | continue; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | # Split field value into small clean word |
---|
| 149 | if (!($subjects = self::patternize($module[$field]))) { |
---|
| 150 | continue; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | # Check contents |
---|
| 154 | if (!($nb = preg_match_all('/('.implode('|', $patterns).')/', implode(' ', $subjects), $_))) { |
---|
| 155 | continue; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | # Add module to result |
---|
| 159 | if (!isset($sorter[$id])) { |
---|
| 160 | $sorter[$id] = 0; |
---|
| 161 | $result[$id] = $module; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | # Increment score by matches count * field weight |
---|
| 165 | $sorter[$id] += $nb * $weight; |
---|
| 166 | $result[$id]['score'] = $sorter[$id]; |
---|
[2146] | 167 | } |
---|
[2148] | 168 | } |
---|
| 169 | # Sort response by matches count |
---|
| 170 | if (!empty($result)) { |
---|
| 171 | array_multisort($sorter, SORT_DESC, $result); |
---|
[2146] | 172 | } |
---|
| 173 | return $result; |
---|
| 174 | } |
---|
| 175 | |
---|
[2214] | 176 | /** |
---|
| 177 | * Quick download and install module. |
---|
| 178 | * |
---|
| 179 | * @param string $url Module package URL |
---|
| 180 | * @param string $dest Path to install module |
---|
| 181 | * @return integer 1 = installed, 2 = update |
---|
| 182 | */ |
---|
[2146] | 183 | public function process($url, $dest) |
---|
| 184 | { |
---|
[2149] | 185 | $this->download($url, $dest); |
---|
| 186 | return $this->install($dest); |
---|
| 187 | } |
---|
| 188 | |
---|
[2214] | 189 | /** |
---|
| 190 | * Download a module. |
---|
| 191 | * |
---|
| 192 | * @param string $url Module package URL |
---|
| 193 | * @param string $dest Path to put module package |
---|
| 194 | */ |
---|
[2149] | 195 | public function download($url, $dest) |
---|
| 196 | { |
---|
[3025] | 197 | // Check and add default protocol if necessary |
---|
| 198 | if (!preg_match('%^http[s]?:\/\/%',$url)) { |
---|
| 199 | $url = 'http://'.$url; |
---|
[2146] | 200 | } |
---|
[3025] | 201 | // Download package |
---|
| 202 | if ($client = netHttp::initClient($url, $path)) { |
---|
| 203 | try { |
---|
| 204 | $client->setUserAgent($this->user_agent); |
---|
| 205 | $client->useGzip(false); |
---|
| 206 | $client->setPersistReferers(false); |
---|
| 207 | $client->setOutput($dest); |
---|
| 208 | $client->get($path); |
---|
| 209 | unset($client); |
---|
| 210 | } |
---|
| 211 | catch (Exception $e) { |
---|
| 212 | unset($client); |
---|
| 213 | throw new Exception(__('An error occurred while downloading the file.')); |
---|
| 214 | } |
---|
| 215 | } else { |
---|
[2146] | 216 | throw new Exception(__('An error occurred while downloading the file.')); |
---|
| 217 | } |
---|
[2149] | 218 | } |
---|
[2146] | 219 | |
---|
[2214] | 220 | /** |
---|
| 221 | * Install a previously downloaded module. |
---|
| 222 | * |
---|
| 223 | * @param string $path Module package URL |
---|
| 224 | * @param string $path Path to module package |
---|
| 225 | * @return integer 1 = installed, 2 = update |
---|
| 226 | */ |
---|
[2149] | 227 | public function install($path) |
---|
| 228 | { |
---|
| 229 | return dcModules::installPackage($path, $this->modules); |
---|
[2146] | 230 | } |
---|
| 231 | |
---|
[2214] | 232 | /** |
---|
| 233 | * User Agent String. |
---|
| 234 | * |
---|
| 235 | * @param string $str User agent string |
---|
| 236 | */ |
---|
| 237 | public function agent($str) |
---|
[2146] | 238 | { |
---|
[2214] | 239 | $this->user_agent = $str; |
---|
[2146] | 240 | } |
---|
| 241 | |
---|
[2214] | 242 | /** |
---|
[2222] | 243 | * Split and clean pattern. |
---|
[2214] | 244 | * |
---|
| 245 | * @param string $str String to sanitize |
---|
[2222] | 246 | * @return array Array of cleaned pieces of string or false if none |
---|
[2214] | 247 | */ |
---|
[2222] | 248 | public static function patternize($str) |
---|
[2148] | 249 | { |
---|
[2222] | 250 | $arr = array(); |
---|
| 251 | |
---|
| 252 | foreach(explode(' ', $str) as $_) { |
---|
| 253 | $_ = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $_)); |
---|
| 254 | if (strlen($_) > 2) { |
---|
| 255 | $arr[] = $_; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | return empty($arr) ? false : $arr; |
---|
[2148] | 260 | } |
---|
| 261 | |
---|
[2214] | 262 | /** |
---|
| 263 | * Compare version. |
---|
| 264 | * |
---|
| 265 | * @param string $v1 Version |
---|
| 266 | * @param string $v2 Version |
---|
| 267 | * @param string $op Comparison operator |
---|
| 268 | * @return boolean True is comparison is true, dude! |
---|
| 269 | */ |
---|
| 270 | private static function compare($v1, $v2, $op) |
---|
[2146] | 271 | { |
---|
[2214] | 272 | return version_compare( |
---|
[2566] | 273 | preg_replace('!-r(\d+)$!', '-p$1', $v1), |
---|
| 274 | preg_replace('!-r(\d+)$!', '-p$1', $v2), |
---|
[2214] | 275 | $op |
---|
| 276 | ); |
---|
[2146] | 277 | } |
---|
| 278 | |
---|
[2566] | 279 | /** |
---|
[2214] | 280 | * Sort modules list. |
---|
| 281 | * |
---|
| 282 | * @param array $a A module |
---|
| 283 | * @param array $b A module |
---|
| 284 | * @return integer |
---|
| 285 | */ |
---|
[2146] | 286 | private static function sort($a,$b) |
---|
| 287 | { |
---|
[2566] | 288 | $c = strtolower($a['id']); |
---|
| 289 | $d = strtolower($b['id']); |
---|
| 290 | if ($c == $d) { |
---|
| 291 | return 0; |
---|
| 292 | } |
---|
| 293 | return ($c < $d) ? -1 : 1; |
---|
[2146] | 294 | } |
---|
| 295 | } |
---|