oxtagcloud.php

Go to the documentation of this file.
00001 <?php
00002 
00003 if (!defined('OXTAGCLOUD_MINFONT')) {
00004     define('OXTAGCLOUD_MINFONT', 100);
00005     define('OXTAGCLOUD_MAXFONT', 400);
00006     define('OXTAGCLOUD_MINOCCURENCETOSHOW', 2);
00007     //depends on mysql server configuration
00008     define('OXTAGCLOUD_MINTAGLENGTH', 4);
00009     define('OXTAGCLOUD_STARTPAGECOUNT', 20);
00010     define('OXTAGCLOUD_EXTENDEDCOUNT', 200);
00011 }
00012 
00017 class oxTagCloud extends oxSuperCfg
00018 {
00024     protected $_sCacheKey = "tagcloud_";
00025 
00035     public static function generateTagsFromLongDescription($iLang = 0)
00036     {
00037         $sQ = "select oxid, oxlongdesc".oxLang::getLanguageTag($iLang)." from oxartextends";
00038         $rs = oxDB::getDb(true)->Execute($sQ);
00039         while (!$rs->EOF && $i++ < 100) {
00040             $sLD = '';
00041             try {
00042                 $sLD = strip_tags($rs->fields["oxlongdesc"]);
00043                 $sLD = substr($sLD, 0, strpos($sLD, ' ', 80));
00044                 $sLD = str_replace(array("!","?",".",":","*", "«", "»", ",", "'", '"',"(",")", "&nbsp;", "\n", "\r"), "", $sLD);
00045                 $sLD = trim(strtolower($sLD));
00046                 $aLD = explode(" ", $sLD);
00047         
00048                 $sLD = '';
00049                 $aStopWords = array("die", "das", "die", "dieses", "aus", "in", "das", "den", "und", "mit", "ist", "so", "dem", "de", "die", "el","für"
00050                 ,"das","is","im","hat","nur","du","sie","ihr","diese","als","da","kann","wenn","ein","eine","dein","der","nicht","viel","jede","zum","sich","ja","bis"
00051                 ,"auf","oder","von","des","ab","dieser","vor","dir","von","einige","nach", "bei", "deiner","wie","also","sind","ins","einem", "wird", "am", "-", "22cm", "man", "24x19x4cm", "of", "es", "auch", "einen", "noch", "war", "waren", "wir", "aber", "haben");
00052                 foreach ($aLD as $ld) {
00053                     if ($ld && !in_array($ld, $aStopWords) && !is_numeric($ld)) {
00054                         $sLD .= $ld." ";
00055                     }
00056                 }
00057             } catch (Exception $e) {
00058             }
00059 
00060             $sLD = self::prepareTags($sLD);
00061 
00062             echo $sLD."<br><br>\n";
00063 
00064             $sQ = "update oxartextends set oxtags".oxLang::getLanguageTag($iLang)." = '$sLD' where oxid = '".$rs->fields['oxid']."'";
00065             oxDb::getDb(true)->execute($sQ);
00066             $rs->moveNext();
00067         }
00068     }
00069 
00078     public function getTags($sArtId = null, $blExtended = false)
00079     {
00080         if ($blExtended) {
00081             $iAmount = OXTAGCLOUD_EXTENDEDCOUNT;
00082         } else {
00083             $iAmount = OXTAGCLOUD_STARTPAGECOUNT;
00084         }
00085 
00086         //$oArticle = oxNew("oxarticle");
00087         //$sQ = "select oxtags from oxarticles where " . $oArticle->getSqlActiveSnippet();
00088         $sArticleSelect = " 1 ";
00089         if ($sArtId) {
00090             $sArtId = mysql_real_escape_string($sArtId);
00091             $sArticleSelect = " oxarticles.oxid = '$sArtId' ";
00092             $iAmount = 0;
00093         }
00094 
00095         $sField = "oxartextends.oxtags".oxLang::getInstance()->getLanguageTag();
00096 
00097         $sArtView = getViewName('oxarticles');
00098         $sQ = "select $sField as oxtags from $sArtView as oxarticles left join oxartextends on oxarticles.oxid=oxartextends.oxid where $sArticleSelect";
00099         //$sQ = "select $sField from oxartextends where $sArticleSelect";
00100         $rs = oxDb::getDb(true)->execute($sQ);
00101         $aTags = array();
00102         while ($rs && $rs->recordCount() && !$rs->EOF) {
00103             $sTags = $this->trimTags($rs->fields['oxtags']);
00104             $aArticleTags = explode(' ', $sTags);
00105             foreach ($aArticleTags as $sTag) {
00106                 if (trim($sTag)) {
00107                     ++$aTags[$sTag];
00108                 }
00109             }
00110             $rs->moveNext();
00111         }
00112 
00113         //taking only top tags
00114         if ($iAmount) {
00115             arsort($aTags);
00116             $aTags = array_slice($aTags, 0, $iAmount, true );
00117         }
00118 
00119         ksort($aTags);
00120 
00121         return $aTags;
00122     }
00123 
00132     public function getTagCloud($sArtId = null, $blExtended = false)
00133     {
00134         $sTagCloud = "";
00135         $sCacheKey = $this->_getCacheKey($blExtended);
00136         if ($this->_sCacheKey && !$sArtId) {
00137             $sTagCloud = oxUtils::getInstance()->fromFileCache($sCacheKey);
00138         }
00139 
00140         if ($sTagCloud)
00141             return $sTagCloud;
00142 
00143         startProfile('trimTags');
00144         $aTags = $this->getTags($sArtId, $blExtended);
00145         stopProfile('trimTags');
00146         if (!count($aTags)) {
00147             return $sTagCloud;
00148         }
00149 
00150         $iMaxHit = max( $aTags);
00151         $blSeoIsActive = oxUtils::getInstance()->seoIsActive();
00152         if ( $blSeoIsActive) {
00153             $oSeoEncoder = oxSeoEncoder::getInstance();
00154         }
00155 
00156         $iLang = oxLang::getInstance()->getBaseLanguage();
00157         $sUrl = $this->getConfig()->getShopUrl();
00158 
00159         foreach ($aTags as $sTag => $sRelevance) {
00160             $sLink = $sUrl."index.php?cl=tag&amp;searchtag=".rawurlencode($sTag)."&amp;lang=".$iLang;
00161             if ( $blSeoIsActive) {
00162                 $sLink = $oSeoEncoder->getDynamicUrl( "index.php?cl=tag&amp;searchtag=".rawurlencode($sTag), "tag/$sTag", $iLang );
00163             }
00164             $sTagCloud .= "<a style='font-size:". $this->_getFontSize($sRelevance, $iMaxHit) ."%;' href='$sLink'>".htmlentities($sTag)."</a> ";
00165         }
00166 
00167         if ($this->_sCacheKey && !$sArtId) {
00168             oxUtils::getInstance()->toFileCache($sCacheKey, $sTagCloud);
00169         }
00170 
00171         return $sTagCloud;
00172     }
00173 
00179     /*
00180     public function assignTagsFromSearchKeys()
00181     {
00182         $sArticleTable = getViewName('oxarticles');
00183         $sQ = "select oxid, oxsearchkeys from $sArticleTable where oxsearchkeys <> '' " ;
00184         $rs = oxDb::getDb(true)->execute($sQ);
00185         while ($rs && $rs->RecordCount()>0 && !$rs->EOF)
00186         {
00187             $sOxid = $rs->fields['OXID'];
00188             $sSearchkeys = $rs->fields['OXSEARCHKEYS'];
00189             $sUpdate = "update oxartextends set oxtags = '$sSearchkeys' where not oxtags and oxid = '$sOxid'";
00190             oxDb::getDb()->execute($sUpdate);
00191             $rs->moveNext();
00192         }
00193 
00194         return true;
00195     }*/
00196 
00205     protected function _getFontSize($iHit, $iMaxHit)
00206     {
00207         //handling special case
00208         if ($iMaxHit <= OXTAGCLOUD_MINOCCURENCETOSHOW || !$iMaxHit) {
00209             return OXTAGCLOUD_MINFONT;
00210         }
00211 
00212         $iFontDiff = OXTAGCLOUD_MAXFONT - OXTAGCLOUD_MINFONT;
00213         $iMaxHitDiff = $iMaxHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00214         $iHitDiff = $iHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00215 
00216         if ($iHitDiff < 0) {
00217             $iHitDiff = 0;
00218         }
00219 
00220         $iSize = round($iHitDiff * $iFontDiff / $iMaxHitDiff) + OXTAGCLOUD_MINFONT;
00221 
00222         return $iSize;
00223     }
00224 
00233     public function prepareTags($sTags)
00234     {
00235         $aTags = explode(' ', $sTags);
00236         $sRes = '';
00237         foreach ($aTags as $sTag) {
00238             if (!strlen($sTag)) {
00239                 continue;
00240             }
00241 
00242             if (strlen($sTag) < OXTAGCLOUD_MINTAGLENGTH) {
00243                 $sLength = strlen($sTag);
00244                 for ($i = 0; $i < OXTAGCLOUD_MINTAGLENGTH - $sLength; $i++) {
00245                     $sTag .= '_';
00246                 }
00247             }
00248 
00249             $sRes .= strtolower($sTag) . " ";
00250         }
00251 
00252         return trim ($sRes);
00253     }
00254 
00262     public function trimTags($sTags)
00263     {
00264         $aTags = explode(' ', $sTags);
00265         $sRes = '';
00266         foreach ($aTags as $sTag) {
00267             if (!strlen($sTag)) {
00268                 continue;
00269             }
00270 
00271             while($sTag[strlen($sTag) - 1] == '_') {
00272                 $sTag = substr($sTag, 0, -1);
00273             }
00274 
00275             $sRes .= $sTag . " ";
00276         }
00277 
00278         return trim ($sRes);
00279     }
00280 
00286     public function resetTagCache()
00287     {
00288         $sCacheKey1 = $this->_getCacheKey(true);
00289         oxUtils::getInstance()->toFileCache($sCacheKey1, null);
00290 
00291         $sCacheKey2 = $this->_getCacheKey(false);
00292         oxUtils::getInstance()->toFileCache($sCacheKey2, null);
00293     }
00294 
00302     protected function _getCacheKey($blExtended)
00303     {
00304         return $this->_sCacheKey."_".$this->getConfig()->getShopId()."_".oxLang::getInstance()->getBaseLanguage()."_".$blExtended;
00305     }
00306 
00307 }

Generated on Thu Dec 4 12:04:57 2008 for OXID eShop CE by  doxygen 1.5.5