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