Dotclear

source: inc/core/class.dc.repository.php @ 2148:621d4c6f4e8c

Revision 2148:621d4c6f4e8c, 3.2 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Better repository search engine (and other stuff)

Line 
1<?php
2
3class dcRepository
4{
5     public $core;
6     public $modules;
7
8     protected $xml_url;
9     protected $data;
10
11     public function __construct(dcModules $modules, $xml_url)
12     {
13          $this->core = $modules->core;
14          $this->modules = $modules;
15          $this->xml_url = $xml_url;
16     }
17
18     public function check($force=false)
19     {
20          if (!$this->xml_url) {
21               return false;
22          }
23          if (($parser = dcRepositoryReader::quickParse($this->xml_url, DC_TPL_CACHE, $force)) === false) {
24               return false;
25          }
26
27          $raw_datas = $parser->getModules();
28
29          uasort($raw_datas, array('self','sort'));
30
31          $skipped = array_keys($this->modules->getDisabledModules());
32          foreach ($skipped as $p_id) {
33               if (isset($raw_datas[$p_id])) {
34                    unset($raw_datas[$p_id]);
35               }
36          }
37
38          $updates = array();
39          $current = $this->modules->getModules();
40          foreach ($current as $p_id => $p_infos) {
41               if (isset($raw_datas[$p_id])) {
42                    if (self::compare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) {
43                         $updates[$p_id] = $raw_datas[$p_id];
44                         $updates[$p_id]['root'] = $p_infos['root'];
45                         $updates[$p_id]['root_writable'] = $p_infos['root_writable'];
46                         $updates[$p_id]['current_version'] = $p_infos['version'];
47                    }
48                    unset($raw_datas[$p_id]);
49               }
50          }
51
52          $this->data = array(
53               'new'     => $raw_datas,
54               'update'  => $updates
55          );
56
57          return true;
58     }
59
60     public function get($update=false)
61     {
62          return $this->data[$update ? 'update' : 'new'];
63     }
64
65     public function search($pattern)
66     {
67          $result = array();
68
69          # Split query into small clean words
70          $patterns = explode(' ', $pattern);
71          array_walk($patterns, array('dcRepository','sanitize'));
72
73          # For each modules
74          foreach ($this->data['new'] as $id => $module) {
75
76               # Split modules infos into small clean word
77               $subjects = explode(' ', $id.' '.$module['name'].' '.$module['desc']);
78               array_walk($subjects, array('dcRepository','sanitize'));
79
80               # Check contents
81               if (!($nb = preg_match_all('/('.implode('|', $patterns).')/', implode(' ', $subjects)))) {
82                    continue;
83               }
84
85               # Add module to result
86               if (!isset($sorter[$id])) {
87                    $sorter[$id] = 0;
88                    $result[$id] = $module;
89               }
90
91               # Increment matches count
92               $sorter[$id] += $nb;
93          }
94          # Sort response by matches count
95          if (!empty($result)) {
96               array_multisort($sorter, SORT_DESC, $result);
97          }
98          return $result;
99     }
100
101     public function process($url, $dest)
102     {
103          try {
104               $client = netHttp::initClient($url, $path);
105               $client->setUserAgent(self::agent());
106               $client->useGzip(false);
107               $client->setPersistReferers(false);
108               $client->setOutput($dest);
109               $client->get($path);
110          }
111          catch (Exception $e) {
112               throw new Exception(__('An error occurred while downloading the file.'));
113          }
114
115          unset($client);
116          $ret_code = dcModules::installPackage($dest, $this->modules);
117
118          return $ret_code;
119     }
120
121     public static function agent()
122     {
123          return sprintf('Dotclear/%s)', DC_VERSION);
124     }
125
126     public static function sanitize(&$str, $_)
127     {
128          $str = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $str));
129     }
130
131     private static function compare($v1,$v2,$op)
132     {
133          $v1 = preg_replace('!-r(\d+)$!','-p$1',$v1);
134          $v2 = preg_replace('!-r(\d+)$!','-p$1',$v2);
135          return version_compare($v1,$v2,$op);
136     }
137
138     private static function sort($a,$b)
139     {
140          $c = strtolower($a['id']); 
141          $d = strtolower($b['id']); 
142          if ($c == $d) { 
143               return 0; 
144          } 
145          return ($c < $d) ? -1 : 1; 
146     }
147}
Note: See TracBrowser for help on using the repository browser.

Sites map