xref: /plugin/combo/cli.php (revision 325fe0c5d5016188ce55114b3b4989c2bac99858)
1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12if (!defined('DOKU_INC')) die();
13
14use ComboStrap\Analytics;
15use ComboStrap\Page;
16use ComboStrap\PluginUtility;
17use ComboStrap\Sqlite;
18use splitbrain\phpcli\Options;
19
20require_once(__DIR__ . '/class/Analytics.php');
21
22/**
23 * The memory of the server 128 is not enough
24 */
25ini_set('memory_limit', '256M');
26
27/**
28 * Class cli_plugin_combo
29 *
30 * This is a cli:
31 * https://www.dokuwiki.org/devel:cli_plugins#example
32 *
33 * Usage:
34 *
35 * ```
36 * docker exec -ti $(CONTAINER) /bin/bash
37 * ./bin/plugin.php combo -c
38 * ```
39 * or via the IDE
40 *
41 *
42 * Example:
43 * https://www.dokuwiki.org/tips:grapher
44 *
45 */
46class cli_plugin_combo extends DokuWiki_CLI_Plugin
47{
48    const ANALYTICS = "analytics";
49    const SYNC = "sync";
50
51    /**
52     * register options and arguments
53     * @param Options $options
54     */
55    protected function setup(Options $options)
56    {
57        $options->setHelp(
58            "Manage the analytics database\n\n" .
59            "analytics\n" .
60            "sync"
61        );
62        $options->registerOption('version', 'print version', 'v');
63        $options->registerCommand(self::ANALYTICS, "Update the analytics data");
64        $options->registerOption(
65            'namespaces',
66            "If no namespace is given, the root namespace is assumed.",
67            'n',
68            true
69        );
70        $options->registerOption(
71            'output',
72            "Optional, where to store the analytical data as csv eg. a filename.",
73            'o', 'file');
74        $options->registerOption(
75            'cache',
76            "Optional, returns from the cache if set",
77            'c', false);
78        $options->registerOption(
79            'dry',
80            "Optional, dry-run",
81            'd', false);
82        $options->registerCommand(self::SYNC, "Sync the database");
83
84    }
85
86    /**
87     * The main entry
88     * @param Options $options
89     */
90    protected function main(Options $options)
91    {
92
93        $namespaces = array_map('cleanID', $options->getArgs());
94        if (!count($namespaces)) $namespaces = array(''); //import from top
95
96        $cache = $options->getOpt('cache', false);
97        $depth = $options->getOpt('depth', 0);
98        switch ($options->getCmd()) {
99            case self::ANALYTICS:
100                $output = $options->getOpt('output', '');
101                //if ($output == '-') $output = 'php://stdout';
102                $this->updateAnalyticsData($namespaces, $output, $cache, $depth);
103                break;
104            case self::SYNC:
105                $this->syncPages();
106                break;
107            default:
108                throw new \RuntimeException("Command unknown (" . $options->getCmd() . ")");
109        }
110
111
112    }
113
114    /**
115     * @param array $namespaces
116     * @param $output
117     * @param bool $cache
118     * @param int $depth recursion depth. 0 for unlimited
119     */
120    private function updateAnalyticsData($namespaces = array(), $output = null, $cache = false, $depth = 0)
121    {
122
123        $fileHandle = null;
124        if (!empty($output)) {
125            $fileHandle = @fopen($output, 'w');
126            if (!$fileHandle) $this->fatal("Failed to open $output");
127        }
128
129        $pages = $this->findPages($namespaces, $depth);
130
131
132        if (!empty($fileHandle)) {
133            $header = array(
134                'id',
135                'backlinks',
136                'broken_links',
137                'changes',
138                'chars',
139                'external_links',
140                'external_medias',
141                'h1',
142                'h2',
143                'h3',
144                'h4',
145                'h5',
146                'internal_links',
147                'internal_medias',
148                'words',
149                'score'
150            );
151            fwrite($fileHandle, implode(",", $header) . PHP_EOL);
152        }
153        while ($page = array_shift($pages)) {
154            $id = $page['id'];
155
156
157            echo 'Processing the page ' . $id . "\n";
158
159            $data = Analytics::processAndGetDataAsArray($id, $cache);
160            if (!empty($fileHandle)) {
161                $statistics = $data[Analytics::STATISTICS];
162                $row = array(
163                    'id' => $id,
164                    'backlinks' => $statistics[Analytics::INTERNAL_BACKLINKS_COUNT],
165                    'broken_links' => $statistics[Analytics::INTERNAL_LINKS_BROKEN_COUNT],
166                    'changes' => $statistics[Analytics::EDITS_COUNT],
167                    'chars' => $statistics[Analytics::CHARS_COUNT],
168                    'external_links' => $statistics[Analytics::EXTERNAL_LINKS_COUNT],
169                    'external_medias' => $statistics[Analytics::EXTERNAL_MEDIAS],
170                    'h1' => $statistics[Analytics::HEADERS_COUNT]['h1'],
171                    'h2' => $statistics[Analytics::HEADERS_COUNT]['h2'],
172                    'h3' => $statistics[Analytics::HEADERS_COUNT]['h3'],
173                    'h4' => $statistics[Analytics::HEADERS_COUNT]['h4'],
174                    'h5' => $statistics[Analytics::HEADERS_COUNT]['h5'],
175                    'internal_links' => $statistics[Analytics::INTERNAL_LINKS_COUNT],
176                    'internal_medias' => $statistics[Analytics::INTERNAL_MEDIAS_COUNT],
177                    'words' => $statistics[Analytics::WORDS_COUNT],
178                    'low' => $data[Analytics::QUALITY]['low']
179                );
180                fwrite($fileHandle, implode(",", $row) . PHP_EOL);
181            }
182        }
183        if (!empty($fileHandle)) {
184            fclose($fileHandle);
185        }
186
187    }
188
189    /**
190     * Find the pages in the tree
191     * @param $namespaces
192     * @param $depth
193     * @return array
194     */
195    private function findPages($namespaces = array(), $depth = 0)
196    {
197        // Run as admin to overcome the fact that
198        // anonymous user cannot set all links and backlinnks
199
200
201        global $conf;
202        $datadir = $conf['datadir'];
203
204        /**
205         * Run as admin to overcome the fact that
206         * anonymous user cannot see all links and backlinnks
207         */
208        global $USERINFO;
209        $USERINFO['grps'] = array('admin');
210        global $INPUT;
211        $INPUT->server->set('REMOTE_USER', "cli");
212
213        $pages = array();
214        foreach ($namespaces as $ns) {
215
216            search(
217                $pages,
218                $datadir,
219                'search_universal',
220                array(
221                    'depth' => $depth,
222                    'listfiles' => true,
223                    'listdirs' => false,
224                    'pagesonly' => true,
225                    'skipacl' => true,
226                    'firsthead' => false,
227                    'meta' => false,
228                ),
229                str_replace(':', '/', $ns)
230            );
231
232            // add the ns start page
233            if ($ns && page_exists($ns)) {
234                $pages[] = array(
235                    'id' => $ns,
236                    'ns' => getNS($ns),
237                    'title' => p_get_first_heading($ns, false),
238                    'size' => filesize(wikiFN($ns)),
239                    'mtime' => filemtime(wikiFN($ns)),
240                    'perm' => 16,
241                    'type' => 'f',
242                    'level' => 0,
243                    'open' => 1,
244                );
245            }
246
247        }
248        return $pages;
249    }
250
251    private function syncPages()
252    {
253        $sqlite = Sqlite::getSqlite();
254        $res = $sqlite->query("select ID from pages");
255        if (!$res) {
256            throw new \RuntimeException("An exception has occurred with the alias selection query");
257        }
258        $res2arr = $sqlite->res2arr($res);
259        $sqlite->res_close($res);
260        foreach ($res2arr as $row) {
261            $id = $row['ID'];
262            if (!page_exists($id)) {
263                echo 'Page does not exist on the file system. Deleted from the database (' . $id . ")\n";
264                Page::createFromId($id)->deleteInDb();
265            }
266        }
267
268
269    }
270}
271