1<?php
2
3namespace ComboStrap;
4
5use action_plugin_combo_urlmanager;
6
7include_once(__DIR__ . "/PagesIndex.php");
8
9/**
10 * Class UrlManagerBestEndPage
11 *
12 * A class that implements the BestEndPage Algorithm for the {@link action_plugin_combo_urlmanager urlManager}
13 */
14class UrlManagerBestEndPage
15{
16
17    /**
18     * If the number of names part that match is greater or equal to
19     * this configuration, an Id Redirect is performed
20     * A value of 0 disable and send only HTTP redirect
21     */
22    const CONF_MINIMAL_SCORE_FOR_REDIRECT = 'BestEndPageMinimalScoreForIdRedirect';
23    const CONF_MINIMAL_SCORE_FOR_REDIRECT_DEFAULT = '0';
24
25
26    /**
27     * @param $pageId
28     * @return array - the best poge id and its score
29     * The score is the number of name that matches
30     */
31    public static function getBestEndPageId($pageId)
32    {
33
34        $result = array();
35        $pageName = noNS($pageId);
36
37        $pagesWithSameName = PagesIndex::pagesWithSameName($pageName, $pageId);
38        if (count($pagesWithSameName) > 0) {
39
40            // Default value
41            $bestScore = 0;
42            $bestPage = $pagesWithSameName[0];
43
44            // The name of the dokuwiki id
45            $pageIdNames = explode(':', $pageId);
46
47            // Loop
48            foreach ($pagesWithSameName as $targetPageId => $pageTitle) {
49
50                $targetPageIdNames = explode(':', $targetPageId);
51                $targetPageIdScore = 0;
52                for ($i = 1; $i <= sizeof($pageIdNames); $i++) {
53                    $pageIdName = $pageIdNames[sizeof($pageIdNames) - $i];
54                    $indexTargetPage = sizeof($targetPageIdNames) - $i;
55                    if ($indexTargetPage < 0) {
56                        break;
57                    }
58                    $targetPageIdName = $targetPageIdNames[$indexTargetPage];
59                    if ($targetPageIdName == $pageIdName) {
60                        $targetPageIdScore++;
61                    } else {
62                        break;
63                    }
64
65                }
66                if ($targetPageIdScore > $bestScore) {
67                    $bestScore = $targetPageIdScore;
68                    $bestPage = $targetPageId;
69                }
70
71            }
72
73            $result = array(
74                $bestPage,
75                $bestScore
76            );
77
78        }
79        return $result;
80
81    }
82
83
84    /**
85     * @param $pageId
86     * @return array with the best page and the type of redirect
87     */
88    public static function process($pageId)
89    {
90
91        $return = array();
92        global $conf;
93        $minimalScoreForARedirect = $conf['plugin'][PluginUtility::PLUGIN_BASE_NAME][self::CONF_MINIMAL_SCORE_FOR_REDIRECT];
94
95        list($bestPageId, $bestScore) = self::getBestEndPageId($pageId);
96        if ($bestPageId != null) {
97            $redirectType = action_plugin_combo_urlmanager::REDIRECT_HTTP;
98            if ($minimalScoreForARedirect != 0 && $bestScore >= $minimalScoreForARedirect) {
99                $redirectType = action_plugin_combo_urlmanager::REDIRECT_ID;
100            }
101            $return = array(
102                $bestPageId,
103                $redirectType
104            );
105        }
106        return $return;
107
108    }
109}
110