xref: /plugin/combo/action/backlinkmutation.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3use ComboStrap\ExceptionNotExists;
4use ComboStrap\MarkupCacheDependencies;
5use ComboStrap\CacheLog;
6use ComboStrap\CacheManager;
7use ComboStrap\Event;
8use ComboStrap\ExceptionCompile;
9use ComboStrap\FileSystems;
10use ComboStrap\LogUtility;
11use ComboStrap\Meta\Store\MetadataDokuWikiStore;
12use ComboStrap\MarkupPath;
13use ComboStrap\MetadataMutation;
14use ComboStrap\PagePath;
15use ComboStrap\Reference;
16use ComboStrap\References;
17
18
19/**
20 * Refresh the analytics when a backlink mutation occurs for a page
21 */
22class action_plugin_combo_backlinkmutation extends DokuWiki_Action_Plugin
23{
24
25
26    public const BACKLINK_MUTATION_EVENT_NAME = 'backlink_mutation';
27
28
29    public function register(Doku_Event_Handler $controller)
30    {
31
32
33        /**
34         * create the async event
35         */
36        $controller->register_hook(MetadataMutation::PAGE_METADATA_MUTATION_EVENT, 'AFTER', $this, 'create_backlink_mutation', array());
37
38        /**
39         * process the Async event
40         */
41        $controller->register_hook(self::BACKLINK_MUTATION_EVENT_NAME, 'AFTER', $this, 'handle_backlink_mutation');
42
43
44    }
45
46    /**
47     * @param Doku_Event $event
48     * @param $param
49     * @return void
50     */
51    public function handle_backlink_mutation(Doku_Event $event, $param)
52    {
53
54
55        $data = $event->data;
56        $pagePath = $data[PagePath::getPersistentName()];
57        $reference = MarkupPath::createPageFromAbsoluteId($pagePath);
58
59        if ($reference->isSlot()) {
60            return;
61        }
62
63        /**
64         * Delete and recompute analytics
65         */
66        try {
67            $analyticsDocument = $reference->fetchAnalyticsDocument();
68        } catch (ExceptionNotExists $e) {
69            return;
70        }
71        CacheLog::deleteCacheIfExistsAndLog(
72            $analyticsDocument,
73            self::BACKLINK_MUTATION_EVENT_NAME,
74            "Backlink mutation"
75        );
76
77        try {
78            /**
79             * This is only to recompute the {@link \ComboStrap\Meta\Field\BacklinkCount backlinks metric} and
80             * {@link \ComboStrap\LowQualityPage low quality page metrics}
81             * TODO: when the derived meta are in the meta array and not in the {@link renderer_plugin_combo_analytics document},
82             *   we could just compute them there and modify it with a plus 1
83             */
84            $reference->getDatabasePage()->replicateAnalytics();
85        } catch (ExceptionCompile $e) {
86            LogUtility::msg("Backlink Mutation: Error while trying to replicate the analytics. Error: {$e->getMessage()}");
87        }
88
89        /**
90         * Render the (footer slot) if it has a backlink dependency
91         */
92        MarkupCacheDependencies::reRenderSideSlotIfNeeded(
93            $pagePath,
94            MarkupCacheDependencies::BACKLINKS_DEPENDENCY,
95            self::BACKLINK_MUTATION_EVENT_NAME
96        );
97
98
99    }
100
101    /**
102     */
103    function create_backlink_mutation(Doku_Event $event, $params)
104    {
105
106
107        $data = $event->data;
108
109        /**
110         * If this is not a mutation on references we return.
111         */
112        if ($data[MetadataMutation::NAME_ATTRIBUTE] !== References::getPersistentName()) {
113            return;
114        };
115
116        $actualReferenceDatas = $data[MetadataMutation::NEW_VALUE_ATTRIBUTE];
117        $oldReferenceDatas = $data[MetadataMutation::OLD_VALUE_ATTRIBUTE];
118
119        /**
120         * Create an array of the actual reference with the key as path
121         */
122        $actualReferences = [];
123        if ($actualReferenceDatas !== null) {
124            foreach ($actualReferenceDatas as $actualReferenceData) {
125                $actualReferenceWikiPathString = $actualReferenceData[Reference::getPersistentName()];
126                $actualReferences[$actualReferenceWikiPathString] = $actualReferenceWikiPathString;
127            }
128        }
129
130        if ($oldReferenceDatas !== null) {
131            foreach ($oldReferenceDatas as $oldReferenceData) {
132
133                $oldReferenceWikiPathString = $oldReferenceData[Reference::getPersistentName()];
134
135                if (isset($actualReferences[$oldReferenceWikiPathString])) {
136                    unset($actualReferences[$oldReferenceWikiPathString]);
137                    continue;
138                }
139
140                /**
141                 * Deleted reference
142                 */
143                Event::createEvent(
144                    action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME,
145                    [
146                        PagePath::getPersistentName() => $oldReferenceWikiPathString
147                    ]
148                );
149
150            }
151        }
152
153        /**
154         * The new references
155         */
156        foreach ($actualReferences as $newReference) {
157            Event::createEvent(
158                action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME,
159                [PagePath::getPersistentName() => $newReference]);
160        }
161
162
163    }
164
165
166}
167
168
169
170