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 */
12
13namespace ComboStrap;
14
15
16use renderer_plugin_combo_analytics;
17
18class Analytics
19{
20
21
22
23    const DATE_MODIFIED = 'date_modified';
24    /**
25     * Constant in Key or value
26     */
27    const HEADER_POSITION = 'header_id';
28    const INTERNAL_BACKLINK_COUNT = "internal_backlink_count";
29    const WORD_COUNT = 'word_count';
30    const INTERNAL_LINK_DISTANCE = 'internal_link_distance';
31    const INTERWIKI_LINK_COUNT = "interwiki_link_count";
32    const EDITS_COUNT = 'edits_count';
33    const INTERNAL_LINK_BROKEN_COUNT = 'internal_broken_link_count';
34    const TITLE = 'title';
35    const INTERNAL_LINK_COUNT = 'internal_link_count';
36    const LOCAL_LINK_COUNT = "local_link_count"; // ie fragment #hallo
37    const EXTERNAL_MEDIA_COUNT = 'external_media_count';
38    const CHAR_COUNT = 'char_count';
39    const MEDIA_COUNT = 'media_count';
40    const INTERNAL_MEDIA_COUNT = 'internal_media_count';
41    const INTERNAL_BROKEN_MEDIA_COUNT = 'internal_broken_media_count';
42    const EXTERNAL_LINK_COUNT = 'external_link_count';
43    const HEADING_COUNT = 'heading_count';
44    const SYNTAX_COUNT = "syntax_count";
45    const QUALITY = 'quality';
46    const STATISTICS = "statistics";
47    const EMAIL_COUNT = "email_count";
48    const WINDOWS_SHARE_COUNT = "windows_share_count";
49
50    /**
51     * An array of info for errors mostly
52     */
53    const INFO = "info";
54    const H1 = "h1";
55    const LOW = "low";
56    const RULES = "rules";
57    const DETAILS = 'details';
58    const FAILED_MANDATORY_RULES = 'failed_mandatory_rules';
59    const NAME = "name";
60    const PATH = "path";
61    const DESCRIPTION = "description";
62    const METADATA = 'metadata';
63    const CANONICAL = "canonical";
64    const DATE_CREATED = 'date_created';
65    const DATE_END = "date_end";
66    const DATE_START = "date_start";
67
68
69    /**
70     * @param $pageId
71     * @param bool $cache - if true, the data is returned from the cache
72     * @return mixed
73     * The p_render function was stolen from the {@link p_cached_output} function
74     * used the in the switch of the {@link \dokuwiki\Action\Export::preProcess()} function
75     */
76    public static function processAndGetDataAsJson($pageId, $cache = false)
77    {
78
79        return json_decode(self::processAndGetDataAsString($pageId, $cache));
80
81    }
82
83    /**
84     * @param $pageId
85     * @param bool $cache - if true, the analytics will be cached or retrieved from cache
86     * @return bool|string|null
87     */
88    static function processAndGetDataAsString($pageId, $cache = false)
89    {
90
91        global $ID;
92        $oldId = $ID;
93        $ID = $pageId;
94        if (!$cache) {
95            $file = wikiFN($pageId);
96            if (file_exists($file)) {
97                $content = file_get_contents($file);
98                $instructions = RenderUtility::getInstructionsAndStripPEventually($content,false);
99                return p_render(renderer_plugin_combo_analytics::RENDERER_NAME_MODE, $instructions, $info);
100            } else {
101                return false;
102            }
103        } else {
104            $result = p_cached_output(wikiFN($pageId, 0), renderer_plugin_combo_analytics::RENDERER_NAME_MODE, $pageId);
105        }
106        $ID = $oldId;
107        return $result;
108
109    }
110
111    public static function processAndGetDataAsArray($pageId, $cache = false)
112    {
113
114        return json_decode(self::processAndGetDataAsString($pageId, $cache),true);
115
116    }
117
118
119
120
121}
122