1<?php
2/**
3 * Plugin nspages : Displays nicely a list of the pages of a namespace
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Guillaume Turri <guillaume.turri@gmail.com>
7 * @author  Daniel Schranz <xla@gmx.at>
8 * @author  Ignacio Bergmann
9 * @author  Andreas Gohr <gohr@cosmocode.de>
10 * @author  Ghassem Tofighi <ghassem@gmail.com>
11 */
12if(!defined('DOKU_INC')) die();
13require_once 'printers/printerLineBreak.php';
14require_once 'printers/printerOneLine.php';
15require_once 'printers/printerSimpleList.php';
16require_once 'printers/printerNice.php';
17require_once 'printers/printerPictures.php';
18require_once 'printers/printerTree.php';
19require_once 'fileHelper/fileHelper.php';
20require_once 'optionParser.php';
21require_once 'namespaceFinder.php';
22
23/**
24 * All DokuWiki plugins to extend the parser/rendering mechanism
25 * need to inherit from this class
26 */
27class syntax_plugin_nspages extends DokuWiki_Syntax_Plugin {
28    function connectTo($aMode) {
29        $this->Lexer->addSpecialPattern('<nspages[^>]*>', $aMode, 'plugin_nspages');
30    }
31
32    function getSort() {
33        //Execute before html mode
34        return 189;
35    }
36
37    function getType() {
38        return 'substition';
39    }
40
41    function handle($match, $state, $pos, Doku_Handler $handler) {
42        $return = $this->_getDefaultOptions();
43        $return['pos'] = $pos;
44
45        $match = utf8_substr($match, 8, -1); //9 = strlen("<nspages")
46        $match .= ' ';
47
48        optionParser::checkOption($match, "subns", $return['subns'], true);
49        optionParser::checkOption($match, "nopages", $return['nopages'], true);
50        optionParser::checkOption($match, "simpleListe?", $return['simpleList'], true);
51        optionParser::checkOption($match, "tree", $return['tree'], true);
52        optionParser::checkOption($match, "numberedListe?", $return['numberedList'], true);
53        optionParser::checkOption($match, "simpleLineBreak", $return['lineBreak'], true);
54        optionParser::checkOption($match, "title", $return['title'], true);
55        optionParser::checkOption($match, "idAndTitle", $return['idAndTitle'], true);
56        optionParser::checkOption($match, "h1", $return['title'], true);
57        optionParser::checkOption($match, "simpleLine", $return['simpleLine'], true);
58        optionParser::checkOption($match, "sort(By)?Id", $return['sortid'], true);
59        optionParser::checkOption($match, "reverse", $return['reverse'], true);
60        optionParser::checkOption($match, "pagesinns", $return['pagesinns'], true);
61        optionParser::checkOption($match, "nat(ural)?Order", $return['natOrder'], true);
62        optionParser::checkOption($match, "sort(By)?Date", $return['sortDate'], true);
63        optionParser::checkOption($match, "sort(By)?CreationDate", $return['sortByCreationDate'], true);
64        optionParser::checkOption($match, "hidenopages", $return['hidenopages'], true);
65        optionParser::checkOption($match, "hidenosubns", $return['hidenosubns'], true);
66        optionParser::checkOption($match, "showhidden", $return['showhidden'], true);
67        optionParser::checkOption($match, "(use)?Pictures?", $return['usePictures'], true);
68        optionParser::checkOption($match, "(modification)?Dates?OnPictures?", $return['modificationDateOnPictures'], true);
69        optionParser::checkOption($match, "displayModificationDates?", $return["displayModificationDate"], true);
70        optionParser::checkOption($match, "includeItemsInTOC", $return["includeItemsInTOC"], true);
71        optionParser::checkRecurse($match, $return['maxDepth']);
72        optionParser::checkNbColumns($match, $return['nbCol']);
73        optionParser::checkSimpleStringArgument($match, $return['textPages'], $this, 'textPages');
74        optionParser::checkSimpleStringArgument($match, $return['customTitle'], $this, 'customTitle');
75        optionParser::checkSimpleStringArgument($match, $return['sortByMetadata'], $this, 'sortByMetadata');
76        optionParser::checkSimpleStringArgument($match, $return['textNS'], $this, 'textNS');
77        optionParser::checkDictOrder($match, $return['dictOrder'], $this);
78        optionParser::checkRegEx($match, "pregPages?On=\"([^\"]*)\"", $return['pregPagesOn']);
79        optionParser::checkRegEx($match, "pregPages?Off=\"([^\"]*)\"", $return['pregPagesOff']);
80        optionParser::checkRegEx($match, "pregPages?TitleOn=\"([^\"]*)\"", $return['pregPagesTitleOn']);
81        optionParser::checkRegEx($match, "pregPages?TitleOff=\"([^\"]*)\"", $return['pregPagesTitleOff']);
82        optionParser::checkRegEx($match, "pregNSOn=\"([^\"]*)\"", $return['pregNSOn']);
83        optionParser::checkRegEx($match, "pregNSOff=\"([^\"]*)\"", $return['pregNSOff']);
84        optionParser::checkRegEx($match, "pregNSTitleOn=\"([^\"]*)\"", $return['pregNSTitleOn']);
85        optionParser::checkRegEx($match, "pregNSTitleOff=\"([^\"]*)\"", $return['pregNSTitleOff']);
86        optionParser::checkNbItemsMax($match, $return['nbItemsMax']);
87        optionParser::checkGlobalExclude($this->getConf('global_exclude'), $return['excludedPages'], $return['excludedNS']);
88        optionParser::checkExclude($match, $return['excludedPages'], $return['excludedNS']);
89        optionParser::checkAnchorName($match, $return['anchorName']);
90        optionParser::checkActualTitle($match, $return['actualTitleLevel']);
91        optionParser::checkSimpleStringArgument($match, $return['defaultPicture'], $this, 'defaultPicture');
92
93        //Now, only the wanted namespace remains in $match
94        $nsFinder = new namespaceFinder($match);
95        $return['wantedNS'] = $nsFinder->getWantedNs();
96        $return['safe'] = $nsFinder->isNsSafe();
97        $return['wantedDir'] = $nsFinder->getWantedDirectory();
98
99        return $return;
100    }
101
102    private function _getDefaultOptions(){
103        return array(
104            'subns'         => false, 'nopages' => false, 'simpleList' => false, 'lineBreak' => false,
105            'excludedPages' => array(), 'excludedNS' => array(),
106            'title'         => false, 'wantedNS' => '', 'wantedDir' => '', 'safe' => true,
107            'textNS'        => '', 'textPages' => '', 'pregPagesOn' => array(),
108            'customTitle'   => null,
109            'pregPagesOff'  => array(), 'pregNSOn' => array(), 'pregNSOff' => array(),
110            'pregPagesTitleOn' => array(), 'pregPagesTitleOff' => array(),
111            'pregNSTitleOn' => array(), 'pregNSTitleOff' => array(),
112            'maxDepth'      => (int) 1, 'nbCol' => 3, 'simpleLine' => false,
113            'sortid'        => false, 'reverse' => false,
114            'pagesinns'     => false, 'anchorName' => null, 'actualTitleLevel' => false,
115            'idAndTitle'    => false, 'nbItemsMax' => 0, 'numberedList' => false,
116            'natOrder'      => false, 'sortDate' => false,
117            'hidenopages'   => false, 'hidenosubns' => false, 'usePictures' => false,
118            'showhidden'    => false, 'dictOrder' => false,
119            'modificationDateOnPictures' => false,
120            'displayModificationDate' => false,
121            'sortByCreationDate' => false, 'defaultPicture' => null, 'tree' => false,
122            'includeItemsInTOC' => false
123        );
124    }
125
126    function render($mode, Doku_Renderer $renderer, $data) {
127        $this->_deactivateTheCacheIfNeeded($renderer);
128
129        if ($data['modificationDateOnPictures']){
130            action_plugin_nspages::logUseLegacySyntax();
131        }
132
133        //Load lang now rather than at handle-time, otherwise it doesn't
134        //behave well with the translation plugin (it seems like we cache strings
135        //even if the lang doesn't match)
136        $this->_denullifyLangOptions($data);
137        $this->_denullifyPictureOptions($data);
138        $printer = $this->_selectPrinter($mode, $renderer, $data);
139
140        if( ! $this->_isNamespaceUsable($data)){
141            $printer->printUnusableNamespace($data['wantedNS']);
142            return TRUE;
143        }
144
145        $fileHelper = new fileHelper($data, $this->getConf('custom_title_allow_list_metadata'));
146        $pages = $fileHelper->getPages();
147        $subnamespaces = $fileHelper->getSubnamespaces();
148        if ( $this->_shouldPrintPagesAmongNamespaces($data) ){
149            $subnamespaces = array_merge($subnamespaces, $pages);
150        }
151
152        $printer->printBeginning();
153        $this->_print($printer, $data, $subnamespaces, $pages);
154        $printer->printEnd();
155        return TRUE;
156    }
157
158    function _denullifyLangOptions(&$data){
159        if ( is_null($data['textNS']) ){
160            $data['textNS'] = $this->getLang('subcats');
161        }
162
163        if ( is_null($data['textPages']) ){
164            $data['textPages'] = $this->getLang('pagesinthiscat');
165        }
166    }
167
168    function _denullifyPictureOptions(&$data){
169        if ( is_null($data['defaultPicture']) ){
170            $data['defaultPicture'] = $this->getConf('default_picture');
171        }
172    }
173
174    private function _shouldPrintPagesAmongNamespaces($data){
175        return $data['pagesinns'];
176    }
177
178    private function _print($printer, $data, $subnamespaces, $pages){
179        if($data['subns']) {
180            $printer->printTOC($subnamespaces, 'subns', $data['textNS'], $data['hidenosubns']);
181        }
182
183        if(!$this->_shouldPrintPagesAmongNamespaces($data)) {
184
185            if ( $this->_shouldPrintTransition($data) ){
186              $printer->printTransition();
187            }
188
189            if(!$data['nopages']) {
190                $printer->printTOC($pages, 'page', $data['textPages'], $data['hidenopages']);
191            }
192        }
193    }
194
195    private function _shouldPrintTransition($data){
196        return $data['textPages'] === '' && !$data['nopages'] && $data['subns'];
197    }
198
199    private function _isNamespaceUsable($data){
200        global $conf;
201        return @opendir($conf['datadir'] . '/' . $data['wantedDir']) !== false && $data['safe'];
202    }
203
204    private function _selectPrinter($mode, &$renderer, $data){
205        if($data['simpleList']) {
206            return new nspages_printerSimpleList($this, $mode, $renderer, $data);
207        } else if($data['numberedList']){
208            return new nspages_printerSimpleList($this, $mode, $renderer, $data, true);
209        } else if($data['simpleLine']) {
210            return new nspages_printerOneLine($this, $mode, $renderer, $data);
211        } else if ($data['lineBreak']){
212            return new nspages_printerLineBreak($this, $mode, $renderer, $data);
213        } else if ($data['usePictures'] && $mode == 'xhtml') { //This printer doesn't support non html mode yet
214            return new nspages_printerPictures($this, $mode, $renderer, $data);
215        } else if ($data['tree']) {
216            return new nspages_printerTree($this, $mode, $renderer, $data);
217        } else if($mode == 'xhtml') {
218            return new nspages_printerNice($this, $mode, $renderer, $data['nbCol'], $data['anchorName'], $data);
219        }
220        return new nspages_printerSimpleList($this, $mode, $renderer, $data);
221    }
222
223    private function _deactivateTheCacheIfNeeded(&$renderer) {
224        if ($this->getConf('cache') == 1){
225            $renderer->nocache(); //disable cache
226        }
227    }
228}
229