Skip to content

Commit

Permalink
Improvements and many corrections for the syntaxes
Browse files Browse the repository at this point in the history
Count syntax:
* simplification by using a taglink creation for each tags and not creating all taglinks at once causing an empty list when tags have a coma.
Tag syntax:
* transfering the taglist to the render part where it is really used
* direct use of the taglist to update the metadata
* direct use of the resolve_pageid to update the relation reference metadata
Topic syntax:
* commenting the relation reference metadata update, causing issue with backlinks of the pointed pages when tags are removed from them.
Helper:
* Creating a taglink function
* rewrite of the _updateTagIndex function
* simplification of the _generateTagIndex using less ressources
  • Loading branch information
YoBoY committed Feb 26, 2012
1 parent 36b4eb5 commit d0e6331
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 67 deletions.
107 changes: 67 additions & 40 deletions helper.php
Expand Up @@ -122,29 +122,37 @@ function tagLinks($tags) {
if (empty($tags) || ($tags[0] == '')) return ''; if (empty($tags) || ($tags[0] == '')) return '';


foreach ($tags as $tag) { foreach ($tags as $tag) {
$svtag = $tag; $links[] = $this->tagLink($tag);
$title = str_replace('_', ' ', noNS($tag));
resolve_pageid($this->namespace, $tag, $exists); // resolve shortcuts
if ($exists) {
$class = 'wikilink1';
$url = wl($tag);
if ($conf['useheading']) {
// important: set sendond param to false to prevent recursion!
$heading = p_get_first_heading($tag, false);
if ($heading) $title = $heading;
}
} else {
$class = 'wikilink1';
$url = wl($tag, array('do'=>'showtag', 'tag'=>$svtag));
}
$links[] = '<a href="'.$url.'" class="'.$class.'" title="'.hsc($tag).
'" rel="tag">'.hsc($title).'</a>';
$this->references[$tag] = $exists;
} }


return implode(','.DOKU_LF.DOKU_TAB, $links); return implode(','.DOKU_LF.DOKU_TAB, $links);
} }


/**
* Returns the link for one given tag
*/
function tagLink($tag) {
$svtag = $tag;
$title = str_replace('_', ' ', noNS($tag));
resolve_pageid($this->namespace, $tag, $exists); // resolve shortcuts
if ($exists) {
$class = 'wikilink1';
$url = wl($tag);
if ($conf['useheading']) {
// important: set sendond param to false to prevent recursion!
$heading = p_get_first_heading($tag, false);
if ($heading) $title = $heading;
}
} else {
$class = 'wikilink1';
$url = wl($tag, array('do'=>'showtag', 'tag'=>$svtag));
}
$link = '<a href="'.$url.'" class="'.$class.'" title="'.hsc($tag).
'" rel="tag">'.hsc($title).'</a>';
$this->references[$tag] = $exists;
return $link;
}

/** /**
* Returns a list of pages with a certain tag; very similar to ft_backlinks() * Returns a list of pages with a certain tag; very similar to ft_backlinks()
* *
Expand Down Expand Up @@ -413,7 +421,10 @@ function _refreshTagIndex($id, $tags) {
function _updateTagIndex($id, $tags) { function _updateTagIndex($id, $tags) {
global $ID, $INFO; global $ID, $INFO;


// if nothing to do
if (!is_array($tags) || empty($tags)) return false; if (!is_array($tags) || empty($tags)) return false;

// track changes
$changed = false; $changed = false;


// clean array first // clean array first
Expand All @@ -422,31 +433,42 @@ function _updateTagIndex($id, $tags) {
$tags[$i] = utf8_strtolower($tags[$i]); $tags[$i] = utf8_strtolower($tags[$i]);
} }


// clear no longer used tags $knowntags = array(); // track known tags
if ($ID == $id) { // clear all no more used tags
$oldtags = $INFO['meta']['subject']; foreach($this->topic_idx as $tag => $pages) {
if (!is_array($oldtags)) $oldtags = explode(' ', $oldtags); if (!is_array($pages)) {
foreach ($oldtags as $oldtag) { // clear unvalid type topic
if (!$oldtag) continue; // skip empty tags unset($this->topic_idx[$tag]);
$oldtag = utf8_strtolower($oldtag); $changed = true;
if (in_array($oldtag, $tags)) continue; // tag is still there } elseif (in_array($id, $pages)) {
if (!is_array($this->topic_idx[$oldtag])) if(in_array($tag, $tags)) $knowntags[] = $tag; // nothing to do
$this->topic_idx[$oldtag] = array(); else {
$this->topic_idx[$oldtag] = array_diff($this->topic_idx[$oldtag], array($id)); // tag deleted from the page
$this->topic_idx[$tag] = array_diff($this->topic_idx[$tag], array($id));
$changed = true;
}
} elseif (in_array($tag, $tags)) {
// tag added to the page
$this->topic_idx[$tag][] = $id;
$knowntags[] = $tag;
$changed = true;
}
// clean empty topic
if (count($this->topic_idx[$tag]) == 0 || (!$tag)) {
unset($this->topic_idx[$tag]);
$changed = true; $changed = true;
} }
} }


// fill tag in // only new topics to add now
foreach ($tags as $tag) { $newtopics = array_diff($tags, $knowntags);
if (!$tag) continue; // skip empty tags if (count($newtopics) != 0 ) {
if (!is_array($this->topic_idx[$tag])) $this->topic_idx[$tag] = array(); foreach($newtopics as $tag) {
if (!in_array($id, $this->topic_idx[$tag])) { if (!tag) continue; //skip empty tags
$this->topic_idx[$tag][] = $id; $this->topic_idx[$tag] = array($id);
$changed = true; $changed = true;
} }
} }

// save tag index // save tag index
if ($changed) return $this->_saveIndex(); if ($changed) return $this->_saveIndex();
else return true; else return true;
Expand Down Expand Up @@ -485,19 +507,24 @@ function _importTagIndex() {
} }


/** /**
* Generates the tag index * Generates the tag index if file missing
*/ */
function _generateTagIndex() { function _generateTagIndex() {
global $conf; global $conf;


require_once (DOKU_INC.'inc/search.php'); require_once (DOKU_INC.'inc/search.php');

$topic_index = array();
$pages = array(); $pages = array();
search($pages, $conf['datadir'], 'search_allpages', array()); search($pages, $conf['datadir'], 'search_allpages', array());
foreach ($pages as $page) { foreach ($pages as $page) {
$this->_generateTagData($page); $tags = p_get_metadata($page['id'], 'subject');
if (!is_array($tags)) $tags = explode(' ', $tags);
foreach($tags as $tag) {
if (!$tag) continue; // drop empty tags
$topic_index[utf8_strtolower($tag)][] = $page['id'];
}
} }
return true; return io_saveFile($this->idx_dir.'/topic.idx', serialize($topic_index));
} }


/** /**
Expand Down
19 changes: 19 additions & 0 deletions lang/fr/lang.php
@@ -0,0 +1,19 @@
<?php
/**
* French language file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/

// custom language strings for the plugin
$lang['tags'] = 'Tags';
$lang['topic'] = 'Topic';
$lang['rebuildindex'] = 'Reconstruire l\'index des tags';
$lang['toolbar_icon'] = 'Insérer des Tags';
$lang['empty_output'] = 'Pas de résultat';

$lang['missing_pagelistplugin'] = 'Le plugin Pagelist doit être installé pour la syntaxe topic.';

$lang['menu'] = 'Gestion de l\'indexation des Tags';

//Setup VIM: ex: et ts=2 :
18 changes: 15 additions & 3 deletions lang/fr/settings.php
Expand Up @@ -8,11 +8,23 @@
*/ */


// for the configuration manager // for the configuration manager
$lang['namespace'] = 'espace de nommage par defaut pour les tags'; $lang['namespace'] = 'Dossier par defaut pour les tags';
$lang['pingtechnorati'] = 'Ping Technorati';
$lang['toolbar_icon'] = 'Afficher l\'icone dans la barre d\'outils (purger data/cache/* et le cache du navigateur si il n\'apparait pas)';


$lang['sortkey'] = 'trier par:'; $lang['sortkey'] = 'trier par&nbsp;:';
$lang['sortkey_o_cdate'] = 'date de creation'; $lang['sortkey_o_cdate'] = 'date de creation';
$lang['sortkey_o_mdate'] = 'date de modification';
$lang['sortkey_o_pagename'] = 'nom de la page'; $lang['sortkey_o_pagename'] = 'nom de la page';
$lang['sortkey_o_id'] = 'ID de la page'; $lang['sortkey_o_id'] = 'ID de la page';
$lang['sortkey_o_title'] = 'titre';


//Setup VIM: ex: et ts=2 enc=utf-8 : $lang['sortorder'] = 'ordre';
$lang['sortorder_o_ascending'] = 'croissant';
$lang['sortorder_o_descending'] = 'décroissant';

$lang['pagelist_flags'] = 'Paramètres d\'affichage pour lister les pages (séparés par des virgules, pour la liste des paramètres voir la documentation du plugin pagelist)';

$lang['list_tags_of_subns'] = 'Lister les tags présent aussi dans les sous-dossiers (syntaxe count)';

//Setup VIM: ex: et ts=2 enc=utf-8 :
13 changes: 4 additions & 9 deletions syntax/count.php
Expand Up @@ -95,11 +95,6 @@ function handle($match, $state, $pos, &$handler) {


function render($mode, &$renderer, $data) { function render($mode, &$renderer, $data) {
if(!($my = plugin_load('helper', 'tag'))) return false; if(!($my = plugin_load('helper', 'tag'))) return false;
$taglinks = $my->tagLinks(array_keys($data));
$taglinks = explode(',', $taglinks);

// Prevent displaying a warning when result is empty
$tl = @array_combine(array_keys($data), $taglinks);


// $data -> tag as key; value as count of tag occurence // $data -> tag as key; value as count of tag occurence
$class = "inline"; // valid: inline, ul, pagelist $class = "inline"; // valid: inline, ul, pagelist
Expand All @@ -122,11 +117,11 @@ function render($mode, &$renderer, $data) {
$renderer->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'" colspan="2">'.$this->getLang('empty_output').'</td>'.DOKU_LF; $renderer->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'" colspan="2">'.$this->getLang('empty_output').'</td>'.DOKU_LF;
$renderer->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF; $renderer->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
} else { } else {
foreach($data as $key => $tag) { foreach($data as $tagname => $count) {
if($tag <= 0) continue; // don't display tags with zero occurences if($count <= 0) continue; // don't display tags with zero occurences
$renderer->doc .= DOKU_TAB.'<tr>'.DOKU_LF.DOKU_TAB.DOKU_TAB; $renderer->doc .= DOKU_TAB.'<tr>'.DOKU_LF.DOKU_TAB.DOKU_TAB;
$renderer->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'">'.$tl[$key].'</td>'.DOKU_LF; $renderer->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'">'.$my->tagLink($tagname).'</td>'.DOKU_LF;
$renderer->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'">'.$tag.'</td>'.DOKU_LF; $renderer->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'">'.$count.'</td>'.DOKU_LF;
$renderer->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF; $renderer->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
} }
} }
Expand Down
39 changes: 26 additions & 13 deletions syntax/tag.php
Expand Up @@ -42,42 +42,55 @@ function connectTo($mode) {
} }


function handle($match, $state, $pos, &$handler) { function handle($match, $state, $pos, &$handler) {
global $ID;
global $REV;

$tags = trim(substr($match, 6, -2)); // strip markup & whitespace $tags = trim(substr($match, 6, -2)); // strip markup & whitespace
$tags = preg_replace(array('/[[:blank:]]+/', '/\s+/'), " ", $tags); // replace linebreaks and multiple spaces with one space character $tags = preg_replace(array('/[[:blank:]]+/', '/\s+/'), " ", $tags); // replace linebreaks and multiple spaces with one space character


if (!$tags) return false; if (!$tags) return false;

// load the helper_plugin_tag
if (!$my =& plugin_load('helper', 'tag')) return false; if (!$my =& plugin_load('helper', 'tag')) return false;
$tags = $my->_parseTagList($tags); // split tags
$this->tags = array_merge($this->tags, $tags); // split tags and returns for renderer
// disable update of tags when viewing old page revisions return $my->_parseTagList($tags);
if($ACT != 'preview' && !$REV) $my->_updateTagIndex($ID, $this->tags);
return $tags;
} }


function render($mode, &$renderer, $data) { function render($mode, &$renderer, $data) {
global $ID;
global $REV;

if ($data === false) return false; if ($data === false) return false;
if (!$my =& plugin_load('helper', 'tag')) return false; if (!$my =& plugin_load('helper', 'tag')) return false;
$tags = $my->tagLinks($data);
if (!$tags) return true;


// XHTML output // XHTML output
if ($mode == 'xhtml') { if ($mode == 'xhtml') {
$tags = $my->tagLinks($data);
if (!$tags) return true;
$renderer->doc .= '<div class="tags"><span>'.DOKU_LF. $renderer->doc .= '<div class="tags"><span>'.DOKU_LF.
DOKU_TAB.$tags.DOKU_LF. DOKU_TAB.$tags.DOKU_LF.
'</span></div>'.DOKU_LF; '</span></div>'.DOKU_LF;
return true; return true;


// for metadata renderer // for metadata renderer
} elseif ($mode == 'metadata' && $ACT != 'preview' && !$REV) { } elseif ($mode == 'metadata' && $ACT != 'preview' && !$REV) {
// merge with previous tags
$this->tags = array_merge($this->tags, $data);
// update tags in topic.idx
$my->_updateTagIndex($ID, $this->tags);

if ($renderer->capture) $renderer->doc .= DOKU_LF.strip_tags($tags).DOKU_LF; if ($renderer->capture) $renderer->doc .= DOKU_LF.strip_tags($tags).DOKU_LF;
foreach ($my->references as $ref => $exists) {
$renderer->meta['relation']['references'][$ref] = $exists; // add references if tag page exists
foreach ($data as $tag) {
resolve_pageid($my->namespace, $tag, $exists); // resolve shortcuts
if ($exists) $renderer->meta['relation']['references'][$tag] = $exists;
} }

// erase tags on persistent metadata no more used
if (isset($renderer->persistent['subject'])) unset($renderer->persistent['subject']);

// update the metadata
if (!is_array($renderer->meta['subject'])) $renderer->meta['subject'] = array(); if (!is_array($renderer->meta['subject'])) $renderer->meta['subject'] = array();
$renderer->meta['subject'] = array_merge($renderer->meta['subject'], $data); $renderer->meta['subject'] = $this->tags;
return true; return true;
} }
return false; return false;
Expand Down
4 changes: 2 additions & 2 deletions syntax/topic.php
Expand Up @@ -87,12 +87,12 @@ function render($mode, &$renderer, $data) {
return true; return true;


// for metadata renderer // for metadata renderer
} elseif ($mode == 'metadata') { /* } elseif ($mode == 'metadata') {
foreach ($pages as $page) { foreach ($pages as $page) {
$renderer->meta['relation']['references'][$page['id']] = true; $renderer->meta['relation']['references'][$page['id']] = true;
} }
return true; return true;*/ // causes issues with backlinks
} }
return false; return false;
} }
Expand Down

0 comments on commit d0e6331

Please sign in to comment.