xref: /plugin/combo/action/analytics.php (revision f3748b38fbeea8fd460b3c8b25641c56e84a356c)
1<?php
2
3use ComboStrap\Analytics;
4use Combostrap\AnalyticsMenuItem;
5use ComboStrap\Auth;
6use ComboStrap\LogUtility;
7use ComboStrap\Page;
8use ComboStrap\Sqlite;
9
10/**
11 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
12 *
13 * This source code is licensed under the GPL license found in the
14 * COPYING  file in the root directory of this source tree.
15 *
16 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
17 * @author   ComboStrap <support@combostrap.com>
18 *
19 */
20
21require_once(__DIR__ . '/../class/'.'Analytics.php');
22require_once(__DIR__ . '/../class/'.'Auth.php');
23require_once(__DIR__ . '/../class/'.'AnalyticsMenuItem.php');
24
25/**
26 * Class action_plugin_combo_analytics
27 * Update the analytics data
28 */
29class action_plugin_combo_analytics extends DokuWiki_Action_Plugin
30{
31
32
33    public function register(Doku_Event_Handler $controller)
34    {
35
36        /**
37         * Analytics to refresh because they have lost or gain a backlinks
38         * are done via Sqlite table (The INDEXER_TASKS_RUN gives a way to
39         * manipulate this queue)
40         *
41         * There is no need to do it at page write
42         * https://www.dokuwiki.org/devel:event:io_wikipage_write
43         * because after the page is written, the page is shown and trigger the index tasks run
44         */
45        $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'handle_refresh_analytics', array());
46
47        /**
48         * Add a icon in the page tools menu
49         * https://www.dokuwiki.org/devel:event:menu_items_assembly
50         */
51        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'handle_page_tools');
52
53    }
54
55    public function handle_refresh_analytics(Doku_Event $event, $param)
56    {
57
58        /**
59         * Check that the actual page has analytics data
60         * (if there is a cache, it's pretty quick)
61         */
62        global $ID;
63        Analytics::process($ID,true);
64
65        /**
66         * Check the analytics to refresh
67         */
68        $sqlite = Sqlite::getSqlite();
69        $res = $sqlite->query("SELECT ID FROM ANALYTICS_TO_REFRESH");
70        if (!$res) {
71            LogUtility::msg("There was a problem during the select: {$sqlite->getAdapter()->getDb()->errorInfo()}");
72        }
73        $rows = $sqlite->res2arr($res,true);
74        $sqlite->res_close($res);
75        foreach($rows as $row){
76            $page = new Page($row['ID']);
77            $page->refreshAnalytics();
78        }
79
80    }
81
82    public function handle_page_tools(Doku_Event $event, $param){
83
84        if (!Auth::isLoggedIn()){
85            return;
86        }
87
88        /**
89         * The `view` property defines the menu that is currently built
90         * https://www.dokuwiki.org/devel:menus
91         * If this is not the page menu, return
92         */
93        if($event->data['view'] != 'page') return;
94
95        global $INFO;
96        if(!$INFO['exists']) {
97            return;
98        }
99        array_splice($event->data['items'], -1, 0, array(new AnalyticsMenuItem()));
100
101    }
102}
103
104
105
106