Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cUriBuilderMR
  • ModRewrite
  • ModRewrite_ContentController
  • ModRewrite_ContentExpertController
  • ModRewrite_ContentTestController
  • ModRewrite_ControllerAbstract
  • ModRewriteBase
  • ModRewriteController
  • ModRewriteDebugger
  • ModRewriteTest
  • ModRewriteUrlStack
  • ModRewriteUrlUtil

Functions

  • mr_arrayValue
  • mr_buildGeneratedCode
  • mr_buildNewUrl
  • mr_conCopyArtLang
  • mr_conMoveArticles
  • mr_conSaveArticle
  • mr_conSyncArticle
  • mr_debugOutput
  • mr_getConfiguration
  • mr_getRequest
  • mr_header
  • mr_i18n
  • mr_loadConfiguration
  • mr_queryAndNextRecord
  • mr_removeMultipleChars
  • mr_requestCleanup
  • mr_runFrontendController
  • mr_setClientLanguageId
  • mr_setConfiguration
  • mr_strCopyCategory
  • mr_strMovedownCategory
  • mr_strMoveSubtree
  • mr_strMoveUpCategory
  • mr_strNewCategory
  • mr_strNewTree
  • mr_strRenameCategory
  • mr_strSyncCategory
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * AMR url stack class
  4:  *
  5:  * @package     Plugin
  6:  * @subpackage  ModRewrite
  7:  * @version     SVN Revision $Rev:$
  8:  * @id          $Id: class.modrewriteurlstack.php 6077 2013-12-07 23:40:41Z dominik.ziegler $:
  9:  * @author      Murat Purc <murat@purc.de>
 10:  * @copyright   four for business AG <www.4fb.de>
 11:  * @license     http://www.contenido.org/license/LIZENZ.txt
 12:  * @link        http://www.4fb.de
 13:  * @link        http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * Mod rewrite url stack class. Provides features to collect urls and to get the
 20:  * pretty path and names of categories/articles at one go.
 21:  *
 22:  * Main goal of this class is to collect urls and to get the urlpath and urlname
 23:  * of the related categories/articles at one go. This will reduce the queries
 24:  * against the database.
 25:  * Therefore the full advantage will be taken by rewriting the urls at codeoutput
 26:  * in front_content.php, where you will be able to collect all urls at once...
 27:  *
 28:  * Usage:
 29:  * <code>
 30:  * // get the instance
 31:  * $oMRUrlStack = ModRewriteUrlStack::getInstance();
 32:  *
 33:  * // add several urls to fill the stack
 34:  * $oMRUrlStack->add('front_content.php?idcat=123');
 35:  * $oMRUrlStack->add('front_content.php?idart=321');
 36:  * $oMRUrlStack->add('front_content.php?idcatart=213');
 37:  * $oMRUrlStack->add('front_content.php?idcatlang=213');
 38:  * $oMRUrlStack->add('front_content.php?idartlang=312');
 39:  *
 40:  * // now the first call will get the pretty path and names from database at one go
 41:  * $aPrettyParts = $oMRUrlStack->getPrettyUrlParts('front_content.php?idcat=123');
 42:  * echo $aPrettyParts['urlpath']; // something like 'Main-category-name/Category-name/Another-category-name/'
 43:  * echo $aPrettyParts['urlname']; // something like 'Name-of-an-article'
 44:  * </code>
 45:  *
 46:  * @author      Murat Purc <murat@purc.de>
 47:  * @package     Plugin
 48:  * @subpackage  ModRewrite
 49:  */
 50: class ModRewriteUrlStack {
 51: 
 52:     /**
 53:      * Self instance
 54:      *
 55:      * @var  ModRewriteUrlStack
 56:      */
 57:     private static $_instance;
 58: 
 59:     /**
 60:      * Database object
 61:      *
 62:      * @var  cDb
 63:      */
 64:     private $_oDb;
 65: 
 66:     /**
 67:      * Array for urls
 68:      *
 69:      * @var  array
 70:      */
 71:     private $_aUrls = array();
 72: 
 73:     /**
 74:      * Url stack array
 75:      *
 76:      * @var  array
 77:      */
 78:     private $_aStack = array();
 79: 
 80:     /**
 81:      * CONTENIDO related parameter array
 82:      *
 83:      * @var  array
 84:      */
 85:     private $_aConParams = array(
 86:         'idcat' => 1, 'idart' => 1, 'lang' => 1, 'idcatlang' => 1, 'idcatart' => 1, 'idartlang' => 1
 87:     );
 88: 
 89:     /**
 90:      * Database tables array
 91:      *
 92:      * @var  array
 93:      */
 94:     private $_aTab;
 95: 
 96:     /**
 97:      * Language id
 98:      *
 99:      * @var  int
100:      */
101:     private $_idLang;
102: 
103:     /**
104:      * Constructor, sets some properties.
105:      */
106:     private function __construct() {
107:         global $cfg, $lang;
108:         $this->_oDb = cRegistry::getDb();
109:         $this->_aTab = $cfg['tab'];
110:         $this->_idLang = $lang;
111:     }
112: 
113:     /**
114:      * Returns a instance of ModRewriteUrlStack (singleton implementation)
115:      *
116:      * @return  ModRewriteUrlStack
117:      */
118:     public static function getInstance() {
119:         if (self::$_instance == NULL) {
120:             self::$_instance = new ModRewriteUrlStack();
121:         }
122:         return self::$_instance;
123:     }
124: 
125:     /**
126:      * Adds an url to the stack
127:      *
128:      * @param  string $url Url, like front_content.php?idcat=123...
129:      */
130:     public function add($url) {
131:         $url = ModRewrite::urlPreClean($url);
132:         if (isset($this->_aUrls[$url])) {
133:             return;
134:         }
135: 
136:         $aUrl = $this->_extractUrl($url);
137: 
138:         // cleanup parameter
139:         foreach ($aUrl['params'] as $p => $v) {
140:             if (!isset($this->_aConParams[$p])) {
141:                 unset($aUrl['params'][$p]);
142:             } else {
143:                 $aUrl['params'][$p] = (int) $v;
144:             }
145:         }
146: 
147:         // add language id, if not available
148:         if ((int) mr_arrayValue($aUrl['params'], 'lang') == 0) {
149:             $aUrl['params']['lang'] = $this->_idLang;
150:         }
151: 
152:         $sStackId = $this->_makeStackId($aUrl['params']);
153:         $this->_aUrls[$url] = $sStackId;
154:         $this->_aStack[$sStackId] = array('params' => $aUrl['params']);
155:     }
156: 
157:     /**
158:      * Returns the pretty urlparts (only category path an article name) of the
159:      * desired url.
160:      *
161:      * @param   string  Url, like front_content.php?idcat=123...
162:      * @return  array   Assoziative array like
163:      * <code>
164:      * $arr['urlpath']
165:      * $arr['urlname']
166:      * </code>
167:      */
168:     public function getPrettyUrlParts($url) {
169:         $url = ModRewrite::urlPreClean($url);
170:         if (!isset($this->_aUrls[$url])) {
171:             $this->add($url);
172:         }
173: 
174:         $sStackId = $this->_aUrls[$url];
175:         if (!isset($this->_aStack[$sStackId]['urlpath'])) {
176:             $this->_chunkSetPrettyUrlParts();
177:         }
178:         $aPretty = array(
179:             'urlpath' => $this->_aStack[$sStackId]['urlpath'],
180:             'urlname' => $this->_aStack[$sStackId]['urlname']
181:         );
182:         return $aPretty;
183:     }
184: 
185:     /**
186:      * Extracts passed url using parse_urla and adds also the 'params' array to it
187:      *
188:      * @param   string  Url, like front_content.php?idcat=123...
189:      * @return  array  Components containing result of parse_url with additional
190:      *                 'params' array
191:      */
192:     private function _extractUrl($url) {
193:         $aUrl = @parse_url($url);
194:         if (isset($aUrl['query'])) {
195:             $aUrl['query'] = str_replace('&amp;', '&', $aUrl['query']);
196:             parse_str($aUrl['query'], $aUrl['params']);
197:         }
198:         if (!isset($aUrl['params']) && !is_array($aUrl['params'])) {
199:             $aUrl['params'] = array();
200:         }
201:         return $aUrl;
202:     }
203: 
204:     /**
205:      * Extracts article or category related parameter from passed params array
206:      * and generates an identifier.
207:      *
208:      * @param   array   $aParams  Parameter array
209:      * @return  string  Composed stack id
210:      */
211:     private function _makeStackId(array $aParams) {
212:         # idcatart
213:         if ((int) mr_arrayValue($aParams, 'idart') > 0) {
214:             $sStackId = 'idart_' . $aParams['idart'] . '_lang_' . $aParams['lang'];
215:         } elseif ((int) mr_arrayValue($aParams, 'idartlang') > 0) {
216:             $sStackId = 'idartlang_' . $aParams['idartlang'];
217:         } elseif ((int) mr_arrayValue($aParams, 'idcatart') > 0) {
218:             $sStackId = 'idcatart_' . $aParams['idcatart'] . '_lang_' . $aParams['lang'];
219:         } elseif ((int) mr_arrayValue($aParams, 'idcat') > 0) {
220:             $sStackId = 'idcat_' . $aParams['idcat'] . '_lang_' . $aParams['lang'];
221:         } elseif ((int) mr_arrayValue($aParams, 'idcatlang') > 0) {
222:             $sStackId = 'idcatlang_' . $aParams['idcatlang'];
223:         } else {
224:             $sStackId = 'lang_' . $aParams['lang'];
225:         }
226:         return $sStackId;
227:     }
228: 
229:     /**
230:      * Main function to get the urlparts of urls.
231:      *
232:      * Composes the query by looping thru stored but non processed urls, executes
233:      * the query and adds the (urlpath and urlname) result to the stack.
234:      */
235:     private function _chunkSetPrettyUrlParts() {
236:         // collect stack parameter to get urlpath and urlname
237:         $aStack = array();
238:         foreach ($this->_aStack as $stackId => $item) {
239:             if (!isset($item['urlpath'])) {
240:                 // pretty url is to create
241:                 $aStack[$stackId] = $item;
242:             }
243:         }
244: 
245:         // now, it's time to compose the where clause of the query
246:         $sWhere = '';
247:         foreach ($aStack as $stackId => $item) {
248:             $aP = $item['params'];
249:             if ((int) mr_arrayValue($aP, 'idart') > 0) {
250:                 $sWhere .= '(al.idart = ' . $aP['idart'] . ' AND al.idlang = ' . $aP['lang'] . ') OR ';
251:             } elseif ((int) mr_arrayValue($aP, 'idartlang') > 0) {
252:                 $sWhere .= '(al.idartlang = ' . $aP['idartlang'] . ') OR ';
253:             } elseif ((int) mr_arrayValue($aP, 'idcat') > 0) {
254:                 $sWhere .= '(cl.idcat = ' . $aP['idcat'] . ' AND cl.idlang = ' . $aP['lang'] . ' AND cl.startidartlang = al.idartlang) OR ';
255:             } elseif ((int) mr_arrayValue($aP, 'idcatart') > 0) {
256:                 $sWhere .= '(ca.idcatart = ' . $aP['idcatart'] . ' AND ca.idart = al.idart AND al.idlang = ' . $aP['lang'] . ') OR ';
257:             } elseif ((int) mr_arrayValue($aP, 'idcatlang') > 0) {
258:                 $sWhere .= '(cl.idcatlang = ' . $aP['idcatlang'] . ' AND cl.startidartlang = al.idartlang) OR ';
259:             }
260:         }
261:         if ($sWhere == '') {
262:             return;
263:         }
264:         $sWhere = substr($sWhere, 0, -4);
265:         $sWhere = str_replace(' OR ', " OR \n", $sWhere);
266: 
267:         // compose query and execute it
268:         $sql = <<<SQL
269: SELECT
270:         al.idartlang, al.idart, al.idlang as lang, al.urlname, cl.idcatlang, cl.idcat,
271:         cl.urlpath, ca.idcatart
272: FROM
273:         {$this->_aTab['art_lang']} AS al, {$this->_aTab['cat_lang']} AS cl, {$this->_aTab['cat_art']} AS ca
274: WHERE
275:         al.idart = ca.idart AND
276:         ca.idcat = cl.idcat AND
277:         al.idlang = cl.idlang AND
278:         ($sWhere)
279: SQL;
280:         ModRewriteDebugger::add($sql, 'ModRewriteUrlStack->_chunkSetPrettyUrlParts() $sql');
281: 
282:         $aNewStack = array();
283: 
284:         // create array of fields, which are to reduce step by step from record set below
285:         $aFields = array('', 'idart', 'idartlang', 'idcatart', 'idcat');
286: 
287:         $this->_oDb->query($sql);
288:         while ($this->_oDb->nextRecord()) {
289:             $aRS = $this->_oDb->getRecord();
290: 
291:             // loop thru fields array
292:             foreach ($aFields as $field) {
293:                 if (isset($aRS[$field])) {
294:                     // reduce existing field
295:                     unset($aRS[$field]);
296:                 }
297:                 $rsStackID = $this->_makeStackId($aRS);
298:                 if (isset($aStack[$rsStackID])) {
299:                     // matching stack entry found, add urlpath and urlname to the new stack
300:                     $aNewStack[$rsStackID]['urlpath'] = $aRS['urlpath'];
301:                     $aNewStack[$rsStackID]['urlname'] = $aRS['urlname'];
302:                     break;
303:                 }
304:             }
305:         }
306:         ModRewriteDebugger::add($aNewStack, 'ModRewriteUrlStack->_chunkSetPrettyUrlParts() $aNewStack');
307: 
308:         // merge stack data
309:         $this->_aStack = array_merge($this->_aStack, $aNewStack);
310:     }
311: 
312: }
313: 
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0