File: class.dc.store.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (287 lines) | stat: -rw-r--r-- 6,731 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

/**
@ingroup DC_CORE
@brief Repository modules XML feed reader
@since 2.6

Provides an object to parse XML feed of modules from repository.
*/
class dcStore
{
	/** @var	object	dcCore instance */
	public $core;
	/** @var	object	dcModules instance */
	public $modules;

	/** @var	array	Modules fields to search on and their weighting */
	public static $weighting = array('id' => 10, 'name' => 8, 'author' => 6, 'tags' => 4, 'desc' => 2);

	/** @var	string	User agent used to query repository */
	protected $user_agent = 'DotClear.org RepoBrowser/0.1';
	/** @var	string	XML feed URL */
	protected $xml_url;
	/** @var	array	Array of new/update modules from repository */
	protected $data;

	/**
	 * Constructor.
	 *
	 * @param	object	$modules		dcModules instance
	 * @param	string	$xml_url		XML feed URL
	 */
	public function __construct(dcModules $modules, $xml_url)
	{
		$this->core = $modules->core;
		$this->modules = $modules;
		$this->xml_url = $xml_url;
		$this->user_agent = sprintf('Dotclear/%s)', DC_VERSION);

		$this->check();
	}

	/**
	 * Check repository.
	 *
	 * @param	boolean	$force		Force query repository
	 * @return	boolean	True if get feed or cache
	 */
	public function check($force=false)
	{
		if (!$this->xml_url) {
			return false;
		}
		if (($parser = dcStoreReader::quickParse($this->xml_url, DC_TPL_CACHE, $force)) === false) {
			return false;
		}

		$raw_datas = $parser->getModules();

		uasort($raw_datas, array('self','sort'));

		$skipped = array_keys($this->modules->getDisabledModules());
		foreach ($skipped as $p_id) {
			if (isset($raw_datas[$p_id])) {
				unset($raw_datas[$p_id]);
			}
		}

		$updates = array();
		$current = $this->modules->getModules();
		foreach ($current as $p_id => $p_infos) {
			if (isset($raw_datas[$p_id])) {
				if (dcUtils::versionsCompare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) {
					$updates[$p_id] = $raw_datas[$p_id];
					$updates[$p_id]['root'] = $p_infos['root'];
					$updates[$p_id]['root_writable'] = $p_infos['root_writable'];
					$updates[$p_id]['current_version'] = $p_infos['version'];
				}
				unset($raw_datas[$p_id]);
			}
		}

		$this->data = array(
			'new'	=> $raw_datas,
			'update'	=> $updates
		);

		return true;
	}

	/**
	 * Get a list of modules.
	 *
	 * @param	boolean	$update	True to get update modules, false for new ones
	 * @return	array	List of update/new modules
	 */
	public function get($update=false)
	{
		return $this->data[$update ? 'update' : 'new'];
	}

	/**
	 * Search a module.
	 *
	 * Search string is cleaned, split and compare to split:
	 * - module id and clean id,
	 * - module name, clean name,
	 * - module desccription.
	 *
	 * Every time a part of query is find on module,
	 * result accuracy grow. Result is sorted by accuracy.
	 *
	 * @param	string	$pattern	String to search
	 * @return	array	Match modules
	 */
	public function search($pattern)
	{
		$result = array();

		# Split query into small clean words
		if (!($patterns = self::patternize($pattern))) {
			return $result;
		}

		# For each modules
		foreach ($this->data['new'] as $id => $module) {
			$module['id'] = $id;

			# Loop through required module fields
			foreach(self::$weighting as $field => $weight) {

				# Skip fields which not exsist on module
				if (empty($module[$field])) {
					continue;
				}

				# Split field value into small clean word
				if (!($subjects = self::patternize($module[$field]))) {
					continue;
				}

				# Check contents
				if (!($nb = preg_match_all('/('.implode('|', $patterns).')/', implode(' ', $subjects), $_))) {
					continue;
				}

				# Add module to result
				if (!isset($sorter[$id])) {
					$sorter[$id] = 0;
					$result[$id] = $module;
				}

				# Increment score by matches count * field weight
				$sorter[$id] += $nb * $weight;
				$result[$id]['score'] = $sorter[$id];
			}
		}
		# Sort response by matches count
		if (!empty($result)) {
			array_multisort($sorter, SORT_DESC, $result);
		}
		return $result;
	}

	/**
	 * Quick download and install module.
	 *
	 * @param	string	$url	Module package URL
	 * @param	string	$dest	Path to install module
	 * @return	integer		1 = installed, 2 = update
	 */
	public function process($url, $dest)
	{
		$this->download($url, $dest);
		return $this->install($dest);
	}

	/**
	 * Download a module.
	 *
	 * @param	string	$url	Module package URL
	 * @param	string	$dest	Path to put module package
	 */
	public function download($url, $dest)
	{
		try {
			$client = netHttp::initClient($url, $path);
			$client->setUserAgent($this->user_agent);
			$client->useGzip(false);
			$client->setPersistReferers(false);
			$client->setOutput($dest);
			$client->get($path);
			unset($client);
		}
		catch (Exception $e) {
			unset($client);
			throw new Exception(__('An error occurred while downloading the file.'));
		}
	}

	/**
	 * Install a previously downloaded module.
	 *
	 * @param	string	$path	Module package URL
	 * @param	string	$path	Path to module package
	 * @return	integer		1 = installed, 2 = update
	 */
	public function install($path)
	{
		return dcModules::installPackage($path, $this->modules);
	}

	/**
	 * User Agent String.
	 *
	 * @param	string	$str		User agent string
	 */
	public function agent($str)
	{
		$this->user_agent = $str;
	}

	/**
	 * Split and clean pattern.
	 *
	 * @param	string	$str		String to sanitize
	 * @return	array	Array of cleaned pieces of string or false if none
	 */
	public static function patternize($str)
	{
		$arr = array();

		foreach(explode(' ', $str) as $_) {
			$_ = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $_));
			if (strlen($_) > 2) {
				$arr[] = $_;
			}
		}

		return empty($arr) ? false : $arr;
	}

	/**
	 * Compare version.
	 *
	 * @param	string	$v1		Version
	 * @param	string	$v2		Version
	 * @param	string	$op		Comparison operator
	 * @return	boolean	True is comparison is true, dude!
	 */
	private static function compare($v1, $v2, $op)
	{
		return version_compare(
			preg_replace('!-r(\d+)$!', '-p$1', $v1),
			preg_replace('!-r(\d+)$!', '-p$1', $v2),
			$op
		);
	}

	/**
	 * Sort modules list.
	 *
	 * @param	array	$a		A module
	 * @param	array	$b		A module
	 * @return	integer
	 */
	private static function sort($a,$b)
	{
		$c = strtolower($a['id']);
		$d = strtolower($b['id']);
		if ($c == $d) {
			return 0;
		}
		return ($c < $d) ? -1 : 1;
	}
}