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 getMethods() { 23 $result = array(); 24 $result[] = array( 25 'name' => 'getTagsByRegExp', 26 'desc' => 'returns tags for given Regular Expression', 27 'params' => array( 28 'tags (required)' => 'string', 29 'namespace (optional)' => 'string',), 30 'return' => array('tags' => 'array'), 31 ); 32 $result[] = array( 33 'name' => 'getTagsByNamespace', 34 'desc' => 'returns tags for given namespace', 35 'params' => array( 36 'namespace' => 'string',), 37 'return' => array('tags' => 'array'), 38 ); 39 $result[] = array( 40 'name' => 'getTagsBySiteID', 41 'desc' => 'returns tags for given siteID', 42 'params' => array( 43 'siteID' => 'string',), 44 'return' => array('tags' => 'array'), 45 ); 46 47 return $result; 48 } 49 50 /** 51 * Search index for tags using preg_match 52 * @param tags 53 * @param $ns 54 * return tags 55 */ 56 function getTagsByRegExp($tag_expr = null, $ns = '',$acl_safe = false){ 57 $Htag = $this->Htag; 58 if(!$Htag) return false; 59 $tags = array_map('trim', idx_getIndex('subject','_w')); 60 $tag_label_r = array(); 61 foreach($tags as $tag){ 62 if( (is_null($tag_expr) || @preg_match('/^'.$tag_expr.'$/i',$tag)) && $this->_checkTagInNamespace($tag,$ns,$acl_safe)){ 63 //$label =stristr($tag,':'); 64 $label = strrchr($tag,':'); 65 $label = $label !=''?$label:$tag; 66 $tag_label_r[$tag] = ucwords(trim(str_replace('_',' ',trim($label,':')))); 67 } 68 } 69 asort($tag_label_r); 70 return $tag_label_r; 71 } 72 73 /* 74 * Return all tags for a defined namespace 75 * @param namespace 76 * @param acl_safe 77 * @return tags for namespace 78 */ 79 function getTagsByNamespace($ns = '',$acl_safe = true){ 80 return array_keys($this->getTagsByRegExp(null, $ns, $acl_safe)); 81 } 82 83 /* 84 * Return all tags for a defined site 85 * @param siteID 86 * @return tags for site 87 */ 88 function getTagsBySiteID($siteID){ 89 $meta = p_get_metadata($siteID,'subject'); 90 if($meta === NULL) $meta=array(); 91 return $meta; 92 } 93 94 function _tagCompare ($tag1,$tag2){ 95 return $tag1==$tag2; 96 } 97 private function _checkTagInNamespace($tag,$ns,$acl_safe=true){ 98 $Htag = $this->Htag; 99 if(!$Htag) return false; 100 if($ns == '') return true; 101 $indexer = idx_get_indexer(); 102 $pages = $indexer->lookupKey('subject', $tag, array($this, '_tagCompare')); 103 foreach($page_r as $page){ 104 if($Htag->_isVisible($page,$ns)) { 105 if (!$acl_safe) return true; 106 $perm = auth_quickaclcheck($page); 107 if (!$perm < AUTH_READ) 108 return true; 109 110 } 111 112 } 113 return false; 114 } 115 116 117 /** 118 * Categorysize Tags by the first part before a ':' 119 * @param array $tags Array of tags 120 * <pre> 121 * array('category1:tag1','category1:tag2','category2:tag1','category2:tag2') 122 * </pre> 123 * @returns array multidimensional array 124 * <pre> 125 * [category1] => 'category1:tag1' 126 * => 'category1:tag2' 127 * [category2] => 'category2:tag1' 128 * => 'category2:tag2' 129 * </pre> 130 */ 131 public function categorysizeTags($tags) 132 { 133 $catTags = array(); 134 if ( empty($tags) ) return array(); 135 foreach($tags as $nsTag){ 136 list($category, $tag) = explode(':', $nsTag, 2); 137 if ( empty($tag) ) { 138 $tag = $category; 139 $category = ''; 140 } 141 142 $catTags[$category][$tag]++; 143 } 144 ksort($catTags); 145 return $catTags; 146 } 147} 148