Zikula_Template_Plugins
[ class tree: Zikula_Template_Plugins ] [ index: Zikula_Template_Plugins ] [ all elements ]

Source for file function.keywords.php

Documentation is available at function.keywords.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright (c) 2004, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: function.keywords.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @package Zikula_Template_Plugins
  10.  * @subpackage Functions
  11.  */
  12.  
  13. /**
  14.  * Smarty function to get the meta keywords
  15.  *
  16.  * This function will take the contents of the page and transfer it
  17.  * into a keyword list. If stopwords are defined, they are filtered out.
  18.  * The keywords are sorted by count.
  19.  * As a default, the whole page contents are taken as a base for keyword
  20.  * generation. If set, the contents of "contents" are taken.
  21.  * Beware that the function always returns the site keywords if "generate
  22.  * meta keywords" is turned off.
  23.  * PLEASE NOTE: This function adds additional overhead when dynamic keyword
  24.  * generation is turned on. You should use Xanthia page caching in this case.
  25.  *
  26.  * available parameters:
  27.  *  - contents    if set, this wil be taken as a base for the keywords
  28.  *  - dynamic     if set, the keywords will be created from the content / mainconent
  29.  *                oterwise we use the page vars. The rules are:
  30.  *                1) If dynamic keywords disabled in admin settings then use static keywords
  31.  *                2) if parameter "dynamic" not set or empty then always use main content (default),
  32.  *                3) if parameter "dynamic" set and not empty then use page vars if any set - otherwise use content.
  33.  *  - assign      if set, the keywords will be assigned to this variable
  34.  *
  35.  * Example
  36.  * <meta name="KEYWORDS" content="<!--[keywords]-->">
  37.  *
  38.  * @author   Jörg Napp
  39.  * @since    03. Feb. 04
  40.  * @param    array    $params     All attributes passed to this function from the template
  41.  * @param    object   $smarty     Reference to the Smarty object
  42.  * @return   string   the keywords
  43.  */
  44. function smarty_function_keywords($params&$smarty)
  45. {
  46.     if (pnConfigGetVar('dyn_keywords'== 1{
  47.         $keywordsarray PageUtil::getVar('keywords');
  48.  
  49.         // Check for empty keywords array
  50.         $emptyPageVar (empty($keywordsarray));
  51.  
  52.         if (!isset($params['dynamic']|| empty($params['dynamic']|| $emptyPageVar{
  53.             if (isset($params['contents'])) {
  54.                 $pagecontent $params['contents'];
  55.             else if (isset($smarty->_tpl_vars['maincontent'])) {
  56.                 $pagecontent $smarty->_tpl_vars['maincontent'];
  57.             else {
  58.                 return false;
  59.             }
  60.  
  61.             // get the contents of the page.
  62.             $pagecontent strtolower(strip_tags(html_entity_decode($pagecontent)));
  63.             
  64.             // strip the contents to an array at each non-letter
  65.             // this might be an issue for languages with other charsets.
  66.             $keywords preg_split ('/\W/'$pagecontent);
  67.  
  68.             // sort the array by the most used keys first
  69.             $keywords array_count_values($keywords);
  70.             arsort($keywords);
  71.  
  72.             // Remove entry that counts the number of blanks
  73.             unset ($keywords['']);
  74.             
  75.             // get back the keywords from the indexes
  76.             $keywords array_keys($keywords);
  77.             
  78.             // remove stopwords
  79.             // not correct here: The _user language_ is not relevant, only
  80.             // the language of the document. How to get this?
  81.             $stopword_file dirname(__FILE__'/stopwords/' pnUserGetLang('.txt';
  82.             if (file_exists($stopword_file)) {
  83.                 $stopwords file($stopword_file);
  84.                 $stopwords array_map('rtrim'$stopwords);
  85.                 $keywords array_diff($keywords$stopwords);
  86.             }
  87.             
  88.             // make it a comma-separated string
  89.             $keywords implode(','$keywords);
  90.         else {
  91.             // use PageUtil vars instead, no need to take care about stopwords
  92.             $ak array_keys($keywordsarray);  // einmal AK, immer AK ;-)
  93.             foreach($ak as $v{
  94.                 $keywordsarray[$vtrim($keywordsarray[$v]);
  95.                 if (empty($keywordsarray[$v])) {
  96.                     unset($keywordsarray[$v]);
  97.                 }
  98.             }
  99.             $keywords implode(','$keywordsarray);        
  100.        }
  101.     else {
  102.         $keywords pnConfigGetVar('metakeywords');
  103.     }
  104.  
  105.     if (isset($params['assign'])) {
  106.         $smarty->assign($params['assign']$keywords);
  107.     else {
  108.         return $keywords;
  109.     }
  110. }
  111.  
  112.  
  113. if (!function_exists('html_entity_decode')) {
  114.     /**
  115.      * html_entity_decode()
  116.      *
  117.      * Convert all HTML entities to their applicable characters
  118.      * This function is a fallback if html_entity_decode isn't defined
  119.      * in the PHP version used (i.e. PHP < 4.3.0).
  120.      * Please note that this function doesn't support all parameters
  121.      * of the original html_entity_decode function.
  122.      *
  123.      * @param  string $string the this function converts all HTML entities to their applicable characters from string.
  124.      * @return the converted string
  125.      * @link http://php.net/html_entity_decode The documentation of html_entity_decode
  126.      ***/
  127.     function html_entity_decode($string)
  128.     {
  129.         $trans_tbl get_html_translation_table(HTML_ENTITIES);
  130.         $trans_tbl array_flip($trans_tbl);
  131.         return (strtr($string$trans_tbl));
  132.     }
  133. }

Documentation generated on Fri, 18 Jul 2008 21:45:27 +0200 by phpDocumentor 1.4.1