aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/extension/Manager.php
blob: f417bb43bc37d4f27ea6f76acabf7c6cb686fc66 (plain) (blame)
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
<?php

namespace dokuwiki\plugin\extension;

/**
 * Manages info about installation of extensions
 */
class Manager
{
    /** @var Extension The managed extension */
    protected Extension $extension;

    /** @var string path to the manager.dat */
    protected string $path;

    /** @var array the data from the manager.dat */
    protected array $data = [];

    /**
     * Initialize the Manager
     *
     * @param Extension $extension
     */
    public function __construct(Extension $extension)
    {
        $this->extension = $extension;
        $this->path = $this->extension->getInstallDir() . '/manager.dat';
        $this->data = $this->readFile();
    }

    /**
     * This updates the timestamp and URL in the manager.dat file
     *
     * It is called by Installer when installing or updating an extension
     *
     * @param $url
     */
    public function storeUpdate($url)
    {
        $this->data['downloadurl'] = $url;
        if (isset($this->data['installed'])) {
            // it's an update
            $this->data['updated'] = date('r');
        } else {
            // it's a new install
            $this->data['installed'] = date('r');
        }

        $data = '';
        foreach ($this->data as $k => $v) {
            $data .= $k . '=' . $v . DOKU_LF;
        }
        io_saveFile($this->path, $data);
    }


    /**
     * Reads the manager.dat file and fills the managerInfo array
     */
    protected function readFile()
    {
        $data = [];
        if (!is_readable($this->path)) return $data;

        $file = (array)@file($this->path);
        foreach ($file as $line) {
            [$key, $value] = sexplode('=', $line, 2, '');
            $key = trim($key);
            $value = trim($value);
            // backwards compatible with old plugin manager
            if ($key == 'url') $key = 'downloadurl';
            $data[$key] = $value;
        }

        return $data;
    }

    /**
     * @return \DateTime|null
     */
    public function getLastUpdate()
    {
        $date = $this->data['updated'] ?? $this->data['installed'] ?? '';
        if (!$date) return null;
        try {
            return new \DateTime($date);
        } catch (\Exception $e) {
            return null;
        }
    }

    public function getDownloadURL()
    {
        return $this->data['downloadurl'] ?? '';
    }

    /**
     * @return \DateTime|null
     */
    public function getInstallDate()
    {
        $date = $this->data['installed'] ?? '';
        if (!$date) return null;
        try {
            return new \DateTime($date);
        } catch (\Exception $e) {
            return null;
        }
    }
}