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); 26*ca455b8eSMichael Große 27f61105deSAdrian Lang return false; 28f61105deSAdrian Lang } 297e6afce6SAndreas Gohr $db->init('tagging', dirname(__FILE__) . '/db/'); 30302a38efSAndreas Gohr $db->create_function('CLEANTAG', array($this, 'cleanTag'), 1); 317e05e623SSzymon Olewniczak $db->create_function('GROUP_SORT', 327e05e623SSzymon Olewniczak function ($group, $newDelimiter) { 337e05e623SSzymon Olewniczak $ex = explode(',', $group); 347e05e623SSzymon Olewniczak sort($ex); 35*ca455b8eSMichael Große 367e05e623SSzymon Olewniczak return implode($newDelimiter, $ex); 377e05e623SSzymon Olewniczak }, 2); 38*ca455b8eSMichael Große 39f61105deSAdrian Lang return $db; 40f61105deSAdrian Lang } 41f61105deSAdrian Lang 42302a38efSAndreas Gohr /** 432ace74f4SAndreas Gohr * Return the user to use for accessing tags 442ace74f4SAndreas Gohr * 452ace74f4SAndreas Gohr * Handles the singleuser mode by returning 'auto' as user. Returnes false when no user is logged in. 462ace74f4SAndreas Gohr * 472ace74f4SAndreas Gohr * @return bool|string 482ace74f4SAndreas Gohr */ 492ace74f4SAndreas Gohr public function getUser() { 500cfde7e9SMichael Große if (!isset($_SERVER['REMOTE_USER'])) { 510cfde7e9SMichael Große return false; 520cfde7e9SMichael Große } 530cfde7e9SMichael Große if ($this->getConf('singleusermode')) { 540cfde7e9SMichael Große return 'auto'; 550cfde7e9SMichael Große } 56*ca455b8eSMichael Große 572ace74f4SAndreas Gohr return $_SERVER['REMOTE_USER']; 582ace74f4SAndreas Gohr } 592ace74f4SAndreas Gohr 602ace74f4SAndreas Gohr /** 61302a38efSAndreas Gohr * Canonicalizes the tag to its lower case nospace form 62302a38efSAndreas Gohr * 63302a38efSAndreas Gohr * @param $tag 640cfde7e9SMichael Große * 65302a38efSAndreas Gohr * @return string 66302a38efSAndreas Gohr */ 67302a38efSAndreas Gohr public function cleanTag($tag) { 68302a38efSAndreas Gohr $tag = str_replace(' ', '', $tag); 6950547994SMichael Große $tag = str_replace('-', '', $tag); 7050547994SMichael Große $tag = str_replace('_', '', $tag); 71302a38efSAndreas Gohr $tag = utf8_strtolower($tag); 72*ca455b8eSMichael Große 73302a38efSAndreas Gohr return $tag; 74302a38efSAndreas Gohr } 75302a38efSAndreas Gohr 7656d82720SAndreas Gohr /** 7731396860SSzymon Olewniczak * Canonicalizes the namespace, remove the first colon and add glob 7831396860SSzymon Olewniczak * 7931396860SSzymon Olewniczak * @param $namespace 8031396860SSzymon Olewniczak * 8131396860SSzymon Olewniczak * @return string 8231396860SSzymon Olewniczak */ 8331396860SSzymon Olewniczak public function globNamespace($namespace) { 8431396860SSzymon Olewniczak return cleanId($namespace) . '%'; 8531396860SSzymon Olewniczak } 8631396860SSzymon Olewniczak 8731396860SSzymon Olewniczak /** 8856d82720SAndreas Gohr * Create or Update tags of a page 8956d82720SAndreas Gohr * 9056d82720SAndreas Gohr * Uses the translation plugin to store the language of a page (if available) 9156d82720SAndreas Gohr * 9256d82720SAndreas Gohr * @param string $id The page ID 9356d82720SAndreas Gohr * @param string $user 9456d82720SAndreas Gohr * @param array $tags 950cfde7e9SMichael Große * 9656d82720SAndreas Gohr * @return bool|SQLiteResult 9756d82720SAndreas Gohr */ 98f61105deSAdrian Lang public function replaceTags($id, $user, $tags) { 9956d82720SAndreas Gohr global $conf; 10056d82720SAndreas Gohr /** @var helper_plugin_translation $trans */ 10156d82720SAndreas Gohr $trans = plugin_load('helper', 'translation'); 10256d82720SAndreas Gohr if ($trans) { 10356d82720SAndreas Gohr $lang = $trans->realLC($trans->getLangPart($id)); 10456d82720SAndreas Gohr } else { 10556d82720SAndreas Gohr $lang = $conf['lang']; 10656d82720SAndreas Gohr } 10756d82720SAndreas Gohr 108f61105deSAdrian Lang $db = $this->getDB(); 109f61105deSAdrian Lang $db->query('BEGIN TRANSACTION'); 110f61105deSAdrian Lang $queries = array(array('DELETE FROM taggings WHERE pid = ? AND tagger = ?', $id, $user)); 111f61105deSAdrian Lang foreach ($tags as $tag) { 11256d82720SAndreas Gohr $queries[] = array('INSERT INTO taggings (pid, tagger, tag, lang) VALUES(?, ?, ?, ?)', $id, $user, $tag, $lang); 113f61105deSAdrian Lang } 114f61105deSAdrian Lang 115f61105deSAdrian Lang foreach ($queries as $query) { 116f61105deSAdrian Lang if (!call_user_func_array(array($db, 'query'), $query)) { 117f61105deSAdrian Lang $db->query('ROLLBACK TRANSACTION'); 118*ca455b8eSMichael Große 119f61105deSAdrian Lang return false; 120f61105deSAdrian Lang } 121f61105deSAdrian Lang } 122*ca455b8eSMichael Große 123f61105deSAdrian Lang return $db->query('COMMIT TRANSACTION'); 124f61105deSAdrian Lang } 125f61105deSAdrian Lang 1260a518a11SAndreas Gohr /** 127b12334e1SAndreas Gohr * Get a list of Tags or Pages matching search criteria 1280a518a11SAndreas Gohr * 129b12334e1SAndreas Gohr * @param array $filter What to search for array('field' => 'searchterm') 130b12334e1SAndreas Gohr * @param string $type What field to return 'tag'|'pid' 131077ff864SAndreas Gohr * @param int $limit Limit to this many results, 0 for all 1320cfde7e9SMichael Große * 1330a518a11SAndreas Gohr * @return array associative array in form of value => count 1340a518a11SAndreas Gohr */ 135077ff864SAndreas Gohr public function findItems($filter, $type, $limit = 0) { 136f61105deSAdrian Lang $db = $this->getDB(); 1370cfde7e9SMichael Große if (!$db) { 1380cfde7e9SMichael Große return array(); 1390cfde7e9SMichael Große } 140f61105deSAdrian Lang 141b12334e1SAndreas Gohr // create WHERE clause 142b12334e1SAndreas Gohr $where = '1=1'; 143b12334e1SAndreas Gohr foreach ($filter as $field => $value) { 144b12334e1SAndreas Gohr // compare clean tags only 145b12334e1SAndreas Gohr if ($field === 'tag') { 146b12334e1SAndreas Gohr $field = 'CLEANTAG(tag)'; 147b12334e1SAndreas Gohr $q = 'CLEANTAG(?)'; 148b12334e1SAndreas Gohr } else { 149b12334e1SAndreas Gohr $q = '?'; 150b12334e1SAndreas Gohr } 151204c069bSMichael Große 152204c069bSMichael Große if (substr($field, 0, 6) === 'notpid') { 153204c069bSMichael Große $field = 'pid'; 154204c069bSMichael Große 155204c069bSMichael Große // detect LIKE filters 156204c069bSMichael Große if ($this->useLike($value)) { 157204c069bSMichael Große $where .= " AND $field NOT LIKE $q"; 158204c069bSMichael Große } else { 159204c069bSMichael Große $where .= " AND $field != $q"; 160204c069bSMichael Große } 161204c069bSMichael Große } else { 162b12334e1SAndreas Gohr // detect LIKE filters 163b12334e1SAndreas Gohr if ($this->useLike($value)) { 164b12334e1SAndreas Gohr $where .= " AND $field LIKE $q"; 165b12334e1SAndreas Gohr } else { 166b12334e1SAndreas Gohr $where .= " AND $field = $q"; 167b12334e1SAndreas Gohr } 168b12334e1SAndreas Gohr } 169204c069bSMichael Große } 1706c37861bSJulian Einwag $where .= ' AND GETACCESSLEVEL(pid) >= ' . AUTH_READ; 17159a953ffSMichael Große 172b12334e1SAndreas Gohr // group and order 173b12334e1SAndreas Gohr if ($type == 'tag') { 174b12334e1SAndreas Gohr $groupby = 'CLEANTAG(tag)'; 175b12334e1SAndreas Gohr $orderby = 'CLEANTAG(tag)'; 176b12334e1SAndreas Gohr } else { 177b12334e1SAndreas Gohr $groupby = $type; 178466524f5SAndreas Gohr $orderby = "cnt DESC, $type"; 179b12334e1SAndreas Gohr } 180b12334e1SAndreas Gohr 181077ff864SAndreas Gohr // limit results 182077ff864SAndreas Gohr if ($limit) { 183077ff864SAndreas Gohr $limit = " LIMIT $limit"; 184077ff864SAndreas Gohr } else { 185077ff864SAndreas Gohr $limit = ''; 186077ff864SAndreas Gohr } 187077ff864SAndreas Gohr 188b12334e1SAndreas Gohr // create SQL 189b12334e1SAndreas Gohr $sql = "SELECT $type AS item, COUNT(*) AS cnt 190b12334e1SAndreas Gohr FROM taggings 191b12334e1SAndreas Gohr WHERE $where 192b12334e1SAndreas Gohr GROUP BY $groupby 193077ff864SAndreas Gohr ORDER BY $orderby 194077ff864SAndreas Gohr $limit 195077ff864SAndreas Gohr "; 196b12334e1SAndreas Gohr 197b12334e1SAndreas Gohr // run query and turn into associative array 198b12334e1SAndreas Gohr $res = $db->query($sql, array_values($filter)); 199f61105deSAdrian Lang $res = $db->res2arr($res); 200b12334e1SAndreas Gohr 201f61105deSAdrian Lang $ret = array(); 202b12334e1SAndreas Gohr foreach ($res as $row) { 203b12334e1SAndreas Gohr $ret[$row['item']] = $row['cnt']; 204f61105deSAdrian Lang } 205*ca455b8eSMichael Große 206f61105deSAdrian Lang return $ret; 207f61105deSAdrian Lang } 208f61105deSAdrian Lang 209b12334e1SAndreas Gohr /** 210b12334e1SAndreas Gohr * Check if the given string is a LIKE statement 211b12334e1SAndreas Gohr * 212b12334e1SAndreas Gohr * @param string $value 2130cfde7e9SMichael Große * 214b12334e1SAndreas Gohr * @return bool 215b12334e1SAndreas Gohr */ 216b12334e1SAndreas Gohr private function useLike($value) { 217b12334e1SAndreas Gohr return strpos($value, '%') === 0 || strrpos($value, '%') === strlen($value) - 1; 218b2073787SDominik Eckelmann } 219b2073787SDominik Eckelmann 220302a38efSAndreas Gohr /** 221302a38efSAndreas Gohr * Constructs the URL to search for a tag 222302a38efSAndreas Gohr * 2235540f91dSAndreas Gohr * @param string $tag 2245540f91dSAndreas Gohr * @param string $ns 2250cfde7e9SMichael Große * 226302a38efSAndreas Gohr * @return string 227302a38efSAndreas Gohr */ 2285540f91dSAndreas Gohr public function getTagSearchURL($tag, $ns = '') { 229bed9f360SAndreas Gohr // wrap tag in quotes if non clean 2302cb25ec1SAndreas Gohr $ctag = utf8_stripspecials($this->cleanTag($tag)); 2310cfde7e9SMichael Große if ($ctag != utf8_strtolower($tag)) { 2320cfde7e9SMichael Große $tag = '"' . $tag . '"'; 2330cfde7e9SMichael Große } 234bed9f360SAndreas Gohr 235bed9f360SAndreas Gohr $ret = '?do=search&id=' . rawurlencode($tag); 2360cfde7e9SMichael Große if ($ns) { 2370cfde7e9SMichael Große $ret .= rawurlencode(' @' . $ns); 2380cfde7e9SMichael Große } 2395540f91dSAndreas Gohr 2405540f91dSAndreas Gohr return $ret; 241f61105deSAdrian Lang } 242f61105deSAdrian Lang 2435540f91dSAndreas Gohr /** 2445540f91dSAndreas Gohr * Calculates the size levels for the given list of clouds 2455540f91dSAndreas Gohr * 2465540f91dSAndreas Gohr * Automatically determines sensible tresholds 2475540f91dSAndreas Gohr * 2485540f91dSAndreas Gohr * @param array $tags list of tags => count 2495540f91dSAndreas Gohr * @param int $levels 2500cfde7e9SMichael Große * 2515540f91dSAndreas Gohr * @return mixed 2525540f91dSAndreas Gohr */ 253f61105deSAdrian Lang public function cloudData($tags, $levels = 10) { 254f61105deSAdrian Lang $min = min($tags); 255f61105deSAdrian Lang $max = max($tags); 256f61105deSAdrian Lang 257f61105deSAdrian Lang // calculate tresholds 258f61105deSAdrian Lang $tresholds = array(); 259f61105deSAdrian Lang for ($i = 0; $i <= $levels; $i++) { 260f61105deSAdrian Lang $tresholds[$i] = pow($max - $min + 1, $i / $levels) + $min - 1; 261f61105deSAdrian Lang } 262f61105deSAdrian Lang 263f61105deSAdrian Lang // assign weights 264f61105deSAdrian Lang foreach ($tags as $tag => $cnt) { 265f61105deSAdrian Lang foreach ($tresholds as $tresh => $val) { 266f61105deSAdrian Lang if ($cnt <= $val) { 267f61105deSAdrian Lang $tags[$tag] = $tresh; 268f61105deSAdrian Lang break; 269f61105deSAdrian Lang } 270f61105deSAdrian Lang $tags[$tag] = $levels; 271f61105deSAdrian Lang } 272f61105deSAdrian Lang } 273*ca455b8eSMichael Große 274f61105deSAdrian Lang return $tags; 275f61105deSAdrian Lang } 276f61105deSAdrian Lang 2775540f91dSAndreas Gohr /** 2785540f91dSAndreas Gohr * Display a tag cloud 2795540f91dSAndreas Gohr * 2805540f91dSAndreas Gohr * @param array $tags list of tags => count 2815540f91dSAndreas Gohr * @param string $type 'tag' 2825540f91dSAndreas Gohr * @param Callable $func The function to print the link (gets tag and ns) 2835540f91dSAndreas Gohr * @param bool $wrap wrap cloud in UL tags? 2845540f91dSAndreas Gohr * @param bool $return returnn HTML instead of printing? 2855540f91dSAndreas Gohr * @param string $ns Add this namespace to search links 2860cfde7e9SMichael Große * 2875540f91dSAndreas Gohr * @return string 2885540f91dSAndreas Gohr */ 2895540f91dSAndreas Gohr public function html_cloud($tags, $type, $func, $wrap = true, $return = false, $ns = '') { 290a66f6715SAndreas Gohr global $INFO; 291a66f6715SAndreas Gohr 292a66f6715SAndreas Gohr $hidden_str = $this->getConf('hiddenprefix'); 293a66f6715SAndreas Gohr $hidden_len = strlen($hidden_str); 294a66f6715SAndreas Gohr 295f61105deSAdrian Lang $ret = ''; 2960cfde7e9SMichael Große if ($wrap) { 2970cfde7e9SMichael Große $ret .= '<ul class="tagging_cloud clearfix">'; 2980cfde7e9SMichael Große } 299f61105deSAdrian Lang if (count($tags) === 0) { 300f61105deSAdrian Lang // Produce valid XHTML (ul needs a child) 301f61105deSAdrian Lang $this->setupLocale(); 302f61105deSAdrian Lang $ret .= '<li><div class="li">' . $this->lang['js']['no' . $type . 's'] . '</div></li>'; 303f61105deSAdrian Lang } else { 304f61105deSAdrian Lang $tags = $this->cloudData($tags); 305f61105deSAdrian Lang foreach ($tags as $val => $size) { 306a66f6715SAndreas Gohr // skip hidden tags for users that can't edit 307a66f6715SAndreas Gohr if ($type == 'tag' and 308a66f6715SAndreas Gohr $hidden_len and 309a66f6715SAndreas Gohr substr($val, 0, $hidden_len) == $hidden_str and 310a66f6715SAndreas Gohr !($this->getUser() && $INFO['writable']) 311a66f6715SAndreas Gohr ) { 312a66f6715SAndreas Gohr continue; 313a66f6715SAndreas Gohr } 314a66f6715SAndreas Gohr 315f61105deSAdrian Lang $ret .= '<li class="t' . $size . '"><div class="li">'; 3165540f91dSAndreas Gohr $ret .= call_user_func($func, $val, $ns); 317f61105deSAdrian Lang $ret .= '</div></li>'; 318f61105deSAdrian Lang } 319f61105deSAdrian Lang } 3200cfde7e9SMichael Große if ($wrap) { 3210cfde7e9SMichael Große $ret .= '</ul>'; 3220cfde7e9SMichael Große } 3230cfde7e9SMichael Große if ($return) { 3240cfde7e9SMichael Große return $ret; 3250cfde7e9SMichael Große } 326f61105deSAdrian Lang echo $ret; 327*ca455b8eSMichael Große 3285540f91dSAndreas Gohr return ''; 329f61105deSAdrian Lang } 330f61105deSAdrian Lang 3315540f91dSAndreas Gohr /** 3325540f91dSAndreas Gohr * Get the link to a search for the given tag 3335540f91dSAndreas Gohr * 3345540f91dSAndreas Gohr * @param string $tag search for this tag 3355540f91dSAndreas Gohr * @param string $ns limit search to this namespace 3360cfde7e9SMichael Große * 3375540f91dSAndreas Gohr * @return string 3385540f91dSAndreas Gohr */ 3395540f91dSAndreas Gohr protected function linkToSearch($tag, $ns = '') { 3405540f91dSAndreas Gohr return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>'; 341f61105deSAdrian Lang } 342f61105deSAdrian Lang 343fb1d0583SAndreas Gohr /** 344fb1d0583SAndreas Gohr * Display the Tags for the current page and prepare the tag editing form 3453496cc8aSAndreas Gohr * 3463496cc8aSAndreas Gohr * @param bool $print Should the HTML be printed or returned? 3470cfde7e9SMichael Große * 3483496cc8aSAndreas Gohr * @return string 349fb1d0583SAndreas Gohr */ 3503496cc8aSAndreas Gohr public function tpl_tags($print = true) { 351f61105deSAdrian Lang global $INFO; 352f61105deSAdrian Lang global $lang; 3533bf0e2f1SMichael Große 3543bf0e2f1SMichael Große $filter = array('pid' => $INFO['id']); 3553bf0e2f1SMichael Große if ($this->getConf('singleusermode')) { 3563bf0e2f1SMichael Große $filter['tagger'] = 'auto'; 3573bf0e2f1SMichael Große } 3583bf0e2f1SMichael Große 3593bf0e2f1SMichael Große $tags = $this->findItems($filter, 'tag'); 3603496cc8aSAndreas Gohr 3613496cc8aSAndreas Gohr $ret = ''; 3623496cc8aSAndreas Gohr 3633496cc8aSAndreas Gohr $ret .= '<div class="plugin_tagging_edit">'; 3643496cc8aSAndreas Gohr $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true); 365f61105deSAdrian Lang 3662ace74f4SAndreas Gohr if ($this->getUser() && $INFO['writable']) { 367f61105deSAdrian Lang $lang['btn_tagging_edit'] = $lang['btn_secedit']; 368e5b42768SSzymon Olewniczak $ret .= '<div id="tagging__edit_buttons_group">'; 3693496cc8aSAndreas Gohr $ret .= html_btn('tagging_edit', $INFO['id'], '', array()); 370dd52fd45SSzymon Olewniczak if (auth_isadmin()) { 371e5b42768SSzymon Olewniczak $ret .= '<label>' . $this->getLang('toggle admin mode') . '<input type="checkbox" id="tagging__edit_toggle_admin" /></label>'; 372dd52fd45SSzymon Olewniczak } 373e5b42768SSzymon Olewniczak $ret .= '</div>'; 3742819ffcdSSzymon Olewniczak $form = new dokuwiki\Form\Form(); 3752819ffcdSSzymon Olewniczak $form->id('tagging__edit'); 3762819ffcdSSzymon Olewniczak $form->setHiddenField('tagging[id]', $INFO['id']); 3772819ffcdSSzymon Olewniczak $form->setHiddenField('call', 'plugin_tagging_save'); 3782819ffcdSSzymon Olewniczak $tags = $this->findItems(array( 3792819ffcdSSzymon Olewniczak 'pid' => $INFO['id'], 380*ca455b8eSMichael Große 'tagger' => $this->getUser(), 3812819ffcdSSzymon Olewniczak ), 'tag'); 382d8e6aa7fSMichael Große $form->addTextarea('tagging[tags]')->val(implode(', ', array_keys($tags)))->addClass('edit'); 383cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_save'])->id('tagging__edit_save'); 384cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel'); 3852819ffcdSSzymon Olewniczak $ret .= $form->toHTML(); 386f61105deSAdrian Lang } 3873496cc8aSAndreas Gohr $ret .= '</div>'; 3883496cc8aSAndreas Gohr 3890cfde7e9SMichael Große if ($print) { 3900cfde7e9SMichael Große echo $ret; 3910cfde7e9SMichael Große } 392*ca455b8eSMichael Große 3933496cc8aSAndreas Gohr return $ret; 394f61105deSAdrian Lang } 395872edc7cSRené Corinth 3968a1a3846SAndreas Gohr /** 397a99b66c1SSzymon Olewniczak * @param string $namespace empty for entire wiki 398a99b66c1SSzymon Olewniczak * 3998a1a3846SAndreas Gohr * @return array 4008a1a3846SAndreas Gohr */ 401cb469644SSzymon Olewniczak public function getAllTags($namespace = '', $order_by = 'tag', $desc = false) { 4028e9d0162SSzymon Olewniczak $order_fields = array('pid', 'tid', 'orig', 'taggers', 'count'); 403f0084ee1SSzymon Olewniczak if (!in_array($order_by, $order_fields)) { 404f0084ee1SSzymon Olewniczak msg('cannot sort by ' . $order_by . ' field does not exists', -1); 405f0084ee1SSzymon Olewniczak $order_by = 'tag'; 406f0084ee1SSzymon Olewniczak } 407872edc7cSRené Corinth 408872edc7cSRené Corinth $db = $this->getDb(); 409872edc7cSRené Corinth 410f0084ee1SSzymon Olewniczak $query = 'SELECT "pid", 411*ca455b8eSMichael Große CLEANTAG("tag") AS "tid", 412f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tag"), \', \') AS "orig", 413f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tagger"), \', \') AS "taggers", 414193a767dSSzymon Olewniczak COUNT(*) AS "count" 41557e45304SSzymon Olewniczak FROM "taggings" 416f0084ee1SSzymon Olewniczak WHERE "pid" LIKE ? 417f0084ee1SSzymon Olewniczak GROUP BY "tid" 418f0084ee1SSzymon Olewniczak ORDER BY ' . $order_by; 419*ca455b8eSMichael Große if ($desc) { 420*ca455b8eSMichael Große $query .= ' DESC'; 421*ca455b8eSMichael Große } 422cb469644SSzymon Olewniczak 423f0084ee1SSzymon Olewniczak $res = $db->query($query, $this->globNamespace($namespace)); 424872edc7cSRené Corinth 4257e05e623SSzymon Olewniczak return $db->res2arr($res); 426872edc7cSRené Corinth } 427872edc7cSRené Corinth 4288a1a3846SAndreas Gohr /** 4298a1a3846SAndreas Gohr * Renames a tag 4308a1a3846SAndreas Gohr * 4318a1a3846SAndreas Gohr * @param string $formerTagName 4328a1a3846SAndreas Gohr * @param string $newTagName 4338a1a3846SAndreas Gohr */ 434872edc7cSRené Corinth public function renameTag($formerTagName, $newTagName) { 435872edc7cSRené Corinth 436872edc7cSRené Corinth if (empty($formerTagName) || empty($newTagName)) { 4378a1a3846SAndreas Gohr msg($this->getLang("admin enter tag names"), -1); 438*ca455b8eSMichael Große 4398a1a3846SAndreas Gohr return; 440872edc7cSRené Corinth } 441872edc7cSRené Corinth 442872edc7cSRené Corinth $db = $this->getDb(); 443872edc7cSRené Corinth 444fb1d0583SAndreas Gohr $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ?', $this->cleanTag($formerTagName)); 445872edc7cSRené Corinth $check = $db->res2arr($res); 446872edc7cSRené Corinth 447872edc7cSRené Corinth if (empty($check)) { 4488a1a3846SAndreas Gohr msg($this->getLang("admin tag does not exists"), -1); 449*ca455b8eSMichael Große 4508a1a3846SAndreas Gohr return; 451872edc7cSRené Corinth } 452872edc7cSRené Corinth 453fb1d0583SAndreas Gohr $res = $db->query("UPDATE taggings SET tag = ? WHERE CLEANTAG(tag) = ?", $newTagName, $this->cleanTag($formerTagName)); 454872edc7cSRené Corinth $db->res2arr($res); 455872edc7cSRené Corinth 456fb1d0583SAndreas Gohr msg($this->getLang("admin renamed"), 1); 457*ca455b8eSMichael Große 4588a1a3846SAndreas Gohr return; 459872edc7cSRené Corinth } 460872edc7cSRené Corinth 4618f630140SSzymon Olewniczak /** 462dd52fd45SSzymon Olewniczak * Rename or delete a tag for all users 463dd52fd45SSzymon Olewniczak * 464dd52fd45SSzymon Olewniczak * @param string $pid 465dd52fd45SSzymon Olewniczak * @param string $formerTagName 466dd52fd45SSzymon Olewniczak * @param string $newTagName 467dd52fd45SSzymon Olewniczak * 468dd52fd45SSzymon Olewniczak * @return array 469dd52fd45SSzymon Olewniczak */ 470dd52fd45SSzymon Olewniczak public function modifyPageTag($pid, $formerTagName, $newTagName) { 471dd52fd45SSzymon Olewniczak 472dd52fd45SSzymon Olewniczak $db = $this->getDb(); 473dd52fd45SSzymon Olewniczak 474dd52fd45SSzymon Olewniczak $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ? AND pid = ?', $this->cleanTag($formerTagName), $pid); 475dd52fd45SSzymon Olewniczak $check = $db->res2arr($res); 476dd52fd45SSzymon Olewniczak 477dd52fd45SSzymon Olewniczak if (empty($check)) { 478dd52fd45SSzymon Olewniczak return array(true, $this->getLang('admin tag does not exists')); 479dd52fd45SSzymon Olewniczak } 480dd52fd45SSzymon Olewniczak 481dd52fd45SSzymon Olewniczak if (empty($newTagName)) { 482dd52fd45SSzymon Olewniczak $res = $db->query('DELETE FROM taggings WHERE pid = ? AND CLEANTAG(tag) = ?', $pid, $this->cleanTag($formerTagName)); 483dd52fd45SSzymon Olewniczak } else { 484dd52fd45SSzymon Olewniczak $res = $db->query('UPDATE taggings SET tag = ? WHERE pid = ? AND CLEANTAG(tag) = ?', $newTagName, $pid, $this->cleanTag($formerTagName)); 485dd52fd45SSzymon Olewniczak } 486dd52fd45SSzymon Olewniczak $db->res2arr($res); 487dd52fd45SSzymon Olewniczak 488dd52fd45SSzymon Olewniczak return array(false, $this->getLang('admin renamed')); 489dd52fd45SSzymon Olewniczak } 490dd52fd45SSzymon Olewniczak 491dd52fd45SSzymon Olewniczak /** 4928f630140SSzymon Olewniczak * Deletes a tag 4938f630140SSzymon Olewniczak * 4948f630140SSzymon Olewniczak * @param array $tags 49531396860SSzymon Olewniczak * @param string $namespace current namespace context as in getAllTags() 4968f630140SSzymon Olewniczak */ 49731396860SSzymon Olewniczak public function deleteTags($tags, $namespace = '') { 498*ca455b8eSMichael Große if (empty($tags)) { 499*ca455b8eSMichael Große return; 500*ca455b8eSMichael Große } 5018f630140SSzymon Olewniczak 50231396860SSzymon Olewniczak $namespace = cleanId($namespace); 50331396860SSzymon Olewniczak 5048f630140SSzymon Olewniczak $db = $this->getDb(); 5058f630140SSzymon Olewniczak 50631396860SSzymon Olewniczak $query = 'DELETE FROM taggings WHERE pid LIKE ? AND (' . 50731396860SSzymon Olewniczak implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')) . ')'; 50831396860SSzymon Olewniczak 50931396860SSzymon Olewniczak $args = array_map(array($this, 'cleanTag'), $tags); 51031396860SSzymon Olewniczak array_unshift($args, $this->globNamespace($namespace)); 51131396860SSzymon Olewniczak $res = $db->query($query, $args); 5128f630140SSzymon Olewniczak 5138f630140SSzymon Olewniczak msg(sprintf($this->getLang("admin deleted"), count($tags), $res->rowCount()), 1); 514*ca455b8eSMichael Große 5158f630140SSzymon Olewniczak return; 5168f630140SSzymon Olewniczak } 517f61105deSAdrian Lang} 518