xref: /template/strap/ComboStrap/FsWikiUtility.php (revision c3437056399326d621a01da73b649707fbb0ae69)
137748cd8SNickeau<?php
237748cd8SNickeau/**
337748cd8SNickeau * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved.
437748cd8SNickeau *
537748cd8SNickeau * This source code is licensed under the GPL license found in the
637748cd8SNickeau * COPYING  file in the root directory of this source tree.
737748cd8SNickeau *
837748cd8SNickeau * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
937748cd8SNickeau * @author   ComboStrap <support@combostrap.com>
1037748cd8SNickeau *
1137748cd8SNickeau */
1237748cd8SNickeau
1337748cd8SNickeaunamespace ComboStrap;
1437748cd8SNickeau
1537748cd8SNickeau/**
1637748cd8SNickeau * Class PageUtility
1737748cd8SNickeau * @package ComboStrap
1837748cd8SNickeau * See also {@link pageutils.php}
1937748cd8SNickeau */
2037748cd8SNickeauclass FsWikiUtility
2137748cd8SNickeau{
2237748cd8SNickeau
2337748cd8SNickeau
2437748cd8SNickeau    /**
2537748cd8SNickeau     * Determine if the current page is a sidebar (a bar)
2637748cd8SNickeau     * @return bool
27*c3437056SNickeau     * TODO: Duplicate of {@link Page::isSlot()}
2837748cd8SNickeau     */
2937748cd8SNickeau    public static function isSideBar()
3037748cd8SNickeau    {
3137748cd8SNickeau        global $INFO;
3237748cd8SNickeau        global $ID;
3337748cd8SNickeau        $isSidebar = false;
3437748cd8SNickeau        if ($INFO != null) {
3537748cd8SNickeau            $id = $INFO['id'];
3637748cd8SNickeau            if ($ID != $id) {
3737748cd8SNickeau                $isSidebar = TRUE;
3837748cd8SNickeau            }
3937748cd8SNickeau        }
4037748cd8SNickeau        return $isSidebar;
4137748cd8SNickeau    }
4237748cd8SNickeau
4337748cd8SNickeau
4437748cd8SNickeau    /**
4537748cd8SNickeau     * Return all pages and/of sub-namespaces (subdirectory) of a namespace (ie directory)
4637748cd8SNickeau     * Adapted from feed.php
4737748cd8SNickeau     *
4837748cd8SNickeau     * @param string $path The container of the pages
4937748cd8SNickeau     * @return array An array of the pages for the namespace
5037748cd8SNickeau     */
5137748cd8SNickeau    static function getChildren($path)
5237748cd8SNickeau    {
5337748cd8SNickeau        require_once(__DIR__ . '/../../../../inc/search.php');
5437748cd8SNickeau        global $conf;
5537748cd8SNickeau
5637748cd8SNickeau
5737748cd8SNickeau        /**
5837748cd8SNickeau         * To a relative file system path
5937748cd8SNickeau         */
6037748cd8SNickeau        $dokuPath = DokuPath::createPagePathFromPath($path);
61*c3437056SNickeau        $relativeFileSystemPath = str_replace(":", "/", $dokuPath->getDokuwikiId());
6237748cd8SNickeau
6337748cd8SNickeau
6437748cd8SNickeau        $data = array();
6537748cd8SNickeau
6637748cd8SNickeau        // Options of the callback function search_universal
6737748cd8SNickeau        // in the search.php file
6837748cd8SNickeau        $search_opts = array(
6937748cd8SNickeau            'depth' => 1,
7037748cd8SNickeau            'pagesonly' => true,
7137748cd8SNickeau            'listfiles' => true,
7237748cd8SNickeau            'listdirs' => true,
7337748cd8SNickeau            'skipacl' => true
7437748cd8SNickeau            //'firsthead' => true
7537748cd8SNickeau        );
7637748cd8SNickeau        // search_universal is a function in inc/search.php that accepts the $search_opts parameters
7737748cd8SNickeau        search($data, // The returned data
7837748cd8SNickeau            $conf['datadir'], // The root
7937748cd8SNickeau            'search_universal', // The recursive function (callback)
8037748cd8SNickeau            $search_opts, // The options given to the recursive function
8137748cd8SNickeau            $relativeFileSystemPath, // The id
8237748cd8SNickeau            1 // Only one level in the tree
8337748cd8SNickeau        );
8437748cd8SNickeau
8537748cd8SNickeau        return $data;
8637748cd8SNickeau    }
8737748cd8SNickeau
8837748cd8SNickeau    /**
89*c3437056SNickeau     * Return the page index of a namespace or null if it does not exist
9037748cd8SNickeau     * ie the index.html
91*c3437056SNickeau     * @param $namespacePath - in dokuwiki format
92*c3437056SNickeau     * @return string - the dokuwiki path
93*c3437056SNickeau     * @deprecated use {@link Page::getHomePageFromNamespace()} instead
9437748cd8SNickeau     */
95*c3437056SNickeau    public static function getHomePagePath($namespacePath): ?string
9637748cd8SNickeau    {
97*c3437056SNickeau        $homePage = Page::getHomePageFromNamespace($namespacePath);
98*c3437056SNickeau        if ($homePage->exists()) {
99*c3437056SNickeau            return $homePage->getAbsolutePath();
10037748cd8SNickeau        } else {
10137748cd8SNickeau            return null;
10237748cd8SNickeau        }
10337748cd8SNickeau    }
10437748cd8SNickeau
10537748cd8SNickeau    public static function getChildrenNamespace($nameSpacePath)
10637748cd8SNickeau    {
10737748cd8SNickeau        require_once(__DIR__ . '/../../../../inc/search.php');
10837748cd8SNickeau        global $conf;
10937748cd8SNickeau
11037748cd8SNickeau        $data = array();
11137748cd8SNickeau
11237748cd8SNickeau        // Options of the callback function search_universal
11337748cd8SNickeau        // in the search.php file
11437748cd8SNickeau        $search_opts = array();
11537748cd8SNickeau        // search_universal is a function in inc/search.php that accepts the $search_opts parameters
11637748cd8SNickeau        search_namespaces($data, // The returned data
11737748cd8SNickeau            $conf['datadir'], // The root
11837748cd8SNickeau            $nameSpacePath, // The directory to search
11937748cd8SNickeau            'd',
12037748cd8SNickeau            1, // Only one level in the tree
12137748cd8SNickeau            $search_opts
12237748cd8SNickeau        );
12337748cd8SNickeau
12437748cd8SNickeau        return $data;
12537748cd8SNickeau    }
12637748cd8SNickeau
12737748cd8SNickeau    /**
12837748cd8SNickeau     * @param $namespacePath
12937748cd8SNickeau     * @return string|null the page path of the parent or null if it does not exist
13037748cd8SNickeau     */
13137748cd8SNickeau    public static function getParentPagePath($namespacePath)
13237748cd8SNickeau    {
13337748cd8SNickeau
13437748cd8SNickeau        /**
13537748cd8SNickeau         * Root case
13637748cd8SNickeau         */
13737748cd8SNickeau        if ($namespacePath === ":") {
13837748cd8SNickeau            return null;
13937748cd8SNickeau        }
14037748cd8SNickeau
14137748cd8SNickeau        /**
14237748cd8SNickeau         * A namespace path does not have a `:` at the end
14337748cd8SNickeau         * only for the root
14437748cd8SNickeau         */
14537748cd8SNickeau        $pos = strrpos($namespacePath, ':');
14637748cd8SNickeau        if ($pos !== false) {
14737748cd8SNickeau            if ($pos == 0) {
14837748cd8SNickeau                $parentNamespacePath = ":";
14937748cd8SNickeau            } else {
15037748cd8SNickeau                $parentNamespacePath = substr($namespacePath, 0, $pos);
15137748cd8SNickeau            }
15237748cd8SNickeau            return self::getHomePagePath($parentNamespacePath);
15337748cd8SNickeau        } else {
15437748cd8SNickeau            return null;
15537748cd8SNickeau        }
15637748cd8SNickeau
15737748cd8SNickeau
15837748cd8SNickeau    }
15937748cd8SNickeau
16037748cd8SNickeau
16137748cd8SNickeau    /**
16237748cd8SNickeau     * Find the pages in the tree
163*c3437056SNickeau     * @param $startPath
164*c3437056SNickeau     * @param int $depth
16537748cd8SNickeau     * @return array
16637748cd8SNickeau     */
167*c3437056SNickeau    public static function getPages($startPath, int $depth = 0): array
16837748cd8SNickeau    {
169*c3437056SNickeau
170*c3437056SNickeau        if ($startPath === null || $startPath === "") {
171*c3437056SNickeau            throw new \RuntimeException("A start path is mandatory");
172*c3437056SNickeau        }
173*c3437056SNickeau
174*c3437056SNickeau
17537748cd8SNickeau        // Run as admin to overcome the fact that
17637748cd8SNickeau        // anonymous user cannot set all links and backlinks
17737748cd8SNickeau        global $conf;
178*c3437056SNickeau        $dataDir = $conf['datadir'];
17937748cd8SNickeau
18037748cd8SNickeau        $pages = array();
18137748cd8SNickeau
182*c3437056SNickeau        // This is a page
183*c3437056SNickeau        if (page_exists($startPath)) {
184*c3437056SNickeau            $pages[] = array(
185*c3437056SNickeau                'id' => $startPath,
186*c3437056SNickeau                'ns' => getNS($startPath),
187*c3437056SNickeau                'title' => p_get_first_heading($startPath, false),
188*c3437056SNickeau                'size' => filesize(wikiFN($startPath)),
189*c3437056SNickeau                'mtime' => filemtime(wikiFN($startPath)),
190*c3437056SNickeau                'perm' => 16,
191*c3437056SNickeau                'type' => 'f',
192*c3437056SNickeau                'level' => 0,
193*c3437056SNickeau                'open' => 1,
194*c3437056SNickeau            );
195*c3437056SNickeau        } else {
196*c3437056SNickeau
197*c3437056SNickeau            $startPath = str_replace(':', '/', $startPath);
198*c3437056SNickeau
199*c3437056SNickeau            /**
200*c3437056SNickeau             * Directory
201*c3437056SNickeau             */
20237748cd8SNickeau            search(
20337748cd8SNickeau                $pages,
204*c3437056SNickeau                $dataDir,
20537748cd8SNickeau                'search_universal',
20637748cd8SNickeau                array(
20737748cd8SNickeau                    'depth' => $depth,
20837748cd8SNickeau                    'listfiles' => true,
20937748cd8SNickeau                    'listdirs' => false,
21037748cd8SNickeau                    'pagesonly' => true,
21137748cd8SNickeau                    'skipacl' => true,
21237748cd8SNickeau                    'firsthead' => false,
21337748cd8SNickeau                    'meta' => false,
21437748cd8SNickeau                ),
215*c3437056SNickeau                $startPath
21637748cd8SNickeau            );
21737748cd8SNickeau        }
21837748cd8SNickeau        return $pages;
21937748cd8SNickeau    }
22037748cd8SNickeau
22137748cd8SNickeau}
222