xref: /template/strap/ComboStrap/FsWikiUtility.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
1*37748cd8SNickeau<?php
2*37748cd8SNickeau/**
3*37748cd8SNickeau * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4*37748cd8SNickeau *
5*37748cd8SNickeau * This source code is licensed under the GPL license found in the
6*37748cd8SNickeau * COPYING  file in the root directory of this source tree.
7*37748cd8SNickeau *
8*37748cd8SNickeau * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9*37748cd8SNickeau * @author   ComboStrap <support@combostrap.com>
10*37748cd8SNickeau *
11*37748cd8SNickeau */
12*37748cd8SNickeau
13*37748cd8SNickeaunamespace ComboStrap;
14*37748cd8SNickeau
15*37748cd8SNickeau/**
16*37748cd8SNickeau * Class PageUtility
17*37748cd8SNickeau * @package ComboStrap
18*37748cd8SNickeau * See also {@link pageutils.php}
19*37748cd8SNickeau */
20*37748cd8SNickeauclass FsWikiUtility
21*37748cd8SNickeau{
22*37748cd8SNickeau
23*37748cd8SNickeau
24*37748cd8SNickeau    /**
25*37748cd8SNickeau     * Determine if the current page is a sidebar (a bar)
26*37748cd8SNickeau     * @return bool
27*37748cd8SNickeau     */
28*37748cd8SNickeau    public static function isSideBar()
29*37748cd8SNickeau    {
30*37748cd8SNickeau        global $INFO;
31*37748cd8SNickeau        global $ID;
32*37748cd8SNickeau        $isSidebar = false;
33*37748cd8SNickeau        if ($INFO != null) {
34*37748cd8SNickeau            $id = $INFO['id'];
35*37748cd8SNickeau            if ($ID != $id) {
36*37748cd8SNickeau                $isSidebar = TRUE;
37*37748cd8SNickeau            }
38*37748cd8SNickeau        }
39*37748cd8SNickeau        return $isSidebar;
40*37748cd8SNickeau    }
41*37748cd8SNickeau
42*37748cd8SNickeau    /**
43*37748cd8SNickeau     * Return the main page id
44*37748cd8SNickeau     * (Not the sidebar)
45*37748cd8SNickeau     * @return mixed|string
46*37748cd8SNickeau     */
47*37748cd8SNickeau    public static function getMainPageId()
48*37748cd8SNickeau    {
49*37748cd8SNickeau        global $ID;
50*37748cd8SNickeau        global $INFO;
51*37748cd8SNickeau        $callingId = $ID;
52*37748cd8SNickeau        // If the component is in a sidebar, we don't want the ID of the sidebar
53*37748cd8SNickeau        // but the ID of the page.
54*37748cd8SNickeau        if ($INFO != null) {
55*37748cd8SNickeau            $callingId = $INFO['id'];
56*37748cd8SNickeau        }
57*37748cd8SNickeau        /**
58*37748cd8SNickeau         * This is the case with event triggered
59*37748cd8SNickeau         * before DokuWiki such as
60*37748cd8SNickeau         * https://www.dokuwiki.org/devel:event:init_lang_load
61*37748cd8SNickeau         */
62*37748cd8SNickeau        if ($callingId == null) {
63*37748cd8SNickeau            global $_REQUEST;
64*37748cd8SNickeau            if (isset($_REQUEST["id"])) {
65*37748cd8SNickeau                $callingId = $_REQUEST["id"];
66*37748cd8SNickeau            }
67*37748cd8SNickeau        }
68*37748cd8SNickeau        return $callingId;
69*37748cd8SNickeau    }
70*37748cd8SNickeau
71*37748cd8SNickeau    /**
72*37748cd8SNickeau     * Return all pages and/of sub-namespaces (subdirectory) of a namespace (ie directory)
73*37748cd8SNickeau     * Adapted from feed.php
74*37748cd8SNickeau     *
75*37748cd8SNickeau     * @param string $path The container of the pages
76*37748cd8SNickeau     * @return array An array of the pages for the namespace
77*37748cd8SNickeau     */
78*37748cd8SNickeau    static function getChildren($path)
79*37748cd8SNickeau    {
80*37748cd8SNickeau        require_once(__DIR__ . '/../../../../inc/search.php');
81*37748cd8SNickeau        global $conf;
82*37748cd8SNickeau
83*37748cd8SNickeau
84*37748cd8SNickeau        /**
85*37748cd8SNickeau         * To a relative file system path
86*37748cd8SNickeau         */
87*37748cd8SNickeau        $dokuPath = DokuPath::createPagePathFromPath($path);
88*37748cd8SNickeau        $relativeFileSystemPath = str_replace(":", "/", $dokuPath->getId());
89*37748cd8SNickeau
90*37748cd8SNickeau
91*37748cd8SNickeau        $data = array();
92*37748cd8SNickeau
93*37748cd8SNickeau        // Options of the callback function search_universal
94*37748cd8SNickeau        // in the search.php file
95*37748cd8SNickeau        $search_opts = array(
96*37748cd8SNickeau            'depth' => 1,
97*37748cd8SNickeau            'pagesonly' => true,
98*37748cd8SNickeau            'listfiles' => true,
99*37748cd8SNickeau            'listdirs' => true,
100*37748cd8SNickeau            'skipacl' => true
101*37748cd8SNickeau            //'firsthead' => true
102*37748cd8SNickeau        );
103*37748cd8SNickeau        // search_universal is a function in inc/search.php that accepts the $search_opts parameters
104*37748cd8SNickeau        search($data, // The returned data
105*37748cd8SNickeau            $conf['datadir'], // The root
106*37748cd8SNickeau            'search_universal', // The recursive function (callback)
107*37748cd8SNickeau            $search_opts, // The options given to the recursive function
108*37748cd8SNickeau            $relativeFileSystemPath, // The id
109*37748cd8SNickeau            1 // Only one level in the tree
110*37748cd8SNickeau        );
111*37748cd8SNickeau
112*37748cd8SNickeau        return $data;
113*37748cd8SNickeau    }
114*37748cd8SNickeau
115*37748cd8SNickeau    /**
116*37748cd8SNickeau     * Return the page index of a namespace of null if it does not exist
117*37748cd8SNickeau     * ie the index.html
118*37748cd8SNickeau     * @param $namespacePath
119*37748cd8SNickeau     * @return string|null
120*37748cd8SNickeau     */
121*37748cd8SNickeau    public static function getHomePagePath($namespacePath)
122*37748cd8SNickeau    {
123*37748cd8SNickeau        global $conf;
124*37748cd8SNickeau
125*37748cd8SNickeau        if ($namespacePath != ":") {
126*37748cd8SNickeau            $namespacePath = $namespacePath . ":";
127*37748cd8SNickeau        }
128*37748cd8SNickeau
129*37748cd8SNickeau        $startPageName = $conf['start'];
130*37748cd8SNickeau        if (page_exists($namespacePath . $startPageName)) {
131*37748cd8SNickeau            // start page inside namespace
132*37748cd8SNickeau            return $namespacePath . $startPageName;
133*37748cd8SNickeau        } elseif (page_exists($namespacePath . noNS(cleanID($namespacePath)))) {
134*37748cd8SNickeau            // page named like the NS inside the NS
135*37748cd8SNickeau            return $namespacePath . noNS(cleanID($namespacePath));
136*37748cd8SNickeau        } elseif (page_exists($namespacePath)) {
137*37748cd8SNickeau            // page like namespace exists
138*37748cd8SNickeau            return substr($namespacePath, 0, -1);
139*37748cd8SNickeau        } else {
140*37748cd8SNickeau            return null;
141*37748cd8SNickeau        }
142*37748cd8SNickeau    }
143*37748cd8SNickeau
144*37748cd8SNickeau    public static function getChildrenNamespace($nameSpacePath)
145*37748cd8SNickeau    {
146*37748cd8SNickeau        require_once(__DIR__ . '/../../../../inc/search.php');
147*37748cd8SNickeau        global $conf;
148*37748cd8SNickeau
149*37748cd8SNickeau        $data = array();
150*37748cd8SNickeau
151*37748cd8SNickeau        // Options of the callback function search_universal
152*37748cd8SNickeau        // in the search.php file
153*37748cd8SNickeau        $search_opts = array();
154*37748cd8SNickeau        // search_universal is a function in inc/search.php that accepts the $search_opts parameters
155*37748cd8SNickeau        search_namespaces($data, // The returned data
156*37748cd8SNickeau            $conf['datadir'], // The root
157*37748cd8SNickeau            $nameSpacePath, // The directory to search
158*37748cd8SNickeau            'd',
159*37748cd8SNickeau            1, // Only one level in the tree
160*37748cd8SNickeau            $search_opts
161*37748cd8SNickeau        );
162*37748cd8SNickeau
163*37748cd8SNickeau        return $data;
164*37748cd8SNickeau    }
165*37748cd8SNickeau
166*37748cd8SNickeau    /**
167*37748cd8SNickeau     * @param $namespacePath
168*37748cd8SNickeau     * @return string|null the page path of the parent or null if it does not exist
169*37748cd8SNickeau     */
170*37748cd8SNickeau    public static function getParentPagePath($namespacePath)
171*37748cd8SNickeau    {
172*37748cd8SNickeau
173*37748cd8SNickeau        /**
174*37748cd8SNickeau         * Root case
175*37748cd8SNickeau         */
176*37748cd8SNickeau        if ($namespacePath === ":") {
177*37748cd8SNickeau            return null;
178*37748cd8SNickeau        }
179*37748cd8SNickeau
180*37748cd8SNickeau        /**
181*37748cd8SNickeau         * A namespace path does not have a `:` at the end
182*37748cd8SNickeau         * only for the root
183*37748cd8SNickeau         */
184*37748cd8SNickeau        $pos = strrpos($namespacePath, ':');
185*37748cd8SNickeau        if ($pos !== false) {
186*37748cd8SNickeau            if ($pos == 0) {
187*37748cd8SNickeau                $parentNamespacePath = ":";
188*37748cd8SNickeau            } else {
189*37748cd8SNickeau                $parentNamespacePath = substr($namespacePath, 0, $pos);
190*37748cd8SNickeau            }
191*37748cd8SNickeau            return self::getHomePagePath($parentNamespacePath);
192*37748cd8SNickeau        } else {
193*37748cd8SNickeau            return null;
194*37748cd8SNickeau        }
195*37748cd8SNickeau
196*37748cd8SNickeau
197*37748cd8SNickeau    }
198*37748cd8SNickeau
199*37748cd8SNickeau
200*37748cd8SNickeau    /**
201*37748cd8SNickeau     * Find the pages in the tree
202*37748cd8SNickeau     * @param $namespaces (default to the root tree)
203*37748cd8SNickeau     * @param $depth
204*37748cd8SNickeau     * @return array
205*37748cd8SNickeau     */
206*37748cd8SNickeau    public static function getPages($namespaces = array(''), $depth = 0)
207*37748cd8SNickeau    {
208*37748cd8SNickeau        // Run as admin to overcome the fact that
209*37748cd8SNickeau        // anonymous user cannot set all links and backlinks
210*37748cd8SNickeau        global $conf;
211*37748cd8SNickeau        $datadir = $conf['datadir'];
212*37748cd8SNickeau
213*37748cd8SNickeau        $pages = array();
214*37748cd8SNickeau        foreach ($namespaces as $ns) {
215*37748cd8SNickeau
216*37748cd8SNickeau            search(
217*37748cd8SNickeau                $pages,
218*37748cd8SNickeau                $datadir,
219*37748cd8SNickeau                'search_universal',
220*37748cd8SNickeau                array(
221*37748cd8SNickeau                    'depth' => $depth,
222*37748cd8SNickeau                    'listfiles' => true,
223*37748cd8SNickeau                    'listdirs' => false,
224*37748cd8SNickeau                    'pagesonly' => true,
225*37748cd8SNickeau                    'skipacl' => true,
226*37748cd8SNickeau                    'firsthead' => false,
227*37748cd8SNickeau                    'meta' => false,
228*37748cd8SNickeau                ),
229*37748cd8SNickeau                str_replace(':', '/', $ns)
230*37748cd8SNickeau            );
231*37748cd8SNickeau
232*37748cd8SNickeau            // add the ns start page
233*37748cd8SNickeau            if ($ns && page_exists($ns)) {
234*37748cd8SNickeau                $pages[] = array(
235*37748cd8SNickeau                    'id' => $ns,
236*37748cd8SNickeau                    'ns' => getNS($ns),
237*37748cd8SNickeau                    'title' => p_get_first_heading($ns, false),
238*37748cd8SNickeau                    'size' => filesize(wikiFN($ns)),
239*37748cd8SNickeau                    'mtime' => filemtime(wikiFN($ns)),
240*37748cd8SNickeau                    'perm' => 16,
241*37748cd8SNickeau                    'type' => 'f',
242*37748cd8SNickeau                    'level' => 0,
243*37748cd8SNickeau                    'open' => 1,
244*37748cd8SNickeau                );
245*37748cd8SNickeau            }
246*37748cd8SNickeau
247*37748cd8SNickeau        }
248*37748cd8SNickeau        return $pages;
249*37748cd8SNickeau    }
250*37748cd8SNickeau
251*37748cd8SNickeau}
252