summaryrefslogtreecommitdiff
path: root/lib/plugins/extension/helper/extension.php
blob: b8981cf914550b579e4bc29fb988a753740fca6b (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/**
 * DokuWiki Plugin extension (Helper Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Michael Hamann <michael@content-space.de>
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

/**
 * Class helper_plugin_extension_extension represents a single extension (plugin or template)
 */
class helper_plugin_extension_extension extends DokuWiki_Plugin {
    private $name;
    private $is_template;
    private $localInfo;
    private $remoteInfo;
    private $managerData;
    /** @var helper_plugin_extension_repository $repository */
    private $repository = null;

    /**
     * @return bool false, this component is not a singleton
     */
    public function isSingleton() {
        return false;
    }

    /**
     * Set the name of the extension this instance shall represents, triggers loading the local and remote data
     *
     * @param string $name        The base name of the extension
     * @param bool   $is_template If the extension is a template
     * @return bool If some (local or remote) data was found
     */
    public function setExtension($name, $is_template) {
        $this->name = $name;
        $this->is_template = $is_template;
        $this->localInfo = array();
        $this->managerData = array();
        $this->remoteInfo = array();

        if ($this->isInstalled()) {
            if ($this->isTemplate()) {
                $infopath = $this->getInstallDir().'/template.info.txt';
            } else {
                $infopath = $this->getInstallDir().'/plugin.info.txt';
            }
            if (is_readable($infopath)) {
                $this->localInfo = confToHash($infopath);
            }

            $this->readManagerData();
        }

        if ($this->repository == null) {
            $this->repository = $this->loadHelper('extension_repository');
        }

        $this->remoteInfo = $this->repository->getData(($this->isTemplate() ? 'template:' : '').$this->getBase());
    }

    /**
     * If the extension is installed locally
     *
     * @return bool If the extension is installed locally
     */
    public function isInstalled() {
        return is_dir($this->getInstallDir());
    }

    /**
     * If the extension is enabled
     *
     * @return bool If the extension is enabled
     */
    public function isEnabled() {
        /* @var Doku_Plugin_Controller $plugin_controller */
        global $plugin_controller;
        return !$plugin_controller->isdisabled($this->name);
    }

    /**
     * If the extension should be updated, i.e. if an updated version is available
     *
     * @return bool If an update is available
     */
    public function updateAvailable() {
        $lastupdate = $this->getLastUpdate();
        if ($lastupdate === false) return false;
        return $this->getInstalledVersion() < $this->getLastUpdate();
    }

    /**
     * If the extension is a template
     *
     * @return bool If this extension is a template
     */
    public function isTemplate() {
        return $this->is_template;
    }

    // Data from plugin.info.txt/template.info.txt or the repo when not available locally
    /**
     * Get the basename of the extension
     *
     * @return string The basename
     */
    public function getBase() {
        return $this->name;
    }

    /**
     * Get the display name of the extension
     *
     * @return string The display name
     */
    public function getName() {
        if (isset($this->localInfo['name'])) return $this->localInfo['name'];
        if (isset($this->remoteInfo['name'])) return $this->remoteInfo['name'];
        return $this->name;
    }

    /**
     * Get the author name of the extension
     *
     * @return string The name of the author
     */
    public function getAuthor() {
        if (isset($this->localInfo['author'])) return $this->localInfo['author'];
        if (isset($this->remoteInfo['author'])) return $this->remoteInfo['author'];
        return $this->getLang('unknownauthor');
    }

    /**
     * Get the email of the author of the extension if there is any
     *
     * @return string|bool The email address or false if there is none
     */
    public function getEmail() {
        // email is only in the local data
        if (isset($this->localInfo['email'])) return $this->localInfo['email'];
        return false;
    }

    /**
     * Get the email id, i.e. the md5sum of the email
     *
     * @return string|bool The md5sum of the email if there is any, false otherwise
     */
    public function getEmailID() {
        if (isset($this->remoteInfo['emailid'])) return $this->remoteInfo['emailid'];
        if (isset($this->localInfo['email'])) return md5($this->localInfo['email']);
        return false;
    }

    /**
     * Get the description of the extension
     *
     * @return string The description
     */
    public function getDescription() {
        if (isset($this->localInfo['desc'])) return $this->localInfo['desc'];
        if (isset($this->remoteInfo['description'])) return $this->remoteInfo['description'];
        return '';
    }

    /**
     * Get the URL of the extension, usually a page on dokuwiki.org
     *
     * @return string The URL
     */
    public function getURL() {
        if (isset($this->localInfo['url'])) return $this->localInfo['url'];
        return 'https://www.dokuwiki.org/plugin:'.$this->name;
    }

    /**
     * Get the installed version of the extension
     *
     * @return string|bool The version, usually in the form yyyy-mm-dd if there is any
     */
    public function getInstalledVersion() {
        if (isset($this->localInfo['date'])) return $this->localInfo['date'];
        if ($this->isInstalled()) return $this->getLang('unknownversion');
        return false;
    }

    /**
     * Get the names of the dependencies of this extension
     *
     * @return array The base names of the dependencies
     */
    public function getDependencies() {
        if (isset($this->remoteInfo['dependencies'])) return $this->remoteInfo['dependencies'];
        return array();
    }

    /**
     * Get the names of all conflicting extensions
     *
     * @return array The names of the conflicting extensions
     */
    public function getConflicts() {
        if (isset($this->remoteInfo['conflicts'])) return $this->remoteInfo['dependencies'];
        return array();
    }

    /**
     * Get the names of similar extensions
     *
     * @return array The names of similar extensions
     */
    public function getSimilarExtensions() {
        if (isset($this->remoteInfo['similar'])) return $this->remoteInfo['similar'];
        return array();
    }

    /**
     * Get the names of the tags of the extension
     *
     * @return array The names of the tags of the extension
     */
    public function getTags() {
        if (isset($this->remoteInfo['tags'])) return $this->remoteInfo['tags'];
        return array();
    }

    /**
     * Get the text of the security warning if there is any
     *
     * @return string|bool The security warning if there is any, false otherwise
     */
    public function getSecurityWarning() {
        if (isset($this->remoteInfo['securitywarning'])) return $this->remoteInfo['securitywarning'];
        return false;
    }

    /**
     * Get the text of the security issue if there is any
     *
     * @return string|bool The security issue if there is any, false otherwise
     */
    public function getSecurityIssue() {
        if (isset($this->remoteInfo['securityissue'])) return $this->remoteInfo['securityissue'];
        return false;
    }

    /**
     * Get the URL of the screenshot of the extension if there is any
     *
     * @return string|bool The screenshot URL if there is any, false otherwise
     */
    public function getScreenshotURL() {
        if (isset($this->remoteInfo['screenshoturl'])) return $this->remoteInfo['screenshoturl'];
        return false;
    }

    /**
     * Get the last used download URL of the extension if there is any
     *
     * @return string|bool The previously used download URL, false if the extension has been installed manually
     */
    public function getLastDownloadURL() {
        if (isset($this->managerData['downloadurl'])) return $this->managerData['downloadurl'];
        return false;
    }

    /**
     * Get the download URL of the extension if there is any
     *
     * @return string|bool The download URL if there is any, false otherwise
     */
    public function getDownloadURL() {
        if (isset($this->remoteInfo['downloadurl'])) return $this->remoteInfo['downloadurl'];
        return false;
    }

    /**
     * Get the bug tracker URL of the extension if there is any
     *
     * @return string|bool The bug tracker URL if there is any, false otherwise
     */
    public function getBugtrackerURL() {
        if (isset($this->remoteInfo['bugtracker'])) return $this->remoteInfo['bugtracker'];
        return false;
    }

    /**
     * Get the URL of the source repository if there is any
     *
     * @return string|bool The URL of the source repository if there is any, false otherwise
     */
    public function getSourcerepoURL() {
        if (isset($this->remoteInfo['sourcerepo'])) return $this->remoteInfo['sourcerepo'];
        return false;
    }

    /**
     * Get the donation URL of the extension if there is any
     *
     * @return string|bool The donation URL if there is any, false otherwise
     */
    public function getDonationURL() {
        if (isset($this->remoteInfo['donationurl'])) return $this->remoteInfo['donationurl'];
        return false;
    }

    /**
     * Get the extension type(s)
     *
     * @return array The type(s) as array of strings
     */
    public function getTypes() {
        if (isset($this->remoteInfo['types'])) return explode(', ', $this->remoteInfo['types']);
        if ($this->isTemplate()) return array(32 => 'template');
        return array();
    }

    /**
     * Get a list of all DokuWiki versions this extension is compatible with
     *
     * @return array The versions in the form yyyy-mm-dd => ('label' => label, 'implicit' => implicit)
     */
    public function getCompatibleVersions() {
        if (isset($this->remoteInfo['compatible'])) return $this->remoteInfo['compatible'];
        return array();
    }

    /**
     * Get the date of the last available update
     *
     * @return string|bool The last available update in the form yyyy-mm-dd if there is any, false otherwise
     */
    public function getLastUpdate() {
        if (isset($this->remoteInfo['lastupdate'])) return $this->remoteInfo['lastupdate'];
        return false;
    }

    /**
     * Get the base path of the extension
     *
     * @return string The base path of the extension
     */
    public function getInstallDir() {
        if ($this->isTemplate()) {
            return basename(tpl_incdir()).$this->name;
        } else {
            return DOKU_PLUGIN.$this->name;
        }
    }

    /**
     * The type of extension installation
     *
     * @return string One of "none", "manual", "git" or "automatic"
     */
    public function getInstallType() {
        if (!$this->isInstalled()) return 'none';
        if (!empty($this->managerData)) return 'automatic';
        if (is_dir($this->getInstallDir().'/.git')) return 'git';
        return 'manual';
    }

    /**
     * If the extension can probably be installed/updated or uninstalled
     *
     * @return bool|string True or one of "nourl", "noparentperms" (template/plugin install path not writable), "noperms" (extension itself not writable)
     */
    public function canModify() {
    }

    /**
     * Install or update the extension
     *
     * @return bool|string True or an error message
     */
    public function installOrUpdate() {
    }

    /**
     * Uninstall the extension
     *
     * @return bool|string True or an error message
     */
    public function uninstall() {
    }

    /**
     * Enable the extension
     *
     * @return bool|string True or an error message
     */
    public function enable() {
        if ($this->isTemplate()) return $this->getLang('notimplemented');
        /* @var Doku_Plugin_Controller $plugin_controller */
        global $plugin_controller;
        if (!$this->isInstalled()) return $this->getLang('notinstalled');
        if (!$this->isEnabled()) return $this->getLang('alreadyenabled');
        if ($plugin_controller->enable($this->name)) {
            return true;
        } else {
            return $this->getLang('pluginlistsaveerror');
        }
    }

    /**
     * Disable the extension
     *
     * @return bool|string True or an error message
     */
    public function disable() {
        if ($this->isTemplate()) return $this->getLang('notimplemented');

        /* @var Doku_Plugin_Controller $plugin_controller */
        global $plugin_controller;
        if (!$this->isInstalled()) return $this->getLang('notinstalled');
        if (!$this->isEnabled()) return $this->getLang('alreadydisabled');
        if ($plugin_controller->disable($this->name)) {
            return true;
        } else {
            return $this->getLang('pluginlistsaveerror');
        }
    }

    /**
     * Read the manager.dat file
     */
    protected function readManagerData() {
        $managerpath = $this->getInstallDir().'/manager.dat';
        if (is_readable($managerpath)) {
            $file = @file($managerpath);
            if(!empty($file)) {
                foreach($file as $line) {
                    list($key, $value) = explode('=', trim($line, PHP_EOL), 2);
                    $key = trim($key);
                    $value = trim($value);
                    // backwards compatible with old plugin manager
                    if($key == 'url') $key = 'downloadurl';
                    $this->managerData[$key] = $value;
                }
            }
        }
    }

    /**
     * Write the manager.data file
     */
    protected function writeManagerData() {
        $managerpath = $this->getInstallDir().'/manager.dat';
        $data = '';
        foreach ($this->managerData as $k => $v) {
            $data .= $k.'='.$v.DOKU_LF;
        }
        io_saveFile($managerpath, $data);
    }
}

// vim:ts=4:sw=4:et: