Source for file class.modrewriteinstaller.php

Documentation is available at class.modrewriteinstaller.php

  1. <?php
  2. /**
  3.  * Installer for Advanced Mod Rewrite Plugin, used by plugin setup.
  4.  *
  5.  * Some features are taken over from initial functions.mod_rewrite_setup.php file beeing created by
  6.  * Stefan Seifarth.
  7.  *
  8.  * @author      Murat Purc <murat@purc.de>
  9.  * @copyright   © ww.purc.de
  10.  * @package     Contenido
  11.  * @subpackage  ModRewrite
  12.  */
  13.  
  14.  
  15. defined('CON_FRAMEWORK'or die('Illegal call');
  16.  
  17. if (!class_exists('PluginSetupAbstract')) {
  18.     throw new Exception('ModRewriteInstaller: Base class "PluginSetupAbstract" doesn\'t exists, classfile must be included before.');
  19. }
  20.  
  21.  
  22. /**
  23.  * Installer for Advanced Mod Rewrite Plugin, used by plugin setup.
  24.  *
  25.  * Some features are taken over from initial functions.mod_rewrite_setup.php file beeing created by
  26.  * Stefan Seifarth (aka stese).
  27.  *
  28.  * @author      Murat Purc <murat@purc.de>
  29.  * @copyright   © ww.purc.de
  30.  * @package     Contenido
  31.  * @subpackage  ModRewrite
  32.  */
  33. class ModRewriteInstaller extends PluginSetupAbstract implements IPluginSetup {
  34.  
  35.     /**
  36.      * Constructor, initializes parent.
  37.      */
  38.     public function _construct(){
  39.         parent::_construct();
  40.     }
  41.  
  42.  
  43.     /**
  44.      * Installs the plugin, interface function implementation.
  45.      *
  46.      * Handle upgrading of mod rewrite needed database table columns
  47.      */
  48.     public function install(){
  49.         // check the existance of art_lang.urlname
  50.         $sql "SELECT * FROM " $this->_cfg['tab']['art_lang'" LIMIT 0,1";
  51.         $this->_db->query($sql);
  52.         if (!$this->_db->next_record(|| !$this->_db->f('urlname')) {
  53.             // add field 'urlname' to table
  54.             $sql "ALTER TABLE " $this->_cfg['tab']['art_lang'" ADD urlname VARCHAR( 128 ) AFTER title";
  55.             $this->_db->query($sql);
  56.         }
  57.  
  58.         // check the existance of cat_lang.urlpath
  59.         $sql "SELECT * FROM " $this->_cfg['tab']['cat_lang'" LIMIT 0,1";
  60.         $this->_db->query($sql);
  61.         if (!$this->_db->next_record(|| !$this->_db->f('urlpath')) {
  62.             // add field 'urlpath' to table
  63.             $sql "ALTER TABLE " $this->_cfg['tab']['cat_lang'" ADD urlpath VARCHAR( 255 ) AFTER urlname";
  64.             $this->_db->query($sql);
  65.         }
  66.  
  67.         // check for empty article fields
  68.         $sql "SELECT idlang, title, idart FROM " $this->_cfg['tab']['art_lang'" WHERE urlname IS NULL OR urlname = ''";
  69.         $this->_db->query($sql);
  70.         while ($this->_db->next_record()) {
  71.             $this->_setArticle($this->_db->f('title')$this->_db->f('idart')$this->_db->f('idlang'));
  72.         }
  73.  
  74.         // check for empty category urlname
  75.         $sql "SELECT name, idcat, idlang FROM " $this->_cfg['tab']['cat_lang'" WHERE urlname IS NULL OR urlname = ''";
  76.         $this->_db->query($sql);
  77.         while ($this->_db->next_record()) {
  78.             $this->_setCategory($this->_db->f('name')$this->_db->f('idcat')$this->_db->f('idlang'));
  79.         }
  80.  
  81.         // check for empty category urlpath
  82.         $sql "SELECT name, idcat, idlang FROM " $this->_cfg['tab']['cat_lang'" WHERE urlpath IS NULL OR urlpath = ''";
  83.         $this->_db->query($sql);
  84.         while ($this->_db->next_record()) {
  85.             $this->_setCategoryPath($this->_db->f('idcat')$this->_db->f('idlang'));
  86.         }
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Upgrade plugin, interface function implementation.
  92.      *
  93.      * Handle upgrading of mod rewrite needed database table columns
  94.      */
  95.     public function upgrade(){
  96.         $this->install();
  97.     }
  98.  
  99.  
  100.     /**
  101.      * Delete plugin, interface function implementation.
  102.      *
  103.      * Handle deleteting of mod rewrite needed database table columns
  104.      */
  105.     public function uninstall({
  106.         // remove field 'urlpath' from 'cat_lang' table
  107.         $sql "ALTER TABLE " $this->_cfg['tab']['cat_lang'" DROP urlpath";
  108.         $this->_db->query($sql);
  109.     }
  110.  
  111.  
  112.     /**
  113.      * Set websafe name in article list
  114.      *
  115.      * insert new websafe name in article list
  116.      *
  117.      * @param   string  original name (will be converted)
  118.      * @param   integer current article id
  119.      * @param   integer current language id
  120.      * @return  boolean true if insert was successfully
  121.      */
  122.     private function _setArticle($sName=""$iArtId=0$iLangId=0$iCatId=0{
  123.         static $db;
  124.         if (!isset($db)) {
  125.             $db new DB_Contenido();
  126.         }
  127.  
  128.         // create websafe name
  129.         $sNewName capiStrCleanURLCharacters($sName);
  130.  
  131.         // check if websafe name already exists
  132.         if ($this->_inArticles($sNewName$iArtId$iLangId$iCatId)) {
  133.             // create new websafe name if exists
  134.             $sNewName capiStrCleanURLCharacters($sName'_' $iArtId;
  135.         }
  136.  
  137.         // check again - and set name
  138.         if (!$this->_inArticles($sNewName$iArtId$iLangId$iCatId)) {
  139.             // insert websafe name in article list
  140.             $sql "UPDATE " $this->_cfg['tab']['art_lang'" SET urlname = '" $sNewName "' WHERE idart = '" $iArtId "' AND idlang = '" $iLangId "'";
  141.             return $db->query($sql);
  142.         else {
  143.             return false;
  144.         }
  145.     }
  146.  
  147.  
  148.     /**
  149.      * Set websafe name in category list
  150.      *
  151.      * insert new websafe name in category list
  152.      *
  153.      * @param   string  original name (will be converted)
  154.      * @param   integer current article id
  155.      * @param   integer current language id
  156.      * @return  boolean true if insert was successfully
  157.      */
  158.     private function _setCategory($sName=''$iCatId=0$iLangId=0{
  159.         static $db;
  160.         if (!isset($db)) {
  161.             $db new DB_Contenido();
  162.         }
  163.  
  164.         // create websafe name
  165.         $sNewName capiStrCleanURLCharacters($sName);
  166.  
  167.         // check if websafe name already exists
  168.         if ($this->_inCategory($sNewName$iCatId$iLangId)) {
  169.             // create new websafe name if exists
  170.             $sNewName capiStrCleanURLCharacters($sName'_' $iCatId;
  171.         }
  172.  
  173.         // check again - and set name
  174.         if (!$this->_inCategory($sNewName$iCatId$iLangId)) {
  175.             // insert websafe name in article list
  176.             $sql "UPDATE " $this->_cfg['tab']['cat_lang'" SET urlname = '$sNewName' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  177.             return $db->query($sql);
  178.         else {
  179.             return false;
  180.         }
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Build and set recursiv path for mod_rewrite rule like server directories
  186.      * (dir1/dir2/dir3)
  187.      *
  188.      * @param   int     $iCatId   Latest category id
  189.      * @param   int     $iLangId  Language id
  190.      * @param   int     $iLastId  Last category id
  191.      * @return     string    linkpath with correct uri
  192.      */
  193.     private function _setCategoryPath($iCatId=0$iLangId=0$iLastId=0{
  194.         static $db;
  195.         if (!isset($db)) {
  196.             $db new DB_Contenido();
  197.         }
  198.  
  199.         $aDirs     array();
  200.         $bFinish   false;
  201.         $iTmpCatId $iCatId;
  202.  
  203.         while ($bFinish == false{
  204.             $sql "SELECT cl.urlname, c.parentid FROM " $this->_cfg['tab']['cat_lang'" cl "
  205.                  . "LEFT JOIN " $this->_cfg['tab']['cat'" c ON cl.idcat = c.idcat "
  206.                  . "WHERE cl.idcat = '$iTmpCatId' AND cl.idlang = '$iLangId'";
  207.             $db->query($sql);
  208.             if ($db->next_record()) {
  209.                 $aDirs[]   $db->f('urlname');
  210.                 $iTmpCatId $db->f('parentid');
  211.  
  212.                 if ($db->f('parentid'== || $db->f('parentid'== $iLastId{
  213.                     $bFinish true;
  214.                 }
  215.             else {
  216.                 $bFinish true;
  217.             }
  218.         }
  219.  
  220.         // reverse array entries and create directory string
  221.         $sPath join('/'array_reverse($aDirs));
  222.  
  223.         // insert urlpath for category
  224.         $sql "UPDATE " $this->_cfg['tab']['cat_lang'" SET urlpath = '$sPath' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  225.         return $db->query($sql);
  226.     }
  227.  
  228.  
  229.     /**
  230.      * Check articles on websafe name
  231.      *
  232.      * Check all articles in the current category on existing same websafe name
  233.      *
  234.      * @param   string  Websafe name to check
  235.      * @param   integer current article id
  236.      * @param   integer current language id
  237.      * @param   integer current category id
  238.      * @return  boolean true if websafename already exists, false if not
  239.      */
  240.     private function _inArticles($sName=''$iArtId=0$iLangId=0$iCatId=0{
  241.         static $db;
  242.         if (!isset($db)) {
  243.             $db new DB_Contenido();
  244.         }
  245.  
  246.         $iCatId = (int) $iCatId;
  247.  
  248.         // handle multipages
  249.         if ($iCatId == 0{
  250.             // get category id if not set
  251.             $sql "SELECT idcat FROM " $this->_cfg['tab']['cat_art'" WHERE idart = '$iArtId'";
  252.             $db->query($sql);
  253.             $db->next_record();
  254.             $iCatId ($db->f('idcat'0$db->f('idcat''0';
  255.         }
  256.  
  257.         $sWhere " ca.idcat = '$iCatId' AND al.idlang = '$iLangId "' AND"
  258.                 . " al.urlname = '" $sName "' AND al.idart <> '$iArtId'";
  259.  
  260.         // check if websafe name is in this category
  261.         $sql "SELECT count(al.idart) as numcats FROM " $this->_cfg['tab']['art_lang'" al LEFT JOIN " $this->_cfg['tab']['cat_art'" ca ON al.idart = ca.idart WHERE " $sWhere;
  262.         $db->query($sql);
  263.         $db->next_record();
  264.  
  265.         return ($db->f('numcats'0true false;
  266.     }
  267.  
  268.  
  269.     /**
  270.      * Check categories on websafe name
  271.      *
  272.      * Check all categories in the main parent category on existing same websafe name
  273.      *
  274.      * @param   string  Websafe name to check
  275.      * @param   integer current category id
  276.      * @param   integer current language id
  277.      * @return  boolean true if websafename already exists, false if not
  278.      */
  279.     private function _inCategory($sName=''$iCatId=0$iLangId=0{
  280.         static $db;
  281.         if (!isset($db)) {
  282.             $db new DB_Contenido();
  283.         }
  284.  
  285.         // get parentid
  286.         $sql "SELECT parentid FROM " $this->_cfg['tab']['cat'" WHERE idcat = '$iCatId'";
  287.         $db->query($sql);
  288.         $db->next_record();
  289.         $iParentId ($db->f('parentid'0$db->f('parentid''0';
  290.  
  291.         $sWhere " c.parentid = '$iParentId' AND cl.idlang = '$iLangId "' AND"
  292.                 . " cl.urlname = '" $sName "' AND cl.idcat <> '$iCatId'";
  293.  
  294.         // check if websafe name is in this category
  295.         $sql "SELECT count(cl.idcat) as numcats FROM " $this->_cfg['tab']['cat_lang'" cl LEFT JOIN " $this->_cfg['tab']['cat'" c ON cl.idcat = c.idcat WHERE " $sWhere;
  296.         $db->query($sql);
  297.         $db->next_record();
  298.  
  299.         return ($db->f('numcats'0true false;
  300.     }
  301.  
  302. }

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