1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-18 03:31:54 +02:00

Optimized News listing pages for less SQL lookups. Enhancement: News article listing by author.

This commit is contained in:
Cameron 2016-05-20 15:06:36 -07:00
parent 86143e2dff
commit b23d477f26
6 changed files with 101 additions and 18 deletions

View File

@ -291,6 +291,27 @@ class news_shortcodes extends e_shortcode
return $this->sc_newsauthor($parm);
}
public function sc_news_author_avatar($parm=null)
{
return $this->sc_newsavatar($parm);
}
public function sc_news_author_signature($parm=null)
{
$user = e107::user($this->news_item['user_id']);
if(!empty($user['user_signature']))
{
return e107::getParser()->toHtml($user['user_signature'], true, 'DESCRIPTION');
}
}
public function sc_news_author_items_url($parm=null)
{
return e107::getUrl()->create('news/list/author',array('author'=>$this->news_item['user_name'])); // e_BASE."news.php?author=".$val
}
public function sc_news_summary($parm=null)
{
return $this->sc_newssummary($parm);
@ -369,7 +390,10 @@ class news_shortcodes extends e_shortcode
function sc_newsavatar($parm=null)
{
return vartrue($this->news_item['user_id']) ? e107::getParser()->parseTemplate("{USER_AVATAR=".$this->news_item['user_id']."}",true) : '';
if(!empty($this->news_item['user_id']))
{
return e107::getParser()->toAvatar($this->news_item['user_id'], $parm);
}
}
/**

View File

@ -39,8 +39,9 @@ class core_news_sef_full_url extends eUrlConfig
'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}'),
'Tag/<tag:{secure}>' => array('list/tag', 'allowVars' => array('page'), 'legacyQuery' => 'tag={tag}'),
'Tag/<tag:{secure}>' => array('list/tag', 'allowVars' => array('page'), 'legacyQuery' => 'tag={tag}&page={page}'),
'Author/<author:{secure}>' => array('list/author', 'allowVars' => array('page'), 'legacyQuery' => 'author={author}&page={$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}'),

View File

@ -102,7 +102,14 @@ class core_news_sef_noid_url extends eUrlConfig
case 'tag': // news/tag/xxxx
$r[0] = 'tag';
$r[1] = $params['tag'];
$r[1] = $params['tag'];
if($page) $parm = array('page' => $page);
break;
case 'author': // news/author/xxxx
$r[0] = 'author';
$r[1] = $params['author'];
if($page) $parm = array('page' => $page);
break;
case 'category':
@ -250,6 +257,11 @@ class core_news_sef_noid_url extends eUrlConfig
$this->legacyQueryString = 'tag='.$parts[1];
return 'list/tag';
break;
case 'author': // url: news/author/xxxxx
$this->legacyQueryString = 'author='.$parts[1].'&page='.$page;
return 'list/author';
break;
# force not found
default:

View File

@ -107,9 +107,9 @@ class core_news_sef_url extends eUrlConfig
'View/<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
'Tag/<tag:{secure}>' => array('list/tag', 'allowVars' => array('page'), 'legacyQuery' => 'tag={tag}'),
'Tag/<tag:{secure}>' => array('list/tag', 'allowVars' => array('page'), 'legacyQuery' => 'tag={tag}&page={page}'),
'Author/<author:{secure}>' => array('list/author', 'allowVars' => array('page'), 'legacyQuery' => 'author={author}&page={page}'),
)
);
}

View File

@ -134,8 +134,8 @@ class core_news_url extends eUrlConfig
$url .= 'default.0.'.$page;
}
else
{
$url .= 'list.'.$params['id'].'.'.$page; // 'category_id' would break news_categories_menu.
{
$url .= 'list.'.$params['id'].'.'.$page; // 'category_id' would break news_categories_menu.
}
break;
@ -146,6 +146,10 @@ class core_news_url extends eUrlConfig
case 'tag':
$url .= 'tag='.$params['tag'].'&page='.$page;
break;
case 'author':
$url .= 'author='.$params['author'].'&page='.$page;
break;
case 'short':
$url .= 'cat.'.$params['id'].'.'.$page;

View File

@ -155,18 +155,36 @@ $nobody_regexp = "'(^|,)(".str_replace(",", "|", e_UC_NOBODY).")(,|$)'";
if(vartrue($_GET['tag']) || substr($action,0,4) == 'tag=')
{
$newsRoute = 'news/list/tag';
$newsRoute = 'news/list/tag';
if(!vartrue($_GET['tag']))
{
list($action,$word) = explode("=",$action,2);
list($action,$word) = explode("=",$action,2);
$_GET['tag'] = $word;
unset($word,$tmp);
}
$newsUrlparms['tag'] = $tp->filter($_GET['tag']);
$newsfrom = intval(varset($_GET['page'],0));
}
if(!empty($_GET['author']) || substr($action,0,4) == 'author=')
{
$newsRoute = 'news/list/author';
if(!vartrue($_GET['author']))
{
list($action,$author) = explode("=",$action,2);
$_GET['author'] = $author;
unset($author,$tmp);
}
$newsUrlparms['author'] = $tp->filter($_GET['author']);
$newsfrom = intval(varset($_GET['page'],0));
}
if(E107_DBG_PATH)
{
echo "<div class='alert alert-info'>";
@ -184,7 +202,7 @@ if(E107_DBG_PATH)
//------------------------------------------------------
// Just title and a few other details
if ($action == 'cat' || $action == 'all' || vartrue($_GET['tag']))
if ($action == 'cat' || $action == 'all' || !empty($_GET['tag']) || !empty($_GET['author']))
{ // --> Cache
if($newsCachedPage = checkCache($cacheString))
{
@ -214,9 +232,9 @@ if ($action == 'cat' || $action == 'all' || vartrue($_GET['tag']))
$renTypeQry = " AND (n.news_render_type REGEXP '(^|,)(".implode("|", $pref['news_list_templates']).")(,|$)')";
}
$news_total = $sql->count("news", "(*)", "WHERE news_class REGEXP '".e_CLASS_REGEXP."' AND NOT (news_class REGEXP ".$nobody_regexp.") AND news_start < ".time()." AND (news_end=0 || news_end>".time().")". str_replace("n.news", "news", $renTypeQry));
// $news_total = $sql->count("news", "(*)", "WHERE news_class REGEXP '".e_CLASS_REGEXP."' AND NOT (news_class REGEXP ".$nobody_regexp.") AND news_start < ".time()." AND (news_end=0 || news_end>".time().")". str_replace("n.news", "news", $renTypeQry));
$query = "
SELECT n.*, u.user_id, u.user_name, u.user_customtitle, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon,
SELECT SQL_CALC_FOUND_ROWS n.*, u.user_id, u.user_name, u.user_customtitle, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon,
nc.category_meta_keywords, nc.category_meta_description
FROM #news AS n
LEFT JOIN #user AS u ON n.news_author = u.user_id
@ -235,10 +253,10 @@ if ($action == 'cat' || $action == 'all' || vartrue($_GET['tag']))
elseif ($action == 'cat') // show archive of all news items in a particular category using list-style template.
{
$news_total = $sql->count("news", "(*)", "WHERE news_class REGEXP '".e_CLASS_REGEXP."' AND NOT (news_class REGEXP ".$nobody_regexp.") AND news_start < ".time()." AND (news_end=0 || news_end>".time().") AND news_category=".intval($sub_action));
// $news_total = $sql->count("news", "(*)", "WHERE news_class REGEXP '".e_CLASS_REGEXP."' AND NOT (news_class REGEXP ".$nobody_regexp.") AND news_start < ".time()." AND (news_end=0 || news_end>".time().") AND news_category=".intval($sub_action));
$query = "
SELECT n.*, u.user_id, u.user_name, u.user_customtitle, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon, nc.category_meta_keywords,
SELECT SQL_CALC_FOUND_ROWS n.*, u.user_id, u.user_name, u.user_customtitle, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon, nc.category_meta_keywords,
nc.category_meta_description
FROM #news AS n
LEFT JOIN #user AS u ON n.news_author = u.user_id
@ -254,7 +272,7 @@ if ($action == 'cat' || $action == 'all' || vartrue($_GET['tag']))
$tagsearch = e107::getParser()->filter($_GET['tag']);
$query = "
SELECT n.*, u.user_id, u.user_name, u.user_customtitle, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon, nc.category_meta_keywords,
SELECT SQL_CALC_FOUND_ROWS n.*, u.user_id, u.user_name, u.user_customtitle, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon, nc.category_meta_keywords,
nc.category_meta_description
FROM #news AS n
LEFT JOIN #user AS u ON n.news_author = u.user_id
@ -266,12 +284,33 @@ if ($action == 'cat' || $action == 'all' || vartrue($_GET['tag']))
LIMIT ".intval($newsfrom).",".NEWSLIST_LIMIT;
$category_name = 'Tag: "'.$tagsearch.'"';
}
elseif(!empty($_GET['author']))
{
$authorSearch = e107::getParser()->filter($_GET['author']);
$query = "
SELECT SQL_CALC_FOUND_ROWS n.*, u.user_id, u.user_name, u.user_customtitle, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon, nc.category_meta_keywords,
nc.category_meta_description
FROM #news AS n
LEFT JOIN #user AS u ON n.news_author = u.user_id
LEFT JOIN #news_category AS nc ON n.news_category = nc.category_id
WHERE u.user_name = '".$authorSearch."'
AND n.news_start < ".time()." AND (n.news_end=0 || n.news_end>".time().")
AND n.news_class REGEXP '".e_CLASS_REGEXP."' AND NOT (n.news_class REGEXP ".$nobody_regexp.")
ORDER BY n.news_datestamp DESC
LIMIT ".intval($newsfrom).",".NEWSLIST_LIMIT;
$category_name = 'Author: "'.$authorSearch.'"';
}
$newsList = array();
if($sql->gen($query))
{
$news_total = $sql->foundRows();
$newsList = $sql->db_getList();
$ogImageCount = 0;
foreach($newsList as $row)
@ -392,7 +431,10 @@ if ($action == 'cat' || $action == 'all' || vartrue($_GET['tag']))
$nitems = defined('NEWS_NEXTPREV_NAVCOUNT') ? '&navcount='.NEWS_NEXTPREV_NAVCOUNT : '' ;
$url = rawurlencode(e107::getUrl()->create($newsRoute, $newsUrlparms));
$parms = 'tmpl_prefix='.deftrue('NEWS_NEXTPREV_TMPL', 'default').'&total='.$news_total.'&amount='.$amount.'&current='.$newsfrom.$nitems.'&url='.$url;
// e107::getDebug()->log($newsUrlparms);
$text .= $tp->parseTemplate("{NEXTPREV={$parms}}");
if(varset($template['caption'])) // v2.x