17c9966a5SAndreas Gohr<?php 27c9966a5SAndreas Gohr 37c9966a5SAndreas Gohrnamespace dokuwiki\plugin\extension; 47c9966a5SAndreas Gohr 57c9966a5SAndreas Gohr/** 6160d3688SAndreas Gohr * Manages info about installation of extensions 77c9966a5SAndreas Gohr */ 87c9966a5SAndreas Gohrclass Manager 97c9966a5SAndreas Gohr{ 107c9966a5SAndreas Gohr /** @var Extension The managed extension */ 117c9966a5SAndreas Gohr protected Extension $extension; 127c9966a5SAndreas Gohr 137c9966a5SAndreas Gohr /** @var string path to the manager.dat */ 147c9966a5SAndreas Gohr protected string $path; 157c9966a5SAndreas Gohr 167c9966a5SAndreas Gohr /** @var array the data from the manager.dat */ 177c9966a5SAndreas Gohr protected array $data = []; 187c9966a5SAndreas Gohr 197c9966a5SAndreas Gohr /** 207c9966a5SAndreas Gohr * Initialize the Manager 217c9966a5SAndreas Gohr * 227c9966a5SAndreas Gohr * @param Extension $extension 237c9966a5SAndreas Gohr */ 247c9966a5SAndreas Gohr public function __construct(Extension $extension) 257c9966a5SAndreas Gohr { 267c9966a5SAndreas Gohr $this->extension = $extension; 277c9966a5SAndreas Gohr $this->path = $this->extension->getInstallDir() . '/manager.dat'; 287c9966a5SAndreas Gohr $this->data = $this->readFile(); 297c9966a5SAndreas Gohr } 307c9966a5SAndreas Gohr 317c9966a5SAndreas Gohr /** 327c9966a5SAndreas Gohr * This updates the timestamp and URL in the manager.dat file 337c9966a5SAndreas Gohr * 347c9966a5SAndreas Gohr * It is called by Installer when installing or updating an extension 357c9966a5SAndreas Gohr * 367c9966a5SAndreas Gohr * @param $url 377c9966a5SAndreas Gohr */ 387c9966a5SAndreas Gohr public function storeUpdate($url) 397c9966a5SAndreas Gohr { 407c9966a5SAndreas Gohr $this->data['downloadurl'] = $url; 417c9966a5SAndreas Gohr if (isset($this->data['installed'])) { 427c9966a5SAndreas Gohr // it's an update 437c9966a5SAndreas Gohr $this->data['updated'] = date('r'); 447c9966a5SAndreas Gohr } else { 457c9966a5SAndreas Gohr // it's a new install 467c9966a5SAndreas Gohr $this->data['installed'] = date('r'); 477c9966a5SAndreas Gohr } 487c9966a5SAndreas Gohr 497c9966a5SAndreas Gohr $data = ''; 507c9966a5SAndreas Gohr foreach ($this->data as $k => $v) { 517c9966a5SAndreas Gohr $data .= $k . '=' . $v . DOKU_LF; 527c9966a5SAndreas Gohr } 537c9966a5SAndreas Gohr io_saveFile($this->path, $data); 547c9966a5SAndreas Gohr } 557c9966a5SAndreas Gohr 567c9966a5SAndreas Gohr 577c9966a5SAndreas Gohr /** 587c9966a5SAndreas Gohr * Reads the manager.dat file and fills the managerInfo array 597c9966a5SAndreas Gohr */ 607c9966a5SAndreas Gohr protected function readFile() 617c9966a5SAndreas Gohr { 627c9966a5SAndreas Gohr $data = []; 637c9966a5SAndreas Gohr if (!is_readable($this->path)) return $data; 647c9966a5SAndreas Gohr 657c9966a5SAndreas Gohr $file = (array)@file($this->path); 667c9966a5SAndreas Gohr foreach ($file as $line) { 677c9966a5SAndreas Gohr [$key, $value] = sexplode('=', $line, 2, ''); 687c9966a5SAndreas Gohr $key = trim($key); 697c9966a5SAndreas Gohr $value = trim($value); 707c9966a5SAndreas Gohr // backwards compatible with old plugin manager 717c9966a5SAndreas Gohr if ($key == 'url') $key = 'downloadurl'; 727c9966a5SAndreas Gohr $data[$key] = $value; 737c9966a5SAndreas Gohr } 747c9966a5SAndreas Gohr 757c9966a5SAndreas Gohr return $data; 767c9966a5SAndreas Gohr } 77160d3688SAndreas Gohr 78*8fe483c9SAndreas Gohr /** 79*8fe483c9SAndreas Gohr * @return \DateTime|null 80*8fe483c9SAndreas Gohr */ 81160d3688SAndreas Gohr public function getLastUpdate() 82160d3688SAndreas Gohr { 83*8fe483c9SAndreas Gohr $date = $this->data['updated'] ?? $this->data['installed'] ?? ''; 84*8fe483c9SAndreas Gohr if (!$date) return null; 85*8fe483c9SAndreas Gohr try { 86*8fe483c9SAndreas Gohr return new \DateTime($date); 87*8fe483c9SAndreas Gohr } catch (\Exception $e) { 88*8fe483c9SAndreas Gohr return null; 89*8fe483c9SAndreas Gohr } 90160d3688SAndreas Gohr } 91160d3688SAndreas Gohr 924fd6a1d7SAndreas Gohr public function getDownloadURL() 93160d3688SAndreas Gohr { 94160d3688SAndreas Gohr return $this->data['downloadurl'] ?? ''; 95160d3688SAndreas Gohr } 96160d3688SAndreas Gohr 97*8fe483c9SAndreas Gohr /** 98*8fe483c9SAndreas Gohr * @return \DateTime|null 99*8fe483c9SAndreas Gohr */ 100160d3688SAndreas Gohr public function getInstallDate() 101160d3688SAndreas Gohr { 102*8fe483c9SAndreas Gohr $date = $this->data['installed'] ?? ''; 103*8fe483c9SAndreas Gohr if (!$date) return null; 104*8fe483c9SAndreas Gohr try { 105*8fe483c9SAndreas Gohr return new \DateTime($date); 106*8fe483c9SAndreas Gohr } catch (\Exception $e) { 107*8fe483c9SAndreas Gohr return null; 108*8fe483c9SAndreas Gohr } 109160d3688SAndreas Gohr } 1107c9966a5SAndreas Gohr} 111