Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12694 anikendra 1
<?php
2
require_once 'spark_type.php';
3
require_once 'spark_types/git_spark.php';
4
require_once 'spark_types/hg_spark.php';
5
require_once 'spark_types/zip_spark.php';
6
 
7
class Spark_source {
8
 
9
    public $url;
10
    public $version_data;
11
    public $version;
12
 
13
    function __construct($url)
14
    {
15
        $this->url = $url;
16
        $this->version_data = json_decode(@file_get_contents("http://$this->url/api/system/latest"));
17
        $this->version = $this->version_data->spark_manager;
18
        $this->warn_if_outdated();
19
    }
20
 
21
    function get_url()
22
    {
23
        return $this->url;
24
    }
25
 
26
    // get details on an individual spark
27
    function get_spark_detail($spark_name, $version = 'HEAD')
28
    {
29
        $json_data = @file_get_contents("http://$this->url/api/packages/$spark_name/versions/$version/spec");
30
        if (!$json_data) return null; // no such spark here
31
        $data = json_decode($json_data);
32
        // if we don't succeed - throw an error
33
        if ($data == null || !$data->success)
34
        {
35
            $message = "Error retrieving spark detail from source: $this->url";
36
            if ($data != null) $message .= " ($data->message)";
37
            throw new Spark_exception($message);
38
        }
39
        // Get the detail for this spark
40
        return $this->get_spark($data->spec);
41
    }
42
 
43
    // get details on multiple sparks by search term
44
    function search($term)
45
    {
46
        $json_data = @file_get_contents("http://$this->url/api/packages/search?q=" . urlencode($term)); 
47
        $data = json_decode($json_data);
48
        // if the data isn't around of success is false, return a warning for this source
49
        if ($data == null || !$data->success)
50
        {
51
            $message = "Error searching source: $this->url";
52
            if ($data != null) $message .= " ($data->message)";
53
            Spark_utils::warning($message);
54
            return array();
55
        }
56
        // Get sparks for each one
57
        $results = array();
58
        foreach($data->results as $data)
59
        {
60
            $results[] = $this->get_spark($data);
61
        }
62
        return $results;
63
    }
64
 
65
    private function warn_if_outdated()
66
    {
67
        if ($this->outdated())
68
        {
69
            Spark_utils::warning("Your installed version of spark is outdated (current version: " . $this->outdated() . " / latest: " . $this->version . ")");
70
            Spark_utils::warning("To upgrade now, use `php tools/spark upgrade-system`");
71
        }
72
    }
73
 
74
    function outdated() {
75
        // Get the version for this source
76
        if (!$this->version) return; // no version found
77
 
78
        // Split versions
79
        list($self_major, $self_minor, $self_patch) = explode('.', SPARK_VERSION);
80
        list($source_major, $source_minor, $source_patch) = explode('.', $this->version);
81
 
82
        // Compare
83
        if ($self_major < $source_major ||
84
            $self_major == $source_major && $self_minor < $source_minor ||
85
            $self_major == $source_major && $self_minor == $source_minor && $self_patch < $source_patch)
86
        {
87
            return SPARK_VERSION;
88
        }
89
    }
90
 
91
    private function get_spark($data)
92
    {
93
        if ($data->repository_type == 'hg') return Mercurial_spark::get_spark($data);
94
        else if ($data->repository_type == 'git') return Git_spark::get_spark($data);
95
        else if ($data->repository_type == 'zip') return new Zip_spark($data);
96
        else throw new Exception('Unknown repository type: ' . $data->repository_type);
97
    }
98
 
99
}