1<?php
2
3namespace ComboStrap;
4
5use ComboStrap\Api\QualityMessageHandler;
6use renderer_plugin_combo_analytics;
7
8class QualityTag
9{
10
11    public const MARKUP_TAG = "quality";
12    const CANONICAL = ":dynamic-quality-monitoring";
13
14    /**
15     * @param WikiPath $wikiPath
16     * @return Message
17     */
18    public static function createQualityReport(WikiPath $wikiPath): Message
19    {
20
21        if (!FileSystems::exists($wikiPath)) {
22            return Message::createInfoMessage("The resource ($wikiPath) does not exist, no quality report can be computed.");
23        }
24
25        try {
26            $path = MarkupPath::createPageFromPathObject($wikiPath)->fetchAnalyticsPath();
27            $analyticsArray = Json::createFromPath($path)->toArray();
28        } catch (ExceptionCompile $e) {
29            return Message::createErrorMessage("Error while trying to read the JSON analytics document. {$e->getMessage()}")
30                ->setStatus(HttpResponseStatus::INTERNAL_ERROR);
31        }
32
33        $rules = $analyticsArray[renderer_plugin_combo_analytics::QUALITY][renderer_plugin_combo_analytics::RULES];
34
35        /**
36         * We may got null
37         * array_key_exists() expects parameter 2 to be array,
38         * null given in /opt/www/datacadamia.com/lib/plugins/combo/action/qualitymessage.php on line 113
39         */
40        if ($rules == null) {
41            return Message::createInfoMessage("No rules found in the analytics document");
42        }
43
44        /**
45         * If there is no info, nothing to show
46         */
47        if (!array_key_exists(renderer_plugin_combo_analytics::INFO, $rules)) {
48            return Message::createInfoMessage("No quality rules information to show");
49        }
50
51        /**
52         * The error info
53         */
54        $qualityInfoRules = $rules[renderer_plugin_combo_analytics::INFO];
55
56        /**
57         * Excluding the excluded rules
58         */
59
60        $excludedRulesConf =ExecutionContext::getActualOrCreateFromEnv()->getConfig()->getValue(QualityMessageHandler::CONF_EXCLUDED_QUALITY_RULES_FROM_DYNAMIC_MONITORING);
61        $excludedRules = preg_split("/,/", $excludedRulesConf);
62        foreach ($excludedRules as $excludedRule) {
63            if (array_key_exists($excludedRule, $qualityInfoRules)) {
64                unset($qualityInfoRules[$excludedRule]);
65            }
66        }
67
68        if (sizeof($qualityInfoRules) <= 0) {
69            return Message::createInfoMessage("No quality rules information to show");
70        }
71
72        $qualityScore = $analyticsArray[renderer_plugin_combo_analytics::QUALITY][renderer_plugin_combo_analytics::SCORING][renderer_plugin_combo_analytics::SCORE];
73        $message = "<p>The page has a " . PluginUtility::getDocumentationHyperLink("quality:score", "quality score") . " of {$qualityScore}.</p>";
74
75        $lowQuality = $analyticsArray[renderer_plugin_combo_analytics::QUALITY][renderer_plugin_combo_analytics::LOW];
76        if ($lowQuality) {
77
78            $mandatoryFailedRules = $analyticsArray[renderer_plugin_combo_analytics::QUALITY][renderer_plugin_combo_analytics::FAILED_MANDATORY_RULES];
79            $rulesUrl = PluginUtility::getDocumentationHyperLink("quality:rule", "rules");
80            $lqPageUrl = PluginUtility::getDocumentationHyperLink("low_quality_page", "low quality page");
81            $message .= "<div class='alert alert-warning'>This is a {$lqPageUrl} because it has failed the following mandatory {$rulesUrl}:";
82            $message .= "<ul style='margin-bottom: 0'>";
83            /**
84             * A low quality page should have
85             * failed mandatory rules
86             * but due to the asycn nature, sometimes
87             * we don't have an array
88             */
89            if (is_array($mandatoryFailedRules)) {
90                foreach ($mandatoryFailedRules as $mandatoryFailedRule) {
91                    $message .= "<li>" . PluginUtility::getDocumentationHyperLink("quality:rule#list", $mandatoryFailedRule) . "</li>";
92                }
93            }
94            $message .= "</ul>";
95            $message .= "</div>";
96        }
97        $message .= "<p>You can still win a couple of points.</p>";
98        $message .= "<ul>";
99        foreach ($qualityInfoRules as $qualityRule => $qualityInfo) {
100            $message .= "<li>$qualityInfo</li>";
101        }
102        $message .= "</ul>";
103
104        $page = MarkupPath::createPageFromPathObject($wikiPath);
105        $qualityMonitoringIndicator = QualityDynamicMonitoringOverwrite::createFromPage($page)->getValueOrDefault();
106        if (!$qualityMonitoringIndicator) {
107            $docLink = PluginUtility::getDocumentationHyperLink(self::CANONICAL, "configuration");
108            $message .= "<p>This page is not quality monitored due its $docLink.</p>";
109        }
110        return Message::createInfoMessage($message);
111
112    }
113
114    public static function renderXhtml(TagAttributes $tagAttributes): string
115    {
116        $pageId = $tagAttributes->getComponentAttributeValue("page-id");
117        if (empty($pageId)) {
118            $contextPath = ExecutionContext::getActualOrCreateFromEnv()->getContextPath();
119        } else {
120            $contextPath = WikiPath::createMarkupPathFromId($pageId);
121        }
122        $message = QualityTag::createQualityReport($contextPath);
123        return $message->getContent();
124
125
126    }
127}
128