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