Want to use 2 or 3 character tags with the sNews CMS tags mod? Now you can.

OK, I hear you, the standard 4 character limit for the MySQL full-text search sucks for the tags, and you don't own the server you're on, so you can't recompile MySQL.

When running an article search, it's not a problem, you don't want people pounding your DB searching for microwords, and MySQL's full-text speed simply can't be touched so it works fine there, but it's a pain when it comes to the tags mod.

This is a problem for myself as well, as I like to use acronyms for tags, such as IE and CSS. Therefore, I have written an alternate tagsearch function. As it uses MySQL's REGEXP operator, it's more resource greedy, but unless you have 10,000 articles/pages, it shouldn't cause problems. However, if you don't want to use tags under four characters, I recommend sticking with the existing full-text tagsearch.

This is a simple drop in replacement, just BACK-UP your snews.php, first rename the existing tagsearch function to tagsearch_orig (or delete it, but I would save it to change over later if needed/desired), now copy the new tagsearch function into the bottom of your snews.php file, just before the closing php tag;

//TAGS ENGINE
function tagsearch($tags_query,$limit = 100,$min_chars = 2) {
    $tags_query = clean(cleanXSS($tags_query));
    echo '<h2>'.l(tag_results).'</h2>';
    if (strlen($tags_query) < $min_chars) {
        echo '<div><p>Tags must be at least '.$min_chars.' characters!</p>';
    } else {
        $tags_query = trim($tags_query);
       
        $now = date("Y-m-d H:i:s",time());
        $query = 'SELECT a.id,a.title,a.seftitle AS asef,a.date AS date,
                   c.name AS name,c.seftitle AS csef,
                   x.name AS xname,x.seftitle AS xsef
            FROM '._PRE.'articles'.' AS a
            LEFT OUTER JOIN '._PRE.'categories'.' as c
                ON category = c.id AND c.published =\'YES\'
            LEFT OUTER JOIN '._PRE.'categories'.' as x
                ON c.subcat =  x.id AND x.published =\'YES\'
            WHERE (a.keywords_meta LIKE \'%'.$tags_query.'%\' AND a.keywords_meta REGEXP \':<:'.$tags_query.':>:\') AND position != 2
                AND a.published = 1
                AND date <= \''.$now.'\'';
        if(!_ADMIN){
            $query = $query.'AND a.visible = \'YES\'';
        }
        
        $query = $query.' ORDER BY date DESC LIMIT '.$limit;
        $result = mysql_query($query);
        $numrows = mysql_num_rows($result);
        if (!$numrows) {
            echo '<div><p>'.l('noresults').'
                <strong>'.stripslashes($tags_query).'</strong>.</p>';
        } else {
            echo '<div id="tag_results"><p><strong>'.$numrows.'</strong> '.l('tagresultsfound').' <strong>'.
            stripslashes($tags_query).'</strong>.</p>';
            while ($r = mysql_fetch_array($result)) {
                $date = date(s('date_format'), strtotime($r['date']));
                if ($r['name']) { $name = ' in '.$r['name']; } else { $name = "";}
                if (isset($r['xsef']))  $link = $r['xsef'].'/'.$r['csef'].'/';
                else $link = isset($r['csef']) ? $r['csef'].'/' : '';
                echo '<p><a href="'._SITE.$link.$r['asef'].'/">'.$r['title'].'</a><br /><span class="tag_sub">added on '.$date.$name.'</span></p>';
            }
        }
    }
    echo '</div>';
}

Make sure you set the $min_chars variable to whatever minimum you like. Now upload your snews.php file and you should be in business. Leave the meta_keyword full-text index alone, as I'm working on an advanced search that will match against it, to be released at a future date :)

I am updating the tags mod to include this option as well.

Share or Bookmark This Post:


Comments

RSS Comments Feed


No comments yet.




(optional, not publicly displayed)


(optional)

Subscribe

RSS Feed

Archives

Powered by HTML5

HTML5 Powered with CSS3 / Styling, Multimedia, and Semantics