This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
86 lines (72 loc) · 2.31 KB
/
helper.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
<?php
/**
* DokuWiki Plugin fksdownloader (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Michal Koutný <michal@fykos.cz>
*/
use dokuwiki\Extension\Plugin;
use Fykosak\FKSDBDownloaderCore\FKSDBDownloader;
use Fykosak\FKSDBDownloaderCore\Requests\Request;
if (!defined('DOKU_INC')) {
die();
}
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class helper_plugin_fksdbexport extends Plugin
{
public const EXPIRATION_FRESH = 0;
public const EXPIRATION_NEVER = 0x7fffffff;
private FKSDBDownloader $downloader;
/**
* @return FKSDBDownloader
* @throws SoapFault
*/
private function getSoap(): FKSDBDownloader
{
if (!isset($this->downloader)) {
$this->downloader = new FKSDBDownloader(
$this->getConf('wsdl'),
$this->getConf('fksdb_login'),
$this->getConf('fksdb_password')
);
}
return $this->downloader;
}
public function download(Request $request, int $expiration): ?string
{
$cached = $this->getFromCache($request->getCacheKey(), $expiration);
if (!$cached) {
try {
$content = $this->getSoap()->download($request);
} catch (Throwable$exception) {
msg($exception->getMessage());
return null;
}
if ($content) {
$this->putToCache($request->getCacheKey(), $content);
}
return $content;
} else {
return $cached;
}
}
private function getFromCache(string $filename, int $expiration): ?string
{
$realFilename = $this->getCacheFilename($filename);
if (file_exists($realFilename) && filemtime($realFilename) + $expiration >= time()) {
return io_readFile($realFilename);
} else {
return null;
}
}
private function putToCache(string $filename, string $content): void
{
$realFilename = $this->getCacheFilename($filename);
io_saveFile($realFilename, $content);
}
public function getCacheFilename(string $filename): string
{
$id = $this->getPluginName() . ':' . $filename;
return metaFN($id, '.xml');
}
}