Adding PHP snippet increases page query count 10 fold

Started by Mayhem30, October 13, 2013, 02:48:45 AM

Previous topic - Next topic

Mayhem30

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?

emanuele



Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Mayhem30

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.

vbgamer45

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.
Community Suite for SMF - Grow your forum with SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com - Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Arantor

Also check the error log...inserts to that get counted.
Holder of controversial views, all of which my own.


Mayhem30

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?

Arantor

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.
Holder of controversial views, all of which my own.



Advertisement: