xref: /plugin/publish/syntax.php (revision ff9bc75f95d3b96a66748a18884bf0b9fa97990c)
1<?php
2// see action.php for details
3
4
5// must be run within DokuWiki
6if(!defined('DOKU_INC')) die();
7
8if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9require_once(DOKU_PLUGIN.'syntax.php');
10require_once(DOKU_PLUGIN.'publish/shared.php');
11require_once(DOKU_INC.'inc/search.php');
12
13
14// filter out pages which can't be approved by the current user
15// then check if they need approving
16function search_helper(&$data, $base, $file, $type, $lvl, $opts) {
17  $ns = $opts[0];
18  $valid_ns = $opts[1];
19  if($type == 'd') { return in_sub_namespace($valid_ns, $ns . ':' . str_replace('/', ':', $file)); }
20  if(!preg_match('#\.txt$#', $file)) { return false; }
21  $id = pathID($ns . $file);
22  if(!in_namespace($valid_ns, $id)) { return false; }
23  if(auth_quickaclcheck($id) < AUTH_DELETE) { return false; } //insufficent permissions
24  $meta = p_get_metadata($id);
25  if($meta['approval'][$meta['last_change']['date']]) {
26      # Already approved
27      return false;
28  }
29  $data[] = array($id, $meta['approval'], $meta['last_change']['date']);
30  return false;
31}
32
33function pagesorter($a, $b){
34    $ac = explode(':',$a[0]);
35    $bc = explode(':',$b[0]);
36    $an = count($ac);
37    $bn = count($bc);
38
39    # Same number of elements, can just string sort
40    if($an == $bn) { return strcmp($a[0], $b[0]); }
41
42    # For each level:
43    # If this is not the last element in either list:
44    #   same -> continue
45    #   otherwise strcmp
46    # If this is the last element in either list, it wins
47    $n = 0;
48    while(true) {
49        if($n + 1 == $an) { return -1; }
50        if($n + 1 == $bn) { return 1; }
51        $s = strcmp($ac[$n], $bc[$n]);
52        if($s != 0) { return $s; }
53        $n += 1;
54    }
55}
56
57class syntax_plugin_publish extends DokuWiki_Syntax_Plugin {
58
59    function getInfo(){ return publish_getInfo(); }
60
61    function pattern() { return '\[APPROVALS.*?\]'; }
62    function getType() { return 'substition'; }
63    function getSort() { return 20; }
64    function PType() { return 'block'; }
65    function connectTo($mode) { $this->Lexer->addSpecialPattern($this->pattern(),$mode,'plugin_publish'); }
66    function handle($match, $state, $pos, &$handler){
67        $namespace = substr($match, 11, -1);
68        return array($match, $state, $pos, $namespace);
69    }
70
71    function render($mode, &$renderer, $data) {
72      global $conf;
73
74      if($mode == 'xhtml'){
75          $ns = cleanID(getNS($data[3] . ":dummy"));
76          $dir = $conf['datadir'] . '/' . str_replace(':', '/', $ns);
77          $pages = array();
78          search($pages, $dir, 'search_helper', array($ns, $this->getConf('apr_namespaces')));
79          if(count($pages) == 0) {
80              $renderer->doc .= '<p class="apr_none">' . $this->getLang('apr_p_none') . '</p>';
81              return true;
82          }
83          usort($pages, pagesorter);
84
85          # Output Table
86          $renderer->doc .= '<table class="apr_table"><tr class="apr_head">';
87          $renderer->doc .= '<th class="apr_page">' . $this->getLang('apr_p_hdr_page') . '</th>';
88          $renderer->doc .= '<th class="apr_prev">' . $this->getLang('apr_p_hdr_previous') . '</th>';
89          $renderer->doc .= '<th class="apr_upd">' . $this->getLang('apr_p_hdr_updated') . '</th>';
90          $renderer->doc .= '</tr>';
91          $working_ns = null;
92          foreach($pages as $page) {
93            # $page: 0 -> pagename, 1 -> approval metadata, 2 -> last changed date
94            $this_ns = getNS($page[0]);
95            if($this_ns != $working_ns) {
96                $name_ns = $this_ns;
97                if($this_ns == '') { $name_ns = 'root'; }
98                $renderer->doc .= '<tr class="apr_ns"><td colspan="3"><a href="';
99                $renderer->doc .= wl($this_ns . ':' . $this->getConf('start'));
100                $renderer->doc .= '">';
101                $renderer->doc .= $name_ns;
102                $renderer->doc .= '</a></td></tr>';
103                $working_ns = $this_ns;
104            }
105            $updated = '<a href="' . wl($page[0]) . '">' . date('d/m/Y H:i', $page[2]) . '</a>';
106            if($page[1] == null || count($page[1]) == 0) {
107                # Has never been approved
108                $approved = '';
109            }else{
110                $keys = array_keys($page[1]);
111                sort($keys);
112                $last = $keys[count($keys)-1];
113                $approved .= sprintf($this->getLang('apr_p_approved'),
114                                $page[1][$last][1],
115                                wl($page[0], 'rev=' . $last),
116                                date('d/m/Y H:i', $last));
117                if($last == $page[2]) { $updated = 'Unchanged'; } //shouldn't be possible:
118                                                                  //the search_helper should have
119                                                                  //excluded this
120            }
121
122            $renderer->doc .= '<tr class="apr_table';
123            if($approved == '') { $renderer->doc .= ' apr_never'; }
124            $renderer->doc .= '"><td class="apr_page"><a href="';
125            $renderer->doc .= wl($page[0]);
126            $renderer->doc .= '">';
127            $renderer->doc .= $page[0];
128            $renderer->doc .= '</a></td><td class="apr_prev">';
129            $renderer->doc .= $approved;
130            $renderer->doc .= '</td><td class="apr_upd">';
131            $renderer->doc .= $updated;
132            $renderer->doc .= '</td></tr>';
133
134            #$renderer->doc .= '<tr><td colspan="3">' . print_r($page, true) . '</td></tr>';
135          }
136          $renderer->doc .= '</table>';
137          return true;
138      }
139      return false;
140    }
141
142
143
144}
145
146?>
147