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