1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-12 09:34:54 +02:00

- Important URL assembling feature, all routes have now safe fallback option

- News configs updates and refactoring, introducing new config, various bugfixes
- News DB changes: news_sef and category_sef fields, titles no more used in URL assembling
- Aliases now not able to match existing URL module
- New URL setting - choose type of SEF string when auto-extracted from titles (implemented in News and Custom pages, leave SEF string empty to see it in action)
- Overall system stability bugfixes
This commit is contained in:
secretr
2011-12-10 00:00:15 +00:00
parent 4d7e06ec70
commit 0d0fa1e2ca
15 changed files with 456 additions and 338 deletions

View File

@@ -0,0 +1,125 @@
<?php
/**
* Copyright (C) e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
*
* $Id$
*
* Full SEF URLs support and the most risky one, almost the same as sef_noid, just working with rules only
*/
class core_news_sef_full_url extends eUrlConfig
{
public function config()
{
return array(
'config' => array(
'allowMain' => true,
'legacy' => '{e_BASE}news.php',
'format' => 'path',
'defaultRoute' => 'list/items',
'urlSuffix' => '.e107', // just for fun!
'allowVars' => false,
'matchValue' => 'empty',
'mapVars' => array(
'news_id' => 'id',
'news_sef' => 'name',
),
),
'rules' => array(
'/' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ),
'Category' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ),
'Category/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_sef' => 'name'), 'legacyQuery' => 'list.{name}.{page}', 'parseCallback' => 'categoryIdByTitle'),
'Short/<name:{sefsecure}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_sef' => 'name'), 'legacyQuery' => 'cat.{name}.{page}', 'parseCallback' => 'categoryIdByTitle'),
'Short/<id:{number}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id'), 'legacyQuery' => 'cat.{id}.{page}'),
'Day/<id:{number}>' => array('list/day', 'allowVars' => array('page'), 'legacyQuery' => 'day.{id}.{page}'),
'Month/<id:{number}>' => array('list/month', 'allowVars' => array('page'), 'legacyQuery' => 'month.{id}.{page}'),
'<category:{sefsecure}>/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('category_sef' => 'category', 'news_sef' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
'<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_id' => 'id', 'news_sef' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
'<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
)
);
}
/**
* Admin callback
* Language file not loaded as all language data is inside the lan_eurl.php (loaded by default on administration URL page)
*/
public function admin()
{
// static may be used for performance
static $admin = array(
'labels' => array(
'name' => LAN_EURL_CORE_NEWS, // Module name
'label' => LAN_EURL_NEWS_REWRITEF_LABEL, // Current profile name
'description' => LAN_EURL_NEWS_REWRITEF_DESCR, //
),
'form' => array(), // Under construction - additional configuration options
'callbacks' => array(), // Under construction - could be used for e.g. URL generator functionallity
);
return $admin;
}
### CUSTOM METHODS ###
/**
* view/item by name callback
* @param eRequest $request
*/
public function itemIdByTitle(eRequest $request)
{
$name = $request->getRequestParam('name');
if(($id = $request->getRequestParam('id')))
{
$request->setRequestParam('name', $id);
return;
}
elseif(!$name) return;
elseif(is_numeric($name))
{
return;
}
$sql = e107::getDb('url');
$name = e107::getParser()->toDB($name);
if($sql->db_Select('news', 'news_id', "news_sef='{$name}'")) // TODO - it'll be news_sef (new) field
{
$name = $sql->db_Fetch();
$request->setRequestParam('name', $name['news_id']);
}
else $request->setRequestParam('name', 0);
}
/**
* list/items by name callback
* @param eRequest $request
*/
public function categoryIdByTitle(eRequest $request)
{
$name = $request->getRequestParam('name');
if(($id = $request->getRequestParam('id')))
{
$request->setRequestParam('name', $id);
return;
}
elseif(!$name) return;
elseif(is_numeric($name))
{
return;
}
$sql = e107::getDb('url');
$id = e107::getParser()->toDB($name);
if($sql->db_Select('news_category', 'category_id', "category_sef='{$name}'")) // TODO - it'll be category_sef (new) field
{
$name = $sql->db_Fetch();
$request->setRequestParam('name', $name['category_id']);
}
else $request->setRequestParam('name', 0);
}
}

View File

@@ -4,9 +4,9 @@
*
* $Id$
*
* Mod rewrite & SEF URLs support, example of manually (rules-less) created/parsed urls
* SEF URLs support, example of manually (rules-less) created/parsed urls
*/
class core_news_rewrite_url extends eUrlConfig
class core_news_sef_noid_url extends eUrlConfig
{
public function config()
{
@@ -63,7 +63,7 @@ class core_news_rewrite_url extends eUrlConfig
{
## news are passing array as it is retrieved from the DB, map vars to proper values
if(isset($params['news_id']) && !empty($params['news_id'])) $params['id'] = $params['news_id'];
if(isset($params['news_title']) && !empty($params['news_title'])) $params['id'] = $params['news_title']; // TODO - news_sef
if(isset($params['news_sef']) && !empty($params['news_sef'])) $params['id'] = $params['news_sef']; // TODO - news_sef
switch ($route[1])
{
@@ -80,7 +80,7 @@ class core_news_rewrite_url extends eUrlConfig
{
## news are passing array as it is retrieved from the DB, map vars to proper values
if(isset($params['category_id']) && !empty($params['category_id'])) $params['id'] = $params['category_id'];
if(isset($params['category_name']) && !empty($params['category_name'])) $params['name'] = $params['category_name']; // TODO - news_sef
if(isset($params['category_sef']) && !empty($params['category_sef'])) $params['name'] = $params['category_sef']; // TODO - news_sef
switch ($route[1])
{
@@ -162,8 +162,8 @@ class core_news_rewrite_url extends eUrlConfig
}
$parts = explode('/', $pathInfo, 2);
switch (strtolower($parts[0]))
$parts[0] = strtolower($parts[0]);
switch ($parts[0])
{
# map to list.xxx.xxx
case 'short':
@@ -247,13 +247,13 @@ class core_news_rewrite_url extends eUrlConfig
### CUSTOM METHODS ###
//retrieve news_id by Title (XXX - news_sef column, equals to news_title if not set explicit)
//retrieve news_id by news_sef (
public function itemIdByTitle($id)
{
$sql = e107::getDb('url');
$tp = e107::getParser();
$id = $tp->toDB($id);
if($sql->db_Select('news', 'news_id', "news_title='{$id}'")) // TODO - it'll be news_url (new) field
if($sql->db_Select('news', 'news_id', "news_sef='{$id}'"))
{
$id = $sql->db_Fetch();
return $id['news_id'];
@@ -261,13 +261,13 @@ class core_news_rewrite_url extends eUrlConfig
return false;
}
//retrieve category_id by Title (XXX - category_sef column, equals to category_name if not set explicit)
//retrieve category_id by Title (XXX - category_sef column, equals to category_sef if not set explicit)
public function categoryIdByTitle($id)
{
$sql = e107::getDb('url');
$tp = e107::getParser();
$id = $tp->toDB($id);
if($sql->db_Select('news_category', 'category_id', "category_name='{$id}'")) // TODO - it'll be category_url (new) field
if($sql->db_Select('news_category', 'category_id', "category_sef='{$id}'"))
{
$id = $sql->db_Fetch();
return $id['category_id'];

View File

@@ -4,12 +4,12 @@
*
* $Id$
*
* Mod rewrite & SEF URLs support, managed entirely by the core router (rules)
* Most balanced config - performance and friendly URLs
* It contains a lot of examples (mostly complex), use them to play around and learn things :/
* Generally, things are much more simpler...
*
*/
class core_news_rewrite_extended_url extends eUrlConfig
class core_news_sef_url extends eUrlConfig
{
public function config()
{
@@ -24,9 +24,12 @@ class core_news_rewrite_extended_url extends eUrlConfig
### default vars mapping (create URL), override per rule is allowed
'mapVars' => array(
'news_id' => 'id',
'news_title' => 'name',
'news_sef' => 'name',
),
### match will only check if parameter is empty to invalidate the assembling vs current rule
'matchValue' => 'empty',
### Numerical array containing allowed vars by default (create URL, used for legacyQuery parsing in parse routine as well),
### false means - disallow all vars beside those required by the rules
### Override per rule is allowed
@@ -62,23 +65,23 @@ class core_news_rewrite_extended_url extends eUrlConfig
## URL with ID and Title - no DB call, balanced performance, name optional
## Demonstrating the usage of custom user defined regex template defined above - 'testIt'
'Category/<id:{testIt}>/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_name' => 'name'), 'legacyQuery' => 'list.{id}.{page}'),
'Category/<id:{testIt}>/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_sef' => 'name'), 'legacyQuery' => 'list.{id}.{page}'),
## URL with Title only - prettiest and slowest! Example with direct regex - no templates
//'Category/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_name' => 'name'), 'legacyQuery' => 'list.{name}.{page}', 'parseCallback' => 'categoryIdByTitle'),
//'Category/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_sef' => 'name'), 'legacyQuery' => 'list.{name}.{page}', 'parseCallback' => 'categoryIdByTitle'),
## URL with ID only - best performance, fallback when no sef name provided
'Category/<id:{number}>' => array('list/category', 'allowVars' => array('page'), 'legacyQuery' => 'list.{id}.{page}', 'mapVars' => array('category_id' => 'id')),
### View item requested by id or string, if you remove the catch ALL example, uncomment at least on row from this block
### leading category name example - could be enabled together with the next example to handle creating of URLs without knowing the category title
// 'View/<category:[\w\pL.\-\s]+>/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// to be noted here - value 'name' is replaced by item id within the callback method; TODO replace news_title with news_sef field
// 'View/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'news_id' => 'id'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// 'View/<category:[\w\pL.\-\s]+>/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_sef' => 'name', 'category_sef' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// to be noted here - value 'name' is replaced by item id within the callback method; TODO replace news_sef with news_sef field
// 'View/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_sef' => 'name', 'news_id' => 'id'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// 'View/<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
## URL with ID and Title - no DB call, balanced performance!
'Short/<id:{number}>/<name:{sefsecure}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_name' => 'name'), 'legacyQuery' => 'cat.{id}.{page}'),
'Short/<id:{number}>/<name:{sefsecure}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_sef' => 'name'), 'legacyQuery' => 'cat.{id}.{page}'),
## fallback when name is not provided
'Short/<id:{number}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id'), 'legacyQuery' => 'cat.{id}.{page}'),
@@ -90,10 +93,13 @@ class core_news_rewrite_extended_url extends eUrlConfig
### View news item - kinda catch all - very bad performance when News is chosen as default namespace - two additional DB queries on every site call!
## Leading category name - uncomment to enable
'<category:{sefsecure}>/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('category_name' => 'category', 'news_title' => 'name', 'news_id' => 'id'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
//'<category:{sefsecure}>/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('category_sef' => 'category', 'news_sef' => 'name', 'news_id' => 'id'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
'View/<id:{number}>/<category:{sefsecure}>/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('category_sef' => 'category', 'news_sef' => 'name', 'news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
// Base location as item view - fallback if category sef is missing
'<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_id' => 'id', 'news_title' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
//'<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_id' => 'id', 'news_sef' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// fallback if news sef is missing
'View/<id:{number}>/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_id' => 'id', 'news_sef' => 'name'), 'legacyQuery' => 'extend.{id}'),
'View/<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
)
@@ -140,7 +146,7 @@ class core_news_rewrite_extended_url extends eUrlConfig
* view/item by name callback
* @param eRequest $request
*/
public function itemIdByTitle(eRequest $request)
/*public function itemIdByTitle(eRequest $request)
{
$name = $request->getRequestParam('name');
if(($id = $request->getRequestParam('id')))
@@ -156,19 +162,19 @@ class core_news_rewrite_extended_url extends eUrlConfig
$sql = e107::getDb('url');
$name = e107::getParser()->toDB($name);
if($sql->db_Select('news', 'news_id', "news_title='{$name}'")) // TODO - it'll be news_sef (new) field
if($sql->db_Select('news', 'news_id', "news_sef='{$name}'")) // TODO - it'll be news_sef (new) field
{
$name = $sql->db_Fetch();
$request->setRequestParam('name', $name['news_id']);
}
else $request->setRequestParam('name', 0);
}
}*/
/**
* list/items by name callback
* @param eRequest $request
*/
public function categoryIdByTitle(eRequest $request)
/*public function categoryIdByTitle(eRequest $request)
{
$name = $request->getRequestParam('name');
if(($id = $request->getRequestParam('id')))
@@ -184,11 +190,11 @@ class core_news_rewrite_extended_url extends eUrlConfig
$sql = e107::getDb('url');
$id = e107::getParser()->toDB($name);
if($sql->db_Select('news_category', 'category_id', "category_name='{$name}'")) // TODO - it'll be category_sef (new) field
if($sql->db_Select('news_category', 'category_id', "category_sef='{$name}'")) // TODO - it'll be category_sef (new) field
{
$name = $sql->db_Fetch();
$request->setRequestParam('name', $name['category_id']);
}
else $request->setRequestParam('name', 0);
}
}*/
}

View File

@@ -23,38 +23,44 @@ class core_news_url extends eUrlConfig
'defaultRoute' => 'list/new',// [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'errorRoute' => '', // [optional] default empty; route (no leading module) used when module is found but no inner route is matched, leave empty to force error 404 page
'urlSuffix' => '', // [optional] default empty; string to append to the URL (e.g. .html), not used when format is 'get' or legacy non-empty
### [optional] used only when assembling URLs via rules();
### if 'empty' - check if the required parameter is empty (results in assemble fail),
### if 1 or true - it uses the route pattern to match every parameter - EXTREMELY SLOW, be warned
'matchValue' => false,
### [optional] vars mapping (create URL routine), override per rule is allowed
### Keys of this array will be used as a map for finding values from the provided parameters array.
### Those values will be assigned to new keys - corresponding values of mapVars array
### It gives extremely flexibility when used with allowVars. For example we pass $news item array as
### it's retrieved from the DB, with no modifications. This gives us the freedom to create any variations of news
### URLs using the DB data with a single line URL rule. Another aspect of this feature is the simplified code
### for URL assembling - we just do eRouter::create($theRoute, $newsDbArray)
### Not used when in selfCreate mod (create url)
'mapVars' => array(
//'news_id' => 'id',
//'news_sef' => 'name',
),
### [optional] allowed vars definition (create URL routine), override per rule is allowed
### This numerical array serves as a filter for passed vars when creating URLs
### Everything outside this scope is ignored while assembling URLs. Exception are route variables.
### For example: when <id:[\d]+> is present in the route string, there is no need to extra allow 'id'
### To disallow everything but route variables, set allowVars to false
### When format is get, false value will disallow everything (no params) and default preserved variables
### will be extracted from mapVars (if available)
### Default value is empty array
### Not used when in selfCreate mod (create url)
'allowVars' => array(/*'page', 'name'*/),
### Those are regex templates, allowing us to avoid the repeating regex patterns writing in your rules.
### varTemplates are merged with the core predefined templates. Full list with core regex templates and examples can be found
### in rewrite_extended news URL config
'varTemplates' => array(/*'testIt' => '[\d]+'*/),
),
'rules' => array(), // rule set array - can't be used with format 'get' and noSingleEntry true
### [optional] vars mapping (create URL routine), override per rule is allowed
### Keys of this array will be used as a map for finding values from the provided parameters array.
### Those values will be assigned to new keys - corresponding values of mapVars array
### It gives extremely flexibility when used with allowVars. For example we pass $news item array as
### it's retrieved from the DB, with no modifications. This gives us the freedom to create any variations of news
### URLs using the DB data with a single line URL rule. Another aspect of this feature is the simplified code
### for URL assembling - we just do eRouter::create($theRoute, $newsDbArray)
### Not used when in selfCreate mod (create url)
'mapVars' => array(
//'news_id' => 'id',
//'news_sef' => 'name',
),
### [optional] allowed vars definition (create URL routine), override per rule is allowed
### This numerical array serves as a filter for passed vars when creating URLs
### Everything outside this scope is ignored while assembling URLs. Exception are route variables.
### For example: when <id:[\d]+> is present in the route string, there is no need to extra allow 'id'
### To disallow everything but route variables, set allowVars to false
### When format is get, false value will disallow everything (no params) and default preserved variables
### will be extracted from mapVars (if available)
### Default value is empty array
### Not used when in selfCreate mod (create url)
'allowVars' => array(/*'page', 'name'*/),
### Those are regex templates, allowing us to avoid the repeating regex patterns writing in your rules.
### varTemplates are merged with the core predefined templates. Full list with core regex templates and examples can be found
### in rewrite_extended news URL config
'varTemplates' => array(/*'testIt' => '[\d]+'*/),
);
}
@@ -89,8 +95,8 @@ class core_news_url extends eUrlConfig
## news are passing array as it is retrieved from the DB, map vars to proper values
if(isset($params['news_id']) && !empty($params['news_id'])) $params['id'] = $params['news_id'];
if(isset($params['news_sef']) && !empty($params['news_sef'])) $params['id'] = $params['news_sef'];
if(isset($params['category_name']) && !empty($params['category_name'])) $params['category'] = $params['category_name'];
//if(isset($params['news_sef']) && !empty($params['news_sef'])) $params['id'] = $params['news_sef'];
//if(isset($params['category_sef']) && !empty($params['category_sef'])) $params['category'] = $params['category_sef'];
$url = 'news.php?';
if('--FROM--' != vartrue($params['page'])) $page = varset($params['page']) ? intval($params['page']) : '0';