1<?php 2 3// must be run within Dokuwiki 4if (!defined('DOKU_INC')) die(); 5class helper_plugin_approve extends DokuWiki_Plugin { 6 7 /** @var helper_plugin_sqlite */ 8 protected $sqlite; 9 10 /** 11 * @return helper_plugin_sqlite 12 */ 13 protected function sqlite() { 14 if (!$this->sqlite) { 15 /** @var helper_plugin_approve_db $db_helper */ 16 $db_helper = plugin_load('helper', 'approve_db'); 17 $this->sqlite = $db_helper->getDB(); 18 } 19 return $this->sqlite; 20 } 21 22 /** 23 * @param $id 24 * @return bool 25 */ 26 public function use_approve_here($id) { 27 //check for config update 28 $key = 'no_apr_namespaces'; 29 $res = $this->sqlite()->query('SELECT value FROM config WHERE key=?', $key); 30 $no_apr_namespaces_db = $this->sqlite()->res2single($res); 31 $no_apr_namespaces_conf = $this->getConf($key); 32 //update internal config 33 if ($no_apr_namespaces_db != $no_apr_namespaces_conf) { 34 $this->sqlite()->query('UPDATE config SET value=? WHERE key=?', $no_apr_namespaces_conf, $key); 35 36 $res = $this->sqlite()->query('SELECT page, hidden FROM page'); 37 $pages = $this->sqlite()->res2arr($res); 38 foreach ($pages as $page) { 39 $id = $page['page']; 40 $hidden = $page['hidden']; 41 $in_hidden_namespace = $this->in_hidden_namespace($id, $no_apr_namespaces_conf); 42 $new_hidden = $in_hidden_namespace ? '1' : '0'; 43 44 if ($hidden != $new_hidden) { 45 $this->sqlite()->query('UPDATE page SET hidden=? WHERE page=?', $new_hidden, $id); 46 } 47 } 48 } 49 50 $res = $this->sqlite()->query('SELECT page FROM page WHERE page=? AND hidden=0', $id); 51 if ($this->sqlite()->res2single($res)) { 52 return true; 53 } 54 return false; 55 } 56 57 /** 58 * @param $id 59 * @return bool|string 60 */ 61 public function find_last_approved($id) { 62 $res = $this->sqlite()->query('SELECT rev FROM revision 63 WHERE page=? AND approved IS NOT NULL 64 ORDER BY rev DESC LIMIT 1', $id); 65 return $this->sqlite()->res2single($res); 66 } 67 68 public function get_hidden_namespaces_list($no_apr_namespaces=null) { 69 if (!$no_apr_namespaces) { 70 $res = $this->sqlite()->query('SELECT value FROM config WHERE key=?', 'no_apr_namespaces'); 71 $no_apr_namespaces = $this->sqlite()->res2single($res); 72 } 73 74 $no_apr_namespaces_list = preg_split('/\s+/', $no_apr_namespaces,-1, 75 PREG_SPLIT_NO_EMPTY); 76 $no_apr_namespaces_list = array_map(function ($namespace) { 77 return ltrim($namespace, ':'); 78 }, $no_apr_namespaces_list); 79 80 return $no_apr_namespaces_list; 81 } 82 83 /** 84 * @param $id 85 * @return bool|string 86 */ 87 public function in_hidden_namespace($id, $no_apr_namespaces=null) { 88 $no_apr_namespaces_list = $this->get_hidden_namespaces_list($no_apr_namespaces); 89 $id = ltrim($id, ':'); 90 foreach ($no_apr_namespaces_list as $namespace) { 91 if (substr($id, 0, strlen($namespace)) == $namespace) { 92 return true; 93 } 94 } 95 return false; 96 } 97 98// /** 99// * Check if we should use approve in page 100// * 101// * @param string $id 102// * 103// * @return bool 104// */ 105// function use_approve_here($id) { 106// $apr_namespaces = $this->getConf('apr_namespaces'); 107// $no_apr_namespaces = $this->getConf('no_apr_namespaces'); 108// 109// if ($this->in_namespace($no_apr_namespaces, $id)) { 110// return false; 111// //use apr_namespaces 112// } elseif (trim($apr_namespaces) != '') { 113// if ($this->in_namespace($apr_namespaces, $id)) { 114// return true; 115// } 116// return false; 117// } 118// 119// return true; 120// } 121 /** 122 * checks if an id is within one of the namespaces in $namespace_list 123 * 124 * @param string $namespace_list 125 * @param string $id 126 * 127 * @return bool 128 */ 129 function in_namespace($namespace_list, $id) { 130 // PHP apparantly does not have closures - 131 // so we will parse $valid ourselves. Wasteful. 132 $namespace_list = preg_split('/\s+/', $namespace_list); 133 134 //if(count($valid) == 0) { return true; }//whole wiki matches 135 if(count($namespace_list) == 1 && $namespace_list[0] == "") { return false; }//whole wiki matches 136 137 $id = trim($id, ':'); 138 $id = explode(':', $id); 139 140 // Check against all possible namespaces 141 foreach($namespace_list as $namespace) { 142 $namespace = explode(':', $namespace); 143 $current_ns_depth = 0; 144 $total_ns_depth = count($namespace); 145 $matching = true; 146 147 // Check each element, untill all elements of $v satisfied 148 while($current_ns_depth < $total_ns_depth) { 149 if($namespace[$current_ns_depth] != $id[$current_ns_depth]) { 150 // not a match 151 $matching = false; 152 break; 153 } 154 $current_ns_depth += 1; 155 } 156 if($matching) { return true; } // a match 157 } 158 return false; 159 } 160 161 function page_sum($ID, $REV) { 162 $m = p_get_metadata($ID); 163 $changelog = new PageChangeLog($ID); 164 165 //sprawdź status aktualnej strony 166 if ($REV != 0) { 167 $ch = $changelog->getRevisionInfo($REV); 168 $sum = $ch['sum']; 169 } else { 170 $sum = $m['last_change']['sum']; 171 } 172 return $sum; 173 } 174} 175