xref: /plugin/tagging/helper.php (revision 0b6fad272ca40ee78c62da769a0f0cf26ad44b6f)
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    /**
330*0b6fad27Ssandos187     * Display a List of Page Links
331*0b6fad27Ssandos187     *
332*0b6fad27Ssandos187     * @param array    $pids   list of pids => count
333*0b6fad27Ssandos187     * @return string
334*0b6fad27Ssandos187     */
335*0b6fad27Ssandos187    public function html_page_list($pids) {
336*0b6fad27Ssandos187        global $INFO;
337*0b6fad27Ssandos187
338*0b6fad27Ssandos187        $ret = '<div class="search_quickresult">';
339*0b6fad27Ssandos187        $ret .= '<ul class="search_quickhits">';
340*0b6fad27Ssandos187
341*0b6fad27Ssandos187        if (count($pids) === 0) {
342*0b6fad27Ssandos187            // Produce valid XHTML (ul needs a child)
343*0b6fad27Ssandos187            $this->setupLocale();
344*0b6fad27Ssandos187            $ret .= '<li><div class="li">' . $this->lang['js']['nopages'] . '</div></li>';
345*0b6fad27Ssandos187        } else {
346*0b6fad27Ssandos187            foreach ($pids as $val => $size) {
347*0b6fad27Ssandos187                $ret .= '<li><div class="li">';
348*0b6fad27Ssandos187                $ret .= html_wikilink($val);
349*0b6fad27Ssandos187                $ret .= '</div></li>';
350*0b6fad27Ssandos187            }
351*0b6fad27Ssandos187        }
352*0b6fad27Ssandos187
353*0b6fad27Ssandos187        $ret .= '</ul>';
354*0b6fad27Ssandos187        $ret .= '</div>';
355*0b6fad27Ssandos187        $ret .= '<div class="clearer"></div>';
356*0b6fad27Ssandos187
357*0b6fad27Ssandos187        return $ret;
358*0b6fad27Ssandos187    }
359*0b6fad27Ssandos187
360*0b6fad27Ssandos187    /**
3615540f91dSAndreas Gohr     * Get the link to a search for the given tag
3625540f91dSAndreas Gohr     *
3635540f91dSAndreas Gohr     * @param string $tag search for this tag
3645540f91dSAndreas Gohr     * @param string $ns  limit search to this namespace
3650cfde7e9SMichael Große     *
3665540f91dSAndreas Gohr     * @return string
3675540f91dSAndreas Gohr     */
3685540f91dSAndreas Gohr    protected function linkToSearch($tag, $ns = '') {
3695540f91dSAndreas Gohr        return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>';
370f61105deSAdrian Lang    }
371f61105deSAdrian Lang
372fb1d0583SAndreas Gohr    /**
373fb1d0583SAndreas Gohr     * Display the Tags for the current page and prepare the tag editing form
3743496cc8aSAndreas Gohr     *
3753496cc8aSAndreas Gohr     * @param bool $print Should the HTML be printed or returned?
3760cfde7e9SMichael Große     *
3773496cc8aSAndreas Gohr     * @return string
378fb1d0583SAndreas Gohr     */
3793496cc8aSAndreas Gohr    public function tpl_tags($print = true) {
380f61105deSAdrian Lang        global $INFO;
381f61105deSAdrian Lang        global $lang;
3823bf0e2f1SMichael Große
3833bf0e2f1SMichael Große        $filter = array('pid' => $INFO['id']);
3843bf0e2f1SMichael Große        if ($this->getConf('singleusermode')) {
3853bf0e2f1SMichael Große            $filter['tagger'] = 'auto';
3863bf0e2f1SMichael Große        }
3873bf0e2f1SMichael Große
3883bf0e2f1SMichael Große        $tags = $this->findItems($filter, 'tag');
3893496cc8aSAndreas Gohr
3903496cc8aSAndreas Gohr        $ret = '';
3913496cc8aSAndreas Gohr
3923496cc8aSAndreas Gohr        $ret .= '<div class="plugin_tagging_edit">';
3933496cc8aSAndreas Gohr        $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true);
394f61105deSAdrian Lang
3952ace74f4SAndreas Gohr        if ($this->getUser() && $INFO['writable']) {
396f61105deSAdrian Lang            $lang['btn_tagging_edit'] = $lang['btn_secedit'];
397e5b42768SSzymon Olewniczak            $ret .= '<div id="tagging__edit_buttons_group">';
3983496cc8aSAndreas Gohr            $ret .= html_btn('tagging_edit', $INFO['id'], '', array());
399dd52fd45SSzymon Olewniczak            if (auth_isadmin()) {
400e5b42768SSzymon Olewniczak                $ret .= '<label>' . $this->getLang('toggle admin mode') . '<input type="checkbox" id="tagging__edit_toggle_admin" /></label>';
401dd52fd45SSzymon Olewniczak            }
402e5b42768SSzymon Olewniczak            $ret .= '</div>';
4032819ffcdSSzymon Olewniczak            $form = new dokuwiki\Form\Form();
4042819ffcdSSzymon Olewniczak            $form->id('tagging__edit');
4052819ffcdSSzymon Olewniczak            $form->setHiddenField('tagging[id]', $INFO['id']);
4062819ffcdSSzymon Olewniczak            $form->setHiddenField('call', 'plugin_tagging_save');
4072819ffcdSSzymon Olewniczak            $tags = $this->findItems(array(
4082819ffcdSSzymon Olewniczak                'pid'    => $INFO['id'],
409ca455b8eSMichael Große                'tagger' => $this->getUser(),
4102819ffcdSSzymon Olewniczak            ), 'tag');
4114b1e7a12SAndreas Gohr            $form->addTextarea('tagging[tags]')->val(implode(', ', array_keys($tags)))->addClass('edit')->attr('rows', 4);
412cf52ec2dSSzymon Olewniczak            $form->addButton('', $lang['btn_save'])->id('tagging__edit_save');
413cf52ec2dSSzymon Olewniczak            $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel');
4142819ffcdSSzymon Olewniczak            $ret .= $form->toHTML();
415f61105deSAdrian Lang        }
4163496cc8aSAndreas Gohr        $ret .= '</div>';
4173496cc8aSAndreas Gohr
4180cfde7e9SMichael Große        if ($print) {
4190cfde7e9SMichael Große            echo $ret;
4200cfde7e9SMichael Große        }
421ca455b8eSMichael Große
4223496cc8aSAndreas Gohr        return $ret;
423f61105deSAdrian Lang    }
424872edc7cSRené Corinth
4258a1a3846SAndreas Gohr    /**
426a99b66c1SSzymon Olewniczak     * @param string $namespace empty for entire wiki
427a99b66c1SSzymon Olewniczak     *
4288a1a3846SAndreas Gohr     * @return array
4298a1a3846SAndreas Gohr     */
430cb469644SSzymon Olewniczak    public function getAllTags($namespace = '', $order_by = 'tag', $desc = false) {
4318e9d0162SSzymon Olewniczak        $order_fields = array('pid', 'tid', 'orig', 'taggers', 'count');
432f0084ee1SSzymon Olewniczak        if (!in_array($order_by, $order_fields)) {
433f0084ee1SSzymon Olewniczak            msg('cannot sort by ' . $order_by . ' field does not exists', -1);
434f0084ee1SSzymon Olewniczak            $order_by = 'tag';
435f0084ee1SSzymon Olewniczak        }
436872edc7cSRené Corinth
437872edc7cSRené Corinth        $db = $this->getDb();
438872edc7cSRené Corinth
439f0084ee1SSzymon Olewniczak        $query = 'SELECT    "pid",
440ca455b8eSMichael Große                            CLEANTAG("tag") AS "tid",
441f0084ee1SSzymon Olewniczak                            GROUP_SORT(GROUP_CONCAT("tag"), \', \') AS "orig",
442f0084ee1SSzymon Olewniczak                            GROUP_SORT(GROUP_CONCAT("tagger"), \', \') AS "taggers",
443193a767dSSzymon Olewniczak                            COUNT(*) AS "count"
44457e45304SSzymon Olewniczak                        FROM "taggings"
445f0084ee1SSzymon Olewniczak                        WHERE "pid" LIKE ?
446f0084ee1SSzymon Olewniczak                        GROUP BY "tid"
447f0084ee1SSzymon Olewniczak                        ORDER BY ' . $order_by;
448ca455b8eSMichael Große        if ($desc) {
449ca455b8eSMichael Große            $query .= ' DESC';
450ca455b8eSMichael Große        }
451cb469644SSzymon Olewniczak
452f0084ee1SSzymon Olewniczak        $res = $db->query($query, $this->globNamespace($namespace));
453872edc7cSRené Corinth
4547e05e623SSzymon Olewniczak        return $db->res2arr($res);
455872edc7cSRené Corinth    }
456872edc7cSRené Corinth
4578a1a3846SAndreas Gohr    /**
45872431326SMichael Große     * Get all pages with tags and their tags
45972431326SMichael Große     *
460790ca788SAndreas Gohr     * @return array ['pid' => ['tag1','tag2','tag3']]
46172431326SMichael Große     */
46272431326SMichael Große    public function getAllTagsByPage() {
46372431326SMichael Große        $query = '
46472431326SMichael Große        SELECT pid, GROUP_CONCAT(tag) AS tags
46572431326SMichael Große        FROM taggings
46672431326SMichael Große        GROUP BY pid
46772431326SMichael Große        ';
46872431326SMichael Große        $db = $this->getDb();
46972431326SMichael Große        $res = $db->query($query);
470790ca788SAndreas Gohr        return array_map(
471790ca788SAndreas Gohr            function ($i) {
472790ca788SAndreas Gohr                return explode(',', $i);
473790ca788SAndreas Gohr            },
474790ca788SAndreas Gohr            array_column($db->res2arr($res), 'tags', 'pid')
475790ca788SAndreas Gohr        );
47672431326SMichael Große    }
47772431326SMichael Große
47872431326SMichael Große    /**
4798a1a3846SAndreas Gohr     * Renames a tag
4808a1a3846SAndreas Gohr     *
4818a1a3846SAndreas Gohr     * @param string $formerTagName
4828a1a3846SAndreas Gohr     * @param string $newTagName
4838a1a3846SAndreas Gohr     */
484872edc7cSRené Corinth    public function renameTag($formerTagName, $newTagName) {
485872edc7cSRené Corinth
486872edc7cSRené Corinth        if (empty($formerTagName) || empty($newTagName)) {
4878a1a3846SAndreas Gohr            msg($this->getLang("admin enter tag names"), -1);
488ca455b8eSMichael Große
4898a1a3846SAndreas Gohr            return;
490872edc7cSRené Corinth        }
491872edc7cSRené Corinth
492872edc7cSRené Corinth        $db = $this->getDb();
493872edc7cSRené Corinth
494fb1d0583SAndreas Gohr        $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ?', $this->cleanTag($formerTagName));
495872edc7cSRené Corinth        $check = $db->res2arr($res);
496872edc7cSRené Corinth
497872edc7cSRené Corinth        if (empty($check)) {
4988a1a3846SAndreas Gohr            msg($this->getLang("admin tag does not exists"), -1);
499ca455b8eSMichael Große
5008a1a3846SAndreas Gohr            return;
501872edc7cSRené Corinth        }
502872edc7cSRené Corinth
503fb1d0583SAndreas Gohr        $res = $db->query("UPDATE taggings SET tag = ? WHERE CLEANTAG(tag) = ?", $newTagName, $this->cleanTag($formerTagName));
504872edc7cSRené Corinth        $db->res2arr($res);
505872edc7cSRené Corinth
506fb1d0583SAndreas Gohr        msg($this->getLang("admin renamed"), 1);
507ca455b8eSMichael Große
5088a1a3846SAndreas Gohr        return;
509872edc7cSRené Corinth    }
510872edc7cSRené Corinth
5118f630140SSzymon Olewniczak    /**
512dd52fd45SSzymon Olewniczak     * Rename or delete a tag for all users
513dd52fd45SSzymon Olewniczak     *
514dd52fd45SSzymon Olewniczak     * @param string $pid
515dd52fd45SSzymon Olewniczak     * @param string $formerTagName
516dd52fd45SSzymon Olewniczak     * @param string $newTagName
517dd52fd45SSzymon Olewniczak     *
518dd52fd45SSzymon Olewniczak     * @return array
519dd52fd45SSzymon Olewniczak     */
520dd52fd45SSzymon Olewniczak    public function modifyPageTag($pid, $formerTagName, $newTagName) {
521dd52fd45SSzymon Olewniczak
522dd52fd45SSzymon Olewniczak        $db = $this->getDb();
523dd52fd45SSzymon Olewniczak
524dd52fd45SSzymon Olewniczak        $res = $db->query('SELECT pid FROM taggings WHERE CLEANTAG(tag) = ? AND pid = ?', $this->cleanTag($formerTagName), $pid);
525dd52fd45SSzymon Olewniczak        $check = $db->res2arr($res);
526dd52fd45SSzymon Olewniczak
527dd52fd45SSzymon Olewniczak        if (empty($check)) {
528dd52fd45SSzymon Olewniczak            return array(true, $this->getLang('admin tag does not exists'));
529dd52fd45SSzymon Olewniczak        }
530dd52fd45SSzymon Olewniczak
531dd52fd45SSzymon Olewniczak        if (empty($newTagName)) {
532dd52fd45SSzymon Olewniczak            $res = $db->query('DELETE FROM taggings WHERE pid = ? AND CLEANTAG(tag) = ?', $pid, $this->cleanTag($formerTagName));
533dd52fd45SSzymon Olewniczak        } else {
534dd52fd45SSzymon Olewniczak            $res = $db->query('UPDATE taggings SET tag = ? WHERE pid = ? AND CLEANTAG(tag) = ?', $newTagName, $pid, $this->cleanTag($formerTagName));
535dd52fd45SSzymon Olewniczak        }
536dd52fd45SSzymon Olewniczak        $db->res2arr($res);
537dd52fd45SSzymon Olewniczak
538dd52fd45SSzymon Olewniczak        return array(false, $this->getLang('admin renamed'));
539dd52fd45SSzymon Olewniczak    }
540dd52fd45SSzymon Olewniczak
541dd52fd45SSzymon Olewniczak    /**
5428f630140SSzymon Olewniczak     * Deletes a tag
5438f630140SSzymon Olewniczak     *
5448f630140SSzymon Olewniczak     * @param array  $tags
54531396860SSzymon Olewniczak     * @param string $namespace current namespace context as in getAllTags()
5468f630140SSzymon Olewniczak     */
54731396860SSzymon Olewniczak    public function deleteTags($tags, $namespace = '') {
548ca455b8eSMichael Große        if (empty($tags)) {
549ca455b8eSMichael Große            return;
550ca455b8eSMichael Große        }
5518f630140SSzymon Olewniczak
55231396860SSzymon Olewniczak        $namespace = cleanId($namespace);
55331396860SSzymon Olewniczak
5541f5991a7SMichael Große        $db = $this->getDB();
5558f630140SSzymon Olewniczak
5561f5991a7SMichael Große        $queryBody = 'FROM taggings WHERE pid LIKE ? AND (' .
55731396860SSzymon Olewniczak            implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')) . ')';
55831396860SSzymon Olewniczak        $args = array_map(array($this, 'cleanTag'), $tags);
55931396860SSzymon Olewniczak        array_unshift($args, $this->globNamespace($namespace));
5608f630140SSzymon Olewniczak
561ca455b8eSMichael Große
5621f5991a7SMichael Große        $affectedPagesQuery= 'SELECT DISTINCT pid ' . $queryBody;
5631f5991a7SMichael Große        $resAffectedPages = $db->query($affectedPagesQuery, $args);
5641f5991a7SMichael Große        $numAffectedPages = count($resAffectedPages->fetchAll());
5651f5991a7SMichael Große
5661f5991a7SMichael Große        $deleteQuery = 'DELETE ' . $queryBody;
5671f5991a7SMichael Große        $db->query($deleteQuery, $args);
5681f5991a7SMichael Große
5691f5991a7SMichael Große        msg(sprintf($this->getLang("admin deleted"), count($tags), $numAffectedPages), 1);
5708f630140SSzymon Olewniczak    }
571f61105deSAdrian Lang}
572