1<?php 2/** 3 * DokuWiki Plugin tagsections (Helper Component) 4 * Based up on the tagfilter helper component 5 * 6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7 * @author lisps 8 */ 9 10// must be run within Dokuwiki 11if (!defined('DOKU_INC')) die(); 12 13if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 14if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 15 16require_once(DOKU_INC.'inc/indexer.php'); 17 18class helper_plugin_tagsections extends DokuWiki_Plugin { 19 20 protected $Htag; 21 22 function __construct() { 23 if (plugin_isdisabled('tag') || (!$this->Htag = plugin_load('helper', 'tag'))) { 24 msg('tag plugin is required by tagsections plugin, but missing', -1); 25 return false; 26 } 27 } 28 29 function getMethods() { 30 $result = array(); 31 $result[] = array( 32 'name' => 'getTagsByRegExp', 33 'desc' => 'returns tags for given Regular Expression', 34 'params' => array( 35 'tags (required)' => 'string', 36 'namespace (optional)' => 'string',), 37 'return' => array('tags' => 'array'), 38 ); 39 $result[] = array( 40 'name' => 'getTagsByNamespace', 41 'desc' => 'returns tags for given namespace', 42 'params' => array( 43 'namespace' => 'string',), 44 'return' => array('tags' => 'array'), 45 ); 46 $result[] = array( 47 'name' => 'getTagsBySiteID', 48 'desc' => 'returns tags for given siteID', 49 'params' => array( 50 'siteID' => 'string',), 51 'return' => array('tags' => 'array'), 52 ); 53 54 return $result; 55 } 56 57 /** 58 * Search index for tags using preg_match 59 * @param tags 60 * @param $ns 61 * return tags 62 */ 63 function getTagsByRegExp($tag_expr = null, $ns = '',$acl_safe = false){ 64 $Htag = $this->Htag; 65 if(!$Htag) return false; 66 $tags = array_map('trim', idx_getIndex('subject','_w')); 67 $tag_label_r = array(); 68 foreach($tags as $tag){ 69 if( (is_null($tag_expr) || @preg_match('/^'.$tag_expr.'$/i',$tag)) && $this->_checkTagInNamespace($tag,$ns,$acl_safe)){ 70 //$label =stristr($tag,':'); 71 $label = strrchr($tag,':'); 72 $label = $label !=''?$label:$tag; 73 $tag_label_r[$tag] = ucwords(trim(str_replace('_',' ',trim($label,':')))); 74 } 75 } 76 asort($tag_label_r); 77 return $tag_label_r; 78 } 79 80 /* 81 * Return all tags for a defined namespace 82 * @param namespace 83 * @param acl_safe 84 * @return tags for namespace 85 */ 86 function getTagsByNamespace($ns = '',$acl_safe = true){ 87 return array_keys($this->getTagsByRegExp(null, $ns, $acl_safe)); 88 } 89 90 /* 91 * Return all tags for a defined site 92 * @param siteID 93 * @return tags for site 94 */ 95 function getTagsBySiteID($siteID){ 96 $meta = p_get_metadata($siteID,'subject'); 97 if($meta === NULL) $meta=array(); 98 return $meta; 99 } 100 101 function _tagCompare ($tag1,$tag2){ 102 return $tag1==$tag2; 103 } 104 private function _checkTagInNamespace($tag,$ns,$acl_safe=true){ 105 $Htag = $this->Htag; 106 if(!$Htag) return false; 107 if($ns == '') return true; 108 $indexer = idx_get_indexer(); 109 $pages = $indexer->lookupKey('subject', $tag, array($this, '_tagCompare')); 110 foreach($page_r as $page){ 111 if($Htag->_isVisible($page,$ns)) { 112 if (!$acl_safe) return true; 113 $perm = auth_quickaclcheck($page); 114 if (!$perm < AUTH_READ) { 115 return true; 116 } 117 } 118 119 } 120 return false; 121 } 122 123 124 /** 125 * Categorysize Tags by the first part before a ':' 126 * @param array $tags Array of tags 127 * <pre> 128 * array('category1:tag1','category1:tag2','category2:tag1','category2:tag2') 129 * </pre> 130 * @returns array multidimensional array 131 * <pre> 132 * [category1] => 'category1:tag1' 133 * => 'category1:tag2' 134 * [category2] => 'category2:tag1' 135 * => 'category2:tag2' 136 * </pre> 137 */ 138 public function categorysizeTags($tags) 139 { 140 $catTags = array(); 141 if ( empty($tags) ) return array(); 142 foreach($tags as $nsTag){ 143 list($category, $tag) = explode(':', $nsTag, 2); 144 if ( empty($tag) ) { 145 $tag = $category; 146 $category = ''; 147 } 148 149 $catTags[$category][$tag]++; 150 } 151 ksort($catTags); 152 return $catTags; 153 } 154} 155