init('tagging', __DIR__ . '/db/');
$db->create_function('CLEANTAG', array($this, 'cleanTag'), 1);
$db->create_function('GROUP_SORT',
function ($group, $newDelimiter) {
$ex = array_filter(explode(',', $group));
sort($ex);
return implode($newDelimiter, $ex);
}, 2);
$db->create_function('GET_NS', 'getNS', 1);
return $db;
}
/**
* Return the user to use for accessing tags
*
* Handles the singleuser mode by returning 'auto' as user. Returnes false when no user is logged in.
*
* @return bool|string
*/
public function getUser() {
if (!isset($_SERVER['REMOTE_USER'])) {
return false;
}
if ($this->getConf('singleusermode')) {
return 'auto';
}
return $_SERVER['REMOTE_USER'];
}
/**
* If plugin elasticsearch is installed, inform it that we have just made changes
* to some data relevant to a page. The page should be re-indexed.
*
* @param string $id
*/
public function updateElasticState($id)
{
/** @var \helper_plugin_elasticsearch_plugins $elasticHelper */
$elasticHelper = plugin_load('helper', 'elasticsearch_plugins');
if ($elasticHelper) {
$elasticHelper->updateRefreshState($id);
}
}
/**
* Canonicalizes the tag to its lower case nospace form
*
* @param $tag
*
* @return string
*/
public function cleanTag($tag) {
$tag = str_replace(array(' ', '-', '_', '#'), '', $tag);
$tag = utf8_strtolower($tag);
return $tag;
}
/**
* Canonicalizes the namespace, remove the first colon and add glob
*
* @param $namespace
*
* @return string
*/
public function globNamespace($namespace) {
return cleanId($namespace) . '*';
}
/**
* Create or Update tags of a page
*
* Uses the translation plugin to store the language of a page (if available)
*
* @param string $id The page ID
* @param string $user
* @param array $tags
*
* @return bool|SQLiteResult
*/
public function replaceTags($id, $user, $tags) {
global $conf;
/** @var helper_plugin_translation $trans */
$trans = plugin_load('helper', 'translation');
if ($trans) {
$lang = $trans->realLC($trans->getLangPart($id));
} else {
$lang = $conf['lang'];
}
$db = $this->getDB();
$db->query('BEGIN TRANSACTION');
$queries = array(array('DELETE FROM taggings WHERE pid = ? AND tagger = ?', $id, $user));
foreach ($tags as $tag) {
$queries[] = array('INSERT INTO taggings (pid, tagger, tag, lang) VALUES(?, ?, ?, ?)', $id, $user, $tag, $lang);
}
foreach ($queries as $query) {
if (!call_user_func_array(array($db, 'query'), $query)) {
$db->query('ROLLBACK TRANSACTION');
return false;
}
}
return $db->query('COMMIT TRANSACTION');
}
/**
* Get a list of Tags or Pages matching search criteria
*
* @param array $filter What to search for array('field' => 'searchterm')
* @param string $type What field to return 'tag'|'pid'
* @param int $limit Limit to this many results, 0 for all
*
* @return array associative array in form of value => count
*/
public function findItems($filter, $type, $limit = 0) {
global $INPUT;
/** @var helper_plugin_tagging_querybuilder $queryBuilder */
$queryBuilder = new \helper_plugin_tagging_querybuilder();
$queryBuilder->setField($type);
$queryBuilder->setLimit($limit);
$queryBuilder->setTags($this->extractFromQuery($filter));
if (isset($filter['ns'])) $queryBuilder->includeNS($filter['ns']);
if (isset($filter['notns'])) $queryBuilder->excludeNS($filter['notns']);
if (isset($filter['tagger'])) $queryBuilder->setTagger($filter['tagger']);
if (isset($filter['pid'])) $queryBuilder->setPid($filter['pid']);
return $this->queryDb($queryBuilder->getQuery());
}
/**
* Constructs the URL to search for a tag
*
* @param string $tag
* @param string $ns
*
* @return string
*/
public function getTagSearchURL($tag, $ns = '') {
$ret = '?do=search&sf=1&q=' . rawurlencode('#' . $this->cleanTag($tag));
if ($ns) {
$ret .= rawurlencode(' @' . $ns);
}
return $ret;
}
/**
* Calculates the size levels for the given list of clouds
*
* Automatically determines sensible tresholds
*
* @param array $tags list of tags => count
* @param int $levels
*
* @return mixed
*/
public function cloudData($tags, $levels = 10) {
$min = min($tags);
$max = max($tags);
// calculate tresholds
$tresholds = array();
for ($i = 0; $i <= $levels; $i++) {
$tresholds[$i] = pow($max - $min + 1, $i / $levels) + $min - 1;
}
// assign weights
foreach ($tags as $tag => $cnt) {
foreach ($tresholds as $tresh => $val) {
if ($cnt <= $val) {
$tags[$tag] = $tresh;
break;
}
$tags[$tag] = $levels;
}
}
return $tags;
}
/**
* Display a tag cloud
*
* @param array $tags list of tags => count
* @param string $type 'tag'
* @param Callable $func The function to print the link (gets tag and ns)
* @param bool $wrap wrap cloud in UL tags?
* @param bool $return returnn HTML instead of printing?
* @param string $ns Add this namespace to search links
*
* @return string
*/
public function html_cloud($tags, $type, $func, $wrap = true, $return = false, $ns = '') {
global $INFO;
$hidden_str = $this->getConf('hiddenprefix');
$hidden_len = strlen($hidden_str);
$ret = '';
if ($wrap) {
$ret .= '
';
}
if (count($tags) === 0) {
// Produce valid XHTML (ul needs a child)
$this->setupLocale();
$ret .= '
' . $this->lang['js']['no' . $type . 's'] . '
';
} else {
$tags = $this->cloudData($tags);
foreach ($tags as $val => $size) {
// skip hidden tags for users that can't edit
if ($type === 'tag' and
$hidden_len and
substr($val, 0, $hidden_len) == $hidden_str and
!($this->getUser() && $INFO['writable'])
) {
continue;
}
$ret .= '
';
$ret .= '';
return $ret;
}
/**
* Get the link to a search for the given tag
*
* @param string $tag search for this tag
* @param string $ns limit search to this namespace
*
* @return string
*/
protected function linkToSearch($tag, $ns = '') {
return '' . $tag . '';
}
/**
* Display the Tags for the current page and prepare the tag editing form
*
* @param bool $print Should the HTML be printed or returned?
*
* @return string
*/
public function tpl_tags($print = true) {
global $INFO;
global $lang;
$filter = array('pid' => $INFO['id']);
if ($this->getConf('singleusermode')) {
$filter['tagger'] = 'auto';
}
$tags = $this->findItems($filter, 'tag');
$ret = '';
$ret .= '