1f61105deSAdrian Lang<?php 2f61105deSAdrian Lang 30cfde7e9SMichael Großeif (!defined('DOKU_INC')) { 40cfde7e9SMichael Große die(); 50cfde7e9SMichael Große} 63496cc8aSAndreas Gohr 7e4543b6dSAdrian Langclass helper_plugin_tagging extends DokuWiki_Plugin { 8f61105deSAdrian Lang 9289f50bdSAndreas Gohr /** 10b12334e1SAndreas Gohr * Gives access to the database 11b12334e1SAndreas Gohr * 12b12334e1SAndreas Gohr * Initializes the SQLite helper and register the CLEANTAG function 13b12334e1SAndreas Gohr * 14b12334e1SAndreas Gohr * @return helper_plugin_sqlite|bool false if initialization fails 15289f50bdSAndreas Gohr */ 16289f50bdSAndreas Gohr public function getDB() { 17302a38efSAndreas Gohr static $db = null; 18f61105deSAdrian Lang if (!is_null($db)) { 19f61105deSAdrian Lang return $db; 20f61105deSAdrian Lang } 21f61105deSAdrian Lang 22302a38efSAndreas Gohr /** @var helper_plugin_sqlite $db */ 23f61105deSAdrian Lang $db = plugin_load('helper', 'sqlite'); 24f61105deSAdrian Lang if (is_null($db)) { 25f61105deSAdrian Lang msg('The tagging plugin needs the sqlite plugin', -1); 26f61105deSAdrian Lang return false; 27f61105deSAdrian Lang } 287e6afce6SAndreas Gohr $db->init('tagging', dirname(__FILE__) . '/db/'); 29302a38efSAndreas Gohr $db->create_function('CLEANTAG', array($this, 'cleanTag'), 1); 307e05e623SSzymon Olewniczak $db->create_function('GROUP_SORT', 317e05e623SSzymon Olewniczak function($group, $newDelimiter) { 327e05e623SSzymon Olewniczak $ex = explode(',', $group); 337e05e623SSzymon Olewniczak sort($ex); 347e05e623SSzymon Olewniczak return implode($newDelimiter, $ex); 357e05e623SSzymon Olewniczak }, 2); 36f61105deSAdrian Lang return $db; 37f61105deSAdrian Lang } 38f61105deSAdrian Lang 39302a38efSAndreas Gohr /** 402ace74f4SAndreas Gohr * Return the user to use for accessing tags 412ace74f4SAndreas Gohr * 422ace74f4SAndreas Gohr * Handles the singleuser mode by returning 'auto' as user. Returnes false when no user is logged in. 432ace74f4SAndreas Gohr * 442ace74f4SAndreas Gohr * @return bool|string 452ace74f4SAndreas Gohr */ 462ace74f4SAndreas Gohr public function getUser() { 470cfde7e9SMichael Große if (!isset($_SERVER['REMOTE_USER'])) { 480cfde7e9SMichael Große return false; 490cfde7e9SMichael Große } 500cfde7e9SMichael Große if ($this->getConf('singleusermode')) { 510cfde7e9SMichael Große return 'auto'; 520cfde7e9SMichael Große } 532ace74f4SAndreas Gohr return $_SERVER['REMOTE_USER']; 542ace74f4SAndreas Gohr } 552ace74f4SAndreas Gohr 562ace74f4SAndreas Gohr /** 57302a38efSAndreas Gohr * Canonicalizes the tag to its lower case nospace form 58302a38efSAndreas Gohr * 59302a38efSAndreas Gohr * @param $tag 600cfde7e9SMichael Große * 61302a38efSAndreas Gohr * @return string 62302a38efSAndreas Gohr */ 63302a38efSAndreas Gohr public function cleanTag($tag) { 64302a38efSAndreas Gohr $tag = str_replace(' ', '', $tag); 6550547994SMichael Große $tag = str_replace('-', '', $tag); 6650547994SMichael Große $tag = str_replace('_', '', $tag); 67302a38efSAndreas Gohr $tag = utf8_strtolower($tag); 68302a38efSAndreas Gohr return $tag; 69302a38efSAndreas Gohr } 70302a38efSAndreas Gohr 7156d82720SAndreas Gohr /** 7231396860SSzymon Olewniczak * Canonicalizes the namespace, remove the first colon and add glob 7331396860SSzymon Olewniczak * 7431396860SSzymon Olewniczak * @param $namespace 7531396860SSzymon Olewniczak * 7631396860SSzymon Olewniczak * @return string 7731396860SSzymon Olewniczak */ 7831396860SSzymon Olewniczak public function globNamespace($namespace) { 7931396860SSzymon Olewniczak return cleanId($namespace) . '%'; 8031396860SSzymon Olewniczak } 8131396860SSzymon Olewniczak 8231396860SSzymon Olewniczak /** 8356d82720SAndreas Gohr * Create or Update tags of a page 8456d82720SAndreas Gohr * 8556d82720SAndreas Gohr * Uses the translation plugin to store the language of a page (if available) 8656d82720SAndreas Gohr * 8756d82720SAndreas Gohr * @param string $id The page ID 8856d82720SAndreas Gohr * @param string $user 8956d82720SAndreas Gohr * @param array $tags 900cfde7e9SMichael Große * 9156d82720SAndreas Gohr * @return bool|SQLiteResult 9256d82720SAndreas Gohr */ 93f61105deSAdrian Lang public function replaceTags($id, $user, $tags) { 9456d82720SAndreas Gohr global $conf; 9556d82720SAndreas Gohr /** @var helper_plugin_translation $trans */ 9656d82720SAndreas Gohr $trans = plugin_load('helper', 'translation'); 9756d82720SAndreas Gohr if ($trans) { 9856d82720SAndreas Gohr $lang = $trans->realLC($trans->getLangPart($id)); 9956d82720SAndreas Gohr } else { 10056d82720SAndreas Gohr $lang = $conf['lang']; 10156d82720SAndreas Gohr } 10256d82720SAndreas Gohr 103f61105deSAdrian Lang $db = $this->getDB(); 104f61105deSAdrian Lang $db->query('BEGIN TRANSACTION'); 105f61105deSAdrian Lang $queries = array(array('DELETE FROM taggings WHERE pid = ? AND tagger = ?', $id, $user)); 106f61105deSAdrian Lang foreach ($tags as $tag) { 10756d82720SAndreas Gohr $queries[] = array('INSERT INTO taggings (pid, tagger, tag, lang) VALUES(?, ?, ?, ?)', $id, $user, $tag, $lang); 108f61105deSAdrian Lang } 109f61105deSAdrian Lang 110f61105deSAdrian Lang foreach ($queries as $query) { 111f61105deSAdrian Lang if (!call_user_func_array(array($db, 'query'), $query)) { 112f61105deSAdrian Lang $db->query('ROLLBACK TRANSACTION'); 113f61105deSAdrian Lang return false; 114f61105deSAdrian Lang } 115f61105deSAdrian Lang } 116f61105deSAdrian Lang return $db->query('COMMIT TRANSACTION'); 117f61105deSAdrian Lang } 118f61105deSAdrian Lang 1190a518a11SAndreas Gohr /** 120b12334e1SAndreas Gohr * Get a list of Tags or Pages matching search criteria 1210a518a11SAndreas Gohr * 122b12334e1SAndreas Gohr * @param array $filter What to search for array('field' => 'searchterm') 123b12334e1SAndreas Gohr * @param string $type What field to return 'tag'|'pid' 124077ff864SAndreas Gohr * @param int $limit Limit to this many results, 0 for all 1250cfde7e9SMichael Große * 1260a518a11SAndreas Gohr * @return array associative array in form of value => count 1270a518a11SAndreas Gohr */ 128077ff864SAndreas Gohr public function findItems($filter, $type, $limit = 0) { 129f61105deSAdrian Lang $db = $this->getDB(); 1300cfde7e9SMichael Große if (!$db) { 1310cfde7e9SMichael Große return array(); 1320cfde7e9SMichael Große } 133f61105deSAdrian Lang 134b12334e1SAndreas Gohr // create WHERE clause 135b12334e1SAndreas Gohr $where = '1=1'; 136b12334e1SAndreas Gohr foreach ($filter as $field => $value) { 137b12334e1SAndreas Gohr // compare clean tags only 138b12334e1SAndreas Gohr if ($field === 'tag') { 139b12334e1SAndreas Gohr $field = 'CLEANTAG(tag)'; 140b12334e1SAndreas Gohr $q = 'CLEANTAG(?)'; 141b12334e1SAndreas Gohr } else { 142b12334e1SAndreas Gohr $q = '?'; 143b12334e1SAndreas Gohr } 144204c069bSMichael Große 145204c069bSMichael Große if (substr($field, 0, 6) === 'notpid') { 146204c069bSMichael Große $field = 'pid'; 147204c069bSMichael Große 148204c069bSMichael Große // detect LIKE filters 149204c069bSMichael Große if ($this->useLike($value)) { 150204c069bSMichael Große $where .= " AND $field NOT LIKE $q"; 151204c069bSMichael Große } else { 152204c069bSMichael Große $where .= " AND $field != $q"; 153204c069bSMichael Große } 154204c069bSMichael Große } else { 155b12334e1SAndreas Gohr // detect LIKE filters 156b12334e1SAndreas Gohr if ($this->useLike($value)) { 157b12334e1SAndreas Gohr $where .= " AND $field LIKE $q"; 158b12334e1SAndreas Gohr } else { 159b12334e1SAndreas Gohr $where .= " AND $field = $q"; 160b12334e1SAndreas Gohr } 161b12334e1SAndreas Gohr } 162204c069bSMichael Große } 1636c37861bSJulian Einwag $where .= ' AND GETACCESSLEVEL(pid) >= ' . AUTH_READ; 16459a953ffSMichael Große 165b12334e1SAndreas Gohr // group and order 166b12334e1SAndreas Gohr if ($type == 'tag') { 167b12334e1SAndreas Gohr $groupby = 'CLEANTAG(tag)'; 168b12334e1SAndreas Gohr $orderby = 'CLEANTAG(tag)'; 169b12334e1SAndreas Gohr } else { 170b12334e1SAndreas Gohr $groupby = $type; 171466524f5SAndreas Gohr $orderby = "cnt DESC, $type"; 172b12334e1SAndreas Gohr } 173b12334e1SAndreas Gohr 174077ff864SAndreas Gohr // limit results 175077ff864SAndreas Gohr if ($limit) { 176077ff864SAndreas Gohr $limit = " LIMIT $limit"; 177077ff864SAndreas Gohr } else { 178077ff864SAndreas Gohr $limit = ''; 179077ff864SAndreas Gohr } 180077ff864SAndreas Gohr 181b12334e1SAndreas Gohr // create SQL 182b12334e1SAndreas Gohr $sql = "SELECT $type AS item, COUNT(*) AS cnt 183b12334e1SAndreas Gohr FROM taggings 184b12334e1SAndreas Gohr WHERE $where 185b12334e1SAndreas Gohr GROUP BY $groupby 186077ff864SAndreas Gohr ORDER BY $orderby 187077ff864SAndreas Gohr $limit 188077ff864SAndreas Gohr "; 189b12334e1SAndreas Gohr 190b12334e1SAndreas Gohr // run query and turn into associative array 191b12334e1SAndreas Gohr $res = $db->query($sql, array_values($filter)); 192f61105deSAdrian Lang $res = $db->res2arr($res); 193b12334e1SAndreas Gohr 194f61105deSAdrian Lang $ret = array(); 195b12334e1SAndreas Gohr foreach ($res as $row) { 196b12334e1SAndreas Gohr $ret[$row['item']] = $row['cnt']; 197f61105deSAdrian Lang } 198f61105deSAdrian Lang return $ret; 199f61105deSAdrian Lang } 200f61105deSAdrian Lang 201b12334e1SAndreas Gohr /** 202b12334e1SAndreas Gohr * Check if the given string is a LIKE statement 203b12334e1SAndreas Gohr * 204b12334e1SAndreas Gohr * @param string $value 2050cfde7e9SMichael Große * 206b12334e1SAndreas Gohr * @return bool 207b12334e1SAndreas Gohr */ 208b12334e1SAndreas Gohr private function useLike($value) { 209b12334e1SAndreas Gohr return strpos($value, '%') === 0 || strrpos($value, '%') === strlen($value) - 1; 210b2073787SDominik Eckelmann } 211b2073787SDominik Eckelmann 212302a38efSAndreas Gohr /** 213302a38efSAndreas Gohr * Constructs the URL to search for a tag 214302a38efSAndreas Gohr * 2155540f91dSAndreas Gohr * @param string $tag 2165540f91dSAndreas Gohr * @param string $ns 2170cfde7e9SMichael Große * 218302a38efSAndreas Gohr * @return string 219302a38efSAndreas Gohr */ 2205540f91dSAndreas Gohr public function getTagSearchURL($tag, $ns = '') { 221bed9f360SAndreas Gohr // wrap tag in quotes if non clean 2222cb25ec1SAndreas Gohr $ctag = utf8_stripspecials($this->cleanTag($tag)); 2230cfde7e9SMichael Große if ($ctag != utf8_strtolower($tag)) { 2240cfde7e9SMichael Große $tag = '"' . $tag . '"'; 2250cfde7e9SMichael Große } 226bed9f360SAndreas Gohr 227bed9f360SAndreas Gohr $ret = '?do=search&id=' . rawurlencode($tag); 2280cfde7e9SMichael Große if ($ns) { 2290cfde7e9SMichael Große $ret .= rawurlencode(' @' . $ns); 2300cfde7e9SMichael Große } 2315540f91dSAndreas Gohr 2325540f91dSAndreas Gohr return $ret; 233f61105deSAdrian Lang } 234f61105deSAdrian Lang 2355540f91dSAndreas Gohr /** 2365540f91dSAndreas Gohr * Calculates the size levels for the given list of clouds 2375540f91dSAndreas Gohr * 2385540f91dSAndreas Gohr * Automatically determines sensible tresholds 2395540f91dSAndreas Gohr * 2405540f91dSAndreas Gohr * @param array $tags list of tags => count 2415540f91dSAndreas Gohr * @param int $levels 2420cfde7e9SMichael Große * 2435540f91dSAndreas Gohr * @return mixed 2445540f91dSAndreas Gohr */ 245f61105deSAdrian Lang public function cloudData($tags, $levels = 10) { 246f61105deSAdrian Lang $min = min($tags); 247f61105deSAdrian Lang $max = max($tags); 248f61105deSAdrian Lang 249f61105deSAdrian Lang // calculate tresholds 250f61105deSAdrian Lang $tresholds = array(); 251f61105deSAdrian Lang for ($i = 0; $i <= $levels; $i++) { 252f61105deSAdrian Lang $tresholds[$i] = pow($max - $min + 1, $i / $levels) + $min - 1; 253f61105deSAdrian Lang } 254f61105deSAdrian Lang 255f61105deSAdrian Lang // assign weights 256f61105deSAdrian Lang foreach ($tags as $tag => $cnt) { 257f61105deSAdrian Lang foreach ($tresholds as $tresh => $val) { 258f61105deSAdrian Lang if ($cnt <= $val) { 259f61105deSAdrian Lang $tags[$tag] = $tresh; 260f61105deSAdrian Lang break; 261f61105deSAdrian Lang } 262f61105deSAdrian Lang $tags[$tag] = $levels; 263f61105deSAdrian Lang } 264f61105deSAdrian Lang } 265f61105deSAdrian Lang return $tags; 266f61105deSAdrian Lang } 267f61105deSAdrian Lang 2685540f91dSAndreas Gohr /** 2695540f91dSAndreas Gohr * Display a tag cloud 2705540f91dSAndreas Gohr * 2715540f91dSAndreas Gohr * @param array $tags list of tags => count 2725540f91dSAndreas Gohr * @param string $type 'tag' 2735540f91dSAndreas Gohr * @param Callable $func The function to print the link (gets tag and ns) 2745540f91dSAndreas Gohr * @param bool $wrap wrap cloud in UL tags? 2755540f91dSAndreas Gohr * @param bool $return returnn HTML instead of printing? 2765540f91dSAndreas Gohr * @param string $ns Add this namespace to search links 2770cfde7e9SMichael Große * 2785540f91dSAndreas Gohr * @return string 2795540f91dSAndreas Gohr */ 2805540f91dSAndreas Gohr public function html_cloud($tags, $type, $func, $wrap = true, $return = false, $ns = '') { 281a66f6715SAndreas Gohr global $INFO; 282a66f6715SAndreas Gohr 283a66f6715SAndreas Gohr $hidden_str = $this->getConf('hiddenprefix'); 284a66f6715SAndreas Gohr $hidden_len = strlen($hidden_str); 285a66f6715SAndreas Gohr 286f61105deSAdrian Lang $ret = ''; 2870cfde7e9SMichael Große if ($wrap) { 2880cfde7e9SMichael Große $ret .= '<ul class="tagging_cloud clearfix">'; 2890cfde7e9SMichael Große } 290f61105deSAdrian Lang if (count($tags) === 0) { 291f61105deSAdrian Lang // Produce valid XHTML (ul needs a child) 292f61105deSAdrian Lang $this->setupLocale(); 293f61105deSAdrian Lang $ret .= '<li><div class="li">' . $this->lang['js']['no' . $type . 's'] . '</div></li>'; 294f61105deSAdrian Lang } else { 295f61105deSAdrian Lang $tags = $this->cloudData($tags); 296f61105deSAdrian Lang foreach ($tags as $val => $size) { 297a66f6715SAndreas Gohr // skip hidden tags for users that can't edit 298a66f6715SAndreas Gohr if ($type == 'tag' and 299a66f6715SAndreas Gohr $hidden_len and 300a66f6715SAndreas Gohr substr($val, 0, $hidden_len) == $hidden_str and 301a66f6715SAndreas Gohr !($this->getUser() && $INFO['writable']) 302a66f6715SAndreas Gohr ) { 303a66f6715SAndreas Gohr continue; 304a66f6715SAndreas Gohr } 305a66f6715SAndreas Gohr 306f61105deSAdrian Lang $ret .= '<li class="t' . $size . '"><div class="li">'; 3075540f91dSAndreas Gohr $ret .= call_user_func($func, $val, $ns); 308f61105deSAdrian Lang $ret .= '</div></li>'; 309f61105deSAdrian Lang } 310f61105deSAdrian Lang } 3110cfde7e9SMichael Große if ($wrap) { 3120cfde7e9SMichael Große $ret .= '</ul>'; 3130cfde7e9SMichael Große } 3140cfde7e9SMichael Große if ($return) { 3150cfde7e9SMichael Große return $ret; 3160cfde7e9SMichael Große } 317f61105deSAdrian Lang echo $ret; 3185540f91dSAndreas Gohr return ''; 319f61105deSAdrian Lang } 320f61105deSAdrian Lang 3215540f91dSAndreas Gohr /** 3225540f91dSAndreas Gohr * Get the link to a search for the given tag 3235540f91dSAndreas Gohr * 3245540f91dSAndreas Gohr * @param string $tag search for this tag 3255540f91dSAndreas Gohr * @param string $ns limit search to this namespace 3260cfde7e9SMichael Große * 3275540f91dSAndreas Gohr * @return string 3285540f91dSAndreas Gohr */ 3295540f91dSAndreas Gohr protected function linkToSearch($tag, $ns = '') { 3305540f91dSAndreas Gohr return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>'; 331f61105deSAdrian Lang } 332f61105deSAdrian Lang 333fb1d0583SAndreas Gohr /** 334fb1d0583SAndreas Gohr * Display the Tags for the current page and prepare the tag editing form 3353496cc8aSAndreas Gohr * 3363496cc8aSAndreas Gohr * @param bool $print Should the HTML be printed or returned? 3370cfde7e9SMichael Große * 3383496cc8aSAndreas Gohr * @return string 339fb1d0583SAndreas Gohr */ 3403496cc8aSAndreas Gohr public function tpl_tags($print = true) { 341f61105deSAdrian Lang global $INFO; 342f61105deSAdrian Lang global $lang; 3433bf0e2f1SMichael Große 3443bf0e2f1SMichael Große $filter = array('pid' => $INFO['id']); 3453bf0e2f1SMichael Große if ($this->getConf('singleusermode')) { 3463bf0e2f1SMichael Große $filter['tagger'] = 'auto'; 3473bf0e2f1SMichael Große } 3483bf0e2f1SMichael Große 3493bf0e2f1SMichael Große $tags = $this->findItems($filter, 'tag'); 3503496cc8aSAndreas Gohr 3513496cc8aSAndreas Gohr $ret = ''; 3523496cc8aSAndreas Gohr 3533496cc8aSAndreas Gohr $ret .= '<div class="plugin_tagging_edit">'; 3543496cc8aSAndreas Gohr $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true); 355f61105deSAdrian Lang 3562ace74f4SAndreas Gohr if ($this->getUser() && $INFO['writable']) { 357f61105deSAdrian Lang $lang['btn_tagging_edit'] = $lang['btn_secedit']; 358e5b42768SSzymon Olewniczak $ret .= '<div id="tagging__edit_buttons_group">'; 3593496cc8aSAndreas Gohr $ret .= html_btn('tagging_edit', $INFO['id'], '', array()); 360dd52fd45SSzymon Olewniczak if (auth_isadmin()) { 361e5b42768SSzymon Olewniczak $ret .= '<label>' . $this->getLang('toggle admin mode') . '<input type="checkbox" id="tagging__edit_toggle_admin" /></label>'; 362dd52fd45SSzymon Olewniczak } 363e5b42768SSzymon Olewniczak $ret .= '</div>'; 3642819ffcdSSzymon Olewniczak $form = new dokuwiki\Form\Form(); 3652819ffcdSSzymon Olewniczak $form->id('tagging__edit'); 3662819ffcdSSzymon Olewniczak $form->setHiddenField('tagging[id]', $INFO['id']); 3672819ffcdSSzymon Olewniczak $form->setHiddenField('call', 'plugin_tagging_save'); 3682819ffcdSSzymon Olewniczak $tags = $this->findItems(array( 3692819ffcdSSzymon Olewniczak 'pid' => $INFO['id'], 3702819ffcdSSzymon Olewniczak 'tagger' => $this->getUser() 3712819ffcdSSzymon Olewniczak ), 'tag'); 372*d8e6aa7fSMichael Große $form->addTextarea('tagging[tags]')->val(implode(', ', array_keys($tags)))->addClass('edit'); 373cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_save'])->id('tagging__edit_save'); 374cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel'); 3752819ffcdSSzymon Olewniczak $ret .= $form->toHTML(); 376f61105deSAdrian Lang } 3773496cc8aSAndreas Gohr $ret .= '</div>'; 3783496cc8aSAndreas Gohr 3790cfde7e9SMichael Große if ($print) { 3800cfde7e9SMichael Große echo $ret; 3810cfde7e9SMichael Große } 3823496cc8aSAndreas Gohr return $ret; 383f61105deSAdrian Lang } 384872edc7cSRené Corinth 3858a1a3846SAndreas Gohr /** 386a99b66c1SSzymon Olewniczak * @param string $namespace empty for entire wiki 387a99b66c1SSzymon Olewniczak * 3888a1a3846SAndreas Gohr * @return array 3898a1a3846SAndreas Gohr */ 390cb469644SSzymon Olewniczak public function getAllTags($namespace='', $order_by='tag', $desc=false) { 3918e9d0162SSzymon Olewniczak $order_fields = array('pid', 'tid', 'orig', 'taggers', 'count'); 392f0084ee1SSzymon Olewniczak if (!in_array($order_by, $order_fields)) { 393f0084ee1SSzymon Olewniczak msg('cannot sort by '.$order_by. ' field does not exists', -1); 394f0084ee1SSzymon Olewniczak $order_by='tag'; 395f0084ee1SSzymon Olewniczak } 396872edc7cSRené Corinth 397872edc7cSRené Corinth $db = $this->getDb(); 398872edc7cSRené Corinth 399f0084ee1SSzymon Olewniczak $query = 'SELECT "pid", 400f0084ee1SSzymon Olewniczak CLEANTAG("tag") as "tid", 401f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tag"), \', \') AS "orig", 402f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tagger"), \', \') AS "taggers", 403193a767dSSzymon Olewniczak COUNT(*) AS "count" 40457e45304SSzymon Olewniczak FROM "taggings" 405f0084ee1SSzymon Olewniczak WHERE "pid" LIKE ? 406f0084ee1SSzymon Olewniczak GROUP BY "tid" 407f0084ee1SSzymon Olewniczak ORDER BY '.$order_by; 408cb469644SSzymon Olewniczak if ($desc) $query .= ' DESC'; 409cb469644SSzymon Olewniczak 410f0084ee1SSzymon Olewniczak $res = $db->query($query, $this->globNamespace($namespace)); 411872edc7cSRené Corinth 4127e05e623SSzymon Olewniczak return $db->res2arr($res); 413872edc7cSRené Corinth } 414872edc7cSRené Corinth 4158a1a3846SAndreas Gohr /** 4168a1a3846SAndreas Gohr * Renames a tag 4178a1a3846SAndreas Gohr * 4188a1a3846SAndreas Gohr * @param string $formerTagName 4198a1a3846SAndreas Gohr * @param string $newTagName 4208a1a3846SAndreas Gohr */ 421872edc7cSRené Corinth public function renameTag($formerTagName, $newTagName) { 422872edc7cSRené Corinth 423872edc7cSRené Corinth if (empty($formerTagName) || empty($newTagName)) { 4248a1a3846SAndreas Gohr msg($this->getLang("admin enter tag names"), -1); 4258a1a3846SAndreas Gohr return; 426872edc7cSRené Corinth } 427872edc7cSRené Corinth 428872edc7cSRené Corinth $db = $this->getDb(); 429872edc7cSRené Corinth 430fb1d0583SAndreas Gohr $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ?', $this->cleanTag($formerTagName)); 431872edc7cSRené Corinth $check = $db->res2arr($res); 432872edc7cSRené Corinth 433872edc7cSRené Corinth if (empty($check)) { 4348a1a3846SAndreas Gohr msg($this->getLang("admin tag does not exists"), -1); 4358a1a3846SAndreas Gohr return; 436872edc7cSRené Corinth } 437872edc7cSRené Corinth 438fb1d0583SAndreas Gohr $res = $db->query("UPDATE taggings SET tag = ? WHERE CLEANTAG(tag) = ?", $newTagName, $this->cleanTag($formerTagName)); 439872edc7cSRené Corinth $db->res2arr($res); 440872edc7cSRené Corinth 441fb1d0583SAndreas Gohr msg($this->getLang("admin renamed"), 1); 4428a1a3846SAndreas Gohr return; 443872edc7cSRené Corinth } 444872edc7cSRené Corinth 4458f630140SSzymon Olewniczak /** 446dd52fd45SSzymon Olewniczak * Rename or delete a tag for all users 447dd52fd45SSzymon Olewniczak * 448dd52fd45SSzymon Olewniczak * @param string $pid 449dd52fd45SSzymon Olewniczak * @param string $formerTagName 450dd52fd45SSzymon Olewniczak * @param string $newTagName 451dd52fd45SSzymon Olewniczak * 452dd52fd45SSzymon Olewniczak * @return array 453dd52fd45SSzymon Olewniczak */ 454dd52fd45SSzymon Olewniczak public function modifyPageTag($pid, $formerTagName, $newTagName) { 455dd52fd45SSzymon Olewniczak 456dd52fd45SSzymon Olewniczak $db = $this->getDb(); 457dd52fd45SSzymon Olewniczak 458dd52fd45SSzymon Olewniczak $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ? AND pid = ?', $this->cleanTag($formerTagName), $pid); 459dd52fd45SSzymon Olewniczak $check = $db->res2arr($res); 460dd52fd45SSzymon Olewniczak 461dd52fd45SSzymon Olewniczak if (empty($check)) { 462dd52fd45SSzymon Olewniczak return array(true, $this->getLang('admin tag does not exists')); 463dd52fd45SSzymon Olewniczak } 464dd52fd45SSzymon Olewniczak 465dd52fd45SSzymon Olewniczak if (empty($newTagName)) { 466dd52fd45SSzymon Olewniczak $res = $db->query('DELETE FROM taggings WHERE pid = ? AND CLEANTAG(tag) = ?', $pid, $this->cleanTag($formerTagName)); 467dd52fd45SSzymon Olewniczak } else { 468dd52fd45SSzymon Olewniczak $res = $db->query('UPDATE taggings SET tag = ? WHERE pid = ? AND CLEANTAG(tag) = ?', $newTagName, $pid, $this->cleanTag($formerTagName)); 469dd52fd45SSzymon Olewniczak } 470dd52fd45SSzymon Olewniczak $db->res2arr($res); 471dd52fd45SSzymon Olewniczak 472dd52fd45SSzymon Olewniczak return array(false, $this->getLang('admin renamed')); 473dd52fd45SSzymon Olewniczak } 474dd52fd45SSzymon Olewniczak 475dd52fd45SSzymon Olewniczak /** 4768f630140SSzymon Olewniczak * Deletes a tag 4778f630140SSzymon Olewniczak * 4788f630140SSzymon Olewniczak * @param array $tags 47931396860SSzymon Olewniczak * @param string $namespace current namespace context as in getAllTags() 4808f630140SSzymon Olewniczak */ 48131396860SSzymon Olewniczak public function deleteTags($tags, $namespace='') { 4828f630140SSzymon Olewniczak if (empty($tags)) return; 4838f630140SSzymon Olewniczak 48431396860SSzymon Olewniczak $namespace = cleanId($namespace); 48531396860SSzymon Olewniczak 4868f630140SSzymon Olewniczak $db = $this->getDb(); 4878f630140SSzymon Olewniczak 48831396860SSzymon Olewniczak $query = 'DELETE FROM taggings WHERE pid LIKE ? AND (' . 48931396860SSzymon Olewniczak implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')).')'; 49031396860SSzymon Olewniczak 49131396860SSzymon Olewniczak $args = array_map(array($this, 'cleanTag'), $tags); 49231396860SSzymon Olewniczak array_unshift($args, $this->globNamespace($namespace)); 49331396860SSzymon Olewniczak $res = $db->query($query, $args); 4948f630140SSzymon Olewniczak 4958f630140SSzymon Olewniczak msg(sprintf($this->getLang("admin deleted"), count($tags), $res->rowCount()), 1); 4968f630140SSzymon Olewniczak return; 4978f630140SSzymon Olewniczak } 498f61105deSAdrian Lang} 499