1f61105deSAdrian Lang<?php 2f61105deSAdrian Lang 3f61105deSAdrian Langif(!defined('DOKU_INC')) die(); 43496cc8aSAndreas Gohr 5e4543b6dSAdrian Langclass helper_plugin_tagging extends DokuWiki_Plugin { 6f61105deSAdrian Lang 7289f50bdSAndreas Gohr /** 8b12334e1SAndreas Gohr * Gives access to the database 9b12334e1SAndreas Gohr * 10b12334e1SAndreas Gohr * Initializes the SQLite helper and register the CLEANTAG function 11b12334e1SAndreas Gohr * 12b12334e1SAndreas Gohr * @return helper_plugin_sqlite|bool false if initialization fails 13289f50bdSAndreas Gohr */ 14289f50bdSAndreas Gohr public function getDB() { 15302a38efSAndreas Gohr static $db = null; 16f61105deSAdrian Lang if(!is_null($db)) { 17f61105deSAdrian Lang return $db; 18f61105deSAdrian Lang } 19f61105deSAdrian Lang 20302a38efSAndreas Gohr /** @var helper_plugin_sqlite $db */ 21f61105deSAdrian Lang $db = plugin_load('helper', 'sqlite'); 22f61105deSAdrian Lang if(is_null($db)) { 23f61105deSAdrian Lang msg('The tagging plugin needs the sqlite plugin', -1); 24f61105deSAdrian Lang return false; 25f61105deSAdrian Lang } 267e6afce6SAndreas Gohr $db->init('tagging', dirname(__FILE__) . '/db/'); 27302a38efSAndreas Gohr $db->create_function('CLEANTAG', array($this, 'cleanTag'), 1); 28f61105deSAdrian Lang return $db; 29f61105deSAdrian Lang } 30f61105deSAdrian Lang 31302a38efSAndreas Gohr /** 322ace74f4SAndreas Gohr * Return the user to use for accessing tags 332ace74f4SAndreas Gohr * 342ace74f4SAndreas Gohr * Handles the singleuser mode by returning 'auto' as user. Returnes false when no user is logged in. 352ace74f4SAndreas Gohr * 362ace74f4SAndreas Gohr * @return bool|string 372ace74f4SAndreas Gohr */ 382ace74f4SAndreas Gohr public function getUser() { 392ace74f4SAndreas Gohr if(!isset($_SERVER['REMOTE_USER'])) return false; 402ace74f4SAndreas Gohr if($this->getConf('singleusermode')) return 'auto'; 412ace74f4SAndreas Gohr return $_SERVER['REMOTE_USER']; 422ace74f4SAndreas Gohr } 432ace74f4SAndreas Gohr 442ace74f4SAndreas Gohr /** 45302a38efSAndreas Gohr * Canonicalizes the tag to its lower case nospace form 46302a38efSAndreas Gohr * 47302a38efSAndreas Gohr * @param $tag 48302a38efSAndreas Gohr * @return string 49302a38efSAndreas Gohr */ 50302a38efSAndreas Gohr public function cleanTag($tag) { 51302a38efSAndreas Gohr $tag = str_replace(' ', '', $tag); 5250547994SMichael Große $tag = str_replace('-', '', $tag); 5350547994SMichael Große $tag = str_replace('_', '', $tag); 54302a38efSAndreas Gohr $tag = utf8_strtolower($tag); 55302a38efSAndreas Gohr return $tag; 56302a38efSAndreas Gohr } 57302a38efSAndreas Gohr 5856d82720SAndreas Gohr /** 5956d82720SAndreas Gohr * Create or Update tags of a page 6056d82720SAndreas Gohr * 6156d82720SAndreas Gohr * Uses the translation plugin to store the language of a page (if available) 6256d82720SAndreas Gohr * 6356d82720SAndreas Gohr * @param string $id The page ID 6456d82720SAndreas Gohr * @param string $user 6556d82720SAndreas Gohr * @param array $tags 6656d82720SAndreas Gohr * @return bool|SQLiteResult 6756d82720SAndreas Gohr */ 68f61105deSAdrian Lang public function replaceTags($id, $user, $tags) { 6956d82720SAndreas Gohr global $conf; 7056d82720SAndreas Gohr /** @var helper_plugin_translation $trans */ 7156d82720SAndreas Gohr $trans = plugin_load('helper', 'translation'); 7256d82720SAndreas Gohr if($trans) { 7356d82720SAndreas Gohr $lang = $trans->realLC($trans->getLangPart($id)); 7456d82720SAndreas Gohr } else { 7556d82720SAndreas Gohr $lang = $conf['lang']; 7656d82720SAndreas Gohr } 7756d82720SAndreas Gohr 78f61105deSAdrian Lang $db = $this->getDB(); 79f61105deSAdrian Lang $db->query('BEGIN TRANSACTION'); 80f61105deSAdrian Lang $queries = array(array('DELETE FROM taggings WHERE pid = ? AND tagger = ?', $id, $user)); 81f61105deSAdrian Lang foreach($tags as $tag) { 8256d82720SAndreas Gohr $queries[] = array('INSERT INTO taggings (pid, tagger, tag, lang) VALUES(?, ?, ?, ?)', $id, $user, $tag, $lang); 83f61105deSAdrian Lang } 84f61105deSAdrian Lang 85f61105deSAdrian Lang foreach($queries as $query) { 86f61105deSAdrian Lang if(!call_user_func_array(array($db, 'query'), $query)) { 87f61105deSAdrian Lang $db->query('ROLLBACK TRANSACTION'); 88f61105deSAdrian Lang return false; 89f61105deSAdrian Lang } 90f61105deSAdrian Lang } 91f61105deSAdrian Lang return $db->query('COMMIT TRANSACTION'); 92f61105deSAdrian Lang } 93f61105deSAdrian Lang 940a518a11SAndreas Gohr /** 95b12334e1SAndreas Gohr * Get a list of Tags or Pages matching search criteria 960a518a11SAndreas Gohr * 97b12334e1SAndreas Gohr * @param array $filter What to search for array('field' => 'searchterm') 98b12334e1SAndreas Gohr * @param string $type What field to return 'tag'|'pid' 99077ff864SAndreas Gohr * @param int $limit Limit to this many results, 0 for all 1000a518a11SAndreas Gohr * @return array associative array in form of value => count 1010a518a11SAndreas Gohr */ 102077ff864SAndreas Gohr public function findItems($filter, $type, $limit = 0) { 103f61105deSAdrian Lang $db = $this->getDB(); 104b12334e1SAndreas Gohr if(!$db) return array(); 105f61105deSAdrian Lang 106b12334e1SAndreas Gohr // create WHERE clause 107b12334e1SAndreas Gohr $where = '1=1'; 108b12334e1SAndreas Gohr foreach($filter as $field => $value) { 109b12334e1SAndreas Gohr // compare clean tags only 110b12334e1SAndreas Gohr if($field === 'tag') { 111b12334e1SAndreas Gohr $field = 'CLEANTAG(tag)'; 112b12334e1SAndreas Gohr $q = 'CLEANTAG(?)'; 113b12334e1SAndreas Gohr } else { 114b12334e1SAndreas Gohr $q = '?'; 115b12334e1SAndreas Gohr } 116204c069bSMichael Große 117204c069bSMichael Große if (substr($field,0,6) === 'notpid') { 118204c069bSMichael Große $field = 'pid'; 119204c069bSMichael Große 120204c069bSMichael Große // detect LIKE filters 121204c069bSMichael Große if($this->useLike($value)) { 122204c069bSMichael Große $where .= " AND $field NOT LIKE $q"; 123204c069bSMichael Große } else { 124204c069bSMichael Große $where .= " AND $field != $q"; 125204c069bSMichael Große } 126204c069bSMichael Große } else { 127b12334e1SAndreas Gohr // detect LIKE filters 128b12334e1SAndreas Gohr if($this->useLike($value)) { 129b12334e1SAndreas Gohr $where .= " AND $field LIKE $q"; 130b12334e1SAndreas Gohr } else { 131b12334e1SAndreas Gohr $where .= " AND $field = $q"; 132b12334e1SAndreas Gohr } 133b12334e1SAndreas Gohr } 134204c069bSMichael Große } 135*59a953ffSMichael Große $where .= 'AND GETACCESSLEVEL(item) >= ' . AUTH_READ; 136*59a953ffSMichael Große 137b12334e1SAndreas Gohr // group and order 138b12334e1SAndreas Gohr if($type == 'tag') { 139b12334e1SAndreas Gohr $groupby = 'CLEANTAG(tag)'; 140b12334e1SAndreas Gohr $orderby = 'CLEANTAG(tag)'; 141b12334e1SAndreas Gohr } else { 142b12334e1SAndreas Gohr $groupby = $type; 143466524f5SAndreas Gohr $orderby = "cnt DESC, $type"; 144b12334e1SAndreas Gohr } 145b12334e1SAndreas Gohr 146077ff864SAndreas Gohr // limit results 147077ff864SAndreas Gohr if($limit) { 148077ff864SAndreas Gohr $limit = " LIMIT $limit"; 149077ff864SAndreas Gohr } else { 150077ff864SAndreas Gohr $limit = ''; 151077ff864SAndreas Gohr } 152077ff864SAndreas Gohr 153b12334e1SAndreas Gohr // create SQL 154b12334e1SAndreas Gohr $sql = "SELECT $type AS item, COUNT(*) AS cnt 155b12334e1SAndreas Gohr FROM taggings 156b12334e1SAndreas Gohr WHERE $where 157b12334e1SAndreas Gohr GROUP BY $groupby 158077ff864SAndreas Gohr ORDER BY $orderby 159077ff864SAndreas Gohr $limit 160077ff864SAndreas Gohr "; 161b12334e1SAndreas Gohr 162b12334e1SAndreas Gohr // run query and turn into associative array 163b12334e1SAndreas Gohr $res = $db->query($sql, array_values($filter)); 164f61105deSAdrian Lang $res = $db->res2arr($res); 165b12334e1SAndreas Gohr 166f61105deSAdrian Lang $ret = array(); 167b12334e1SAndreas Gohr foreach($res as $row) { 168*59a953ffSMichael Große if (!isHiddenPage($row['item'])) { 169b12334e1SAndreas Gohr $ret[$row['item']] = $row['cnt']; 170f61105deSAdrian Lang } 17159cd4311SPPPCR } 172f61105deSAdrian Lang return $ret; 173f61105deSAdrian Lang } 174f61105deSAdrian Lang 175b12334e1SAndreas Gohr /** 176b12334e1SAndreas Gohr * Check if the given string is a LIKE statement 177b12334e1SAndreas Gohr * 178b12334e1SAndreas Gohr * @param string $value 179b12334e1SAndreas Gohr * @return bool 180b12334e1SAndreas Gohr */ 181b12334e1SAndreas Gohr private function useLike($value) { 182b12334e1SAndreas Gohr return strpos($value, '%') === 0 || strrpos($value, '%') === strlen($value) - 1; 183b2073787SDominik Eckelmann } 184b2073787SDominik Eckelmann 185302a38efSAndreas Gohr /** 186302a38efSAndreas Gohr * Constructs the URL to search for a tag 187302a38efSAndreas Gohr * 1885540f91dSAndreas Gohr * @param string $tag 1895540f91dSAndreas Gohr * @param string $ns 190302a38efSAndreas Gohr * @return string 191302a38efSAndreas Gohr */ 1925540f91dSAndreas Gohr public function getTagSearchURL($tag, $ns = '') { 193bed9f360SAndreas Gohr // wrap tag in quotes if non clean 1942cb25ec1SAndreas Gohr $ctag = utf8_stripspecials($this->cleanTag($tag)); 195bed9f360SAndreas Gohr if($ctag != utf8_strtolower($tag)) $tag = '"' . $tag . '"'; 196bed9f360SAndreas Gohr 197bed9f360SAndreas Gohr $ret = '?do=search&id=' . rawurlencode($tag); 1985540f91dSAndreas Gohr if($ns) $ret .= rawurlencode(' @' . $ns); 1995540f91dSAndreas Gohr 2005540f91dSAndreas Gohr return $ret; 201f61105deSAdrian Lang } 202f61105deSAdrian Lang 2035540f91dSAndreas Gohr /** 2045540f91dSAndreas Gohr * Calculates the size levels for the given list of clouds 2055540f91dSAndreas Gohr * 2065540f91dSAndreas Gohr * Automatically determines sensible tresholds 2075540f91dSAndreas Gohr * 2085540f91dSAndreas Gohr * @param array $tags list of tags => count 2095540f91dSAndreas Gohr * @param int $levels 2105540f91dSAndreas Gohr * @return mixed 2115540f91dSAndreas Gohr */ 212f61105deSAdrian Lang public function cloudData($tags, $levels = 10) { 213f61105deSAdrian Lang $min = min($tags); 214f61105deSAdrian Lang $max = max($tags); 215f61105deSAdrian Lang 216f61105deSAdrian Lang // calculate tresholds 217f61105deSAdrian Lang $tresholds = array(); 218f61105deSAdrian Lang for($i = 0; $i <= $levels; $i++) { 219f61105deSAdrian Lang $tresholds[$i] = pow($max - $min + 1, $i / $levels) + $min - 1; 220f61105deSAdrian Lang } 221f61105deSAdrian Lang 222f61105deSAdrian Lang // assign weights 223f61105deSAdrian Lang foreach($tags as $tag => $cnt) { 224f61105deSAdrian Lang foreach($tresholds as $tresh => $val) { 225f61105deSAdrian Lang if($cnt <= $val) { 226f61105deSAdrian Lang $tags[$tag] = $tresh; 227f61105deSAdrian Lang break; 228f61105deSAdrian Lang } 229f61105deSAdrian Lang $tags[$tag] = $levels; 230f61105deSAdrian Lang } 231f61105deSAdrian Lang } 232f61105deSAdrian Lang return $tags; 233f61105deSAdrian Lang } 234f61105deSAdrian Lang 2355540f91dSAndreas Gohr /** 2365540f91dSAndreas Gohr * Display a tag cloud 2375540f91dSAndreas Gohr * 2385540f91dSAndreas Gohr * @param array $tags list of tags => count 2395540f91dSAndreas Gohr * @param string $type 'tag' 2405540f91dSAndreas Gohr * @param Callable $func The function to print the link (gets tag and ns) 2415540f91dSAndreas Gohr * @param bool $wrap wrap cloud in UL tags? 2425540f91dSAndreas Gohr * @param bool $return returnn HTML instead of printing? 2435540f91dSAndreas Gohr * @param string $ns Add this namespace to search links 2445540f91dSAndreas Gohr * @return string 2455540f91dSAndreas Gohr */ 2465540f91dSAndreas Gohr public function html_cloud($tags, $type, $func, $wrap = true, $return = false, $ns = '') { 247a66f6715SAndreas Gohr global $INFO; 248a66f6715SAndreas Gohr 249a66f6715SAndreas Gohr $hidden_str = $this->getConf('hiddenprefix'); 250a66f6715SAndreas Gohr $hidden_len = strlen($hidden_str); 251a66f6715SAndreas Gohr 252f61105deSAdrian Lang $ret = ''; 2530dbdc3fcSAdrian Lang if($wrap) $ret .= '<ul class="tagging_cloud clearfix">'; 254f61105deSAdrian Lang if(count($tags) === 0) { 255f61105deSAdrian Lang // Produce valid XHTML (ul needs a child) 256f61105deSAdrian Lang $this->setupLocale(); 257f61105deSAdrian Lang $ret .= '<li><div class="li">' . $this->lang['js']['no' . $type . 's'] . '</div></li>'; 258f61105deSAdrian Lang } else { 259f61105deSAdrian Lang $tags = $this->cloudData($tags); 260f61105deSAdrian Lang foreach($tags as $val => $size) { 261a66f6715SAndreas Gohr // skip hidden tags for users that can't edit 262a66f6715SAndreas Gohr if($type == 'tag' and 263a66f6715SAndreas Gohr $hidden_len and 264a66f6715SAndreas Gohr substr($val, 0, $hidden_len) == $hidden_str and 265a66f6715SAndreas Gohr !($this->getUser() && $INFO['writable']) 266a66f6715SAndreas Gohr ) { 267a66f6715SAndreas Gohr continue; 268a66f6715SAndreas Gohr } 269a66f6715SAndreas Gohr 270f61105deSAdrian Lang $ret .= '<li class="t' . $size . '"><div class="li">'; 2715540f91dSAndreas Gohr $ret .= call_user_func($func, $val, $ns); 272f61105deSAdrian Lang $ret .= '</div></li>'; 273f61105deSAdrian Lang } 274f61105deSAdrian Lang } 275f61105deSAdrian Lang if($wrap) $ret .= '</ul>'; 276f61105deSAdrian Lang if($return) return $ret; 277f61105deSAdrian Lang echo $ret; 2785540f91dSAndreas Gohr return ''; 279f61105deSAdrian Lang } 280f61105deSAdrian Lang 2815540f91dSAndreas Gohr /** 2825540f91dSAndreas Gohr * Get the link to a search for the given tag 2835540f91dSAndreas Gohr * 2845540f91dSAndreas Gohr * @param string $tag search for this tag 2855540f91dSAndreas Gohr * @param string $ns limit search to this namespace 2865540f91dSAndreas Gohr * @return string 2875540f91dSAndreas Gohr */ 2885540f91dSAndreas Gohr protected function linkToSearch($tag, $ns = '') { 2895540f91dSAndreas Gohr return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>'; 290f61105deSAdrian Lang } 291f61105deSAdrian Lang 292fb1d0583SAndreas Gohr /** 293fb1d0583SAndreas Gohr * Display the Tags for the current page and prepare the tag editing form 2943496cc8aSAndreas Gohr * 2953496cc8aSAndreas Gohr * @param bool $print Should the HTML be printed or returned? 2963496cc8aSAndreas Gohr * @return string 297fb1d0583SAndreas Gohr */ 2983496cc8aSAndreas Gohr public function tpl_tags($print = true) { 299f61105deSAdrian Lang global $INFO; 300f61105deSAdrian Lang global $lang; 301fbf38c5bSAndreas Gohr $tags = $this->findItems(array('pid' => $INFO['id']), 'tag'); 3023496cc8aSAndreas Gohr 3033496cc8aSAndreas Gohr $ret = ''; 3043496cc8aSAndreas Gohr 3053496cc8aSAndreas Gohr $ret .= '<div class="plugin_tagging_edit">'; 3063496cc8aSAndreas Gohr $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true); 307f61105deSAdrian Lang 3082ace74f4SAndreas Gohr if($this->getUser() && $INFO['writable']) { 309f61105deSAdrian Lang $lang['btn_tagging_edit'] = $lang['btn_secedit']; 3103496cc8aSAndreas Gohr $ret .= html_btn('tagging_edit', $INFO['id'], '', array()); 311289f50bdSAndreas Gohr $form = new Doku_Form(array('id' => 'tagging__edit')); 312fbf38c5bSAndreas Gohr $form->addHidden('tagging[id]', $INFO['id']); 313289f50bdSAndreas Gohr $form->addHidden('call', 'plugin_tagging_save'); 3142ace74f4SAndreas Gohr $form->addElement(form_makeTextField('tagging[tags]', implode(', ', array_keys($this->findItems(array('pid' => $INFO['id'], 'tagger' => $this->getUser()), 'tag'))))); 315289f50bdSAndreas Gohr $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id' => 'tagging__edit_save'))); 316289f50bdSAndreas Gohr $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel'], array('id' => 'tagging__edit_cancel'))); 3173496cc8aSAndreas Gohr $ret .= $form->getForm(); 318f61105deSAdrian Lang } 3193496cc8aSAndreas Gohr $ret .= '</div>'; 3203496cc8aSAndreas Gohr 3213496cc8aSAndreas Gohr if($print) echo $ret; 3223496cc8aSAndreas Gohr return $ret; 323f61105deSAdrian Lang } 324872edc7cSRené Corinth 3258a1a3846SAndreas Gohr /** 3268a1a3846SAndreas Gohr * @return array 3278a1a3846SAndreas Gohr */ 328872edc7cSRené Corinth public function getAllTags() { 329872edc7cSRené Corinth 330872edc7cSRené Corinth $db = $this->getDb(); 3318a1a3846SAndreas Gohr $res = $db->query('SELECT pid, tag, tagger FROM taggings ORDER BY tag'); 332872edc7cSRené Corinth 333872edc7cSRené Corinth $tags_tmp = $db->res2arr($res); 334872edc7cSRené Corinth $tags = array(); 335872edc7cSRené Corinth foreach($tags_tmp as $tag) { 336302a38efSAndreas Gohr $tid = $this->cleanTag($tag['tag']); 337872edc7cSRené Corinth 338fb1d0583SAndreas Gohr if(!isset($tags[$tid]['orig'])) $tags[$tid]['orig'] = array(); 339fb1d0583SAndreas Gohr $tags[$tid]['orig'][] = $tag['tag']; 340872edc7cSRené Corinth 341302a38efSAndreas Gohr if(isset($tags[$tid]['count'])) { 342302a38efSAndreas Gohr $tags[$tid]['count']++; 343302a38efSAndreas Gohr $tags[$tid]['tagger'][] = $tag['tagger']; 344872edc7cSRené Corinth } else { 345302a38efSAndreas Gohr $tags[$tid]['count'] = 1; 346302a38efSAndreas Gohr $tags[$tid]['tagger'] = array($tag['tagger']); 347872edc7cSRené Corinth } 348872edc7cSRené Corinth } 349872edc7cSRené Corinth return $tags; 350872edc7cSRené Corinth } 351872edc7cSRené Corinth 3528a1a3846SAndreas Gohr /** 3538a1a3846SAndreas Gohr * Renames a tag 3548a1a3846SAndreas Gohr * 3558a1a3846SAndreas Gohr * @param string $formerTagName 3568a1a3846SAndreas Gohr * @param string $newTagName 3578a1a3846SAndreas Gohr */ 358872edc7cSRené Corinth public function renameTag($formerTagName, $newTagName) { 359872edc7cSRené Corinth 360872edc7cSRené Corinth if(empty($formerTagName) || empty($newTagName)) { 3618a1a3846SAndreas Gohr msg($this->getLang("admin enter tag names"), -1); 3628a1a3846SAndreas Gohr return; 363872edc7cSRené Corinth } 364872edc7cSRené Corinth 365872edc7cSRené Corinth $db = $this->getDb(); 366872edc7cSRené Corinth 367fb1d0583SAndreas Gohr $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ?', $this->cleanTag($formerTagName)); 368872edc7cSRené Corinth $check = $db->res2arr($res); 369872edc7cSRené Corinth 370872edc7cSRené Corinth if(empty($check)) { 3718a1a3846SAndreas Gohr msg($this->getLang("admin tag does not exists"), -1); 3728a1a3846SAndreas Gohr return; 373872edc7cSRené Corinth } 374872edc7cSRené Corinth 375fb1d0583SAndreas Gohr $res = $db->query("UPDATE taggings SET tag = ? WHERE CLEANTAG(tag) = ?", $newTagName, $this->cleanTag($formerTagName)); 376872edc7cSRené Corinth $db->res2arr($res); 377872edc7cSRené Corinth 378fb1d0583SAndreas Gohr msg($this->getLang("admin renamed"), 1); 3798a1a3846SAndreas Gohr return; 380872edc7cSRené Corinth } 381872edc7cSRené Corinth 382f61105deSAdrian Lang} 383