1<?php
2
3use ComboStrap\CacheLog;
4use ComboStrap\DataType;
5use ComboStrap\Event;
6use ComboStrap\ExceptionBadArgument;
7use ComboStrap\ExceptionNotExists;
8use ComboStrap\LogUtility;
9use ComboStrap\LowQualityCalculatedIndicator;
10use ComboStrap\LowQualityPageOverwrite;
11use ComboStrap\MarkupPath;
12use ComboStrap\MetadataMutation;
13use ComboStrap\PagePath;
14use ComboStrap\Site;
15
16
17/**
18 * Delete the backlinks when there is a page quality mutation
19 */
20class action_plugin_combo_qualitymutation extends DokuWiki_Action_Plugin
21{
22
23
24    public const QUALITY_MUTATION_EVENT_NAME = 'quality_mutation';
25    const CANONICAL = "low_quality";
26    const DESC = "desc";
27
28    public static function getQualityMetas(): array
29    {
30        return [
31            LowQualityCalculatedIndicator::getPersistentName(),
32            LowQualityPageOverwrite::getPersistentName()
33        ];
34    }
35
36
37    public function register(Doku_Event_Handler $controller)
38    {
39
40
41        /**
42         * create the async event
43         */
44        $controller->register_hook(MetadataMutation::PAGE_METADATA_MUTATION_EVENT, 'AFTER', $this, 'create_quality_mutation', array());
45
46        /**
47         * process the Async event
48         */
49        $controller->register_hook(self::QUALITY_MUTATION_EVENT_NAME, 'AFTER', $this, 'handle_quality_mutation');
50
51
52    }
53
54
55    public function handle_quality_mutation(Doku_Event $event, $param)
56    {
57
58
59        $data = $event->data;
60        $path = $data[PagePath::getPersistentName()];
61        $page = MarkupPath::createPageFromAbsoluteId($path);
62
63        if (!$page->getCanBeOfLowQuality()) {
64            return;
65        }
66
67        /**
68         * Delete the html document cache to rewrite the links
69         *
70         */
71        foreach ($page->getBacklinks() as $backlink) {
72            try {
73                $htmlDocument = $backlink->createHtmlFetcherWithItselfAsContextPath();
74            } catch (ExceptionNotExists $e) {
75                continue;
76            }
77            try {
78                $wikiId = $backlink->getWikiId();
79            } catch (ExceptionBadArgument $e) {
80                LogUtility::internalError("Backlink should be only for wiki path");
81                continue;
82            }
83            $desc = $data[self::DESC];
84            CacheLog::deleteCacheIfExistsAndLog(
85                $htmlDocument,
86                self::QUALITY_MUTATION_EVENT_NAME,
87                "The {$wikiId} of {$path} had its HTML cache deleted ($desc)."
88            );
89
90        }
91    }
92
93
94    /**
95     */
96    function create_quality_mutation(Doku_Event $event, $params): void
97    {
98
99        if (!Site::isLowQualityProtectionEnable()) {
100            return;
101        }
102
103        /**
104         * If this is not a mutation on references we return.
105         */
106        $data = $event->data;
107        $variableName = $data["name"];
108        if (!(in_array($variableName, self::getQualityMetas()))) {
109            return;
110        }
111
112        $newValue = DataType::toString($data[MetadataMutation::NEW_VALUE_ATTRIBUTE]);
113        $oldValue = DataType::toString($data[MetadataMutation::OLD_VALUE_ATTRIBUTE]);
114        $path = $data[PagePath::getPersistentName()];
115        Event::createEvent(
116            self::QUALITY_MUTATION_EVENT_NAME,
117            [
118                PagePath::getPersistentName() => $path,
119                self::DESC => "The variable ($variableName) has the new value ($newValue) overriding ($oldValue)"
120            ]
121        );
122
123
124    }
125
126}
127
128
129
130