1 <?php
2 
3 namespace ComboStrap\Api;
4 
5 use ComboStrap\ExceptionNotFound;
6 use ComboStrap\ExecutionContext;
7 use ComboStrap\HttpResponseStatus;
8 use ComboStrap\Identity;
9 use ComboStrap\Mime;
10 use ComboStrap\QualityTag;
11 use ComboStrap\WikiPath;
12 use dokuwiki\Extension\Event;
13 
14 /**
15  * Return the quality report in HTML
16  */
17 class QualityMessageHandler
18 {
19 
20     public const CALL_ID = "combo-quality-message";
21     public const CANONICAL = "quality:dynamic_monitoring";
22 
23     /**
24      * Disable the message totally
25      */
26     public const CONF_DISABLE_QUALITY_MONITORING = "disableDynamicQualityMonitoring";
27     /**
28      * The quality rules that will not show
29      * up in the messages
30      */
31     public const CONF_EXCLUDED_QUALITY_RULES_FROM_DYNAMIC_MONITORING = "excludedQualityRulesFromDynamicMonitoring";
32 
33 
34     public static function handle(Event $event)
35     {
36 
37         // no other ajax call handlers needed
38         $event->stopPropagation();
39         $event->preventDefault();
40 
41         $executingContext = ExecutionContext::getActualOrCreateFromEnv();
42 
43         /**
44          * Shared check between post and get HTTP method
45          */
46         /**
47          * Shared check between post and get HTTP method
48          */
49         try {
50             $id = ApiRouter::getRequestParameter("id");
51         } catch (ExceptionNotFound $e) {
52             $executingContext->response()
53                 ->setStatus(HttpResponseStatus::BAD_REQUEST)
54                 ->setEvent($event)
55                 ->setCanonical(QualityMessageHandler::CANONICAL)
56                 ->setBody("The page id should not be empty", Mime::getHtml())
57                 ->end();
58             return;
59         }
60 
61         /**
62          * Quality is just for the writers
63          */
64         if (!Identity::isWriter($id)) {
65             $executingContext->response()
66                 ->setStatus(HttpResponseStatus::NOT_AUTHORIZED)
67                 ->setEvent($event)
68                 ->setBody("Quality is only for writer", Mime::getHtml())
69                 ->end();
70             return;
71         }
72 
73 
74         $markupPath = WikiPath::createMarkupPathFromId($id);
75         $message = QualityTag::createQualityReport($markupPath);
76         $status = $message->getStatus();
77 
78 
79         $executingContext->response()
80             ->setStatus($status)
81             ->setEvent($event)
82             ->setCanonical(QualityMessageHandler::CANONICAL)
83             ->setBody($message->getContent(), Mime::getHtml())
84             ->end();
85     }
86 }
87