From 30548dfeed6c6c790b400e7e4b32f754a42f6c7b Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 7 Mar 2015 21:08:22 +0100 Subject: [PATCH] Improving performance measuring for the bbcode parser. --- include/bbcode.php | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/include/bbcode.php b/include/bbcode.php index fa4fa72c7a..7e9dcf3bad 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -852,9 +852,13 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal } while ($oldtext != $Text); } + $a->save_timestamp($stamp1, "parser"); + // Handle attached links or videos $Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed); + $stamp1 = microtime(true); + $Text = str_replace(array("\r","\n"), array('
','
'), $Text); if($preserve_nl) @@ -884,10 +888,14 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal if ($simplehtml == 5) $Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text); + $a->save_timestamp($stamp1, "parser"); + // Perform URL Search if ($tryoembed) $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'tryoembed',$Text); + $stamp1 = microtime(true); + if ($simplehtml == 5) $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url]$1[/url]',$Text); else @@ -896,6 +904,8 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal // Handle Diaspora posts $Text = preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi", 'bb_DiasporaLinks', $Text); + $a->save_timestamp($stamp1, "parser"); + // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text if (!$forplaintext) $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text); @@ -907,6 +917,8 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal if ($tryoembed) $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text); + $stamp1 = microtime(true); + $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $Text); $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $Text); //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text); @@ -1078,8 +1090,13 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal $Text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4))\[\/video\]/ism", '', $Text); $Text = preg_replace("/\[audio\](.*?\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\[\/audio\]/ism", '', $Text); + $a->save_timestamp($stamp1, "parser"); + $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text); $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text); + + $stamp1 = microtime(true); + } else { $Text = preg_replace("/\[video\](.*?)\[\/video\]/", '$1', $Text); @@ -1097,9 +1114,13 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal // Youtube extensions if ($tryoembed) { + $a->save_timestamp($stamp1, "parser"); + $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text); $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text); $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text); + + $stamp1 = microtime(true); } $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); @@ -1113,8 +1134,12 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal 'https://www.youtube.com/watch?v=$1', $Text); if ($tryoembed) { - $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); - $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); + $a->save_timestamp($stamp1, "parser"); + + $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); + $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); + + $stamp1 = microtime(true); } $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text); @@ -1128,10 +1153,13 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal // $Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '', $Text); + $a->save_timestamp($stamp1, "parser"); // oembed tag $Text = oembed_bbcode2html($Text); + $stamp1 = microtime(true); + // Avoid triple linefeeds through oembed $Text = str_replace("


", "

", $Text);