add contents

This commit is contained in:
Trevor Batley
2025-10-09 15:04:29 +11:00
parent 170362eec1
commit bce7dd054a
2537 changed files with 301282 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
/**
* Plugin nspages : Displays nicely a list of the pages of a namespace
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
if(!defined('DOKU_INC')) die();
require_once 'namespacePreparer.php';
require_once 'pagePreparer.php';
class fileHelper {
private $files;
private $data;
private $customTitleAllowListMetadata;
function __construct($data, $customTitleAllowListMetadata){
$this->data = $data;
$this->files = $this->searchFiles($data);
$this->customTitleAllowListMetadata = $customTitleAllowListMetadata;
}
private function searchFiles(){
global $conf;
$opt = array(
'depth' => $this->data['maxDepth'], 'keeptxt'=> false, 'listfiles'=> !$this->data['nopages'],
'listdirs' => $this->data['subns'], 'pagesonly'=> true, 'skipacl'=> false,
'sneakyacl' => true, 'hash'=> false, 'meta'=> true, 'showmsg'=> false,
'showhidden'=> $this->data['showhidden'], 'firsthead'=> true
);
$files = array();
search($files, $conf['datadir'], 'search_universal', $opt, $this->data['wantedDir']);
return $files;
}
function getPages(){
$preparer = new pagePreparer($this->data['excludedNS'], $this->data['excludedPages'], $this->data['pregPagesOn'],
$this->data['pregPagesOff'], $this->data['pregPagesTitleOn'], $this->data['pregPagesTitleOff'], $this->data['title'],
$this->data['sortid'], $this->data['idAndTitle'], $this->data['sortDate'], $this->data['sortByCreationDate'],
$this->data['customTitle'], $this->customTitleAllowListMetadata, $this->data['sortByMetadata']);
return $this->getFiles($preparer);
}
function getSubnamespaces(){
$preparer = new namespacePreparer($this->data['excludedNS'], $this->data['pregNSOn'], $this->data['pregNSOff'],
$this->data['pregNSTitleOn'], $this->data['pregNSTitleOff'], $this->data['title'], $this->data['sortid'],
$this->data['idAndTitle'], $this->data['sortDate'], $this->data['sortByCreationDate']);
return $this->getFiles($preparer);
}
private function getFiles($preparer){
$files = array();
foreach($this->files as $item) {
$preparer->prepareFileTitle($item);
if($preparer->isFileWanted($item, false) && $preparer->isFileWanted($item, true)) {
$preparer->prepareFile($item);
$files[] = $item;
}
}
return $files;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* Plugin nspages : Displays nicely a list of the pages of a namespace
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
if(!defined('DOKU_INC')) die();
abstract class filePreparer {
/**
* Array A list of files that shouldn't be displayed
*/
private $excludedFiles;
/**
* Array RegEx that a file should match to be displayed
*/
private $pregOn;
private $pregTitleOn;
/**
* Array RegEx that a file shouldn't match to be displayed
*/
private $pregOff;
private $pregTitleOff;
protected $useTitle;
protected $useIdAndTitle;
protected $sortPageByDate;
protected $sortByCreationDate;
/**
* bool
*/
protected $sortPageById;
function __construct($excludedFiles, $pregOn, $pregOff, $pregTitleOn, $pregTitleOff, $useTitle, $sortPageById,
$useIdAndTitle, $sortPageByDate, $sortByCreationDate){
$this->excludedFiles = $excludedFiles;
$this->pregOn = $pregOn;
$this->pregOff = $pregOff;
$this->pregTitleOn = $pregTitleOn;
$this->pregTitleOff = $pregTitleOff;
$this->useTitle = $useTitle;
$this->sortPageById = $sortPageById;
$this->useIdAndTitle = $useIdAndTitle;
$this->sortPageByDate = $sortPageByDate;
$this->sortByCreationDate = $sortByCreationDate;
}
function isFileWanted($file, $useTitle) {
$wanted = true;
$nameToFilterOn = $useTitle ? $file['title'] : noNS($file['id']);
$pregOn = $useTitle ? $this->pregTitleOn : $this->pregOn;
$pregOff = $useTitle ? $this->pregTitleOff : $this->pregOff;
$wanted &= (!in_array($nameToFilterOn, $this->excludedFiles));
foreach($pregOn as $preg) {
$wanted &= preg_match($preg, $nameToFilterOn);
}
foreach($pregOff as $preg) {
$wanted &= !preg_match($preg, $nameToFilterOn);
}
return $wanted;
}
abstract function prepareFile(&$file);
abstract function prepareFileTitle(&$file);
}

View File

@@ -0,0 +1,81 @@
<?php
/**
* Plugin nspages : Displays nicely a list of the pages of a namespace
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
if(!defined('DOKU_INC')) die();
require_once 'filePreparer.php';
class namespacePreparer extends filePreparer {
function __construct($excludedFiles, $pregOn, $pregOff, $pregTitleOn, $pregTitleOff, $useTitle, $sortPageById, $useIdAndTitle, $sortPageByDate, $sortByCreationDate){
parent::__construct($excludedFiles, $pregOn, $pregOff, $pregTitleOn, $pregTitleOff, $useTitle, $sortPageById, $useIdAndTitle, $sortPageByDate, $sortByCreationDate);
}
function isFileWanted($file, $useTitle){
return $file['type'] == 'd' && parent::isFileWanted($file, $useTitle);
}
function prepareFileTitle(&$ns){
$idMainPage = $this->getMainPageId($ns);
if ( !is_null($idMainPage) ){
$ns['title'] = p_get_first_heading($idMainPage, true);
} else {
$ns['title'] = null;
}
}
/**
* When we display a namespace, we want to:
* - link to it's main page (if such a page exists)
* - get the id of this main page (if the option is active)
*
* @param $ns A structure which represents a namespace
*/
function prepareFile(&$ns){
$ns['nameToDisplay'] = $this->buildNameToDisplay($ns['title'], noNS($ns['id']));
$ns['id'] = $this->buildIdToLinkTo($this->getMainPageId($ns), $ns['id']);
$ns['sort'] = $this->buildSortAttribute($ns['nameToDisplay'], $ns['id'], $ns['mtime']);
}
private function getMainPageId(&$ns){
if (is_null($ns['idMainPage'])){
$idMainPage = $ns['id'].':';
resolve_pageid('', $idMainPage, $exist); //get the id of the main page of the ns
$ns['idMainPage'] = $exist ? $idMainPage : null;
}
return $ns['idMainPage'];
}
private function buildNameToDisplay($title, $defaultName){
if ( ! is_null($title) ){
if($this->useIdAndTitle){
return $defaultName . " - " . $title;
}
if($this->useTitle) {
return $title;
}
}
return $defaultName;
}
private function buildIdToLinkTo($idMainPage, $currentNsId){
if(is_null($idMainPage)) {
return $currentNsId . ':';
} else {
return $idMainPage;
}
}
private function buildSortAttribute($nameToDisplay, $nsId, $mtime){
if ( $this->sortPageById ){
return curNS($nsId);
} else if ( $this->sortPageByDate ){
return $mtime;
} else {
return $nameToDisplay;
}
}
}

View File

@@ -0,0 +1,143 @@
<?php
/**
* Plugin nspages : Displays nicely a list of the pages of a namespace
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
if(!defined('DOKU_INC')) die();
require_once 'filePreparer.php';
class pagePreparer extends filePreparer {
private $customTitle;
private $customTitleAllowListMetadata;
private $sortByMetadata;
function __construct($excludedNs, $excludedFiles, $pregOn, $pregOff, $pregTitleOn, $pregTitleOff, $useTitle,
$sortPageById, $useIdAndTitle, $sortPageByDate, $sortByCreationDate, $customTitle,
$customTitleAllowListMetadata, $sortByMetadata) {
parent::__construct($excludedFiles, $pregOn, $pregOff, $pregTitleOn, $pregTitleOff, $useTitle, $sortPageById,
$useIdAndTitle, $sortPageByDate, $sortByCreationDate);
$this->excludedNs = $excludedNs;
$this->customTitle = $customTitle;
$this->customTitleAllowListMetadata = $customTitleAllowListMetadata;
$this->sortByMetadata = $sortByMetadata;
}
function isFileWanted($file, $useTitle){
return ($file['type'] != 'd') && parent::isFileWanted($file, $useTitle) && $this->passSubNsfilterInRecursiveMode($file);
}
function prepareFileTitle(&$file){
// Nothing to do: for pages the title is already set
}
private function passSubNsfilterInRecursiveMode($file){
$subNss = explode(':', $file['id']);
if ( count($subNss) < 2 ){ //It means we're not in recursive mode
return true;
}
for ($i = 0; $i < count($subNss) - 1; $i++) {
if (in_array($subNss[$i], $this->excludedNs)) {
return false;
}
}
return true;
}
function prepareFile(&$page){
$page['nameToDisplay'] = $this->buildNameToDisplay($page);
$page['sort'] = $this->buildSortAttribute($page['nameToDisplay'], $page['id'], $page['mtime']);
}
/**
* Get the a metadata value from a certain path.
*
* @param $metadata - The metadata object of a page. More details on https://www.dokuwiki.org/devel:metadata
* @param $path - The path.
* Examples:
* date.created
* contributor.0
*
* @return mixed - The metadata value from a certain path.
*/
private function getMetadataFromPath($metadata, $path) {
return array_reduce(
explode('.', $path),
function ($object, $property) {
return is_numeric($property) ? $object[$property] : $object[$property];
},
$metadata
);
}
private function isPathInMetadataAllowList($path) {
$metadataAllowList = explode(',', preg_replace('/\s+/', '', $this->customTitleAllowListMetadata));
return in_array($path, $metadataAllowList);
}
/**
* Get the page custom title from a template.
*
* @param $customTitle - The custom tile template.
* Examples:
* {title} ({data.created} by {user})
* @param $metadata - The metadata object of a page. More details on https://www.dokuwiki.org/devel:metadata
*
* @return string - the custom title
*/
private function getCustomTitleFromTemplate($customTitle, $metadata) {
return preg_replace_callback(
'/{(.*?)}/',
function ($matches) use($metadata) {
$path = $matches[1];
if ($this->isPathInMetadataAllowList($path)) {
return $this->getMetadataFromPath($metadata, $path);
} else {
return $path;
}
},
$customTitle
);
}
private function buildNameToDisplay($page){
$title = $page['title'];
$pageId = $page['id'];
if ($this->customTitle !== null) {
$meta = p_get_metadata($pageId);
return $this->getCustomTitleFromTemplate($this->customTitle, $meta);
}
if($this->useIdAndTitle && $title !== null ){
return noNS($pageId) . " - " . $title;
}
if(!$this->useTitle || $title === null) {
return noNS($pageId);
}
return $title;
}
private function buildSortAttribute($nameToDisplay, $pageId, $mtime){
if ($this->sortByMetadata !== null) {
$meta = p_get_metadata($pageId);
return $this->getMetadataFromPath($meta, $this->sortByMetadata);
} else if($this->sortPageById) {
return noNS($pageId);
} else if ( $this->sortPageByDate) {
return $mtime;
} else if ($this->sortByCreationDate) {
$meta = p_get_metadata($pageId);
return $meta['date']['created'];
} else {
return $nameToDisplay;
}
}
}