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

Documentation generated on Sun, 08 Feb 2009 22:00:40 +0100 by phpDocumentor 1.4.1