Simple Machines Community Forum

General Community => Scripting Help => Topic started by: Mayhem30 on October 13, 2013, 02:48:45 AM

Title: Adding PHP snippet increases page query count 10 fold
Post by: Mayhem30 on October 13, 2013, 02:48:45 AM
I've created a php snippet which allows me to add the 'nofollow' tag to only certain external URL's. It's works just fine, but the problem is that after I add the code, the page query count jumps up a 10 fold.

Before  : Page created in 0.11 seconds with 22 queries.
After    : Page created in 0.137 seconds with 202 queries.

Here is how I added the code : (SMF 1.1.18) - Sources/Subs.php

Find:
// Cleanup whitespace.
$message = strtr($message, array('  ' => ' &nbsp;', "\r" => '', "\n" => '<br />', '<br /> ' => '<br />&nbsp;', '&#13;' => "\n"));


Add After :
// NoFollow domains.
$nofollow_domains = array('competitor1.com', 'competitor2.com', 'competitor3.com');

preg_match_all('~<a.*>~isU',$message,$matches);
for ( $i = 0; $i <= sizeof($matches[0]); $i++)
{
if ( !preg_match( '~nofollow~is',$matches[0][$i]) && !preg_match( '~EXAMPLE.COM~',$matches[0][$i]) )
{
foreach($nofollow_domains as $a)
{
if (stripos($matches[0][$i],$a) == true)
{
$result = trim($matches[0][$i],">");
$result .= ' rel="nofollow">';
$message = str_replace($matches[0][$i], $result, $message);
}
}
}
}


The code first makes sure that a 'nofollow' tag does not already exist in the link + that it's not an internal link (EXAMPLE.COM) .. then proceeds to loop the array to see if the link is a competitors (and adds the nofollow tag when there is a match).

Any idea how I can fix this?
Title: Re: Adding PHP snippet increases page query count 10 fold
Post by: emanuele on October 13, 2013, 04:30:09 AM
I can't see any query in the snippet you posted.
Title: Re: Adding PHP snippet increases page query count 10 fold
Post by: Mayhem30 on October 13, 2013, 06:10:29 AM
Quote from: emanuele on October 13, 2013, 04:30:09 AM
I can't see any query in the snippet you posted.

That's exactly my issue - there isn't any queries. However, my snippet is producing those results and I have no idea why.
Title: Re: Adding PHP snippet increases page query count 10 fold
Post by: vbgamer45 on October 13, 2013, 09:04:29 AM
Add
$db_show_debug = true;

To your settings.php and there should be a link that will show you want the queries are at the bottom of your forum.
Title: Re: Adding PHP snippet increases page query count 10 fold
Post by: Arantor on October 13, 2013, 11:18:11 AM
Also check the error log...inserts to that get counted.
Title: Re: Adding PHP snippet increases page query count 10 fold
Post by: Mayhem30 on October 13, 2013, 03:36:35 PM
Ok, the extra queries were all : INSERT INTO yabbse_log_errors

The errors I'm getting is :

Notice: Undefined offset: 3 in /www/example.com/httpdocs/forum/Sources/Subs.php on line 2421
Notice: Undefined offset: 3 in /www/example.com/httpdocs/forum/Sources/Subs.php on line 2425


Those 2 error codes point to these lines :

if ( !preg_match( '~nofollow~is',$matches[0][$i]) && !preg_match( '~EXAMPLE.COM~',$matches[0][$i]) )
if (stripos($matches[0][$i],$a) == true)


Any ideas what I did wrong?
Title: Re: Adding PHP snippet increases page query count 10 fold
Post by: Arantor on October 13, 2013, 03:58:50 PM
You're using sizeof with your <= loop.

Which means it's doing from 0 to 3, i.e. 4 times, across a loop of 3 items.

It should be:

for ( $i = 0; $i < sizeof($matches[0]); $i++)

Or even better:
for ( $i = 0, $len = sizeof($matches[0]); $i < $len; $i++)

to avoid recalculating it every single iteration.
Title: Re: Adding PHP snippet increases page query count 10 fold
Post by: Mayhem30 on October 13, 2013, 04:34:12 PM
Thank you very much, that did the trick!