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