xref: /plugin/tagging/helper.php (revision 57e4530423023c2291dfc4b90cd4c34dbd0f5a35)
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);
30f61105deSAdrian Lang        return $db;
31f61105deSAdrian Lang    }
32f61105deSAdrian Lang
33302a38efSAndreas Gohr    /**
342ace74f4SAndreas Gohr     * Return the user to use for accessing tags
352ace74f4SAndreas Gohr     *
362ace74f4SAndreas Gohr     * Handles the singleuser mode by returning 'auto' as user. Returnes false when no user is logged in.
372ace74f4SAndreas Gohr     *
382ace74f4SAndreas Gohr     * @return bool|string
392ace74f4SAndreas Gohr     */
402ace74f4SAndreas Gohr    public function getUser() {
410cfde7e9SMichael Große        if (!isset($_SERVER['REMOTE_USER'])) {
420cfde7e9SMichael Große            return false;
430cfde7e9SMichael Große        }
440cfde7e9SMichael Große        if ($this->getConf('singleusermode')) {
450cfde7e9SMichael Große            return 'auto';
460cfde7e9SMichael Große        }
472ace74f4SAndreas Gohr        return $_SERVER['REMOTE_USER'];
482ace74f4SAndreas Gohr    }
492ace74f4SAndreas Gohr
502ace74f4SAndreas Gohr    /**
51302a38efSAndreas Gohr     * Canonicalizes the tag to its lower case nospace form
52302a38efSAndreas Gohr     *
53302a38efSAndreas Gohr     * @param $tag
540cfde7e9SMichael Große     *
55302a38efSAndreas Gohr     * @return string
56302a38efSAndreas Gohr     */
57302a38efSAndreas Gohr    public function cleanTag($tag) {
58302a38efSAndreas Gohr        $tag = str_replace(' ', '', $tag);
5950547994SMichael Große        $tag = str_replace('-', '', $tag);
6050547994SMichael Große        $tag = str_replace('_', '', $tag);
61302a38efSAndreas Gohr        $tag = utf8_strtolower($tag);
62302a38efSAndreas Gohr        return $tag;
63302a38efSAndreas Gohr    }
64302a38efSAndreas Gohr
6556d82720SAndreas Gohr    /**
6631396860SSzymon Olewniczak     * Canonicalizes the namespace, remove the first colon and add glob
6731396860SSzymon Olewniczak     *
6831396860SSzymon Olewniczak     * @param $namespace
6931396860SSzymon Olewniczak     *
7031396860SSzymon Olewniczak     * @return string
7131396860SSzymon Olewniczak     */
7231396860SSzymon Olewniczak    public function globNamespace($namespace) {
7331396860SSzymon Olewniczak        return cleanId($namespace) . '%';
7431396860SSzymon Olewniczak    }
7531396860SSzymon Olewniczak
7631396860SSzymon Olewniczak    /**
7756d82720SAndreas Gohr     * Create or Update tags of a page
7856d82720SAndreas Gohr     *
7956d82720SAndreas Gohr     * Uses the translation plugin to store the language of a page (if available)
8056d82720SAndreas Gohr     *
8156d82720SAndreas Gohr     * @param string $id The page ID
8256d82720SAndreas Gohr     * @param string $user
8356d82720SAndreas Gohr     * @param array  $tags
840cfde7e9SMichael Große     *
8556d82720SAndreas Gohr     * @return bool|SQLiteResult
8656d82720SAndreas Gohr     */
87f61105deSAdrian Lang    public function replaceTags($id, $user, $tags) {
8856d82720SAndreas Gohr        global $conf;
8956d82720SAndreas Gohr        /** @var helper_plugin_translation $trans */
9056d82720SAndreas Gohr        $trans = plugin_load('helper', 'translation');
9156d82720SAndreas Gohr        if ($trans) {
9256d82720SAndreas Gohr            $lang = $trans->realLC($trans->getLangPart($id));
9356d82720SAndreas Gohr        } else {
9456d82720SAndreas Gohr            $lang = $conf['lang'];
9556d82720SAndreas Gohr        }
9656d82720SAndreas Gohr
97f61105deSAdrian Lang        $db = $this->getDB();
98f61105deSAdrian Lang        $db->query('BEGIN TRANSACTION');
99f61105deSAdrian Lang        $queries = array(array('DELETE FROM taggings WHERE pid = ? AND tagger = ?', $id, $user));
100f61105deSAdrian Lang        foreach ($tags as $tag) {
10156d82720SAndreas Gohr            $queries[] = array('INSERT INTO taggings (pid, tagger, tag, lang) VALUES(?, ?, ?, ?)', $id, $user, $tag, $lang);
102f61105deSAdrian Lang        }
103f61105deSAdrian Lang
104f61105deSAdrian Lang        foreach ($queries as $query) {
105f61105deSAdrian Lang            if (!call_user_func_array(array($db, 'query'), $query)) {
106f61105deSAdrian Lang                $db->query('ROLLBACK TRANSACTION');
107f61105deSAdrian Lang                return false;
108f61105deSAdrian Lang            }
109f61105deSAdrian Lang        }
110f61105deSAdrian Lang        return $db->query('COMMIT TRANSACTION');
111f61105deSAdrian Lang    }
112f61105deSAdrian Lang
1130a518a11SAndreas Gohr    /**
114b12334e1SAndreas Gohr     * Get a list of Tags or Pages matching search criteria
1150a518a11SAndreas Gohr     *
116b12334e1SAndreas Gohr     * @param array  $filter What to search for array('field' => 'searchterm')
117b12334e1SAndreas Gohr     * @param string $type   What field to return 'tag'|'pid'
118077ff864SAndreas Gohr     * @param int    $limit  Limit to this many results, 0 for all
1190cfde7e9SMichael Große     *
1200a518a11SAndreas Gohr     * @return array associative array in form of value => count
1210a518a11SAndreas Gohr     */
122077ff864SAndreas Gohr    public function findItems($filter, $type, $limit = 0) {
123f61105deSAdrian Lang        $db = $this->getDB();
1240cfde7e9SMichael Große        if (!$db) {
1250cfde7e9SMichael Große            return array();
1260cfde7e9SMichael Große        }
127f61105deSAdrian Lang
128b12334e1SAndreas Gohr        // create WHERE clause
129b12334e1SAndreas Gohr        $where = '1=1';
130b12334e1SAndreas Gohr        foreach ($filter as $field => $value) {
131b12334e1SAndreas Gohr            // compare clean tags only
132b12334e1SAndreas Gohr            if ($field === 'tag') {
133b12334e1SAndreas Gohr                $field = 'CLEANTAG(tag)';
134b12334e1SAndreas Gohr                $q = 'CLEANTAG(?)';
135b12334e1SAndreas Gohr            } else {
136b12334e1SAndreas Gohr                $q = '?';
137b12334e1SAndreas Gohr            }
138204c069bSMichael Große
139204c069bSMichael Große            if (substr($field, 0, 6) === 'notpid') {
140204c069bSMichael Große                $field = 'pid';
141204c069bSMichael Große
142204c069bSMichael Große                // detect LIKE filters
143204c069bSMichael Große                if ($this->useLike($value)) {
144204c069bSMichael Große                    $where .= " AND $field NOT LIKE $q";
145204c069bSMichael Große                } else {
146204c069bSMichael Große                    $where .= " AND $field != $q";
147204c069bSMichael Große                }
148204c069bSMichael Große            } else {
149b12334e1SAndreas Gohr                // detect LIKE filters
150b12334e1SAndreas Gohr                if ($this->useLike($value)) {
151b12334e1SAndreas Gohr                    $where .= " AND $field LIKE $q";
152b12334e1SAndreas Gohr                } else {
153b12334e1SAndreas Gohr                    $where .= " AND $field = $q";
154b12334e1SAndreas Gohr                }
155b12334e1SAndreas Gohr            }
156204c069bSMichael Große        }
1576c37861bSJulian Einwag        $where .= ' AND GETACCESSLEVEL(pid) >= ' . AUTH_READ;
15859a953ffSMichael Große
159b12334e1SAndreas Gohr        // group and order
160b12334e1SAndreas Gohr        if ($type == 'tag') {
161b12334e1SAndreas Gohr            $groupby = 'CLEANTAG(tag)';
162b12334e1SAndreas Gohr            $orderby = 'CLEANTAG(tag)';
163b12334e1SAndreas Gohr        } else {
164b12334e1SAndreas Gohr            $groupby = $type;
165466524f5SAndreas Gohr            $orderby = "cnt DESC, $type";
166b12334e1SAndreas Gohr        }
167b12334e1SAndreas Gohr
168077ff864SAndreas Gohr        // limit results
169077ff864SAndreas Gohr        if ($limit) {
170077ff864SAndreas Gohr            $limit = " LIMIT $limit";
171077ff864SAndreas Gohr        } else {
172077ff864SAndreas Gohr            $limit = '';
173077ff864SAndreas Gohr        }
174077ff864SAndreas Gohr
175b12334e1SAndreas Gohr        // create SQL
176b12334e1SAndreas Gohr        $sql = "SELECT $type AS item, COUNT(*) AS cnt
177b12334e1SAndreas Gohr                  FROM taggings
178b12334e1SAndreas Gohr                 WHERE $where
179b12334e1SAndreas Gohr              GROUP BY $groupby
180077ff864SAndreas Gohr              ORDER BY $orderby
181077ff864SAndreas Gohr                $limit
182077ff864SAndreas Gohr              ";
183b12334e1SAndreas Gohr
184b12334e1SAndreas Gohr        // run query and turn into associative array
185b12334e1SAndreas Gohr        $res = $db->query($sql, array_values($filter));
186f61105deSAdrian Lang        $res = $db->res2arr($res);
187b12334e1SAndreas Gohr
188f61105deSAdrian Lang        $ret = array();
189b12334e1SAndreas Gohr        foreach ($res as $row) {
190b12334e1SAndreas Gohr            $ret[$row['item']] = $row['cnt'];
191f61105deSAdrian Lang        }
192f61105deSAdrian Lang        return $ret;
193f61105deSAdrian Lang    }
194f61105deSAdrian Lang
195b12334e1SAndreas Gohr    /**
196b12334e1SAndreas Gohr     * Check if the given string is a LIKE statement
197b12334e1SAndreas Gohr     *
198b12334e1SAndreas Gohr     * @param string $value
1990cfde7e9SMichael Große     *
200b12334e1SAndreas Gohr     * @return bool
201b12334e1SAndreas Gohr     */
202b12334e1SAndreas Gohr    private function useLike($value) {
203b12334e1SAndreas Gohr        return strpos($value, '%') === 0 || strrpos($value, '%') === strlen($value) - 1;
204b2073787SDominik Eckelmann    }
205b2073787SDominik Eckelmann
206302a38efSAndreas Gohr    /**
207302a38efSAndreas Gohr     * Constructs the URL to search for a tag
208302a38efSAndreas Gohr     *
2095540f91dSAndreas Gohr     * @param string $tag
2105540f91dSAndreas Gohr     * @param string $ns
2110cfde7e9SMichael Große     *
212302a38efSAndreas Gohr     * @return string
213302a38efSAndreas Gohr     */
2145540f91dSAndreas Gohr    public function getTagSearchURL($tag, $ns = '') {
215bed9f360SAndreas Gohr        // wrap tag in quotes if non clean
2162cb25ec1SAndreas Gohr        $ctag = utf8_stripspecials($this->cleanTag($tag));
2170cfde7e9SMichael Große        if ($ctag != utf8_strtolower($tag)) {
2180cfde7e9SMichael Große            $tag = '"' . $tag . '"';
2190cfde7e9SMichael Große        }
220bed9f360SAndreas Gohr
221bed9f360SAndreas Gohr        $ret = '?do=search&id=' . rawurlencode($tag);
2220cfde7e9SMichael Große        if ($ns) {
2230cfde7e9SMichael Große            $ret .= rawurlencode(' @' . $ns);
2240cfde7e9SMichael Große        }
2255540f91dSAndreas Gohr
2265540f91dSAndreas Gohr        return $ret;
227f61105deSAdrian Lang    }
228f61105deSAdrian Lang
2295540f91dSAndreas Gohr    /**
2305540f91dSAndreas Gohr     * Calculates the size levels for the given list of clouds
2315540f91dSAndreas Gohr     *
2325540f91dSAndreas Gohr     * Automatically determines sensible tresholds
2335540f91dSAndreas Gohr     *
2345540f91dSAndreas Gohr     * @param array $tags list of tags => count
2355540f91dSAndreas Gohr     * @param int   $levels
2360cfde7e9SMichael Große     *
2375540f91dSAndreas Gohr     * @return mixed
2385540f91dSAndreas Gohr     */
239f61105deSAdrian Lang    public function cloudData($tags, $levels = 10) {
240f61105deSAdrian Lang        $min = min($tags);
241f61105deSAdrian Lang        $max = max($tags);
242f61105deSAdrian Lang
243f61105deSAdrian Lang        // calculate tresholds
244f61105deSAdrian Lang        $tresholds = array();
245f61105deSAdrian Lang        for ($i = 0; $i <= $levels; $i++) {
246f61105deSAdrian Lang            $tresholds[$i] = pow($max - $min + 1, $i / $levels) + $min - 1;
247f61105deSAdrian Lang        }
248f61105deSAdrian Lang
249f61105deSAdrian Lang        // assign weights
250f61105deSAdrian Lang        foreach ($tags as $tag => $cnt) {
251f61105deSAdrian Lang            foreach ($tresholds as $tresh => $val) {
252f61105deSAdrian Lang                if ($cnt <= $val) {
253f61105deSAdrian Lang                    $tags[$tag] = $tresh;
254f61105deSAdrian Lang                    break;
255f61105deSAdrian Lang                }
256f61105deSAdrian Lang                $tags[$tag] = $levels;
257f61105deSAdrian Lang            }
258f61105deSAdrian Lang        }
259f61105deSAdrian Lang        return $tags;
260f61105deSAdrian Lang    }
261f61105deSAdrian Lang
2625540f91dSAndreas Gohr    /**
2635540f91dSAndreas Gohr     * Display a tag cloud
2645540f91dSAndreas Gohr     *
2655540f91dSAndreas Gohr     * @param array    $tags   list of tags => count
2665540f91dSAndreas Gohr     * @param string   $type   'tag'
2675540f91dSAndreas Gohr     * @param Callable $func   The function to print the link (gets tag and ns)
2685540f91dSAndreas Gohr     * @param bool     $wrap   wrap cloud in UL tags?
2695540f91dSAndreas Gohr     * @param bool     $return returnn HTML instead of printing?
2705540f91dSAndreas Gohr     * @param string   $ns     Add this namespace to search links
2710cfde7e9SMichael Große     *
2725540f91dSAndreas Gohr     * @return string
2735540f91dSAndreas Gohr     */
2745540f91dSAndreas Gohr    public function html_cloud($tags, $type, $func, $wrap = true, $return = false, $ns = '') {
275a66f6715SAndreas Gohr        global $INFO;
276a66f6715SAndreas Gohr
277a66f6715SAndreas Gohr        $hidden_str = $this->getConf('hiddenprefix');
278a66f6715SAndreas Gohr        $hidden_len = strlen($hidden_str);
279a66f6715SAndreas Gohr
280f61105deSAdrian Lang        $ret = '';
2810cfde7e9SMichael Große        if ($wrap) {
2820cfde7e9SMichael Große            $ret .= '<ul class="tagging_cloud clearfix">';
2830cfde7e9SMichael Große        }
284f61105deSAdrian Lang        if (count($tags) === 0) {
285f61105deSAdrian Lang            // Produce valid XHTML (ul needs a child)
286f61105deSAdrian Lang            $this->setupLocale();
287f61105deSAdrian Lang            $ret .= '<li><div class="li">' . $this->lang['js']['no' . $type . 's'] . '</div></li>';
288f61105deSAdrian Lang        } else {
289f61105deSAdrian Lang            $tags = $this->cloudData($tags);
290f61105deSAdrian Lang            foreach ($tags as $val => $size) {
291a66f6715SAndreas Gohr                // skip hidden tags for users that can't edit
292a66f6715SAndreas Gohr                if ($type == 'tag' and
293a66f6715SAndreas Gohr                    $hidden_len and
294a66f6715SAndreas Gohr                    substr($val, 0, $hidden_len) == $hidden_str and
295a66f6715SAndreas Gohr                    !($this->getUser() && $INFO['writable'])
296a66f6715SAndreas Gohr                ) {
297a66f6715SAndreas Gohr                    continue;
298a66f6715SAndreas Gohr                }
299a66f6715SAndreas Gohr
300f61105deSAdrian Lang                $ret .= '<li class="t' . $size . '"><div class="li">';
3015540f91dSAndreas Gohr                $ret .= call_user_func($func, $val, $ns);
302f61105deSAdrian Lang                $ret .= '</div></li>';
303f61105deSAdrian Lang            }
304f61105deSAdrian Lang        }
3050cfde7e9SMichael Große        if ($wrap) {
3060cfde7e9SMichael Große            $ret .= '</ul>';
3070cfde7e9SMichael Große        }
3080cfde7e9SMichael Große        if ($return) {
3090cfde7e9SMichael Große            return $ret;
3100cfde7e9SMichael Große        }
311f61105deSAdrian Lang        echo $ret;
3125540f91dSAndreas Gohr        return '';
313f61105deSAdrian Lang    }
314f61105deSAdrian Lang
3155540f91dSAndreas Gohr    /**
3165540f91dSAndreas Gohr     * Get the link to a search for the given tag
3175540f91dSAndreas Gohr     *
3185540f91dSAndreas Gohr     * @param string $tag search for this tag
3195540f91dSAndreas Gohr     * @param string $ns  limit search to this namespace
3200cfde7e9SMichael Große     *
3215540f91dSAndreas Gohr     * @return string
3225540f91dSAndreas Gohr     */
3235540f91dSAndreas Gohr    protected function linkToSearch($tag, $ns = '') {
3245540f91dSAndreas Gohr        return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>';
325f61105deSAdrian Lang    }
326f61105deSAdrian Lang
327fb1d0583SAndreas Gohr    /**
328fb1d0583SAndreas Gohr     * Display the Tags for the current page and prepare the tag editing form
3293496cc8aSAndreas Gohr     *
3303496cc8aSAndreas Gohr     * @param bool $print Should the HTML be printed or returned?
3310cfde7e9SMichael Große     *
3323496cc8aSAndreas Gohr     * @return string
333fb1d0583SAndreas Gohr     */
3343496cc8aSAndreas Gohr    public function tpl_tags($print = true) {
335f61105deSAdrian Lang        global $INFO;
336f61105deSAdrian Lang        global $lang;
3373bf0e2f1SMichael Große
3383bf0e2f1SMichael Große        $filter = array('pid' => $INFO['id']);
3393bf0e2f1SMichael Große        if ($this->getConf('singleusermode')) {
3403bf0e2f1SMichael Große            $filter['tagger'] = 'auto';
3413bf0e2f1SMichael Große        }
3423bf0e2f1SMichael Große
3433bf0e2f1SMichael Große        $tags = $this->findItems($filter, 'tag');
3443496cc8aSAndreas Gohr
3453496cc8aSAndreas Gohr        $ret = '';
3463496cc8aSAndreas Gohr
3473496cc8aSAndreas Gohr        $ret .= '<div class="plugin_tagging_edit">';
3483496cc8aSAndreas Gohr        $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true);
349f61105deSAdrian Lang
3502ace74f4SAndreas Gohr        if ($this->getUser() && $INFO['writable']) {
351f61105deSAdrian Lang            $lang['btn_tagging_edit'] = $lang['btn_secedit'];
3523496cc8aSAndreas Gohr            $ret .= html_btn('tagging_edit', $INFO['id'], '', array());
3532819ffcdSSzymon Olewniczak
3542819ffcdSSzymon Olewniczak            $form = new dokuwiki\Form\Form();
3552819ffcdSSzymon Olewniczak            $form->id('tagging__edit');
3562819ffcdSSzymon Olewniczak            $form->setHiddenField('tagging[id]', $INFO['id']);
3572819ffcdSSzymon Olewniczak            $form->setHiddenField('call', 'plugin_tagging_save');
3582819ffcdSSzymon Olewniczak            $tags = $this->findItems(array(
3592819ffcdSSzymon Olewniczak                                        'pid' => $INFO['id'],
3602819ffcdSSzymon Olewniczak                                        'tagger' => $this->getUser()
3612819ffcdSSzymon Olewniczak                                    ), 'tag');
3622819ffcdSSzymon Olewniczak            $form->addTextInput('tagging[tags]')->val(implode(', ', array_keys($tags)))->addClass('edit');
363cf52ec2dSSzymon Olewniczak            $form->addButton('', $lang['btn_save'])->id('tagging__edit_save');
364cf52ec2dSSzymon Olewniczak            $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel');
3652819ffcdSSzymon Olewniczak            $ret .= $form->toHTML();
366f61105deSAdrian Lang        }
3673496cc8aSAndreas Gohr        $ret .= '</div>';
3683496cc8aSAndreas Gohr
3690cfde7e9SMichael Große        if ($print) {
3700cfde7e9SMichael Große            echo $ret;
3710cfde7e9SMichael Große        }
3723496cc8aSAndreas Gohr        return $ret;
373f61105deSAdrian Lang    }
374872edc7cSRené Corinth
3758a1a3846SAndreas Gohr    /**
376a99b66c1SSzymon Olewniczak     * @param string $namespace empty for entire wiki
377a99b66c1SSzymon Olewniczak     *
3788a1a3846SAndreas Gohr     * @return array
3798a1a3846SAndreas Gohr     */
380cb469644SSzymon Olewniczak    public function getAllTags($namespace='', $order_by='tag', $desc=false) {
3818e9d0162SSzymon Olewniczak        $order_fields = array('pid', 'tid', 'orig', 'taggers', 'count');
382cb469644SSzymon Olewniczak        if (!in_array($order_by, $order_fields))
383cb469644SSzymon Olewniczak            throw new Exception('cannot sort by '.$order_by. ' field does not exists');
384872edc7cSRené Corinth
385872edc7cSRené Corinth        $db = $this->getDb();
386872edc7cSRené Corinth
387193a767dSSzymon Olewniczak        $query = 'SELECT    pid,
388193a767dSSzymon Olewniczak                            CLEANTAG(tag) as tid,
389*57e45304SSzymon Olewniczak                            GROUP_CONCAT(tag) AS orig,
390*57e45304SSzymon Olewniczak                            GROUP_CONCAT(tagger) AS taggers,
391193a767dSSzymon Olewniczak                            COUNT(*) AS "count"
392*57e45304SSzymon Olewniczak                        FROM "taggings"
393193a767dSSzymon Olewniczak                        WHERE pid LIKE ?
394193a767dSSzymon Olewniczak                        GROUP BY tid
395cb469644SSzymon Olewniczak                        ORDER BY '.$order_by;
396cb469644SSzymon Olewniczak        if ($desc) $query .= ' DESC';
397cb469644SSzymon Olewniczak
398193a767dSSzymon Olewniczak        $res = $db->query($query, $this->globNamespace($namespace));
399872edc7cSRené Corinth
400*57e45304SSzymon Olewniczak        return array_map(
401*57e45304SSzymon Olewniczak                function($record) {
402*57e45304SSzymon Olewniczak                    $record['orig'] = explode(',', $record['orig']);
403*57e45304SSzymon Olewniczak                    $record['taggers'] = explode(',', $record['taggers']);
404*57e45304SSzymon Olewniczak                    return $record;
405*57e45304SSzymon Olewniczak                }, $db->res2arr($res));
406872edc7cSRené Corinth    }
407872edc7cSRené Corinth
4088a1a3846SAndreas Gohr    /**
4098a1a3846SAndreas Gohr     * Renames a tag
4108a1a3846SAndreas Gohr     *
4118a1a3846SAndreas Gohr     * @param string $formerTagName
4128a1a3846SAndreas Gohr     * @param string $newTagName
4138a1a3846SAndreas Gohr     */
414872edc7cSRené Corinth    public function renameTag($formerTagName, $newTagName) {
415872edc7cSRené Corinth
416872edc7cSRené Corinth        if (empty($formerTagName) || empty($newTagName)) {
4178a1a3846SAndreas Gohr            msg($this->getLang("admin enter tag names"), -1);
4188a1a3846SAndreas Gohr            return;
419872edc7cSRené Corinth        }
420872edc7cSRené Corinth
421872edc7cSRené Corinth        $db = $this->getDb();
422872edc7cSRené Corinth
423fb1d0583SAndreas Gohr        $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ?', $this->cleanTag($formerTagName));
424872edc7cSRené Corinth        $check = $db->res2arr($res);
425872edc7cSRené Corinth
426872edc7cSRené Corinth        if (empty($check)) {
4278a1a3846SAndreas Gohr            msg($this->getLang("admin tag does not exists"), -1);
4288a1a3846SAndreas Gohr            return;
429872edc7cSRené Corinth        }
430872edc7cSRené Corinth
431fb1d0583SAndreas Gohr        $res = $db->query("UPDATE taggings SET tag = ? WHERE CLEANTAG(tag) = ?", $newTagName, $this->cleanTag($formerTagName));
432872edc7cSRené Corinth        $db->res2arr($res);
433872edc7cSRené Corinth
434fb1d0583SAndreas Gohr        msg($this->getLang("admin renamed"), 1);
4358a1a3846SAndreas Gohr        return;
436872edc7cSRené Corinth    }
437872edc7cSRené Corinth
4388f630140SSzymon Olewniczak    /**
4398f630140SSzymon Olewniczak     * Deletes a tag
4408f630140SSzymon Olewniczak     *
4418f630140SSzymon Olewniczak     * @param array $tags
44231396860SSzymon Olewniczak     * @param string $namespace current namespace context as in getAllTags()
4438f630140SSzymon Olewniczak     */
44431396860SSzymon Olewniczak    public function deleteTags($tags, $namespace='') {
4458f630140SSzymon Olewniczak        if (empty($tags)) return;
4468f630140SSzymon Olewniczak
44731396860SSzymon Olewniczak        $namespace = cleanId($namespace);
44831396860SSzymon Olewniczak
4498f630140SSzymon Olewniczak        $db = $this->getDb();
4508f630140SSzymon Olewniczak
45131396860SSzymon Olewniczak        $query = 'DELETE FROM taggings WHERE pid LIKE ? AND (' .
45231396860SSzymon Olewniczak                                implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')).')';
45331396860SSzymon Olewniczak
45431396860SSzymon Olewniczak        $args = array_map(array($this, 'cleanTag'), $tags);
45531396860SSzymon Olewniczak        array_unshift($args, $this->globNamespace($namespace));
45631396860SSzymon Olewniczak        $res = $db->query($query, $args);
4578f630140SSzymon Olewniczak
4588f630140SSzymon Olewniczak        msg(sprintf($this->getLang("admin deleted"), count($tags), $res->rowCount()), 1);
4598f630140SSzymon Olewniczak        return;
4608f630140SSzymon Olewniczak    }
461f61105deSAdrian Lang}
462