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::isSecondarySlot()}
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(string $path): array
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 Page|null the page path of the parent or null if it does not exist
130     */
131    public static function getParentPagePath($namespacePath): ?Page
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 Page::getHomePageFromNamespace($parentNamespacePath);
153
154        } else {
155            return null;
156        }
157
158
159    }
160
161
162    /**
163     * Find the pages in the tree
164     * @param $startPath
165     * @param int $depth
166     * @return array
167     */
168    public static function getPages($startPath, int $depth = 0): array
169    {
170
171        if ($startPath === null || $startPath === "") {
172            throw new \RuntimeException("A start path is mandatory");
173        }
174
175
176        // Run as admin to overcome the fact that
177        // anonymous user cannot set all links and backlinks
178        global $conf;
179        $dataDir = $conf['datadir'];
180
181        $pages = array();
182
183        // This is a page
184        if (page_exists($startPath)) {
185            $pages[] = array(
186                'id' => $startPath,
187                'ns' => getNS($startPath),
188                'title' => p_get_first_heading($startPath, false),
189                'size' => filesize(wikiFN($startPath)),
190                'mtime' => filemtime(wikiFN($startPath)),
191                'perm' => 16,
192                'type' => 'f',
193                'level' => 0,
194                'open' => 1,
195            );
196        } else {
197
198            $startPath = str_replace(':', '/', $startPath);
199
200            /**
201             * Directory
202             */
203            search(
204                $pages,
205                $dataDir,
206                'search_universal',
207                array(
208                    'depth' => $depth,
209                    'listfiles' => true,
210                    'listdirs' => false,
211                    'pagesonly' => true,
212                    'skipacl' => true,
213                    'firsthead' => false,
214                    'meta' => false,
215                ),
216                $startPath
217            );
218        }
219        return $pages;
220    }
221
222}
223