Assembla home | Assembla project page
 

Changeset 138

Show
Ignore:
Timestamp:
03/07/08 19:01:55 (9 months ago)
Author:
Slynderdale
Message:

Fixes #32 and adds transliteration support to Phergie's base as well as making URL and FeedTicker? use the new transliterate function.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Abstract/Base.php

    r106 r138  
    214214 
    215215    /** 
     216    * Decodes the entities of a given string and 
     217    * transliterates the UTF-8 string into corresponding ASCII characters. 
     218    * 
     219    * @param string $text The text to decode. 
     220    * @param string $charSetFrom The charset used as the base chrset to convert from 
     221    * @param string $charSetTo The charset used to convert to 
     222    * @return string 
     223    */ 
     224    public function decodeTranslit($text, $charSetFrom = 'UTF-8', $charSetTo = 'ISO-8859-1') { 
     225        $text = html_entity_decode($text, ENT_QUOTES, $charSetFrom); 
     226        $text = preg_replace('/&#0*([0-9]+);/me', "$this->codeToUtf(\\1)", $text); 
     227        $text = preg_replace('/&#x0*([a-f0-9]+);/mei', "$this->codeToUtf(hexdec(\\1))", $text); 
     228 
     229        // Use the translit extension if installed else fallback on to basic transliteration 
     230        if (extension_loaded('iconv')) { 
     231                  $text = iconv($charSetFrom, $charSetTo.'//TRANSLIT', $text); 
     232        // Transliteration supprt via the translit extension is still experimental 
     233        } else if (false && extension_loaded('translit')) { 
     234            $text = transliterate($text, array('han_transliterate', 'diacritical_remove'), $charSetFrom, $charSetTo); 
     235        } else { 
     236            $text = strtr($text, '��������������������������', 
     237                                 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy'); 
     238            $text = preg_replace('{[^a-z0-9&|"#\'\{\}()�^!�\[\]$*���%�`~=+:/;.,?><\\ _-]}i', '', $text); 
     239            $text = utf8_decode($text); 
     240        } 
     241 
     242        return $text; 
     243    } 
     244 
     245    /** 
     246    * Converts a given unicode to its UTF-8 equivalent. 
     247    * 
     248    * @param int $code The code to decode to UTF-8. 
     249    * @return string 
     250    */ 
     251    public function codeToUtf($code) { 
     252        $code = intval($code); 
     253        switch ($code) { 
     254            // 1 byte, 7 bits 
     255            case 0: 
     256                return chr(0); 
     257                    case ($code & 0x7F): 
     258                        return chr($code); 
     259 
     260                    // 2 bytes, 11 bits 
     261                    case ($code & 0x7FF): 
     262                        return chr(0xC0 | (($code >> 6) & 0x1F)) . 
     263                       chr(0x80 | ($code & 0x3F)); 
     264 
     265                    // 3 bytes, 16 bits 
     266                    case ($code & 0xFFFF): 
     267                        return chr(0xE0 | (($code >> 12) & 0x0F)) . 
     268                       chr(0x80 | (($code >> 6) & 0x3F)) . 
     269                       chr(0x80 | ($code & 0x3F)); 
     270 
     271                    // 4 bytes, 21 bits 
     272                    case ($code & 0x1FFFFF): 
     273                        return chr(0xF0 | ($code >> 18)) . 
     274                       chr(0x80 | (($code >> 12) & 0x3F)) . 
     275                       chr(0x80 | (($code >> 6) & 0x3F)) . 
     276                       chr(0x80 | ($code & 0x3F)); 
     277          } 
     278    } 
     279 
     280    /** 
    216281    * Returns whether or not the current environment meets the requirements 
    217282    * of the plugin in order for it to be run, including the PHP version, 
  • trunk/Phergie/Plugin/FeedTicker.php

    r130 r138  
    9393        // Global Feed Title, Feed URL and Article Title Filters 
    9494        $globalTitle = trim($this->getPluginIni('filter_title')); 
    95                $globalUrl = trim($this->getPluginIni('filter_url')); 
     95        $globalUrl = trim($this->getPluginIni('filter_url')); 
    9696        $globalArticle = trim($this->getPluginIni('filter_article')); 
    97          
     97 
    9898        $i = 0; 
    9999        $this->feeds = array(); 
     
    101101                // Feed and Chan data 
    102102            $feed = $this->getPluginIni('feed' . $i); 
    103             $chans = $this->getPluginIni('chans' . $i);       
     103            $chans = $this->getPluginIni('chans' . $i); 
    104104            // Feed Title, Feed URL and Article Title Filter Data 
    105105            $filterTitle = $this->getPluginIni('filter_title' . $i); 
    106106            $filterUrl = $this->getPluginIni('filter_url' . $i); 
    107107            $filterArticle = $this->getPluginIni('filter_article' . $i); 
    108                        
     108 
    109109            if (!empty($feed) && !empty($chans)) { 
    110110                $this->feeds[] = array($feed, preg_split('#[\s\r\n,]+#', $chans)); 
    111111                // Feed Title, Feed URL and Article Title Filters 
    112                 $this->filterTitle[] = trim(trim(implode('|', array($globalTitle, $filterTitle)), '|')); 
    113                 $this->filterUrl[] = trim(trim(implode('|', array($globalUrl, $filterUrl)), '|')); 
    114                 $this->filterArticle[] = trim(trim(implode('|', array($globalArticle, $filterArticle)), '|')); 
     112                $filterTrim = "| \t\n\r\0\v\0xa0"; 
     113                $this->filterTitle[] = trim(implode('|', array($globalTitle, $filterTitle)), $filterTrim); 
     114                $this->filterUrl[] = trim(implode('|', array($globalUrl, $filterUrl)), $filterTrim); 
     115                $this->filterArticle[] = trim(implode('|', array($globalArticle, $filterArticle)), $filterTrim); 
    115116            } 
    116117        } while (++$i < 10); 
     
    194195            $content = @file_get_contents($url, null, $context); 
    195196            if (empty ($content)) { 
    196                 $this->debug('Feed empty: ' . $url);  
     197                $this->debug('Feed empty: ' . $url); 
    197198                continue; 
    198199            } 
     
    213214            } else { // Trouble 
    214215                $this->debug('Feed format unrecognized: ' . $url); 
    215                 continue;  
     216                continue; 
    216217            } 
    217218        } 
     
    248249          // Feed Title, Feed URL and Article Title Filters 
    249250        $filterTitle = $this->filterTitle[$id]; 
    250         $filterUrl = $this->filterUrl[$id];     
    251         $filterArticle = $this->filterArticle[$id];    
    252          
     251        $filterUrl = $this->filterUrl[$id]; 
     252        $filterArticle = $this->filterArticle[$id]; 
     253 
    253254        // Check against the filters if any are set 
    254           if (($filterTitle && preg_match('/'.$filterTitle.'/im', $title, $match)) || 
    255               ($filterUrl && preg_match('/'.$filterUrl.'/im', $url, $match)) || 
    256               ($filterArticle && preg_match('/'.$filterArticle.'/im', $article, $match))) { 
     255          if (($filterTitle && preg_match('{'.$filterTitle.'}im', $title, $match)) || 
     256              ($filterUrl && preg_match('{'.$filterUrl.'}im', $url, $match)) || 
     257              ($filterArticle && preg_match('{'.$filterArticle.'}im', $article, $match))) { 
    257258              return false; 
    258259          } 
    259260          return true; 
    260261    } 
    261      
     262 
    262263    /** 
    263264    * Transliterates a UTF-8 string into corresponding ASCII characters and 
     
    269270    * @return string 
    270271    */ 
    271     protected function decode($str, $trim = null) 
    272     { 
    273         $out = utf8_decode($str); 
    274         $out = html_entity_decode($out, ENT_QUOTES); 
    275         $out = strtr($out, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy'); 
    276         $out = preg_replace('{[^a-z0-9&|"#\'\{\}()§^!°\[\]$*¨µ£%´`~=+:/;.,?><\\ _-]}i', '', $out); 
    277                 $out = preg_replace('/&#([a-f0-9]+);/mi', "", $out); 
     272    protected function decode($str, $trim = null) { 
     273          $out = $this->decodeTranslit($str); 
    278274        if($trim > 0) { 
    279275            $out = substr($out, 0, $trim) . (strlen($out) > $trim ? '...' : ''); 
  • trunk/Phergie/Plugin/Url.php

    r132 r138  
    117117                $this->cache[] = $url; 
    118118                $this->cache[] = $tinyUrl; 
    119                                 if (count($this->cache) > 10) { 
    120                         array_shift($this->cache); 
    121                       array_shift($this->cache); 
    122                                
     119                                        if (count($this->cache) > 10) { 
     120                          array_shift($this->cache); 
     121                      array_shift($this->cache); 
     122                                       
    123123 
    124124                unset($title, $tinyUrl, $title); 
     
    136136    * @return string 
    137137    */ 
    138     protected function decode($str, $trim=null) 
    139     { 
    140         $out = utf8_decode($str); 
    141         $out = html_entity_decode($out, ENT_QUOTES); 
    142         $out = strtr($out, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy'); 
    143         $out = preg_replace('{[^a-z0-9&|"#\'\{\}()§^!°\[\]$*¨µ£%´`~=+:/;.,?><\\ _-]}i', '', $out); 
    144                 $out = preg_replace('/&#([a-f0-9]+);/mi', "", $out); 
    145         $out = trim($out); 
     138    protected function decode($str, $trim = null) { 
     139          $out = $this->decodeTranslit($str); 
    146140        if($trim > 0) { 
    147141            $out = substr($out, 0, $trim) . (strlen($out) > $trim ? '...' : '');