1f61105deSAdrian Lang<?php 2aa627deeSAndreas Gohr/** 3aa627deeSAndreas Gohr * Tagging Plugin (hlper component) 4aa627deeSAndreas Gohr * 5aa627deeSAndreas Gohr * @license GPL 2 6aa627deeSAndreas 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; 18aa627deeSAndreas Gohr if ($db !== null) { 19f61105deSAdrian Lang return $db; 20f61105deSAdrian Lang } 21f61105deSAdrian Lang 22302a38efSAndreas Gohr /** @var helper_plugin_sqlite $db */ 23f61105deSAdrian Lang $db = plugin_load('helper', 'sqlite'); 24aa627deeSAndreas Gohr if ($db === null) { 25f61105deSAdrian Lang msg('The tagging plugin needs the sqlite plugin', -1); 26ca455b8eSMichael Große 27f61105deSAdrian Lang return false; 28f61105deSAdrian Lang } 29aa627deeSAndreas Gohr $db->init('tagging', __DIR__ . '/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); 35ca455b8eSMichael Große 367e05e623SSzymon Olewniczak return implode($newDelimiter, $ex); 377e05e623SSzymon Olewniczak }, 2); 38ca455b8eSMichael 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 } 56ca455b8eSMichael 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) { 68aa627deeSAndreas Gohr $tag = str_replace(array(' ', '-', '_'), '', $tag); 69302a38efSAndreas Gohr $tag = utf8_strtolower($tag); 70ca455b8eSMichael Große 71302a38efSAndreas Gohr return $tag; 72302a38efSAndreas Gohr } 73302a38efSAndreas Gohr 7456d82720SAndreas Gohr /** 7531396860SSzymon Olewniczak * Canonicalizes the namespace, remove the first colon and add glob 7631396860SSzymon Olewniczak * 7731396860SSzymon Olewniczak * @param $namespace 7831396860SSzymon Olewniczak * 7931396860SSzymon Olewniczak * @return string 8031396860SSzymon Olewniczak */ 8131396860SSzymon Olewniczak public function globNamespace($namespace) { 8231396860SSzymon Olewniczak return cleanId($namespace) . '%'; 8331396860SSzymon Olewniczak } 8431396860SSzymon Olewniczak 8531396860SSzymon Olewniczak /** 8656d82720SAndreas Gohr * Create or Update tags of a page 8756d82720SAndreas Gohr * 8856d82720SAndreas Gohr * Uses the translation plugin to store the language of a page (if available) 8956d82720SAndreas Gohr * 9056d82720SAndreas Gohr * @param string $id The page ID 9156d82720SAndreas Gohr * @param string $user 9256d82720SAndreas Gohr * @param array $tags 930cfde7e9SMichael Große * 9456d82720SAndreas Gohr * @return bool|SQLiteResult 9556d82720SAndreas Gohr */ 96f61105deSAdrian Lang public function replaceTags($id, $user, $tags) { 9756d82720SAndreas Gohr global $conf; 9856d82720SAndreas Gohr /** @var helper_plugin_translation $trans */ 9956d82720SAndreas Gohr $trans = plugin_load('helper', 'translation'); 10056d82720SAndreas Gohr if ($trans) { 10156d82720SAndreas Gohr $lang = $trans->realLC($trans->getLangPart($id)); 10256d82720SAndreas Gohr } else { 10356d82720SAndreas Gohr $lang = $conf['lang']; 10456d82720SAndreas Gohr } 10556d82720SAndreas Gohr 106f61105deSAdrian Lang $db = $this->getDB(); 107f61105deSAdrian Lang $db->query('BEGIN TRANSACTION'); 108f61105deSAdrian Lang $queries = array(array('DELETE FROM taggings WHERE pid = ? AND tagger = ?', $id, $user)); 109f61105deSAdrian Lang foreach ($tags as $tag) { 11056d82720SAndreas Gohr $queries[] = array('INSERT INTO taggings (pid, tagger, tag, lang) VALUES(?, ?, ?, ?)', $id, $user, $tag, $lang); 111f61105deSAdrian Lang } 112f61105deSAdrian Lang 113f61105deSAdrian Lang foreach ($queries as $query) { 114f61105deSAdrian Lang if (!call_user_func_array(array($db, 'query'), $query)) { 115f61105deSAdrian Lang $db->query('ROLLBACK TRANSACTION'); 116ca455b8eSMichael Große 117f61105deSAdrian Lang return false; 118f61105deSAdrian Lang } 119f61105deSAdrian Lang } 120ca455b8eSMichael Große 121f61105deSAdrian Lang return $db->query('COMMIT TRANSACTION'); 122f61105deSAdrian Lang } 123f61105deSAdrian Lang 1240a518a11SAndreas Gohr /** 125b12334e1SAndreas Gohr * Get a list of Tags or Pages matching search criteria 1260a518a11SAndreas Gohr * 127b12334e1SAndreas Gohr * @param array $filter What to search for array('field' => 'searchterm') 128b12334e1SAndreas Gohr * @param string $type What field to return 'tag'|'pid' 129077ff864SAndreas Gohr * @param int $limit Limit to this many results, 0 for all 1300cfde7e9SMichael Große * 1310a518a11SAndreas Gohr * @return array associative array in form of value => count 1320a518a11SAndreas Gohr */ 133077ff864SAndreas Gohr public function findItems($filter, $type, $limit = 0) { 134f61105deSAdrian Lang $db = $this->getDB(); 1350cfde7e9SMichael Große if (!$db) { 1360cfde7e9SMichael Große return array(); 1370cfde7e9SMichael Große } 138f61105deSAdrian Lang 139b12334e1SAndreas Gohr // create WHERE clause 140b12334e1SAndreas Gohr $where = '1=1'; 141b12334e1SAndreas Gohr foreach ($filter as $field => $value) { 142b12334e1SAndreas Gohr // compare clean tags only 143b12334e1SAndreas Gohr if ($field === 'tag') { 144b12334e1SAndreas Gohr $field = 'CLEANTAG(tag)'; 145b12334e1SAndreas Gohr $q = 'CLEANTAG(?)'; 146b12334e1SAndreas Gohr } else { 147b12334e1SAndreas Gohr $q = '?'; 148b12334e1SAndreas Gohr } 149204c069bSMichael Große 150204c069bSMichael Große if (substr($field, 0, 6) === 'notpid') { 151204c069bSMichael Große $field = 'pid'; 152204c069bSMichael Große 153204c069bSMichael Große // detect LIKE filters 154204c069bSMichael Große if ($this->useLike($value)) { 155204c069bSMichael Große $where .= " AND $field NOT LIKE $q"; 156204c069bSMichael Große } else { 157204c069bSMichael Große $where .= " AND $field != $q"; 158204c069bSMichael Große } 159204c069bSMichael Große } else { 160b12334e1SAndreas Gohr // detect LIKE filters 161b12334e1SAndreas Gohr if ($this->useLike($value)) { 162b12334e1SAndreas Gohr $where .= " AND $field LIKE $q"; 163b12334e1SAndreas Gohr } else { 164b12334e1SAndreas Gohr $where .= " AND $field = $q"; 165b12334e1SAndreas Gohr } 166b12334e1SAndreas Gohr } 167204c069bSMichael Große } 1686c37861bSJulian Einwag $where .= ' AND GETACCESSLEVEL(pid) >= ' . AUTH_READ; 16959a953ffSMichael Große 170b12334e1SAndreas Gohr // group and order 171aa627deeSAndreas Gohr if ($type === 'tag') { 172b12334e1SAndreas Gohr $groupby = 'CLEANTAG(tag)'; 173b12334e1SAndreas Gohr $orderby = 'CLEANTAG(tag)'; 174b12334e1SAndreas Gohr } else { 175b12334e1SAndreas Gohr $groupby = $type; 176466524f5SAndreas Gohr $orderby = "cnt DESC, $type"; 177b12334e1SAndreas Gohr } 178b12334e1SAndreas Gohr 179077ff864SAndreas Gohr // limit results 180077ff864SAndreas Gohr if ($limit) { 181077ff864SAndreas Gohr $limit = " LIMIT $limit"; 182077ff864SAndreas Gohr } else { 183077ff864SAndreas Gohr $limit = ''; 184077ff864SAndreas Gohr } 185077ff864SAndreas Gohr 186b12334e1SAndreas Gohr // create SQL 187b12334e1SAndreas Gohr $sql = "SELECT $type AS item, COUNT(*) AS cnt 188b12334e1SAndreas Gohr FROM taggings 189b12334e1SAndreas Gohr WHERE $where 190b12334e1SAndreas Gohr GROUP BY $groupby 191077ff864SAndreas Gohr ORDER BY $orderby 192077ff864SAndreas Gohr $limit 193077ff864SAndreas Gohr "; 194b12334e1SAndreas Gohr 195b12334e1SAndreas Gohr // run query and turn into associative array 196b12334e1SAndreas Gohr $res = $db->query($sql, array_values($filter)); 197f61105deSAdrian Lang $res = $db->res2arr($res); 198b12334e1SAndreas Gohr 199f61105deSAdrian Lang $ret = array(); 200b12334e1SAndreas Gohr foreach ($res as $row) { 201b12334e1SAndreas Gohr $ret[$row['item']] = $row['cnt']; 202f61105deSAdrian Lang } 203ca455b8eSMichael Große 204f61105deSAdrian Lang return $ret; 205f61105deSAdrian Lang } 206f61105deSAdrian Lang 207b12334e1SAndreas Gohr /** 208b12334e1SAndreas Gohr * Check if the given string is a LIKE statement 209b12334e1SAndreas Gohr * 210b12334e1SAndreas Gohr * @param string $value 2110cfde7e9SMichael Große * 212b12334e1SAndreas Gohr * @return bool 213b12334e1SAndreas Gohr */ 214b12334e1SAndreas Gohr private function useLike($value) { 215b12334e1SAndreas Gohr return strpos($value, '%') === 0 || strrpos($value, '%') === strlen($value) - 1; 216b2073787SDominik Eckelmann } 217b2073787SDominik Eckelmann 218302a38efSAndreas Gohr /** 219302a38efSAndreas Gohr * Constructs the URL to search for a tag 220302a38efSAndreas Gohr * 2215540f91dSAndreas Gohr * @param string $tag 2225540f91dSAndreas Gohr * @param string $ns 2230cfde7e9SMichael Große * 224302a38efSAndreas Gohr * @return string 225302a38efSAndreas Gohr */ 2265540f91dSAndreas Gohr public function getTagSearchURL($tag, $ns = '') { 227bed9f360SAndreas Gohr // wrap tag in quotes if non clean 2282cb25ec1SAndreas Gohr $ctag = utf8_stripspecials($this->cleanTag($tag)); 2290cfde7e9SMichael Große if ($ctag != utf8_strtolower($tag)) { 2300cfde7e9SMichael Große $tag = '"' . $tag . '"'; 2310cfde7e9SMichael Große } 232bed9f360SAndreas Gohr 233bed9f360SAndreas Gohr $ret = '?do=search&id=' . rawurlencode($tag); 2340cfde7e9SMichael Große if ($ns) { 2350cfde7e9SMichael Große $ret .= rawurlencode(' @' . $ns); 2360cfde7e9SMichael Große } 2375540f91dSAndreas Gohr 2385540f91dSAndreas Gohr return $ret; 239f61105deSAdrian Lang } 240f61105deSAdrian Lang 2415540f91dSAndreas Gohr /** 2425540f91dSAndreas Gohr * Calculates the size levels for the given list of clouds 2435540f91dSAndreas Gohr * 2445540f91dSAndreas Gohr * Automatically determines sensible tresholds 2455540f91dSAndreas Gohr * 2465540f91dSAndreas Gohr * @param array $tags list of tags => count 2475540f91dSAndreas Gohr * @param int $levels 2480cfde7e9SMichael Große * 2495540f91dSAndreas Gohr * @return mixed 2505540f91dSAndreas Gohr */ 251f61105deSAdrian Lang public function cloudData($tags, $levels = 10) { 252f61105deSAdrian Lang $min = min($tags); 253f61105deSAdrian Lang $max = max($tags); 254f61105deSAdrian Lang 255f61105deSAdrian Lang // calculate tresholds 256f61105deSAdrian Lang $tresholds = array(); 257f61105deSAdrian Lang for ($i = 0; $i <= $levels; $i++) { 258f61105deSAdrian Lang $tresholds[$i] = pow($max - $min + 1, $i / $levels) + $min - 1; 259f61105deSAdrian Lang } 260f61105deSAdrian Lang 261f61105deSAdrian Lang // assign weights 262f61105deSAdrian Lang foreach ($tags as $tag => $cnt) { 263f61105deSAdrian Lang foreach ($tresholds as $tresh => $val) { 264f61105deSAdrian Lang if ($cnt <= $val) { 265f61105deSAdrian Lang $tags[$tag] = $tresh; 266f61105deSAdrian Lang break; 267f61105deSAdrian Lang } 268f61105deSAdrian Lang $tags[$tag] = $levels; 269f61105deSAdrian Lang } 270f61105deSAdrian Lang } 271ca455b8eSMichael Große 272f61105deSAdrian Lang return $tags; 273f61105deSAdrian Lang } 274f61105deSAdrian Lang 2755540f91dSAndreas Gohr /** 2765540f91dSAndreas Gohr * Display a tag cloud 2775540f91dSAndreas Gohr * 2785540f91dSAndreas Gohr * @param array $tags list of tags => count 2795540f91dSAndreas Gohr * @param string $type 'tag' 2805540f91dSAndreas Gohr * @param Callable $func The function to print the link (gets tag and ns) 2815540f91dSAndreas Gohr * @param bool $wrap wrap cloud in UL tags? 2825540f91dSAndreas Gohr * @param bool $return returnn HTML instead of printing? 2835540f91dSAndreas Gohr * @param string $ns Add this namespace to search links 2840cfde7e9SMichael Große * 2855540f91dSAndreas Gohr * @return string 2865540f91dSAndreas Gohr */ 2875540f91dSAndreas Gohr public function html_cloud($tags, $type, $func, $wrap = true, $return = false, $ns = '') { 288a66f6715SAndreas Gohr global $INFO; 289a66f6715SAndreas Gohr 290a66f6715SAndreas Gohr $hidden_str = $this->getConf('hiddenprefix'); 291a66f6715SAndreas Gohr $hidden_len = strlen($hidden_str); 292a66f6715SAndreas Gohr 293f61105deSAdrian Lang $ret = ''; 2940cfde7e9SMichael Große if ($wrap) { 2950cfde7e9SMichael Große $ret .= '<ul class="tagging_cloud clearfix">'; 2960cfde7e9SMichael Große } 297f61105deSAdrian Lang if (count($tags) === 0) { 298f61105deSAdrian Lang // Produce valid XHTML (ul needs a child) 299f61105deSAdrian Lang $this->setupLocale(); 300f61105deSAdrian Lang $ret .= '<li><div class="li">' . $this->lang['js']['no' . $type . 's'] . '</div></li>'; 301f61105deSAdrian Lang } else { 302f61105deSAdrian Lang $tags = $this->cloudData($tags); 303f61105deSAdrian Lang foreach ($tags as $val => $size) { 304a66f6715SAndreas Gohr // skip hidden tags for users that can't edit 305aa627deeSAndreas Gohr if ($type === 'tag' and 306a66f6715SAndreas Gohr $hidden_len and 307a66f6715SAndreas Gohr substr($val, 0, $hidden_len) == $hidden_str and 308a66f6715SAndreas Gohr !($this->getUser() && $INFO['writable']) 309a66f6715SAndreas Gohr ) { 310a66f6715SAndreas Gohr continue; 311a66f6715SAndreas Gohr } 312a66f6715SAndreas Gohr 313f61105deSAdrian Lang $ret .= '<li class="t' . $size . '"><div class="li">'; 3145540f91dSAndreas Gohr $ret .= call_user_func($func, $val, $ns); 315f61105deSAdrian Lang $ret .= '</div></li>'; 316f61105deSAdrian Lang } 317f61105deSAdrian Lang } 3180cfde7e9SMichael Große if ($wrap) { 3190cfde7e9SMichael Große $ret .= '</ul>'; 3200cfde7e9SMichael Große } 3210cfde7e9SMichael Große if ($return) { 3220cfde7e9SMichael Große return $ret; 3230cfde7e9SMichael Große } 324f61105deSAdrian Lang echo $ret; 325ca455b8eSMichael Große 3265540f91dSAndreas Gohr return ''; 327f61105deSAdrian Lang } 328f61105deSAdrian Lang 3295540f91dSAndreas Gohr /** 3305540f91dSAndreas Gohr * Get the link to a search for the given tag 3315540f91dSAndreas Gohr * 3325540f91dSAndreas Gohr * @param string $tag search for this tag 3335540f91dSAndreas Gohr * @param string $ns limit search to this namespace 3340cfde7e9SMichael Große * 3355540f91dSAndreas Gohr * @return string 3365540f91dSAndreas Gohr */ 3375540f91dSAndreas Gohr protected function linkToSearch($tag, $ns = '') { 3385540f91dSAndreas Gohr return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>'; 339f61105deSAdrian Lang } 340f61105deSAdrian Lang 341fb1d0583SAndreas Gohr /** 342fb1d0583SAndreas Gohr * Display the Tags for the current page and prepare the tag editing form 3433496cc8aSAndreas Gohr * 3443496cc8aSAndreas Gohr * @param bool $print Should the HTML be printed or returned? 3450cfde7e9SMichael Große * 3463496cc8aSAndreas Gohr * @return string 347fb1d0583SAndreas Gohr */ 3483496cc8aSAndreas Gohr public function tpl_tags($print = true) { 349f61105deSAdrian Lang global $INFO; 350f61105deSAdrian Lang global $lang; 3513bf0e2f1SMichael Große 3523bf0e2f1SMichael Große $filter = array('pid' => $INFO['id']); 3533bf0e2f1SMichael Große if ($this->getConf('singleusermode')) { 3543bf0e2f1SMichael Große $filter['tagger'] = 'auto'; 3553bf0e2f1SMichael Große } 3563bf0e2f1SMichael Große 3573bf0e2f1SMichael Große $tags = $this->findItems($filter, 'tag'); 3583496cc8aSAndreas Gohr 3593496cc8aSAndreas Gohr $ret = ''; 3603496cc8aSAndreas Gohr 3613496cc8aSAndreas Gohr $ret .= '<div class="plugin_tagging_edit">'; 3623496cc8aSAndreas Gohr $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true); 363f61105deSAdrian Lang 3642ace74f4SAndreas Gohr if ($this->getUser() && $INFO['writable']) { 365f61105deSAdrian Lang $lang['btn_tagging_edit'] = $lang['btn_secedit']; 366e5b42768SSzymon Olewniczak $ret .= '<div id="tagging__edit_buttons_group">'; 3673496cc8aSAndreas Gohr $ret .= html_btn('tagging_edit', $INFO['id'], '', array()); 368dd52fd45SSzymon Olewniczak if (auth_isadmin()) { 369e5b42768SSzymon Olewniczak $ret .= '<label>' . $this->getLang('toggle admin mode') . '<input type="checkbox" id="tagging__edit_toggle_admin" /></label>'; 370dd52fd45SSzymon Olewniczak } 371e5b42768SSzymon Olewniczak $ret .= '</div>'; 3722819ffcdSSzymon Olewniczak $form = new dokuwiki\Form\Form(); 3732819ffcdSSzymon Olewniczak $form->id('tagging__edit'); 3742819ffcdSSzymon Olewniczak $form->setHiddenField('tagging[id]', $INFO['id']); 3752819ffcdSSzymon Olewniczak $form->setHiddenField('call', 'plugin_tagging_save'); 3762819ffcdSSzymon Olewniczak $tags = $this->findItems(array( 3772819ffcdSSzymon Olewniczak 'pid' => $INFO['id'], 378ca455b8eSMichael Große 'tagger' => $this->getUser(), 3792819ffcdSSzymon Olewniczak ), 'tag'); 380*4b1e7a12SAndreas Gohr $form->addTextarea('tagging[tags]')->val(implode(', ', array_keys($tags)))->addClass('edit')->attr('rows', 4); 381cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_save'])->id('tagging__edit_save'); 382cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel'); 3832819ffcdSSzymon Olewniczak $ret .= $form->toHTML(); 384f61105deSAdrian Lang } 3853496cc8aSAndreas Gohr $ret .= '</div>'; 3863496cc8aSAndreas Gohr 3870cfde7e9SMichael Große if ($print) { 3880cfde7e9SMichael Große echo $ret; 3890cfde7e9SMichael Große } 390ca455b8eSMichael Große 3913496cc8aSAndreas Gohr return $ret; 392f61105deSAdrian Lang } 393872edc7cSRené Corinth 3948a1a3846SAndreas Gohr /** 395a99b66c1SSzymon Olewniczak * @param string $namespace empty for entire wiki 396a99b66c1SSzymon Olewniczak * 3978a1a3846SAndreas Gohr * @return array 3988a1a3846SAndreas Gohr */ 399cb469644SSzymon Olewniczak public function getAllTags($namespace = '', $order_by = 'tag', $desc = false) { 4008e9d0162SSzymon Olewniczak $order_fields = array('pid', 'tid', 'orig', 'taggers', 'count'); 401f0084ee1SSzymon Olewniczak if (!in_array($order_by, $order_fields)) { 402f0084ee1SSzymon Olewniczak msg('cannot sort by ' . $order_by . ' field does not exists', -1); 403f0084ee1SSzymon Olewniczak $order_by = 'tag'; 404f0084ee1SSzymon Olewniczak } 405872edc7cSRené Corinth 406872edc7cSRené Corinth $db = $this->getDb(); 407872edc7cSRené Corinth 408f0084ee1SSzymon Olewniczak $query = 'SELECT "pid", 409ca455b8eSMichael Große CLEANTAG("tag") AS "tid", 410f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tag"), \', \') AS "orig", 411f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tagger"), \', \') AS "taggers", 412193a767dSSzymon Olewniczak COUNT(*) AS "count" 41357e45304SSzymon Olewniczak FROM "taggings" 414f0084ee1SSzymon Olewniczak WHERE "pid" LIKE ? 415f0084ee1SSzymon Olewniczak GROUP BY "tid" 416f0084ee1SSzymon Olewniczak ORDER BY ' . $order_by; 417ca455b8eSMichael Große if ($desc) { 418ca455b8eSMichael Große $query .= ' DESC'; 419ca455b8eSMichael Große } 420cb469644SSzymon Olewniczak 421f0084ee1SSzymon Olewniczak $res = $db->query($query, $this->globNamespace($namespace)); 422872edc7cSRené Corinth 4237e05e623SSzymon Olewniczak return $db->res2arr($res); 424872edc7cSRené Corinth } 425872edc7cSRené Corinth 4268a1a3846SAndreas Gohr /** 42772431326SMichael Große * Get all pages with tags and their tags 42872431326SMichael Große * 429790ca788SAndreas Gohr * @return array ['pid' => ['tag1','tag2','tag3']] 43072431326SMichael Große */ 43172431326SMichael Große public function getAllTagsByPage() { 43272431326SMichael Große $query = ' 43372431326SMichael Große SELECT pid, GROUP_CONCAT(tag) AS tags 43472431326SMichael Große FROM taggings 43572431326SMichael Große GROUP BY pid 43672431326SMichael Große '; 43772431326SMichael Große $db = $this->getDb(); 43872431326SMichael Große $res = $db->query($query); 439790ca788SAndreas Gohr return array_map( 440790ca788SAndreas Gohr function ($i) { 441790ca788SAndreas Gohr return explode(',', $i); 442790ca788SAndreas Gohr }, 443790ca788SAndreas Gohr array_column($db->res2arr($res), 'tags', 'pid') 444790ca788SAndreas Gohr ); 44572431326SMichael Große } 44672431326SMichael Große 44772431326SMichael Große /** 4488a1a3846SAndreas Gohr * Renames a tag 4498a1a3846SAndreas Gohr * 4508a1a3846SAndreas Gohr * @param string $formerTagName 4518a1a3846SAndreas Gohr * @param string $newTagName 4528a1a3846SAndreas Gohr */ 453872edc7cSRené Corinth public function renameTag($formerTagName, $newTagName) { 454872edc7cSRené Corinth 455872edc7cSRené Corinth if (empty($formerTagName) || empty($newTagName)) { 4568a1a3846SAndreas Gohr msg($this->getLang("admin enter tag names"), -1); 457ca455b8eSMichael Große 4588a1a3846SAndreas Gohr return; 459872edc7cSRené Corinth } 460872edc7cSRené Corinth 461872edc7cSRené Corinth $db = $this->getDb(); 462872edc7cSRené Corinth 463fb1d0583SAndreas Gohr $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ?', $this->cleanTag($formerTagName)); 464872edc7cSRené Corinth $check = $db->res2arr($res); 465872edc7cSRené Corinth 466872edc7cSRené Corinth if (empty($check)) { 4678a1a3846SAndreas Gohr msg($this->getLang("admin tag does not exists"), -1); 468ca455b8eSMichael Große 4698a1a3846SAndreas Gohr return; 470872edc7cSRené Corinth } 471872edc7cSRené Corinth 472fb1d0583SAndreas Gohr $res = $db->query("UPDATE taggings SET tag = ? WHERE CLEANTAG(tag) = ?", $newTagName, $this->cleanTag($formerTagName)); 473872edc7cSRené Corinth $db->res2arr($res); 474872edc7cSRené Corinth 475fb1d0583SAndreas Gohr msg($this->getLang("admin renamed"), 1); 476ca455b8eSMichael Große 4778a1a3846SAndreas Gohr return; 478872edc7cSRené Corinth } 479872edc7cSRené Corinth 4808f630140SSzymon Olewniczak /** 481dd52fd45SSzymon Olewniczak * Rename or delete a tag for all users 482dd52fd45SSzymon Olewniczak * 483dd52fd45SSzymon Olewniczak * @param string $pid 484dd52fd45SSzymon Olewniczak * @param string $formerTagName 485dd52fd45SSzymon Olewniczak * @param string $newTagName 486dd52fd45SSzymon Olewniczak * 487dd52fd45SSzymon Olewniczak * @return array 488dd52fd45SSzymon Olewniczak */ 489dd52fd45SSzymon Olewniczak public function modifyPageTag($pid, $formerTagName, $newTagName) { 490dd52fd45SSzymon Olewniczak 491dd52fd45SSzymon Olewniczak $db = $this->getDb(); 492dd52fd45SSzymon Olewniczak 493dd52fd45SSzymon Olewniczak $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ? AND pid = ?', $this->cleanTag($formerTagName), $pid); 494dd52fd45SSzymon Olewniczak $check = $db->res2arr($res); 495dd52fd45SSzymon Olewniczak 496dd52fd45SSzymon Olewniczak if (empty($check)) { 497dd52fd45SSzymon Olewniczak return array(true, $this->getLang('admin tag does not exists')); 498dd52fd45SSzymon Olewniczak } 499dd52fd45SSzymon Olewniczak 500dd52fd45SSzymon Olewniczak if (empty($newTagName)) { 501dd52fd45SSzymon Olewniczak $res = $db->query('DELETE FROM taggings WHERE pid = ? AND CLEANTAG(tag) = ?', $pid, $this->cleanTag($formerTagName)); 502dd52fd45SSzymon Olewniczak } else { 503dd52fd45SSzymon Olewniczak $res = $db->query('UPDATE taggings SET tag = ? WHERE pid = ? AND CLEANTAG(tag) = ?', $newTagName, $pid, $this->cleanTag($formerTagName)); 504dd52fd45SSzymon Olewniczak } 505dd52fd45SSzymon Olewniczak $db->res2arr($res); 506dd52fd45SSzymon Olewniczak 507dd52fd45SSzymon Olewniczak return array(false, $this->getLang('admin renamed')); 508dd52fd45SSzymon Olewniczak } 509dd52fd45SSzymon Olewniczak 510dd52fd45SSzymon Olewniczak /** 5118f630140SSzymon Olewniczak * Deletes a tag 5128f630140SSzymon Olewniczak * 5138f630140SSzymon Olewniczak * @param array $tags 51431396860SSzymon Olewniczak * @param string $namespace current namespace context as in getAllTags() 5158f630140SSzymon Olewniczak */ 51631396860SSzymon Olewniczak public function deleteTags($tags, $namespace = '') { 517ca455b8eSMichael Große if (empty($tags)) { 518ca455b8eSMichael Große return; 519ca455b8eSMichael Große } 5208f630140SSzymon Olewniczak 52131396860SSzymon Olewniczak $namespace = cleanId($namespace); 52231396860SSzymon Olewniczak 5231f5991a7SMichael Große $db = $this->getDB(); 5248f630140SSzymon Olewniczak 5251f5991a7SMichael Große $queryBody = 'FROM taggings WHERE pid LIKE ? AND (' . 52631396860SSzymon Olewniczak implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')) . ')'; 52731396860SSzymon Olewniczak $args = array_map(array($this, 'cleanTag'), $tags); 52831396860SSzymon Olewniczak array_unshift($args, $this->globNamespace($namespace)); 5298f630140SSzymon Olewniczak 530ca455b8eSMichael Große 5311f5991a7SMichael Große $affectedPagesQuery= 'SELECT DISTINCT pid ' . $queryBody; 5321f5991a7SMichael Große $resAffectedPages = $db->query($affectedPagesQuery, $args); 5331f5991a7SMichael Große $numAffectedPages = count($resAffectedPages->fetchAll()); 5341f5991a7SMichael Große 5351f5991a7SMichael Große $deleteQuery = 'DELETE ' . $queryBody; 5361f5991a7SMichael Große $db->query($deleteQuery, $args); 5371f5991a7SMichael Große 5381f5991a7SMichael Große msg(sprintf($this->getLang("admin deleted"), count($tags), $numAffectedPages), 1); 5398f630140SSzymon Olewniczak } 540f61105deSAdrian Lang} 541