1<?php 2 3namespace dokuwiki\plugin\extension; 4 5/** 6 * Manages info about installation of extensions 7 */ 8class Manager 9{ 10 /** @var Extension The managed extension */ 11 protected Extension $extension; 12 13 /** @var string path to the manager.dat */ 14 protected string $path; 15 16 /** @var array the data from the manager.dat */ 17 protected array $data = []; 18 19 /** 20 * Initialize the Manager 21 * 22 * @param Extension $extension 23 */ 24 public function __construct(Extension $extension) 25 { 26 $this->extension = $extension; 27 $this->path = $this->extension->getInstallDir() . '/manager.dat'; 28 $this->data = $this->readFile(); 29 } 30 31 /** 32 * This updates the timestamp and URL in the manager.dat file 33 * 34 * It is called by Installer when installing or updating an extension 35 * 36 * @param $url 37 */ 38 public function storeUpdate($url) 39 { 40 $this->data['downloadurl'] = $url; 41 if (isset($this->data['installed'])) { 42 // it's an update 43 $this->data['updated'] = date('r'); 44 } else { 45 // it's a new install 46 $this->data['installed'] = date('r'); 47 } 48 49 $data = ''; 50 foreach ($this->data as $k => $v) { 51 $data .= $k . '=' . $v . DOKU_LF; 52 } 53 io_saveFile($this->path, $data); 54 } 55 56 57 /** 58 * Reads the manager.dat file and fills the managerInfo array 59 */ 60 protected function readFile() 61 { 62 $data = []; 63 if (!is_readable($this->path)) return $data; 64 65 $file = (array)@file($this->path); 66 foreach ($file as $line) { 67 [$key, $value] = sexplode('=', $line, 2, ''); 68 $key = trim($key); 69 $value = trim($value); 70 // backwards compatible with old plugin manager 71 if ($key == 'url') $key = 'downloadurl'; 72 $data[$key] = $value; 73 } 74 75 return $data; 76 } 77 78 /** 79 * @return \DateTime|null 80 */ 81 public function getLastUpdate() 82 { 83 $date = $this->data['updated'] ?? $this->data['installed'] ?? ''; 84 if (!$date) return null; 85 try { 86 return new \DateTime($date); 87 } catch (\Exception $e) { 88 return null; 89 } 90 } 91 92 public function getDownloadURL() 93 { 94 return $this->data['downloadurl'] ?? ''; 95 } 96 97 /** 98 * @return \DateTime|null 99 */ 100 public function getInstallDate() 101 { 102 $date = $this->data['installed'] ?? ''; 103 if (!$date) return null; 104 try { 105 return new \DateTime($date); 106 } catch (\Exception $e) { 107 return null; 108 } 109 } 110} 111