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 /** 3300b6fad27Ssandos187 * Display a List of Page Links 3310b6fad27Ssandos187 * 3320b6fad27Ssandos187 * @param array $pids list of pids => count 3330b6fad27Ssandos187 * @return string 3340b6fad27Ssandos187 */ 3350b6fad27Ssandos187 public function html_page_list($pids) { 3360b6fad27Ssandos187 $ret = '<div class="search_quickresult">'; 3370b6fad27Ssandos187 $ret .= '<ul class="search_quickhits">'; 3380b6fad27Ssandos187 3390b6fad27Ssandos187 if (count($pids) === 0) { 3400b6fad27Ssandos187 // Produce valid XHTML (ul needs a child) 3410b6fad27Ssandos187 $ret .= '<li><div class="li">' . $this->lang['js']['nopages'] . '</div></li>'; 3420b6fad27Ssandos187 } else { 343*bdf1ecf0SAnna Dabrowska foreach (array_keys($pids) as $val) { 3440b6fad27Ssandos187 $ret .= '<li><div class="li">'; 3450b6fad27Ssandos187 $ret .= html_wikilink($val); 3460b6fad27Ssandos187 $ret .= '</div></li>'; 3470b6fad27Ssandos187 } 3480b6fad27Ssandos187 } 3490b6fad27Ssandos187 3500b6fad27Ssandos187 $ret .= '</ul>'; 3510b6fad27Ssandos187 $ret .= '</div>'; 3520b6fad27Ssandos187 $ret .= '<div class="clearer"></div>'; 3530b6fad27Ssandos187 3540b6fad27Ssandos187 return $ret; 3550b6fad27Ssandos187 } 3560b6fad27Ssandos187 3570b6fad27Ssandos187 /** 3585540f91dSAndreas Gohr * Get the link to a search for the given tag 3595540f91dSAndreas Gohr * 3605540f91dSAndreas Gohr * @param string $tag search for this tag 3615540f91dSAndreas Gohr * @param string $ns limit search to this namespace 3620cfde7e9SMichael Große * 3635540f91dSAndreas Gohr * @return string 3645540f91dSAndreas Gohr */ 3655540f91dSAndreas Gohr protected function linkToSearch($tag, $ns = '') { 3665540f91dSAndreas Gohr return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>'; 367f61105deSAdrian Lang } 368f61105deSAdrian Lang 369fb1d0583SAndreas Gohr /** 370fb1d0583SAndreas Gohr * Display the Tags for the current page and prepare the tag editing form 3713496cc8aSAndreas Gohr * 3723496cc8aSAndreas Gohr * @param bool $print Should the HTML be printed or returned? 3730cfde7e9SMichael Große * 3743496cc8aSAndreas Gohr * @return string 375fb1d0583SAndreas Gohr */ 3763496cc8aSAndreas Gohr public function tpl_tags($print = true) { 377f61105deSAdrian Lang global $INFO; 378f61105deSAdrian Lang global $lang; 3793bf0e2f1SMichael Große 3803bf0e2f1SMichael Große $filter = array('pid' => $INFO['id']); 3813bf0e2f1SMichael Große if ($this->getConf('singleusermode')) { 3823bf0e2f1SMichael Große $filter['tagger'] = 'auto'; 3833bf0e2f1SMichael Große } 3843bf0e2f1SMichael Große 3853bf0e2f1SMichael Große $tags = $this->findItems($filter, 'tag'); 3863496cc8aSAndreas Gohr 3873496cc8aSAndreas Gohr $ret = ''; 3883496cc8aSAndreas Gohr 3893496cc8aSAndreas Gohr $ret .= '<div class="plugin_tagging_edit">'; 3903496cc8aSAndreas Gohr $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true); 391f61105deSAdrian Lang 3922ace74f4SAndreas Gohr if ($this->getUser() && $INFO['writable']) { 393f61105deSAdrian Lang $lang['btn_tagging_edit'] = $lang['btn_secedit']; 394e5b42768SSzymon Olewniczak $ret .= '<div id="tagging__edit_buttons_group">'; 3953496cc8aSAndreas Gohr $ret .= html_btn('tagging_edit', $INFO['id'], '', array()); 396dd52fd45SSzymon Olewniczak if (auth_isadmin()) { 397e5b42768SSzymon Olewniczak $ret .= '<label>' . $this->getLang('toggle admin mode') . '<input type="checkbox" id="tagging__edit_toggle_admin" /></label>'; 398dd52fd45SSzymon Olewniczak } 399e5b42768SSzymon Olewniczak $ret .= '</div>'; 4002819ffcdSSzymon Olewniczak $form = new dokuwiki\Form\Form(); 4012819ffcdSSzymon Olewniczak $form->id('tagging__edit'); 4022819ffcdSSzymon Olewniczak $form->setHiddenField('tagging[id]', $INFO['id']); 4032819ffcdSSzymon Olewniczak $form->setHiddenField('call', 'plugin_tagging_save'); 4042819ffcdSSzymon Olewniczak $tags = $this->findItems(array( 4052819ffcdSSzymon Olewniczak 'pid' => $INFO['id'], 406ca455b8eSMichael Große 'tagger' => $this->getUser(), 4072819ffcdSSzymon Olewniczak ), 'tag'); 4084b1e7a12SAndreas Gohr $form->addTextarea('tagging[tags]')->val(implode(', ', array_keys($tags)))->addClass('edit')->attr('rows', 4); 409cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_save'])->id('tagging__edit_save'); 410cf52ec2dSSzymon Olewniczak $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel'); 4112819ffcdSSzymon Olewniczak $ret .= $form->toHTML(); 412f61105deSAdrian Lang } 4133496cc8aSAndreas Gohr $ret .= '</div>'; 4143496cc8aSAndreas Gohr 4150cfde7e9SMichael Große if ($print) { 4160cfde7e9SMichael Große echo $ret; 4170cfde7e9SMichael Große } 418ca455b8eSMichael Große 4193496cc8aSAndreas Gohr return $ret; 420f61105deSAdrian Lang } 421872edc7cSRené Corinth 4228a1a3846SAndreas Gohr /** 423a99b66c1SSzymon Olewniczak * @param string $namespace empty for entire wiki 424a99b66c1SSzymon Olewniczak * 4258a1a3846SAndreas Gohr * @return array 4268a1a3846SAndreas Gohr */ 427cb469644SSzymon Olewniczak public function getAllTags($namespace = '', $order_by = 'tag', $desc = false) { 4288e9d0162SSzymon Olewniczak $order_fields = array('pid', 'tid', 'orig', 'taggers', 'count'); 429f0084ee1SSzymon Olewniczak if (!in_array($order_by, $order_fields)) { 430f0084ee1SSzymon Olewniczak msg('cannot sort by ' . $order_by . ' field does not exists', -1); 431f0084ee1SSzymon Olewniczak $order_by = 'tag'; 432f0084ee1SSzymon Olewniczak } 433872edc7cSRené Corinth 434872edc7cSRené Corinth $db = $this->getDb(); 435872edc7cSRené Corinth 436f0084ee1SSzymon Olewniczak $query = 'SELECT "pid", 437ca455b8eSMichael Große CLEANTAG("tag") AS "tid", 438f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tag"), \', \') AS "orig", 439f0084ee1SSzymon Olewniczak GROUP_SORT(GROUP_CONCAT("tagger"), \', \') AS "taggers", 440193a767dSSzymon Olewniczak COUNT(*) AS "count" 44157e45304SSzymon Olewniczak FROM "taggings" 442f0084ee1SSzymon Olewniczak WHERE "pid" LIKE ? 443f0084ee1SSzymon Olewniczak GROUP BY "tid" 444f0084ee1SSzymon Olewniczak ORDER BY ' . $order_by; 445ca455b8eSMichael Große if ($desc) { 446ca455b8eSMichael Große $query .= ' DESC'; 447ca455b8eSMichael Große } 448cb469644SSzymon Olewniczak 449f0084ee1SSzymon Olewniczak $res = $db->query($query, $this->globNamespace($namespace)); 450872edc7cSRené Corinth 4517e05e623SSzymon Olewniczak return $db->res2arr($res); 452872edc7cSRené Corinth } 453872edc7cSRené Corinth 4548a1a3846SAndreas Gohr /** 45572431326SMichael Große * Get all pages with tags and their tags 45672431326SMichael Große * 457790ca788SAndreas Gohr * @return array ['pid' => ['tag1','tag2','tag3']] 45872431326SMichael Große */ 45972431326SMichael Große public function getAllTagsByPage() { 46072431326SMichael Große $query = ' 46172431326SMichael Große SELECT pid, GROUP_CONCAT(tag) AS tags 46272431326SMichael Große FROM taggings 46372431326SMichael Große GROUP BY pid 46472431326SMichael Große '; 46572431326SMichael Große $db = $this->getDb(); 46672431326SMichael Große $res = $db->query($query); 467790ca788SAndreas Gohr return array_map( 468790ca788SAndreas Gohr function ($i) { 469790ca788SAndreas Gohr return explode(',', $i); 470790ca788SAndreas Gohr }, 471790ca788SAndreas Gohr array_column($db->res2arr($res), 'tags', 'pid') 472790ca788SAndreas Gohr ); 47372431326SMichael Große } 47472431326SMichael Große 47572431326SMichael Große /** 4768a1a3846SAndreas Gohr * Renames a tag 4778a1a3846SAndreas Gohr * 4788a1a3846SAndreas Gohr * @param string $formerTagName 4798a1a3846SAndreas Gohr * @param string $newTagName 4808a1a3846SAndreas Gohr */ 481872edc7cSRené Corinth public function renameTag($formerTagName, $newTagName) { 482872edc7cSRené Corinth 483872edc7cSRené Corinth if (empty($formerTagName) || empty($newTagName)) { 4848a1a3846SAndreas Gohr msg($this->getLang("admin enter tag names"), -1); 485ca455b8eSMichael Große 4868a1a3846SAndreas Gohr return; 487872edc7cSRené Corinth } 488872edc7cSRené Corinth 489872edc7cSRené Corinth $db = $this->getDb(); 490872edc7cSRené Corinth 491fb1d0583SAndreas Gohr $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ?', $this->cleanTag($formerTagName)); 492872edc7cSRené Corinth $check = $db->res2arr($res); 493872edc7cSRené Corinth 494872edc7cSRené Corinth if (empty($check)) { 4958a1a3846SAndreas Gohr msg($this->getLang("admin tag does not exists"), -1); 496ca455b8eSMichael Große 4978a1a3846SAndreas Gohr return; 498872edc7cSRené Corinth } 499872edc7cSRené Corinth 500fb1d0583SAndreas Gohr $res = $db->query("UPDATE taggings SET tag = ? WHERE CLEANTAG(tag) = ?", $newTagName, $this->cleanTag($formerTagName)); 501872edc7cSRené Corinth $db->res2arr($res); 502872edc7cSRené Corinth 503fb1d0583SAndreas Gohr msg($this->getLang("admin renamed"), 1); 504ca455b8eSMichael Große 5058a1a3846SAndreas Gohr return; 506872edc7cSRené Corinth } 507872edc7cSRené Corinth 5088f630140SSzymon Olewniczak /** 509dd52fd45SSzymon Olewniczak * Rename or delete a tag for all users 510dd52fd45SSzymon Olewniczak * 511dd52fd45SSzymon Olewniczak * @param string $pid 512dd52fd45SSzymon Olewniczak * @param string $formerTagName 513dd52fd45SSzymon Olewniczak * @param string $newTagName 514dd52fd45SSzymon Olewniczak * 515dd52fd45SSzymon Olewniczak * @return array 516dd52fd45SSzymon Olewniczak */ 517dd52fd45SSzymon Olewniczak public function modifyPageTag($pid, $formerTagName, $newTagName) { 518dd52fd45SSzymon Olewniczak 519dd52fd45SSzymon Olewniczak $db = $this->getDb(); 520dd52fd45SSzymon Olewniczak 521dd52fd45SSzymon Olewniczak $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ? AND pid = ?', $this->cleanTag($formerTagName), $pid); 522dd52fd45SSzymon Olewniczak $check = $db->res2arr($res); 523dd52fd45SSzymon Olewniczak 524dd52fd45SSzymon Olewniczak if (empty($check)) { 525dd52fd45SSzymon Olewniczak return array(true, $this->getLang('admin tag does not exists')); 526dd52fd45SSzymon Olewniczak } 527dd52fd45SSzymon Olewniczak 528dd52fd45SSzymon Olewniczak if (empty($newTagName)) { 529dd52fd45SSzymon Olewniczak $res = $db->query('DELETE FROM taggings WHERE pid = ? AND CLEANTAG(tag) = ?', $pid, $this->cleanTag($formerTagName)); 530dd52fd45SSzymon Olewniczak } else { 531dd52fd45SSzymon Olewniczak $res = $db->query('UPDATE taggings SET tag = ? WHERE pid = ? AND CLEANTAG(tag) = ?', $newTagName, $pid, $this->cleanTag($formerTagName)); 532dd52fd45SSzymon Olewniczak } 533dd52fd45SSzymon Olewniczak $db->res2arr($res); 534dd52fd45SSzymon Olewniczak 535dd52fd45SSzymon Olewniczak return array(false, $this->getLang('admin renamed')); 536dd52fd45SSzymon Olewniczak } 537dd52fd45SSzymon Olewniczak 538dd52fd45SSzymon Olewniczak /** 5398f630140SSzymon Olewniczak * Deletes a tag 5408f630140SSzymon Olewniczak * 5418f630140SSzymon Olewniczak * @param array $tags 54231396860SSzymon Olewniczak * @param string $namespace current namespace context as in getAllTags() 5438f630140SSzymon Olewniczak */ 54431396860SSzymon Olewniczak public function deleteTags($tags, $namespace = '') { 545ca455b8eSMichael Große if (empty($tags)) { 546ca455b8eSMichael Große return; 547ca455b8eSMichael Große } 5488f630140SSzymon Olewniczak 54931396860SSzymon Olewniczak $namespace = cleanId($namespace); 55031396860SSzymon Olewniczak 5511f5991a7SMichael Große $db = $this->getDB(); 5528f630140SSzymon Olewniczak 5531f5991a7SMichael Große $queryBody = 'FROM taggings WHERE pid LIKE ? AND (' . 55431396860SSzymon Olewniczak implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')) . ')'; 55531396860SSzymon Olewniczak $args = array_map(array($this, 'cleanTag'), $tags); 55631396860SSzymon Olewniczak array_unshift($args, $this->globNamespace($namespace)); 5578f630140SSzymon Olewniczak 558ca455b8eSMichael Große 5591f5991a7SMichael Große $affectedPagesQuery= 'SELECT DISTINCT pid ' . $queryBody; 5601f5991a7SMichael Große $resAffectedPages = $db->query($affectedPagesQuery, $args); 5611f5991a7SMichael Große $numAffectedPages = count($resAffectedPages->fetchAll()); 5621f5991a7SMichael Große 5631f5991a7SMichael Große $deleteQuery = 'DELETE ' . $queryBody; 5641f5991a7SMichael Große $db->query($deleteQuery, $args); 5651f5991a7SMichael Große 5661f5991a7SMichael Große msg(sprintf($this->getLang("admin deleted"), count($tags), $numAffectedPages), 1); 5678f630140SSzymon Olewniczak } 568f61105deSAdrian Lang} 569