Overview

Packages

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