1<?php
2
3namespace ComboStrap;
4
5use ComboStrap\Web\Url;
6
7class Robots
8{
9
10    const INDEX_DELAY = 'indexdelay';
11
12    /**
13     * @throws ExceptionNotEnabled - if the page can be indexed
14     */
15    public static function canBeIndexedAndGetFollowValue(MarkupPath $page, ExecutionContext $executionContext): string
16    {
17
18        $action = $executionContext->getExecutingAction();
19        if ($action !== ExecutionContext::SHOW_ACTION) {
20            return "nofollow";
21        }
22
23        /**
24         * No indexing for slot page
25         */
26        if ($page->isSlot()) {
27            return "follow";
28        }
29
30        /**
31         * Resolution of the Google Search Console Issue
32         * `Alternative page with proper canonical tag`
33         * when Google crawls URl with functional Query String such as
34         *
35         * https://datacadamia.com/lang/java/comment?redirectId=java:comment&redirectOrigin=canonical
36         * https://datacadamia.com/os/windows/pathext?referer=https://gerardnico.com/os/windows/pathext
37         *
38         * TODO: Ultimately, we should use a script that returns only the good url
39         *   doing a redirect with a query to the resource is not Search Engine friendly
40         */
41        $queryProperties = Url::createFromGetOrPostGlobalVariable()->getQueryProperties();
42        foreach ($queryProperties as $key => $value) {
43            if ($key !== DokuwikiId::DOKUWIKI_ID_ATTRIBUTE) {
44                // follow but no index as we return a value
45                return "follow";
46            }
47        }
48
49        if ($page->isLowQualityPage() && LowQualityPage::isProtectionEnabled()) {
50            if (LowQualityPage::getLowQualityProtectionMode() !== PageProtection::CONF_VALUE_ACL) {
51                return "follow";
52            }
53        }
54        if ($page->isLatePublication() && PagePublicationDate::isLatePublicationProtectionEnabled()) {
55            if (PagePublicationDate::getLatePublicationProtectionMode() == PageProtection::CONF_VALUE_ACL) {
56                return "nofollow";
57            } else {
58                return "follow";
59            }
60        }
61        throw new ExceptionNotEnabled();
62    }
63}
64