Source for file class.modrewriteurlutil.php

Documentation is available at class.modrewriteurlutil.php

  1. <?php
  2. /**
  3.  * Includes Mod Rewrite url utility class.
  4.  *
  5.  * @author      Murat Purc <murat@purc.de>
  6.  * @copyright   © Murat Purc 2008
  7.  * @package     Contenido
  8.  * @subpackage  ModRewrite
  9.  */
  10.  
  11.  
  12. if(!defined('CON_FRAMEWORK')) {
  13.     die('Illegal call');
  14. }
  15.  
  16.  
  17. /**
  18.  * Mod Rewrite url utility class. Handles convertion of Urls from contenido core
  19.  * based url composition pattern to AMR (Advanced Mod Rewrite) url composition
  20.  * pattern and vice versa.
  21.  *
  22.  * @author      Murat Purc <murat@purc.de>
  23.  * @date        28.10.2008
  24.  * @package     Contenido
  25.  * @subpackage  ModRewrite
  26.  */
  27. class ModRewriteUrlUtil extends ModRewriteBase {
  28.  
  29.     /**
  30.      * Self instance (singleton implementation)
  31.      * @var  ModRewriteUrlUtil 
  32.      */
  33.     private static $_instance;
  34.  
  35.     /**
  36.      * Contenido category word separator
  37.      * @var  string 
  38.      */
  39.     private $_catWordSep = '-';
  40.  
  41.     /**
  42.      * AMR category word separator
  43.      * @var  string 
  44.      */
  45.     private $_mrCatWordSep;
  46.  
  47.     /**
  48.      * Contenido category separator
  49.      * @var  string 
  50.      */
  51.     private $_catSep = '/';
  52.  
  53.     /**
  54.      * AMR category separator
  55.      * @var  string 
  56.      */
  57.     private $_mrCatSep;
  58.  
  59.     /**
  60.      * Contenido article separator
  61.      * @var  string 
  62.      */
  63.     private $_artSep = '/';
  64.  
  65.     /**
  66.      * AMR article separator
  67.      * @var  string 
  68.      */
  69.     private $_mrArtSep;
  70.  
  71.     /**
  72.      * Contenido article word separator
  73.      * @var  string 
  74.      */
  75.     private $_artWordSep = '-';
  76.  
  77.     /**
  78.      * AMR article word separator
  79.      * @var  string 
  80.      */
  81.     private $_mrArtWordSep;
  82.  
  83.     /**
  84.      * AMR extension used for articlenames (e. g. .html)
  85.      * @var  string 
  86.      */
  87.     private $_mrExt;
  88.  
  89.  
  90.     /**
  91.      * Contructor, sets some AMR configuration related properties
  92.      */
  93.     private function __construct({
  94.         parent::initialize();
  95.  
  96.         $aCfg parent::getConfig();
  97.         $this->_mrCatWordSep = $aCfg['category_word_seperator'];
  98.         $this->_mrCatSep     = $aCfg['category_seperator'];
  99.         $this->_mrArtSep     = $aCfg['article_seperator'];
  100.         $this->_mrArtWordSep = $aCfg['article_word_seperator'];
  101.         $this->_mrExt        = $aCfg['file_extension'];
  102.     }
  103.  
  104.  
  105.     /**
  106.      * Returns self instance (singleton pattern)
  107.      * @return  ModRewriteUrlUtil 
  108.      */
  109.     public static function getInstance({
  110.         if (self::$_instance == null{
  111.             self::$_instance new ModRewriteUrlUtil();
  112.         }
  113.         return self::$_instance;
  114.     }
  115.  
  116.  
  117.     /**
  118.      * Converts passed AMR url path to Contenido url path.
  119.      *
  120.      * @param   string  $urlPath  AMR url path
  121.      * @return  string  Contenido url path
  122.      */
  123.     public function toContenidoUrlPath($urlPath{
  124.         $newUrlPath $this->_toUrlPath(
  125.             $urlPath$this->_mrCatSep$this->_catSep$this->_mrCatWordSep$this->_catWordSep,
  126.             $this->_mrArtSep$this->_artSep
  127.         );
  128. #echo "<pre><strong>toContenidoUrlPath($urlPath) return $newUrlPath</strong></pre>";
  129.         return $newUrlPath;
  130.     }
  131.  
  132.     /**
  133.      * Converts passed Contenido url path to AMR url path.
  134.      *
  135.      * @param   string  $urlPath  Contenido url path
  136.      * @return  string  AMR url path
  137.      */
  138.     public function toModRewriteUrlPath($urlPath{
  139. #echo "<pre><strong>toModRewriteUrlPath($urlPath)</strong></pre>";
  140.         $newUrlPath $this->_toUrlPath(
  141.             $urlPath$this->_catSep$this->_mrCatSep$this->_catWordSep$this->_mrCatWordSep,
  142.             $this->_artSep$this->_mrArtSep
  143.         );
  144.         return $newUrlPath;
  145.     }
  146.  
  147.  
  148.     /**
  149.      * Converts passed url path to a another url path (Contenido to AMR and vice versa).
  150.      *
  151.      * @param   string  $urlPath         Source url path
  152.      * @param   string  $fromCatSep      Source category seperator
  153.      * @param   string  $toCatSep        Destination category seperator
  154.      * @param   string  $fromCatWordSep  Source category word seperator
  155.      * @param   string  $toCatWordSep    Destination category word seperator
  156.      * @param   string  $fromArtSep      Source article seperator
  157.      * @param   string  $toArtSep        Destination article seperator
  158.      * @return  string  Destination url path
  159.      */
  160.     private function _toUrlPath($urlPath$fromCatSep$toCatSep$fromCatWordSep$toCatWordSep,
  161.                                 $fromArtSep$toArtSep{
  162.         if ((string) $urlPath == ''{
  163.             return $urlPath;
  164.         }
  165.  
  166.         if (substr($urlPath-1== $fromArtSep{
  167.             $urlPath substr($urlPath0-1'{TAS}';
  168.         }
  169.  
  170.         // pre replace category word seperator and category seperator
  171.         $urlPath str_replace($fromCatWordSep'{CWS}'$urlPath);
  172.         $urlPath str_replace($fromCatSep'{CS}'$urlPath);
  173.  
  174.         // replace category word seperator
  175.         $urlPath str_replace('{CWS}'$toCatWordSep$urlPath);
  176.         $urlPath str_replace('{CS}'$toCatSep$urlPath);
  177.  
  178.         $urlPath str_replace('{TAS}'$toArtSep$urlPath);
  179.  
  180.         return $urlPath;
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Converts passed AMR url name to Contenido url name.
  186.      *
  187.      * @param   string  $urlName  AMR url name
  188.      * @return  string  Contenido url name
  189.      */
  190.     public function toContenidoUrlName($urlName{
  191.         $newUrlName $this->_toUrlName($urlName$this->_mrArtWordSep$this->_artWordSep);
  192.         return $newUrlName;
  193.     }
  194.  
  195.  
  196.     /**
  197.      * Converts passed Contenido url name to AMR url name.
  198.      *
  199.      * @param   string  $urlName  Contenido url name
  200.      * @return  string  AMR url name
  201.      */
  202.     public function toModRewriteUrlName($urlName{
  203.         $newUrlName $this->_toUrlName($urlName$this->_artWordSep$this->_mrArtWordSep);
  204.         return $newUrlName;
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Converts passed url name to a another url name (Contenido to AMR and vice versa).
  210.      *
  211.      * @param   string  $urlName         Source url name
  212.      * @param   string  $fromArtWordSep  Source article word seperator
  213.      * @param   string  $toArtWordSep    Destination article word seperator
  214.      * @return  string  Destination url name
  215.      */
  216.     private function _toUrlName($urlName$fromArtWordSep$toArtWordSep{
  217.         if ((string) $urlName == ''{
  218.             return $urlName;
  219.         }
  220. #echo "<pre>\$fromArtWordSep: $fromArtWordSep\n\$toArtWordSep: $toArtWordSep</pre>";
  221.  
  222.         $urlName str_replace($this->_mrExt'{EXT}'$urlName);
  223.  
  224.         // replace article word seperator
  225.         $urlName str_replace($fromArtWordSep$toArtWordSep$urlName);
  226.  
  227.         $urlName str_replace('{EXT}'$this->_mrExt$urlName);
  228. #echo "<pre>\$urlName: $urlName</pre>";
  229.  
  230.         return $urlName;
  231.     }
  232.  
  233.  
  234.     /**
  235.      * Converts passed AMR url to Contenido url.
  236.      *
  237.      * @param   string  $url  AMR url
  238.      * @return  string  Contenido url
  239.      */
  240.     public function toContenidoUrl($url{
  241.         if (strpos($url$this->_mrExt=== false{
  242.             $newUrl $this->toContenidoUrlPath($url);
  243.         else {
  244.             // replace category word and article word seperator
  245.             $path   substr($url0strrpos($url$this->_mrArtSep1);
  246.             $name   substr($urlstrrpos($url$this->_mrArtSep1);
  247.             $newUrl $this->toContenidoUrlPath($path$this->toContenidoUrlName($name);
  248.         }
  249.         return $newUrl;
  250.     }
  251.  
  252.  
  253.     /**
  254.      * Converts passed AMR url to Contenido url.
  255.      *
  256.      * @param   string  $url  AMR url
  257.      * @return  string  Contenido url
  258.      */
  259.     public function toModRewriteUrl($url{
  260.         if (strpos($url$this->_mrExt=== false{
  261.             $newUrl $this->toModRewriteUrlPath($url);
  262.         else {
  263.             // replace category word and article word seperator
  264.             $path   substr($url0strrpos($url$this->_artSep1);
  265.             $name   substr($urlstrrpos($url$this->_artSep1);
  266.             $newUrl $this->toModRewriteUrlPath($path$this->toModRewriteUrlName($name);
  267.         }
  268.         return $newUrl;
  269.     }
  270.  
  271.  
  272.     /**
  273.      * Converts passed url to a another url (Contenido to AMR and vice versa).
  274.      *
  275.      * @param   string  $urlPath         Source url path
  276.      * @param   string  $fromCatSep      Source category seperator
  277.      * @param   string  $toCatSep        Destination category seperator
  278.      * @param   string  $fromCatWordSep  Source category word seperator
  279.      * @param   string  $toCatWordSep    Destination category word seperator
  280.      * @param   string  $fromArtSep      Source article seperator
  281.      * @param   string  $toArtSep        Destination article seperator
  282.      * @param   string  $fromArtWordSep  Source article word seperator
  283.      * @param   string  $toArtWordSep    Destination article word seperator
  284.      * @return  string  Destination url
  285.      */
  286.     private function _toUrl($url$fromCatSep$toCatSep$fromCatWordSep$toCatWordSep,
  287.                             $fromArtSep$toArtSep$fromArtWordSep$toArtWordSep{
  288.         if ((string) $url == ''{
  289.             return $url;
  290.         }
  291.  
  292.         $url str_replace($this->_mrExt'{EXT}'$url);
  293.  
  294.         // replace category seperator
  295.         $url str_replace($fromCatSep$toCatSep$url);
  296.  
  297.         // replace article seperator
  298.         $url str_replace($fromArtSep$toArtSep$url);
  299.  
  300.         $url str_replace('{EXT}'$this->_mrExt$url);
  301.  
  302.         if (strpos($url$this->_mrExt=== false{
  303.             // no articlename, replace category word seperator
  304.             $url str_replace($fromCatWordSep$toCatWordSep$url);
  305. #echo "<pre>\$url: $url</pre>";
  306.         else {
  307.             // replace category word and article word seperator
  308.             $path str_replace($fromCatWordSep$toCatWordSepsubstr($url0strrpos($url$toArtSep1));
  309.             $file str_replace($fromArtWordSep$toArtWordSepsubstr($urlstrrpos($url$toArtSep1));
  310.             $url $path $file;
  311. #echo "<pre>\$path: $path; \$file: $file</pre>";
  312.         }
  313.  
  314.         return $url;
  315.     }
  316.  
  317. }

Documentation generated on Tue, 25 Nov 2008 22:07:30 +0100 by phpDocumentor 1.4.1