1<?php
2/**
3 * Plugin page index: index table for pages in a name space
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Kite <Kite@puzzlers.org>
7 * @based_on   "externallink" plugin by Otto Vainio <plugins@valjakko.net>
8 */
9
10if(!defined('DOKU_INC')) {
11 define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
12}
13if(!defined('DOKU_PLUGIN')) {
14 define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15}
16
17require_once(DOKU_PLUGIN.'syntax.php');
18require_once(DOKU_INC.'inc/search.php');
19
20
21function search_list_index(&$data,$base,$file,$type,$lvl,$opts){
22 global $ID;
23 //we do nothing with directories
24 if($type == 'd') return false;
25 if(preg_match('#\.txt$#',$file)){
26 //check ACL
27  $id = pathID($file);
28  if(auth_quickaclcheck($id) < AUTH_READ){
29  return false;
30 }
31 if($opts['ns'].":$id" <> $ID) {
32  $data[] = array(
33   'id'    => $opts['ns'].":$id",
34   'type'  => $type,
35   'level' => $lvl );
36   }
37 }
38 return false;
39}
40
41
42
43
44/**
45 * All DokuWiki plugins to extend the parser/rendering mechanism
46 * need to inherit from this class
47 */
48class syntax_plugin_pageindex extends DokuWiki_Syntax_Plugin {
49
50 /**
51  * return some info
52  */
53 function getInfo(){
54  return array(
55   'author' => 'Kite',
56   'email'  => 'kite@puzzlers.org',
57   'date'   => '2009-02-01',
58   'name'   => 'Page Index',
59   'desc'   => 'Presents an index list of files in the current namespace',
60   'url'    => 'https://www.dokuwiki.org/plugin:pageindex',
61  );
62 }
63
64 /**
65  * What kind of syntax are we?
66  */
67 function getType(){
68  return 'substition';
69 }
70
71 // Just before build in links
72 function getSort(){ return 299; }
73
74 /**
75  * What about paragraphs?
76  */
77 function getPType(){
78  return 'block';
79 }
80
81 function connectTo($mode) {
82    $this->Lexer->addSpecialPattern('~~PAGEINDEX[^~]*~~',$mode,'plugin_pageindex');
83    //$this->Lexer->addSpecialPattern('~~PAGEINDEX~~',$mode,'plugin_pageindex');
84 }
85
86
87 /**
88  * Handle the match
89  */
90 function handle($match, $state, $pos, &$handler){
91  $match = preg_replace("%~~PAGEINDEX(=(.*))?~~%", "\\2", $match);
92  //echo "\n\t<!-- syntax_plugin_pageindex.handle() found >> $match << -->\n";
93  return $match;
94 }
95
96 /**
97  * Create output
98  */
99 function render($mode, &$renderer, $data) {
100  if($mode == 'xhtml'){
101   $text=$this->_pageindex($renderer, $data);
102   $renderer->doc .= $text;
103   return true;
104  }
105  return false;
106 }
107
108
109 function _pageindex(&$renderer, $data) {
110  global $conf;
111  global $ID;
112
113  //$renderer->doc .= "\n\n<!-- syntax_plugin_pageindex._pageindex(\$renderer, \"$data\") -->\n";
114  $parameters = split(';', $data);
115  $ns  = cleanID(getNS("$parameters[0]:dummy"));
116  #fixme use appropriate function
117  if(empty($ns)){
118   $ns = dirname(str_replace(':',DIRECTORY_SEPARATOR,$ID));  // 2007/12/30 Kite - use localized constant
119   if($ns == '.') $ns ='';
120  }
121  //$ns  = utf8_encodeFN(str_replace(':',DIRECTORY_SEPARATOR,$ns));   // 2007/12/30 Kite - use localized constant
122  //$ns  = utf8_encodeFN($ns);
123
124  $search_data = array();   // Oct 3, 2006 renamed $data to $search_data for clarity
125  $dir = $conf['datadir']. DIRECTORY_SEPARATOR .str_replace(':',DIRECTORY_SEPARATOR,$ns);   // 2007/12/30 Kite - use localized constant
126  $ns = str_replace(DIRECTORY_SEPARATOR,':',$ns);
127  $renderer->doc .= "\n<!-- \$dir = $dir  \$ns = $ns -->\n";
128  search($search_data,          // results   == renamed $data to $search_data
129   $dir,                  // folder root
130   'search_list_index',   // handler
131   array('ns' => $ns));   // options
132  // Remove the items not wanted in the list
133  if(is_array($parameters)) {
134   $skipitems = array_slice($parameters, 1);
135   foreach($search_data as $item) {
136    $found = false;
137        // Add ns if user didn't
138        foreach($skipitems as $skip) {
139         $skip = strpos($skip,":") ? $skip : "$ns:$skip";
140         if($item['id'] == $skip) {
141          $found = true;
142          break;
143     }
144    }
145    if(!$found) {
146     // Pass this one through
147     $checked[] = $item;
148    } else {
149     //$renderer->doc .= "<!-- rejected entry ".$item['id']." -->\n";
150    }
151   }
152  }
153
154  if(count($checked)) {  // use the filtered data rather than $search_data
155/* Option to use an HTML List */
156
157   $renderer->doc .= html_buildlist($checked,
158    'idx',
159    'html_list_index',
160    'html_li_index');
161
162/* Option to use the PageList plugin */
163/*
164      $pages = $checked;
165      $pagelist =& plugin_load('helper', 'pagelist');
166      if (!$pagelist) return false; // failed to load plugin
167      $pagelist->startList();
168      foreach ($pages as $page){
169       $pagelist->addPage($page);
170      }
171      $renderer->doc .= $pagelist->finishList();
172*/
173  } else {
174   $renderer->doc .= "\n\t<p>There are no documents to show.</p>\n";
175  }
176 } // _pageindex()
177} // syntax_plugin_pageindex
178
179