1<?php
2
3/**
4 * DokuWiki Plugin doxycode (Remote Component)
5 *
6 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author      Lukas Probsthain <lukas.probsthain@gmail.com>
8 */
9
10use dokuwiki\Extension\RemotePlugin;
11use dokuwiki\Remote\AccessDeniedException;
12
13/**
14 * Class remote_plugin_doxycode
15 *
16 * This remote component implements the following methods for the dokuwiki remote API:
17 * - receive the current tag file configuration of doxycode
18 * - upload a doxygen tag file (e.g. from a CI/CD pipeline)
19 *
20 * @author      Lukas Probsthain <lukas.probsthain@gmail.com>
21 */
22class remote_plugin_doxycode extends RemotePlugin
23{
24    /**
25     * Returns details about the remote plugin methods
26     *
27     * @return array Information about all provided methods. {@see dokuwiki\Remote\RemoteAPI}
28     */
29    public function _getMethods()
30    {
31        return array(
32            'listTagFiles' => array(
33                'args' => array(),
34                'return' => 'Array of Tag File Configurations',
35                'doc' => 'Get the current tag file configuration of doxycode.',
36            ),'uploadTagFile' => array(
37                'args' => array('string', 'string'),
38                'return' => 'bool',
39                'name' => 'uploadTagFile',
40                'doc' => 'Upload a tag file to the tag file directory.'
41            ),
42        );
43    }
44
45    /**
46     * List all Tag File Configurations for Doxycode
47     *
48     * @return Array Doxyoce tag file configuration
49     */
50    public function listTagFiles()
51    {
52        /** @var helper_plugin_doxycode_tagmanager $tagmanager */
53        $tagmanager = plugin_load('helper', 'doxycode_tagmanager');
54
55        $tag_config = $tagmanager->loadTagFileConfig();
56
57        return $tag_config;
58    }
59
60    /**
61     * Upload a Doxycode Tag File
62     *
63     * The tag file will only be accepted if a configuration exists for it and if it is enabled.
64     * Uploads for remote tag files will not be accepted
65     *
66     * @param string $filename The filename of the doxygen tag file
67     * @param string $file Contents of the doxygen tag file
68     * @return bool If the upload was succesful
69     */
70    public function uploadTagFile($filename, $file)
71    {
72        $tagname = pathinfo($filename, PATHINFO_FILENAME);
73
74        /** @var helper_plugin_doxycode_tagmanager $tagmanager */
75        $tagmanager = plugin_load('helper', 'doxycode_tagmanager');
76
77        $tag_config = $tagmanager->loadTagFileConfig();
78
79        // filter out disabled items
80        $tag_config = $tagmanager->filterConfig($tag_config, ['isConfigEnabled']);
81
82        // filter out remote configurations (we do not allow uploading them)
83        $tag_config = $tagmanager->filterConfig($tag_config, ['isValidRemoteConfig'], true);
84
85        // check file against existing configuration
86        if (!in_array($tagname, array_keys($tag_config))) {
87            return array(false);
88        }
89
90        // we have a valid configuration
91
92        // TODO: should we always update the file?
93        // we could also check if the file is updated, so mtime is not update
94        // this way the cache does not get invalidated!
95        $existing_hash = '';
96
97        $existing_file = $tagmanager->getFileName($tagname);
98        if (file_exists($existing_file)) {
99            $existing_content = @file_get_contents($existing_file);
100
101            $existing_hash = md5($existing_content);
102        }
103        $new_hash = md5($file);
104
105        if ($existing_hash !== $new_hash) {
106            // move file into position
107
108            // TODO: we should also check if we have a valid tag file on hand!
109            // possibilities: parse XML, check project name (make setup more complicated!)
110
111            @file_put_contents($existing_file, $file);
112        }
113
114        return array(true);
115    }
116}
117