Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Updated Article Break function  (Read 1122 times)

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Updated Article Break function
« on: August 22, 2010, 09:33:38 AM »

Ok.  This modification lightens the code for the [break] function in sNews.  
Final version of this idea: http://snewscms.com/forum/index.php?topic=8324.msg64913#msg64913

In theory, it should lighten the total PHP memory count when using function articles.  Meaning, currently the whole article is pulled from the database and then PHP parses the [break].  This version has the parsing in the database...

0. BACKUP!!!!

1. Add a new function.  I have mine before the articles function...
Code: [Select]
function fix_html( $text ) {
if (substr_count ($text, '&') > 0) {
$text = str_replace('&', '&', str_replace('&', '&', $text));
}
if (preg_match('<p>',$text)) {
if (substr_count ($text, '<p>') > substr_count ($text, '</p>')) {
$text .='</p>';
}
}
return str_replace('[break]', '', $text);
}

2. In function articles(), find the last line and add the remainder of the code, before it:
Code: [Select]
$summary = $_ID
? ''
: 'CASE
WHEN text LIKE "%[break]%"
THEN SUBSTRING_INDEX( text, "[break]", 1 )
ELSE text
END as ';
$has_break = ', CASE
       WHEN text LIKE "%[break]%" THEN 1
       ELSE ""
     END as break_exists';

// get the rows for category

3.  Same area... add the $summary and $has_break cases to the queries:
Code: [Select]
// get the rows for category
if ($_catID) {
$query_articles = 'SELECT
a.id AS aid,title,a.seftitle AS asef,'. $summary .'text,a.date,
a.displaytitle,a.displayinfo,a.commentable,a.visible '. $has_break .'
FROM '._PRE.'articles'.' AS a
WHERE position = 1
AND a.published =1
AND category = '.$_catID.$visible.'
ORDER BY artorder ASC,date DESC
LIMIT '.($currentPage - 1) * $article_limit.','.$article_limit;
} else {
$query_articles = 'SELECT
a.id AS aid,title,a.seftitle AS asef,'. $summary .'text,a.date,
displaytitle,displayinfo,commentable,a.visible,
c.name AS name,c.seftitle AS csef,
x.name AS xname,x.seftitle AS xsef
'. $has_break .'

4. further down, find the first line and add the second:
Code: [Select]
$text = stripslashes($r['text']);
$text = fix_html($text);

5. Comment out this block in function articles:
Code: [Select]
if (!empty($currentPage)) {
$short_display = strpos($text, '[break]');
$shorten = $short_display == 0 ? 9999000 : $short_display;
} else {
$shorten = 9999000;
}

6. Further down.... Find the first line, comment it out and add the second:
Code: [Select]
file_include(str_replace('[break]', '',$text), $shorten);
file_include($text);

7.  Find the first line in articles(), comment it out and add the second:
Code: [Select]
case ($tag == 'readmore' && strlen($r['text']) > $shorten):
case ($tag == 'readmore' && $r['break_exists']):

8. In function extras, find the first line, comment it out and add the second:
Code: [Select]
file_include($r['text'], 9999000);
file_include($r['text']);

9. Function file_include.  Find the last line noted, and follow the example of the previous code:
Code: [Select]
function file_include($text) { //, $shorten) {
/*
$fulltext = substr($text, 0, $shorten);
if (substr_count ($fulltext, '&') > 0) {
$fulltext = str_replace('&', '&amp;', str_replace('&amp;', '&', $fulltext));
}
if ($shorten < 9999000 && preg_match('<p>',$fulltext)) {
if (substr_count ($fulltext, '<p>') > substr_count ($fulltext, '</p>')) {
$fulltext .='</p>';
}
}
*/
// New line
$fulltext = $text;

    $ins = strpos($fulltext, '[/func]');

10. For the RSS Feed:  Find the last line, comment it out and add the previous code:
 
Code: [Select]
// ADDED
  $summary = $rss_item == 'rss-articles'
  ? ', CASE
  WHEN text LIKE "%[break]%" THEN SUBSTRING_INDEX( text, "[break]", 1 )
END as summary'
  : '';
// MODDED
  $result = mysql_query("SELECT *".$summary." FROM $query DESC LIMIT $limit");
  $result = mysql_query("SELECT * FROM $query DESC LIMIT $limit");

11. Later down in the function, find the last line, comment it out and add the previous:
Code: [Select]
$text = $r['summary'] ? $r['summary'] : $r['text'];
$text = fix_html($text);
$text = $r['text'];
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Updated Article Break function
« Reply #1 on: February 02, 2012, 04:35:37 PM »

Hello nukpana
I would like to try this mod, but I have hard time to understand how to install,

for example, what do you mean here?   ::)
9. Function file_include.  Find the last line noted, and follow the example of the previous code:

If you don't mind, is easy for you to write more clear the steps, and (maybe) the code!  :
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Updated Article Break function
« Reply #2 on: February 02, 2012, 09:04:34 PM »

for example, what do you mean here?   ::)
9. Function file_include.  Find the last line noted, and follow the example of the previous code:

What it means is:
1) Find this line in the function -  $ins = strpos($fulltext, '[/func]');
..... it is the last line shown in the code block of step 9.

2) Above it, insert:
   // New line
   $fulltext = $text;


3) And everything else above it.... you need to comment it out so it will be disabled...
   - with /* after the first line of the function - function file_include($text) { //, $shorten) {
   - and */ above the part you added in the step above this one.

Does this help?  8)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Updated Article Break function
« Reply #3 on: February 02, 2012, 11:05:03 PM »

Probably yes, seems like a puzzle, as I was looking to the previous code (8.) lol
obviously my English is not good, I will try to install..  :)

Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Updated Article Break function
« Reply #4 on: February 03, 2012, 11:12:08 AM »

I have Warning: Missing argument 2 for file_include()

where is called from step6
file_include($text);

I change to
file_include($text, $shorten);

seems like working, I have to test futher :)
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Updated Article Break function
« Reply #5 on: March 17, 2013, 10:49:38 AM »

Hello!!
I try to use this mod with [ADDON] showArticles
http://snewscms.com/forum/index.php/topic,10371.msg68728.html

how to change this to make it work?
Code: [Select]
file_include(str_replace('[break]', '',str_replace('&', '&amp;', $text)), $shorten);
Logged