-
Notifications
You must be signed in to change notification settings - Fork 0
/
habaripackages.php
executable file
·239 lines (206 loc) · 5.73 KB
/
habaripackages.php
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
<?php
/**
*
* update() updates list
* upgrage($package_name) upgrades given package
* install($package_name) installs given package
* remove($package_name) removes given package
* clean() cleans out tmp files
*
*
*/
class HabariPackages
{
private static $types = array();
private static $types_location = array();
const UPDATE_INTERVAL = 259000;
// this will be like in Posts::get
public static function get( $param_array )
{
}
public static function require_updates()
{
return (
time() > ( Options::get( 'hpm__last_update' ) + self::UPDATE_INTERVAL )
|| version_compare( Options::get( 'hpm__repo_version' ), Version::get_habariversion() ) != 0
);
}
public static function update()
{
$package_list = array();
$packages = array();
foreach ( self::get_repos() as $repo ) {
$packages = self::update_packages( $repo );
if ( $packages === false ) {
Session::notice( sprintf( "Could not update packages from %s", $repo ) );
}
else {
$package_list = array_merge( $package_list, $packages );
}
}
Options::set( 'hpm__last_update', time() );
// get rid of orphaned/incompatible packages
if ( $package_list ) {
DB::query(
'DELETE FROM {packages} WHERE id NOT IN (' . Utils::placeholder_string( count($package_list) ) . ')',
$package_list
);
// @todo add message about number of obsolete packages deleted
}
else {
// there are no compatible packages, so crap 'em all
DB::query( 'DELETE FROM {packages}' );
}
}
public static function upgrade( $package_name )
{
$package = HabariPackage::get( $package_name );
if ( $package->status == 'upgrade' ) {
$package->upgrade();
}
return $package;
}
public static function install( $package_name )
{
$package = HabariPackage::get( $package_name );
$package->install();
return $package;
}
public static function remove( $package_name )
{
$package = HabariPackage::get( $package_name );
if ( $package->status != '' ) {
$package->remove();
}
return $package;
}
// clean tmp files left behind
public static function clean()
{
// remove all tmp files and dirs starting with "HPM-"
}
public static function get_repos()
{
$repos = array_map( 'trim', (array) explode( ',', Options::get( 'hpm__repos' ) ) );
return $repos;
}
/**
* @todo the server should return all versions and let hpm decide which version to take
*/
public static function update_packages( $repo )
{
$client = new RemoteRequest( $repo, 'GET' );
if ( Error::is_error( $client->execute() ) ) {
return false;
}
try {
$packages = $client->get_response_body();
//Utils::debug( $packages );
$packages = new SimpleXMLElement( $packages );
$package_list = array();
foreach ( $packages->package as $package ) {
if ( ! $package['guid'] || ! $package->versions ) {
continue;
}
$new_package = (array) $package->attributes();
$new_package = $new_package['@attributes'];
$new_package['description'] = strval( $package->description );
$versions = array();
//Utils::debug($package->versions);
foreach( $package->versions->version as $version ) {
$version = (array) $version->attributes();
$version = $version['@attributes'];
if ( isset( $version['habari_version'] ) && self::is_compatible( $version['habari_version'] ) ) {
$versions[$version['version']] = $version;
}
}
//Utils::debug( $new_package, $versions );
uksort( $versions, create_function('$a,$b','return version_compare($b,$a);') );
$version = current($versions);
if ( $version ) {
$new_package = array_merge( $version, $new_package );
if ( $old_package = HabariPackage::get( $new_package['guid'] ) ) {
if ( isset($new_package['version']) && version_compare( $new_package['version'], $old_package->version, '>' ) ) {
if ( $old_package->status == 'installed' ) {
$new_package['status'] = 'upgrade';
}
DB::update( DB::table('packages'), $new_package, array('guid'=>$new_package['guid']) );
$package_list[] = $old_package->id;
}
else {
$package_list[] = $old_package->id;
}
}
else {
DB::insert( DB::table('packages'), $new_package );
$package_list[] = DB::last_insert_id();
}
}
}
Options::set( 'hpm__repo_version', Version::get_habariversion() );
return $package_list;
}
catch ( Exception $e ) {
Utils::debug( $e );
return false;
}
}
public static function is_compatible( $ver )
{
$habari_ver = explode( '.', str_replace( array('-','_',' '), '.', Version::get_habariversion() ) );
$ver = explode( '.', str_replace( array('-','_',' '), '.', $ver ) );
$habari_ver = array_pad( $habari_ver, count($ver), '0' );
foreach ( $habari_ver as $i => $el ) {
if ( isset( $ver[$i] ) ) {
if ( $ver[$i] != 'x' && version_compare( $el, $ver[$i] ) != 0 ) {
return false;
}
}
else {
return false;
}
}
return true;
}
public static function list_package_types()
{
if ( self::$types ) {
return self::$types;
}
self::$types = array(
'system',
'plugin',
'theme',
'class'
);
self::$types = array_merge( Plugins::filter( 'package_types', array() ), self::$types );
return self::$types;
}
public static function type_location( $type )
{
$types = self::list_package_types();
if ( is_numeric( $type ) ) {
$type = $types[$type];
}
$type_locations = array(
'system'=>'',
'plugin'=>'/user/plugins',
'theme'=>'/user/themes',
'class'=>'/user/classes',
);
return $type_locations[$type];
}
public static function tempnam()
{
return tempnam( self::tempdir(), 'HPM-' );
}
public static function tempdir()
{
$tmp_dir = HABARI_PATH . "/3rdparty/hpm-tmp";
if ( !is_dir($tmp_dir) ) {
mkdir( $tmp_dir, 0777 );
}
return $tmp_dir;
}
}
?>