add contents
This commit is contained in:
174
lib/plugins/nspages/printers/printer.php
Normal file
174
lib/plugins/nspages/printers/printer.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?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 'sorters.php';
|
||||
|
||||
abstract class nspages_printer {
|
||||
protected $plugin;
|
||||
protected $renderer;
|
||||
protected $mode;
|
||||
private $pos;
|
||||
private $actualTitleLevel;
|
||||
private $natOrder;
|
||||
private $nbItemsMax;
|
||||
private $dictOrder;
|
||||
protected $_displayModificationDate;
|
||||
protected $_sorter;
|
||||
protected $includeItemsInTOC;
|
||||
|
||||
// Static to prevent conflicts if there are several <nspages> tag in a same page
|
||||
static private $builtSectionIds = array();
|
||||
|
||||
function __construct($plugin, $mode, $renderer, $data){
|
||||
$this->plugin = $plugin;
|
||||
$this->renderer =& $renderer;
|
||||
$this->mode = $mode;
|
||||
$this->pos = $data['pos'];
|
||||
$this->natOrder = $data['natOrder'];
|
||||
$this->actualTitleLevel = $data['actualTitleLevel'];
|
||||
$this->nbItemsMax = $data['nbItemsMax'];
|
||||
$this->dictOrder = $data['dictOrder'];
|
||||
$this->_displayModificationDate = $data['displayModificationDate']
|
||||
|| $data['modificationDateOnPictures']; // This is a deprecated option. We should kill it after checking no users are still using it
|
||||
$this->_sorter = $this->_getSorter($data['reverse']);
|
||||
$this->includeItemsInTOC = $data['includeItemsInTOC'] && $mode === 'xhtml';
|
||||
}
|
||||
|
||||
function printTOC($tab, $type, $text, $hideno){
|
||||
$this->_printHeader($tab, $type, $text, $hideno);
|
||||
|
||||
if(empty($tab)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->_print($tab, $type);
|
||||
}
|
||||
|
||||
abstract function _print($tab, $type);
|
||||
|
||||
function printUnusableNamespace($wantedNS){
|
||||
$this->renderer->section_open(1);
|
||||
$this->renderer->cdata($this->plugin->getLang('doesntexist').$wantedNS);
|
||||
$this->renderer->section_close();
|
||||
}
|
||||
|
||||
private function _printHeader(&$tab, $type, $text, $hideno) {
|
||||
if(empty($tab) && $hideno) return;
|
||||
|
||||
$this->_sorter->sort($tab);
|
||||
$this->_keepOnlyNMaxItems($tab);
|
||||
|
||||
if($text != '') {
|
||||
if($this->actualTitleLevel){
|
||||
$this->renderer->header($text, $this->actualTitleLevel, $this->pos);
|
||||
} else if($this->mode == 'xhtml') {
|
||||
$this->renderer->doc .= '<p class="catpageheadline">';
|
||||
$this->renderer->cdata($text);
|
||||
$this->renderer->doc .= '</p>';
|
||||
} else {
|
||||
$this->renderer->linebreak();
|
||||
$this->renderer->p_open();
|
||||
$this->renderer->cdata($text);
|
||||
$this->renderer->p_close();
|
||||
}
|
||||
}
|
||||
|
||||
if(empty($tab)) {
|
||||
$this->renderer->p_open();
|
||||
$this->renderer->cdata($this->plugin->getLang(($type == 'page') ? 'nopages' : 'nosubns'));
|
||||
$this->renderer->p_close();
|
||||
}
|
||||
}
|
||||
|
||||
private function _getSorter($reverse) {
|
||||
if ( $this->natOrder ){
|
||||
return new nspages_naturalOrder_sorter($reverse);
|
||||
} else if ($this->dictOrder) {
|
||||
return new nspages_dictOrder_sorter($reverse, $this->dictOrder);
|
||||
} else {
|
||||
return new nspages_default_sorter($reverse);
|
||||
}
|
||||
}
|
||||
|
||||
private function _keepOnlyNMaxItems(&$tab){
|
||||
if ($this->nbItemsMax){
|
||||
$tab = array_slice($tab, 0, $this->nbItemsMax);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Array $item Represents the file
|
||||
*/
|
||||
protected function _printElement($item, $level=1, $node=false) {
|
||||
$this->_printElementOpen($level, $node);
|
||||
$this->_printElementContent($item, $level);
|
||||
$this->_printElementClose();
|
||||
}
|
||||
|
||||
protected function _printElementOpen($level=1, $node=false) {
|
||||
if($item['type'] !== 'd') {
|
||||
$this->renderer->listitem_open($level, $node);
|
||||
} else { //Case of a subnamespace
|
||||
if($this->mode == 'xhtml') {
|
||||
$this->renderer->doc .= '<li class="closed">';
|
||||
} else {
|
||||
$this->renderer->listitem_open($level, $node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function _printElementContent($item, $level=1) {
|
||||
$this->renderer->listcontent_open();
|
||||
$this->_printElementLink($item, $level);
|
||||
$this->renderer->listcontent_close();
|
||||
}
|
||||
|
||||
protected function _printElementLink($item, $level=1) {
|
||||
$linkText = "";
|
||||
if ($this->_displayModificationDate) {
|
||||
$linkText = '[' . date('Y-m-d', $item["mtime"]) . '] - ';
|
||||
}
|
||||
$linkText .= $item['nameToDisplay'];
|
||||
if ($this->includeItemsInTOC){
|
||||
$anchorId = $this->buildAnchorId($item);
|
||||
$this->renderer->doc .= '<span id="' . $anchorId . '">';
|
||||
$this->renderer->toc_additem($anchorId, $linkText, $this->renderer->getLastLevel() + $level);
|
||||
}
|
||||
$this->renderer->internallink(':'.$item['id'], $linkText);
|
||||
if ($this->includeItemsInTOC){
|
||||
$this->renderer->doc .= "</span>";
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildAnchorId($item){
|
||||
// Prefix with "nspages_" to avoid collisions with headers
|
||||
return "nspages_" . sectionID($item['id'], self::$builtSectionIds);
|
||||
}
|
||||
|
||||
protected function _printElementClose() {
|
||||
$this->renderer->listitem_close();
|
||||
}
|
||||
|
||||
function printBeginning(){
|
||||
if($this->mode == 'xhtml') {
|
||||
$this->renderer->doc .= '<div class="plugin_nspages">';
|
||||
}
|
||||
}
|
||||
|
||||
function printEnd(){
|
||||
//this is needed to make sure everything after the plugin is written below the output
|
||||
if($this->mode == 'xhtml') {
|
||||
$this->renderer->doc .= '<div class="catpageeofidx"></div>';
|
||||
$this->renderer->doc .= '</div>';
|
||||
} else {
|
||||
$this->renderer->linebreak();
|
||||
}
|
||||
}
|
||||
|
||||
function printTransition(){ }
|
||||
}
|
29
lib/plugins/nspages/printers/printerLineBreak.php
Normal file
29
lib/plugins/nspages/printers/printerLineBreak.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
||||
*/
|
||||
|
||||
if(!defined('DOKU_INC')) die();
|
||||
require_once 'printer.php';
|
||||
|
||||
class nspages_printerLineBreak extends nspages_printer {
|
||||
function __construct($plugin, $mode, $renderer, $data){
|
||||
parent::__construct($plugin, $mode, $renderer, $data);
|
||||
}
|
||||
|
||||
function _print($tab, $type) {
|
||||
$firstItem = true;
|
||||
foreach($tab as $item) {
|
||||
if ( ! $firstItem ){
|
||||
$this->renderer->linebreak();
|
||||
}
|
||||
$this->_printElementLink($item);
|
||||
$firstItem = false;
|
||||
}
|
||||
}
|
||||
|
||||
function printTransition(){
|
||||
$this->renderer->cdata(', ');
|
||||
}
|
||||
|
||||
}
|
147
lib/plugins/nspages/printers/printerNice.php
Normal file
147
lib/plugins/nspages/printers/printerNice.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?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 'printer.php';
|
||||
require_once 'rendererXhtmlHelper.php';
|
||||
|
||||
class nspages_printerNice extends nspages_printer {
|
||||
private $nbCols;
|
||||
private $anchorName;
|
||||
|
||||
function __construct($plugin, $mode, $renderer, $nbCols, $anchorName, $data){
|
||||
parent::__construct($plugin, $mode, $renderer, $data);
|
||||
if ( $this->mode !== 'xhtml' ){
|
||||
throw Exception('nspages_printerNice can only work in xhtml mode');
|
||||
}
|
||||
$this->nbCols = $this->_computeActualNbCols($nbCols);
|
||||
$this->anchorName = $anchorName;
|
||||
}
|
||||
|
||||
private function _computeActualNbCols($nbCols){
|
||||
$nbCols = (int) $nbCols;
|
||||
if(!isset($nbCols) || is_null($nbCols) || $nbCols < 1) {
|
||||
$nbCols = 3;
|
||||
}
|
||||
return $nbCols;
|
||||
}
|
||||
|
||||
function _print($tab, $type) {
|
||||
$nbItemsPrinted = 0;
|
||||
|
||||
$nbItemPerColumns = $this->_computeNbItemPerColumns(sizeof($tab));
|
||||
$actualNbCols = count($nbItemPerColumns);
|
||||
$helper = new rendererXhtmlHelper($this->renderer, $actualNbCols, $this->plugin, $this->anchorName);
|
||||
|
||||
$helper->openColumn();
|
||||
$firstCharOfLastAddedPage = $this->_firstChar($tab[0]);
|
||||
|
||||
$helper->printHeaderChar($firstCharOfLastAddedPage);
|
||||
$helper->openListOfItems();
|
||||
|
||||
$idxCol = 0;
|
||||
foreach($tab as $item) {
|
||||
//change to the next column if necessary
|
||||
if($nbItemsPrinted == $nbItemPerColumns[$idxCol]) {
|
||||
$idxCol++;
|
||||
$helper->closeListOfItems();
|
||||
$helper->closeColumn();
|
||||
$helper->openColumn();
|
||||
|
||||
$newLetter = $this->_firstChar($item);
|
||||
if($newLetter != $firstCharOfLastAddedPage) {
|
||||
$firstCharOfLastAddedPage = $newLetter;
|
||||
$helper->printHeaderChar($firstCharOfLastAddedPage);
|
||||
} else {
|
||||
$helper->printHeaderChar($firstCharOfLastAddedPage, true);
|
||||
}
|
||||
$helper->openListOfItems();
|
||||
}
|
||||
|
||||
$newLetter = $this->_firstChar($item);
|
||||
if($newLetter != $firstCharOfLastAddedPage) {
|
||||
$firstCharOfLastAddedPage = $newLetter;
|
||||
$helper->closeListOfItems();
|
||||
$helper->printHeaderChar($firstCharOfLastAddedPage);
|
||||
$helper->openListOfItems();
|
||||
}
|
||||
|
||||
$this->_printElement($item);
|
||||
$nbItemsPrinted++;
|
||||
}
|
||||
$helper->closeListOfItems();
|
||||
$helper->closeColumn();
|
||||
}
|
||||
|
||||
private function _firstChar($item) {
|
||||
$return_char = utf8_strtoupper(utf8_substr($item['sort'], 0, 1));
|
||||
$uniord_char = $this->_uniord($return_char);
|
||||
|
||||
// korean support. See #111 for more context
|
||||
if ($uniord_char > 44031 && $uniord_char < 55204) {
|
||||
$return_char = ['ㄱ','ㄱ','ㄴ','ㄷ','ㄷ','ㄹ','ㅁ','ㅂ','ㅂ','ㅅ','ㅅ','ㅇ','ㅈ','ㅈ','ㅊ','ㅋ','ㅌ','ㅍ','ㅎ'][($uniord_char-44032)/588];
|
||||
}
|
||||
|
||||
return $return_char;
|
||||
}
|
||||
|
||||
/**
|
||||
* This code is from:
|
||||
* https://stackoverflow.com/questions/9361303/
|
||||
*/
|
||||
private function _uniord($c) {
|
||||
if (ord($c{0}) >=0 && ord($c{0}) <= 127)
|
||||
return ord($c{0});
|
||||
if (ord($c{0}) >= 192 && ord($c{0}) <= 223)
|
||||
return (ord($c{0})-192)*64 + (ord($c{1})-128);
|
||||
if (ord($c{0}) >= 224 && ord($c{0}) <= 239)
|
||||
return (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
|
||||
if (ord($c{0}) >= 240 && ord($c{0}) <= 247)
|
||||
return (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
|
||||
if (ord($c{0}) >= 248 && ord($c{0}) <= 251)
|
||||
return (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
|
||||
if (ord($c{0}) >= 252 && ord($c{0}) <= 253)
|
||||
return (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
|
||||
if (ord($c{0}) >= 254 && ord($c{0}) <= 255)
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the number of element to display per column
|
||||
* When $nbItems / $nbCols isn't an int, we make sure, for aesthetic reasons,
|
||||
* that the first are the ones which have the more items
|
||||
* Moreover, if we don't have enought items to display, we may choose to display less than the number of columns wanted
|
||||
*
|
||||
* @param int $nbItems The total number of items to display
|
||||
* @return an array which contains $nbCols int.
|
||||
*/
|
||||
private function _computeNbItemPerColumns($nbItems) {
|
||||
$result = array();
|
||||
|
||||
if($nbItems < $this->nbCols) {
|
||||
for($idx = 0; $idx < $nbItems; $idx++) {
|
||||
$result[] = $idx + 1;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
$collength = $nbItems / $this->nbCols;
|
||||
$nbItemPerCol = array();
|
||||
for($idx = 0; $idx < $this->nbCols; $idx++) {
|
||||
$nbItemPerCol[] = ceil(($idx + 1) * $collength) - ceil($idx * $collength);
|
||||
}
|
||||
rsort($nbItemPerCol);
|
||||
|
||||
$result[] = $nbItemPerCol[0];
|
||||
for($idx = 1; $idx < $this->nbCols; $idx++) {
|
||||
$result[] = end($result) + $nbItemPerCol[$idx];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
28
lib/plugins/nspages/printers/printerOneLine.php
Normal file
28
lib/plugins/nspages/printers/printerOneLine.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?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 'printer.php';
|
||||
|
||||
class nspages_printerOneLine extends nspages_printer {
|
||||
function __construct($plugin, $mode, $renderer, $data){
|
||||
parent::__construct($plugin, $mode, $renderer, $data);
|
||||
}
|
||||
|
||||
function _print($tab, $type) {
|
||||
$sep = '';
|
||||
foreach($tab as $item) {
|
||||
$this->renderer->cdata($sep);
|
||||
$this->_printElementLink($item);
|
||||
$sep = ', ';
|
||||
}
|
||||
}
|
||||
|
||||
function printTransition(){
|
||||
$this->renderer->cdata(', ');
|
||||
}
|
||||
}
|
59
lib/plugins/nspages/printers/printerPictures.php
Normal file
59
lib/plugins/nspages/printers/printerPictures.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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 'printer.php';
|
||||
|
||||
class nspages_printerPictures extends nspages_printer {
|
||||
private static $_dims = array('w' => 350, 'h' => 220);
|
||||
|
||||
function __construct($plugin, $mode, $renderer, $data){
|
||||
parent::__construct($plugin, $mode, $renderer, $data);
|
||||
$this->_defaultPicture = $data['defaultPicture'];
|
||||
}
|
||||
|
||||
function _print($tab, $type) {
|
||||
$this->renderer->doc .= '<div class="nspagesPicturesModeMain">';
|
||||
foreach($tab as $item) {
|
||||
$picture = $this->_getFirstImage($item['id']);
|
||||
$url = wl($item['id']);
|
||||
|
||||
// TODO: implement support for non-HTML mode
|
||||
// Note that, wrt indexing, it's not an issue to build a <a> ourselves instead of using the api
|
||||
// because non xhtml mode (eg: "metadata" mode) isn't plugged on this xhtml specific code
|
||||
$optionalId = '';
|
||||
if ($this->includeItemsInTOC) {
|
||||
$anchorId = $this->buildAnchorId($item);
|
||||
$optionalId = 'id="' . $anchorId . '"';
|
||||
$this->renderer->toc_additem($anchorId, $item['nameToDisplay'], $this->renderer->getLastLevel() + 1);
|
||||
}
|
||||
|
||||
$this->renderer->doc .= '<a href="'. $url .'" title="'.$item['nameToDisplay'].'">';
|
||||
$this->renderer->doc .= '<div class="nspagesPicturesModeImg" style="background-image:url('. $picture .')" ' . $optionalId . '>';
|
||||
$this->renderer->doc .= '<span class="nspagesPicturesModeTitle">'.$item['nameToDisplay'];
|
||||
if ( $this->_displayModificationDate ){
|
||||
$this->renderer->doc .= '</span><span class="nspagesPicturesDate">' . date('d/m/Y', $item['mtime']);
|
||||
}
|
||||
$this->renderer->doc .= '</span></div></a>';
|
||||
}
|
||||
$this->renderer->doc .= '</div>';
|
||||
}
|
||||
|
||||
private function _getFirstImage($pageId){
|
||||
$meta = p_get_metadata($pageId);
|
||||
$picture = $meta['relation']['firstimage'];
|
||||
if ( $picture != "" ){
|
||||
return ml($picture, self::$_dims, true);
|
||||
} else {
|
||||
if ( $this->_defaultPicture == '' ){
|
||||
return "lib/tpl/dokuwiki/images/logo.png";
|
||||
} else {
|
||||
return ml($this->_defaultPicture, self::$_dims, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
46
lib/plugins/nspages/printers/printerSimpleList.php
Normal file
46
lib/plugins/nspages/printers/printerSimpleList.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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 'printer.php';
|
||||
|
||||
class nspages_printerSimpleList extends nspages_printer {
|
||||
private $useNumberedList;
|
||||
|
||||
function __construct($plugin, $mode, $renderer, $data, $useNumberedList = false){
|
||||
parent::__construct($plugin, $mode, $renderer, $data);
|
||||
$this->useNumberedList = $useNumberedList;
|
||||
}
|
||||
|
||||
function _print($tab, $type) {
|
||||
$this->_openList();
|
||||
$this->_printItems($tab);
|
||||
$this->_closeList();
|
||||
}
|
||||
|
||||
private function _openList() {
|
||||
if ( $this->useNumberedList ){
|
||||
$this->renderer->listo_open();
|
||||
} else {
|
||||
$this->renderer->listu_open();
|
||||
}
|
||||
}
|
||||
|
||||
private function _printItems($tab){
|
||||
foreach($tab as $item) {
|
||||
$this->_printElement($item);
|
||||
}
|
||||
}
|
||||
|
||||
private function _closeList() {
|
||||
if ( $this->useNumberedList ){
|
||||
$this->renderer->listo_close();
|
||||
} else {
|
||||
$this->renderer->listu_close();
|
||||
}
|
||||
}
|
||||
}
|
213
lib/plugins/nspages/printers/printerTree.php
Normal file
213
lib/plugins/nspages/printers/printerTree.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?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 'printer.php';
|
||||
|
||||
class nspages_printerTree extends nspages_printer {
|
||||
private $rootNS;
|
||||
|
||||
function __construct($plugin, $mode, $renderer, $data){
|
||||
parent::__construct($plugin, $mode, $renderer, $data);
|
||||
$this->rootNS = $data['wantedNS'] . ':';
|
||||
}
|
||||
|
||||
function _print($tab, $type) {
|
||||
$tree = $this->_groupByNs($tab);
|
||||
$trimmedTree = $this->_getTrimmedTree($tree);
|
||||
$orderedTree = $this->_orderTree($trimmedTree);
|
||||
$this->_printTree($orderedTree);
|
||||
}
|
||||
|
||||
/**
|
||||
* We received the nodes all ordered together, but building the tree has probably
|
||||
* lost the order for namespaces, we hence need to sort again each node
|
||||
*/
|
||||
function _orderTree($tree) {
|
||||
// We only need to sort "children". We don't need to sort "pages" because with the current
|
||||
// workflow of the plugin nodes are provided already sorted to _print, and the way we
|
||||
// build the tree preserves the order of the pages.
|
||||
// An optimization could be to disable the preliminary sort and to instead sort pages here.
|
||||
// That could save some CPU cycles because instead of sorting a big list we would sort
|
||||
// several smaller ones. However it would require
|
||||
// - a regression test which assert on the order of the pages when a flag is passed to
|
||||
// have a custom sort (eg: "-h1") to ensure we don't have the correct order just because
|
||||
// the DW search API returned sorted results based on the id of the pages
|
||||
// - benchmarking (because it could be detrimental if usort has a constant overhead which
|
||||
// would make several small sort more costly than a single one bigger)
|
||||
$this->_sorter->sort($tree->children);
|
||||
|
||||
foreach($tree->children as $subTree){
|
||||
$this->_orderTree($subTree);
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
|
||||
private function _groupByNs($tab) {
|
||||
$tree = new NspagesTreeNsNode(':');
|
||||
foreach($tab as $item){
|
||||
$this->_fillTree($tree, $this->_getNS($item), $item, '');
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rid of the "trunk" of the tree. ie: remove the first "empty" nodes. It prevents printing
|
||||
* something like
|
||||
* - A
|
||||
* - B
|
||||
* - C
|
||||
* - page1
|
||||
* - page2
|
||||
* - page3
|
||||
* when the ns the user asked for is actully ns C
|
||||
*/
|
||||
private function _getTrimmedTree($tree){
|
||||
if ($tree->id === $this->rootNS){
|
||||
return $tree;
|
||||
} else {
|
||||
if (is_null($tree->children)) {
|
||||
// This case should never happen. But I handle it neverthelss because if I'm wrong
|
||||
// then the recursion will never end
|
||||
return $tree;
|
||||
}
|
||||
$firstAndOnlyChild = reset($tree->children);
|
||||
return $this->_getTrimmedTree($firstAndOnlyChild);
|
||||
}
|
||||
}
|
||||
|
||||
private function _getNS($item) {
|
||||
if($item['type'] === 'd'){
|
||||
// If $item is itself a namespace then:
|
||||
// - its 'id' will look like 'a:b:c:'
|
||||
// - its 'ns' will look like 'a:b''
|
||||
// What we want is array ['a', 'b', 'c']
|
||||
|
||||
$IdSplit = explode(':', $item['id']);
|
||||
array_pop($IdSplit); // Remove the last element (which is "empty string" because of the final colon
|
||||
return $IdSplit;
|
||||
} else {
|
||||
// It $item is a page then:
|
||||
// - its 'id' will look like 'a:b:page'
|
||||
// - its 'ns' will look like 'a:b'
|
||||
// What we want is array ['a', 'b']
|
||||
if ($item['ns'] === false) {
|
||||
// Special case of the pages at the root of the wiki: for them "ns" is set to boolean FALSE
|
||||
return array();
|
||||
} else {
|
||||
return explode(':', $item['ns']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function _fillTree($tree, $keys, $item, $parentId) {
|
||||
if (empty($keys)){ // We've reach the end of the journey. We register the data of $item
|
||||
if($item['type'] === 'd') {
|
||||
$tree->self = $item;
|
||||
} else {
|
||||
$tree->pages []= $item;
|
||||
}
|
||||
} else { // We're not at the place of $item in the tree yet, we continue to go down
|
||||
$key = $keys[0];
|
||||
$currentId = $parentId . $key . ':';
|
||||
if (!array_key_exists($key, $tree->children)){
|
||||
$node = new NspagesTreeNsNode($currentId);
|
||||
$tree->children[$key] = $node;
|
||||
}
|
||||
array_shift($keys);
|
||||
$this->_fillTree($tree->children[$key], $keys, $item, $currentId);
|
||||
}
|
||||
}
|
||||
|
||||
private function _printTree($tree) {
|
||||
$this->renderer->listu_open();
|
||||
|
||||
foreach($tree->children as $subTree){
|
||||
$this->_printSubTree($subTree, 1);
|
||||
}
|
||||
|
||||
foreach($tree->pages as $page){
|
||||
$this->_printElement($page, 1);
|
||||
}
|
||||
|
||||
$this->renderer->listu_close();
|
||||
}
|
||||
|
||||
private function _printSubTree($tree, $level) {
|
||||
$this->_printElementOpen($level);
|
||||
if ( !is_null($tree->self) ){
|
||||
$this->_printElementContent($tree->self, $level);
|
||||
} else {
|
||||
$this->renderer->doc .= '<div>' . $tree->id . '</div>';
|
||||
}
|
||||
|
||||
$hasInnerData = !empty($tree->children) || !empty($tree->pages);
|
||||
if($hasInnerData){
|
||||
$this->renderer->listu_open();
|
||||
}
|
||||
foreach($tree->children as $subTree){
|
||||
$this->_printSubTree($subTree, $level+1);
|
||||
}
|
||||
foreach($tree->pages as $page){
|
||||
$this->_printElement($page, $level+1);
|
||||
}
|
||||
if($hasInnerData){
|
||||
$this->renderer->listu_close();
|
||||
}
|
||||
$this->_printElementClose();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent a namespace and its inner content
|
||||
*/
|
||||
class NspagesTreeNsNode implements ArrayAccess {
|
||||
/**
|
||||
* The list of pages directly in the namespace (does not include pages in subnamespaces)
|
||||
*/
|
||||
public $pages = array();
|
||||
|
||||
/**
|
||||
* The list of subnamespaces at level n+1 (does not include their own subnamespaces)
|
||||
*/
|
||||
public $children = array();
|
||||
|
||||
/**
|
||||
* The data about the current namespace iteslf. It may be empty in two cases:
|
||||
* - when nspages is displaying only pages (because in that case we did not search for ns)
|
||||
* - when this instance represent the root of the tree (because nspages doesn't display it)
|
||||
*/
|
||||
public $self = null;
|
||||
|
||||
/**
|
||||
* Used to represent the current namespace when we're in a case where we want to display it
|
||||
* but when $self is empty.
|
||||
* In practice it is used to represent namespace nodes when we're asked to display pages only
|
||||
*/
|
||||
public $id = null;
|
||||
|
||||
function __construct($id){
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement ArrayAccess because instances of this class should be sortable with nspages_sorter
|
||||
* implementations and that those implementation are performing sorts based on $item["sort"].
|
||||
*/
|
||||
public function offsetSet($offset, $value) {
|
||||
throw new BadMethodCallException("Not implemented by design");
|
||||
}
|
||||
public function offsetExists($offset) {
|
||||
return $offset == "sort";
|
||||
}
|
||||
public function offsetUnset($offset) {
|
||||
unset($this->container[$offset]);
|
||||
}
|
||||
public function offsetGet($offset) {
|
||||
return $this->offsetExists($offset) ? $this->self["sort"] : null;
|
||||
}
|
||||
}
|
65
lib/plugins/nspages/printers/rendererXhtmlHelper.php
Normal file
65
lib/plugins/nspages/printers/rendererXhtmlHelper.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?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();
|
||||
|
||||
class rendererXhtmlHelper {
|
||||
private $renderer;
|
||||
private $percentWidth;
|
||||
private $plugin;
|
||||
private $anchorName;
|
||||
|
||||
function __construct($renderer, $nbCols, $plugin, $anchorName){
|
||||
$this->renderer =& $renderer;
|
||||
$this->percentWidth = $this->buildWidth($nbCols);
|
||||
$this->plugin = $plugin;
|
||||
$this->anchorName = $anchorName;
|
||||
}
|
||||
|
||||
private function buildWidth($nbCols){
|
||||
return (100 / $nbCols) . '%';
|
||||
}
|
||||
|
||||
function printHeaderChar($char, $continued = false){
|
||||
$text = $char;
|
||||
if ( $continued ){
|
||||
$text .= $this->plugin->getLang('continued');
|
||||
}
|
||||
|
||||
$this->renderer->doc .= '<div '
|
||||
. $this->fullAnchor($char, $continued)
|
||||
. 'class="catpagechars';
|
||||
if ( $continued ){
|
||||
$this->renderer->doc .= ' continued';
|
||||
}
|
||||
$this->renderer->doc .= '">' . $text . "</div>\n";
|
||||
}
|
||||
|
||||
private function fullAnchor($char, $continued){
|
||||
if ( $continued === true || is_null($this->anchorName) ){
|
||||
return '';
|
||||
}
|
||||
|
||||
return 'id="nspages_' . $this->anchorName . '_' . $char . '" ';
|
||||
}
|
||||
|
||||
function openColumn(){
|
||||
$this->renderer->doc .= "\n".'<div class="catpagecol" style="width: '.$this->percentWidth.'" >';
|
||||
}
|
||||
|
||||
function closeColumn(){
|
||||
$this->renderer->doc .= "</div>\n";
|
||||
}
|
||||
|
||||
function openListOfItems(){
|
||||
$this->renderer->doc .= "<ul class=\"nspagesul\">\n";
|
||||
}
|
||||
|
||||
function closeListOfItems(){
|
||||
$this->renderer->doc .= '</ul>';
|
||||
}
|
||||
}
|
70
lib/plugins/nspages/printers/sorters.php
Normal file
70
lib/plugins/nspages/printers/sorters.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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 nspages_sorter {
|
||||
protected $reverse;
|
||||
|
||||
function __construct($reverse){
|
||||
$this->reverse = $reverse;
|
||||
}
|
||||
|
||||
function sort(&$array){
|
||||
$this->actualSort($array);
|
||||
if ($this->reverse) {
|
||||
$array = array_reverse($array);
|
||||
}
|
||||
}
|
||||
|
||||
protected function actualSort(&$array){
|
||||
usort($array, array($this, 'comparator'));
|
||||
}
|
||||
|
||||
abstract function comparator($item1, $item2);
|
||||
}
|
||||
|
||||
class nspages_default_sorter extends nspages_sorter {
|
||||
function __construct($reverse){
|
||||
parent::__construct($reverse);
|
||||
}
|
||||
|
||||
function comparator($item1, $item2){
|
||||
return strcasecmp($item1['sort'], $item2['sort']);
|
||||
}
|
||||
}
|
||||
|
||||
class nspages_naturalOrder_sorter extends nspages_sorter {
|
||||
function __construct($reverse){
|
||||
parent::__construct($reverse);
|
||||
}
|
||||
|
||||
function comparator($item1, $item2){
|
||||
return strnatcasecmp($item1['sort'], $item2['sort']);
|
||||
}
|
||||
}
|
||||
|
||||
class nspages_dictOrder_sorter extends nspages_sorter {
|
||||
private $dictOrder;
|
||||
|
||||
function __construct($reverse, $dictOrder){
|
||||
parent::__construct($reverse);
|
||||
$this->dictOrder = $dictOrder;
|
||||
}
|
||||
|
||||
function actualSort(&$array){
|
||||
$oldLocale=setlocale(LC_ALL, 0);
|
||||
setlocale(LC_COLLATE, $this->dictOrder);
|
||||
usort($array, array($this, "comparator"));
|
||||
setlocale(LC_COLLATE, $oldLocale);
|
||||
}
|
||||
|
||||
function comparator($item1, $item2){
|
||||
return strcoll($item1['sort'], $item2['sort']);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user