1ff9bc75fSJarrod Lowe<?php 2f576111dSAndreas Gohr/** 3f576111dSAndreas Gohr * DokuWiki Plugin publish (Syntax Component) 4f576111dSAndreas Gohr * 5f576111dSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6f576111dSAndreas Gohr * @author Jarrod Lowe <dokuwiki@rrod.net> 7f576111dSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 8f576111dSAndreas Gohr */ 9ff9bc75fSJarrod Lowe 10ff9bc75fSJarrod Lowe 11ff9bc75fSJarrod Lowe// must be run within DokuWiki 12ff9bc75fSJarrod Loweif(!defined('DOKU_INC')) die(); 13ff9bc75fSJarrod Lowe 14ff9bc75fSJarrod Lowe 15ff9bc75fSJarrod Loweclass syntax_plugin_publish extends DokuWiki_Syntax_Plugin { 161317c887SDominik Eckelmann 171317c887SDominik Eckelmann /** 181317c887SDominik Eckelmann * @var helper_plugin_publish 191317c887SDominik Eckelmann */ 20f576111dSAndreas Gohr private $hlp; 21ff9bc75fSJarrod Lowe 22*b9379480SLarsDW223 function __construct(){ 23f576111dSAndreas Gohr $this->hlp = plugin_load('helper','publish'); 24f576111dSAndreas Gohr } 25ff9bc75fSJarrod Lowe 2614c32fa0SDominik Eckelmann function pattern() { 2714c32fa0SDominik Eckelmann return '\[APPROVALS.*?\]'; 2814c32fa0SDominik Eckelmann } 2914c32fa0SDominik Eckelmann 3014c32fa0SDominik Eckelmann function getType() { 3114c32fa0SDominik Eckelmann return 'substition'; 3214c32fa0SDominik Eckelmann } 3314c32fa0SDominik Eckelmann 3414c32fa0SDominik Eckelmann function getSort() { 3514c32fa0SDominik Eckelmann return 20; 3614c32fa0SDominik Eckelmann } 3714c32fa0SDominik Eckelmann 3814c32fa0SDominik Eckelmann function PType() { 3914c32fa0SDominik Eckelmann return 'block'; 4014c32fa0SDominik Eckelmann } 4114c32fa0SDominik Eckelmann 4214c32fa0SDominik Eckelmann function connectTo($mode) { 4314c32fa0SDominik Eckelmann $this->Lexer->addSpecialPattern($this->pattern(),$mode,'plugin_publish'); 4414c32fa0SDominik Eckelmann } 4514c32fa0SDominik Eckelmann 465ccce1abSMichael Große function handle($match, $state, $pos, Doku_Handler $handler){ 47ff9bc75fSJarrod Lowe $namespace = substr($match, 11, -1); 48ff9bc75fSJarrod Lowe return array($match, $state, $pos, $namespace); 49ff9bc75fSJarrod Lowe } 50ff9bc75fSJarrod Lowe 515ccce1abSMichael Große function render($mode, Doku_Renderer $renderer, $data) { 52ff9bc75fSJarrod Lowe global $conf; 53ff9bc75fSJarrod Lowe 5414c32fa0SDominik Eckelmann if($mode != 'xhtml') { 5514c32fa0SDominik Eckelmann return false; 5614c32fa0SDominik Eckelmann } 5714c32fa0SDominik Eckelmann 5814c32fa0SDominik Eckelmann list($match, $state, $pos, $namespace) = $data; 5914c32fa0SDominik Eckelmann 6014c32fa0SDominik Eckelmann $namespace = cleanID(getNS($namespace . ":*")); 6114c32fa0SDominik Eckelmann 629eaf4fb5SMichael Große $pages = $this->hlp->getPagesFromNamespace($namespace); 6314c32fa0SDominik Eckelmann 64ff9bc75fSJarrod Lowe if(count($pages) == 0) { 65ff9bc75fSJarrod Lowe $renderer->doc .= '<p class="apr_none">' . $this->getLang('apr_p_none') . '</p>'; 66ff9bc75fSJarrod Lowe return true; 67ff9bc75fSJarrod Lowe } 6814c32fa0SDominik Eckelmann 69f576111dSAndreas Gohr usort($pages, array($this,'_pagesorter')); 70ff9bc75fSJarrod Lowe 71f576111dSAndreas Gohr // Output Table 72ff9bc75fSJarrod Lowe $renderer->doc .= '<table class="apr_table"><tr class="apr_head">'; 73ff9bc75fSJarrod Lowe $renderer->doc .= '<th class="apr_page">' . $this->getLang('apr_p_hdr_page') . '</th>'; 74ff9bc75fSJarrod Lowe $renderer->doc .= '<th class="apr_prev">' . $this->getLang('apr_p_hdr_previous') . '</th>'; 75ff9bc75fSJarrod Lowe $renderer->doc .= '<th class="apr_upd">' . $this->getLang('apr_p_hdr_updated') . '</th>'; 76ff9bc75fSJarrod Lowe $renderer->doc .= '</tr>'; 7714c32fa0SDominik Eckelmann 7814c32fa0SDominik Eckelmann 79ff9bc75fSJarrod Lowe $working_ns = null; 80ff9bc75fSJarrod Lowe foreach($pages as $page) { 81f576111dSAndreas Gohr // $page: 0 -> pagename, 1 -> approval metadata, 2 -> last changed date 82ff9bc75fSJarrod Lowe $this_ns = getNS($page[0]); 8314c32fa0SDominik Eckelmann 84ff9bc75fSJarrod Lowe if($this_ns != $working_ns) { 85ff9bc75fSJarrod Lowe $name_ns = $this_ns; 86ff9bc75fSJarrod Lowe if($this_ns == '') { $name_ns = 'root'; } 87ff9bc75fSJarrod Lowe $renderer->doc .= '<tr class="apr_ns"><td colspan="3"><a href="'; 88ff9bc75fSJarrod Lowe $renderer->doc .= wl($this_ns . ':' . $this->getConf('start')); 89ff9bc75fSJarrod Lowe $renderer->doc .= '">'; 90ff9bc75fSJarrod Lowe $renderer->doc .= $name_ns; 919d4b9aa0SMichael Große $renderer->doc .= '</a> '; 929d4b9aa0SMichael Große $renderer->doc .= '<button class="publish__approveNS" type="button" ns="' . $name_ns .'">' . $this->getLang('approveNS') . '</button>'; 939d4b9aa0SMichael Große $renderer->doc .= '</td></tr>'; 94ff9bc75fSJarrod Lowe $working_ns = $this_ns; 95ff9bc75fSJarrod Lowe } 9614c32fa0SDominik Eckelmann 9713608ae1SAndreas Gohr $updated = '<a href="' . wl($page[0]) . '">' . dformat($page[2]) . '</a>'; 98ff9bc75fSJarrod Lowe if($page[1] == null || count($page[1]) == 0) { 99f576111dSAndreas Gohr // Has never been approved 100ff9bc75fSJarrod Lowe $approved = ''; 101ff9bc75fSJarrod Lowe }else{ 102ff9bc75fSJarrod Lowe $keys = array_keys($page[1]); 103ff9bc75fSJarrod Lowe sort($keys); 104ff9bc75fSJarrod Lowe $last = $keys[count($keys)-1]; 10514c32fa0SDominik Eckelmann $approved = sprintf($this->getLang('apr_p_approved'), 106ff9bc75fSJarrod Lowe $page[1][$last][1], 107ff9bc75fSJarrod Lowe wl($page[0], 'rev=' . $last), 10813608ae1SAndreas Gohr dformat($last)); 109ff9bc75fSJarrod Lowe if($last == $page[2]) { $updated = 'Unchanged'; } //shouldn't be possible: 110ff9bc75fSJarrod Lowe //the search_helper should have 111ff9bc75fSJarrod Lowe //excluded this 112ff9bc75fSJarrod Lowe } 113ff9bc75fSJarrod Lowe 114ff9bc75fSJarrod Lowe $renderer->doc .= '<tr class="apr_table'; 115ff9bc75fSJarrod Lowe if($approved == '') { $renderer->doc .= ' apr_never'; } 116ff9bc75fSJarrod Lowe $renderer->doc .= '"><td class="apr_page"><a href="'; 117ff9bc75fSJarrod Lowe $renderer->doc .= wl($page[0]); 118ff9bc75fSJarrod Lowe $renderer->doc .= '">'; 119ff9bc75fSJarrod Lowe $renderer->doc .= $page[0]; 120ff9bc75fSJarrod Lowe $renderer->doc .= '</a></td><td class="apr_prev">'; 121ff9bc75fSJarrod Lowe $renderer->doc .= $approved; 122ff9bc75fSJarrod Lowe $renderer->doc .= '</td><td class="apr_upd">'; 123ff9bc75fSJarrod Lowe $renderer->doc .= $updated; 124ff9bc75fSJarrod Lowe $renderer->doc .= '</td></tr>'; 125ff9bc75fSJarrod Lowe 126f576111dSAndreas Gohr //$renderer->doc .= '<tr><td colspan="3">' . print_r($page, true) . '</td></tr>'; 127ff9bc75fSJarrod Lowe } 128ff9bc75fSJarrod Lowe $renderer->doc .= '</table>'; 129ff9bc75fSJarrod Lowe return true; 130ff9bc75fSJarrod Lowe } 13114c32fa0SDominik Eckelmann 132ff9bc75fSJarrod Lowe 133f576111dSAndreas Gohr 134f576111dSAndreas Gohr /** 135f576111dSAndreas Gohr * Custom sort callback 136f576111dSAndreas Gohr */ 137f576111dSAndreas Gohr function _pagesorter($a, $b){ 138f576111dSAndreas Gohr $ac = explode(':',$a[0]); 139f576111dSAndreas Gohr $bc = explode(':',$b[0]); 140f576111dSAndreas Gohr $an = count($ac); 141f576111dSAndreas Gohr $bn = count($bc); 142f576111dSAndreas Gohr 143f576111dSAndreas Gohr // Same number of elements, can just string sort 144f576111dSAndreas Gohr if($an == $bn) { return strcmp($a[0], $b[0]); } 145f576111dSAndreas Gohr 146f576111dSAndreas Gohr // For each level: 147f576111dSAndreas Gohr // If this is not the last element in either list: 148f576111dSAndreas Gohr // same -> continue 149f576111dSAndreas Gohr // otherwise strcmp 150f576111dSAndreas Gohr // If this is the last element in either list, it wins 151f576111dSAndreas Gohr $n = 0; 152f576111dSAndreas Gohr while(true) { 153f576111dSAndreas Gohr if($n + 1 == $an) { return -1; } 154f576111dSAndreas Gohr if($n + 1 == $bn) { return 1; } 155f576111dSAndreas Gohr $s = strcmp($ac[$n], $bc[$n]); 156f576111dSAndreas Gohr if($s != 0) { return $s; } 157f576111dSAndreas Gohr $n += 1; 158f576111dSAndreas Gohr } 159f576111dSAndreas Gohr } 160ff9bc75fSJarrod Lowe 161ff9bc75fSJarrod Lowe} 162ff9bc75fSJarrod Lowe 163