3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* Disclaimer While we make every effort to ensure that this code is fit for its intended purpose, we make no guarantees as to its functionality. CoreTrek AS will accept no responsibility for the loss of data or any other damage or financial loss caused by use of this code. Copyright This programming code is copyright of CoreTrek AS. Permission to run this code is given to approved users of CoreTrek's publishing system CorePublish. This source code may not be copied, modified or otherwise repurposed for use by a third party without the written permission of CoreTrek AS. Contact webmaster@coretrek.com for information. */ /** * Class SeoFrontendUtil is a class for getting seo information on a frontend site. * <p> * You can create your own frontend class to override the default behaviour. * Create a class in your site code, and set the domain config SeoFrontendUtilClass * to the full path of your class. * <p> * Example: SeoFrontendUtilClass config ==> 'site:misc.MySeoFrontendUtil' * * @package cplib.common.seo.util */ class SeoFrontendUtil extends CtBase { public static $canonicalUrlParameterWhitelist = array(); /** * Get the dominant seo entity object from the current page rendering. * * The domination rules: * * 1. If there is a runtime article, this article is used. * 2. If there is a runtime category, this is used. * 3. Fallback to site, if no article of category. * * @return CpSeoObject */ public function getDominantSeoObject() { $log = $this->getCorePublish()->getLog(); $site = $this->getCorePublish()->getSite(); // Caching seoObejct in static variable static $dominantSeoObject = null; if(!isset($dominantSeoObject)) { $log->debug(__FILE__,__LINE__,'Getting dominant SeoObject, setting to static variable cache.'); $runtimeArticle = $site->getRuntimeValue('article'); $runtimeCategory = $site->getRunTimeValue('category'); if($runtimeArticle instanceof Article) { $dominantSeoObject = $runtimeArticle; } else if($runtimeCategory instanceof Category) { $dominantSeoObject = $runtimeCategory; } else { $dominantSeoObject = $site; } } return $dominantSeoObject; } /** * Build page title from the current rendering page SeoObjects. * * <p> * The title is contructed by these rules: * <ol> * <li>If there is a runtime article, and this article has set a custom seo title, use this title.</li> * <li>If the article seo title is the same as article header, we use: "article header - site seo title"</li> * <li>If there is a runtime category, and this category has set a custom seo title, use this title.</li> * <li>If the category seo title is the same as category name, we use "category name - site seo title".</li> * <li>If none of the above, use site seo title.</li> * </ol> * * @return string */ public function getPageSeoTitle() { $site = $this->getCorePublish()->getSite(); $runtimeArticle = $site->getRuntimeValue('article'); $runtimeCategory = $site->getRunTimeValue('category'); $title = $site->getSeoTitle(); if($runtimeArticle instanceof Article) { if($runtimeArticle->getSeoTitle() == $runtimeArticle->getHeader()) { $title = $runtimeArticle->getHeader() . (strlen($title) > 0?' - ' . $title:''); } else { $title = $runtimeArticle->getSeoTitle(); } } else if($runtimeCategory instanceof Category) { // XXX should we check for main article here? if($runtimeCategory->getSeoTitle() == $runtimeCategory->getName()) { if ($runtimeCategory->getCategoryID() == $site->getSiteConfig("frontpage", $site->getActiveLanguage())) { $title = $title .= ' - '.$runtimeCategory->getName(); } else { $title = $runtimeCategory->getName() . (strlen($title) > 0?' - ' . $title:''); } } else { $title = $runtimeCategory->getSeoTitle(); } } return $title; } /** * Get meta description for the current rendering page. * Description is fetched from the dominant SeoObject returned by * getDominantSeoObject method. * * @return string */ public function getPageSeoDescription() { return $this->getDominantSeoObject()->getSeoDescription(); } /** * Get meta keywords for the the current rendering page. * Keywords is fetched from the dominant SeoObject as returned by * getDominantSeoObject method. * * @return string */ public function getPageSeoKeywordString() { return $this->getDominantSeoObject()->getSeoKeywordString(); } /** * Registers a whitelist parameter for the canonical url. * * <p> * See the documentation for getPageCanonicalUrl() for more details * about what white list parameters are * </p> * * @see getPageCanonicalUrl() * @param string $parameter * @param boolean $resetExisting */ public static function registerCanonicalUrlWhitelistParameter($parameter, $resetExisting = false) { if ($resetExisting == true) { self::$canonicalUrlParameterWhitelist = array(); } self::$canonicalUrlParameterWhitelist[] = $parameter; } /** * Returns the canonical url for the current page. * * <p> * A known problem within web pages in general, and dynamic page from publishing * systems in particular, is that there might be several url's to the exact * same page. E.g the two following urls: * <br/> * <pre> * http://www.coretrek.no/category.php?categoryID=123 * http://www.coretrek.no/category123.html * </pre> * will present the exact same content, because the latter url is simply an * alias to the first one, its just another way of reaching the same page. * <br/> * The problem is that search engines do not know this, and they see these * pages as two different pages. E.g. google has functionality that detects * duplicate pages, and marks them as duplicates. * by specifying canonical link, search engines can see which urls are related * and they can detect the main url. * * <p> * See http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html * and http://bugzilla.coretrek.com/show_bug.cgi?id=4176 * </p> * * <p> * You can also register whitelist parameters for the url, by using the * method SeoFrontendUtil::registerCanonicalUrlWhitelistParameter().<br/> * whitelist parameters are page/url parameters which do NOT affect page * contents. <br/> * For example, the following two urls are normally expected to give * different output:<br/> * <pre> * http://site.com/category.php?categoryID=1&page=1 * http://site.com/category.php?categoryID=1&page=2 * </pre> * <br/> * This is because you'd normally expect the page parameter to mean that * there is a new page displaying, having different contents. * <br/> * However, if the two url parameters switch place, it would still be the * same page. The same applies if another url parameter is added, which does * not affect the content of the page: * <br/> * <pre> * http://site.com/category.php?categoryID=1&page=1 * http://site.com/category.php?categoryID=1&page=1&fromurl=htt://www.coretrek.no * </pre> * <br/> * The urls above will probably return the exact same content, because it is * unlikely that the "fromurl" parameter affects the content. This means * that this parameter should be skipped from the canonical url, and this can * be done by registering it as a whitelist parameter. This can be done like * this: <br/> * <code> * SeoFrontendUtil::registerCanonicalUrlWhitelistParameter('fromurl); * </code> * This call has to be done early, before the getPageCanonicalUrl() is called, which * normally happens through the method CorePublishSite.getCommonHeadHtmlElements(), * which is called in include/layouttemplates/head.php. * </p> * <p> * Global whitelist parameters can also be set as a site config named * "seo.canonical.canonicalUrlParameterWhitelist". * </p> * * @return string a string containing the canonical url for the current page */ public function getPageCanonicalUrl() { $site = $this->getCorePublish()->getSite(); $domain = $this->getCorePublish()->getDomain(); $urlutil = $this->getCorePublish()->getUrlUtil(); $sysdb = $this->getCorePublish()->getSystemDatabase(); $db = $this->getCorePublish()->getDb(); $urlutil->setURL(); $parameterKeys = $urlutil->getParameterKeys(); // Whitelist of parameters that are ok to remove $whitelist = array(); $whitelistConfig = $site->getConfig("seo.canonical.canonicalUrlParameterWhitelist"); if ($whitelistConfig instanceof CpParameter) { $whitelist = explode(",", $whitelistConfig->getValueAsString()); } // Append default whitelist parameters $whitelist[] = "printMe"; // merge with static whitelist for early registrations $whitelist = array_merge($whitelist, self::$canonicalUrlParameterWhitelist); $whitelist = array_fill_keys($whitelist, true); $currentUrl = $urlutil->getURL(); $currentEntity = $this->getDominantSeoObject(); if ($currentEntity instanceof Article) { $currentUrl = $domain->getArticleUrlSimple($currentEntity); $whitelist["articleID"] = true; } elseif ($currentEntity instanceof Category) { // If requested category is frontpage category, we always // return the site's html root $frontpageCategoryID = $site->getConfigValue('frontpage'); if($currentEntity->getExternalLinkCategoryID() == $frontpageCategoryID || $currentEntity->getExternalLinkCategoryID() < 1 && $currentEntity->getCategoryId() == $frontpageCategoryID) { // category is front page category $frontpageUrl = $site->getUrl(); if($site->getActiveLanguage() != $site->getDefaultLanguage()) { $fpUrlObject = new CtUrlUtil($frontpageUrl); $fpUrlObject->setParameter('lang', $site->getActiveLanguage()); $frontpageUrl = $fpUrlObject->getUrl(); } $currentUrl = $frontpageUrl; } else { // regular category $currentUrl = $domain->getCategoryUrl($currentEntity); } $whitelist["categoryID"] = true; } $newurl = new CtUrlUtil($currentUrl); // Remove any whitelisted parameters foreach ($parameterKeys as $parameterKey) { if (!isset($whitelist[$parameterKey])) { $newurl->setParameter($parameterKey, $urlutil->getParameter($parameterKey)); } } return $newurl->getURL(); } /** * Returns a link rel element in the format used by getCommonHeadHtmlElements in CorePublish site. * * <p> * This methods tries to create the proper link rel tag to print out for the current * entity (site, category, article) according to the rules given here: * https://developers.google.com/webmasters/smartphone-sites/details * </p> * * @return array */ public function getMultiChannelPageCanonicalHeadElements() { // Global objects $domain = $this->getCorePublish()->getDomain(); $urlutil = $this->getCorePublish()->getUrlUtil(); $sysdb = $this->getCorePublish()->getSystemDatabase(); $db = $this->getCorePublish()->getDb(); // Manager objects $channelManager = $this->getCorePublish()->getChannelManager(); // current runtime objects $currentSite = $this->getCorePublish()->getSite(); $currentCategory= $this->getCorePublish()->getCategory(); $currentChannel = $channelManager->getChannel(); // Base/fallback values $eqSite = $currentSite; $eqChannel = $currentChannel; $currentUrl = null; // Resolve 'the other' site if ($currentChannel->isMobile()) { // If we are on mobile, we should show a canonical url to the // regular web page $eqSiteId = $channelManager->getDefaultSiteforSite($currentSite->getSiteID()); } else { // If we are on the regular site, we should show a canonical url // to the mobile site $eqSiteId = $channelManager->getMobileSiteforSite($currentSite->getSiteID()); } // If 'the other site' has another site ID than the current, we must // resolve other equivalent categories. // If it's the same ID we are using the same tree for both mobile and default web, // and there is no need to handle this at all as the url's will be the same. // In this case we fall back to the regular canonical url if ($eqSiteId != $currentSite->getSiteID()) { if ($eqSiteId > 0) { $tmpSite = $domain->getSiteById($db, $eqSiteId); if ($tmpSite instanceof CorePublishSite) { $eqSite = $tmpSite; $eqChannel = $channelManager->getChannelForSite($eqSite); } } // Get the current entity we are processing $currentEntity = $this->getDominantSeoObject(); // Special handling of front page url if ($currentEntity instanceof Category && $currentEntity->isFrontpage()) { $currentUrl = $eqSite->getUrl(); $eqEntity = $eqSite; } elseif ($currentEntity instanceof CtChannelExposable) { try { // Regular articles and categories $eqEntity = $currentEntity->getChannelEquivalentEntity($eqChannel); if ($eqEntity instanceof CtEntity == false) { // No given entity was found // If the current entity is an article, // check if current article is cross published // to the other site/channel. If it is, just // use the same article if ($currentEntity instanceof Article && $currentEntity->isExposedToChannel($eqChannel)) { $eqEntity = $currentEntity; } } // Try getting the url if ($eqEntity instanceof Article) { $currentUrl = $eqEntity->getUrl($eqSite); } elseif($eqEntity instanceof Category) { $currentUrl = $eqEntity->getUrl(); } } catch (CtNestedException $ex) { $this->getCorePublish()->getLog()->error(__FILE__, __LINE__, "Got exception when resolving url", $ex); $currentUrl = null; } } // If we found a url, return the proper elements if (strlen($currentUrl)) { if ($currentChannel->isMobile()) { // If we are on mobile, return canonical url to the desktop url return array(array ("rel"=>"canonical","href"=>$currentUrl)); } else { // If we are on default, return alternate link AND canonical to // ourselves. $locale = $eqSite->getActiveLanguage(); return array( array ("rel"=>"canonical","href"=>$this->getPageCanonicalUrl()), array( 'rel'=>'alternate', 'media'=>'only screen and (max-width: 640px)', 'type'=>'text/html', 'title'=> $eqEntity->getSeoTitle(), "href"=>$currentUrl ) ); } } } // If we did not find anything, just return the default canonical url return array(array ("rel"=>"canonical","href"=>$this->getPageCanonicalUrl())); } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.40.0000.01519.04
8.3.30.0080.01119.29
8.3.20.0080.00020.41
8.3.10.0060.00320.51
8.3.00.0040.00419.26
8.2.170.0090.01222.96
8.2.160.0070.00720.65
8.2.150.0000.00824.18
8.2.140.0090.00024.66
8.2.130.0040.00426.16
8.2.120.0050.00320.93
8.2.110.0070.00322.30
8.2.100.0090.00317.91
8.2.90.0040.00417.88
8.2.80.0060.00318.16
8.2.70.0080.00018.05
8.2.60.0000.00918.22
8.2.50.0090.00018.10
8.2.40.0000.00818.28
8.2.30.0090.00019.66
8.2.20.0000.00817.87
8.2.10.0090.00018.16
8.2.00.0030.00517.84
8.1.270.0030.00623.79
8.1.260.0080.00026.35
8.1.250.0000.00728.09
8.1.240.0030.00620.82
8.1.230.0040.00719.21
8.1.220.0090.00017.77
8.1.210.0040.00418.77
8.1.200.0030.00717.48
8.1.190.0080.00017.35
8.1.180.0050.00518.10
8.1.170.0040.00418.76
8.1.160.0000.00722.18
8.1.150.0000.00818.98
8.1.140.0040.00417.47
8.1.130.0000.00717.92
8.1.120.0030.00517.55
8.1.110.0090.00017.64
8.1.100.0030.00517.59
8.1.90.0000.00717.55
8.1.80.0000.00817.61
8.1.70.0000.00717.52
8.1.60.0040.00817.58
8.1.50.0060.00317.52
8.1.40.0030.00517.47
8.1.30.0040.00417.69
8.1.20.0030.00517.78
8.1.10.0000.00817.71
8.1.00.0040.00417.50
8.0.300.0060.00319.85
8.0.290.0000.01116.88
8.0.280.0050.00218.61
8.0.270.0080.00017.34
8.0.260.0030.00317.40
8.0.250.0000.00717.19
8.0.240.0040.00417.18
8.0.230.0030.00317.17
8.0.220.0000.00717.09
8.0.210.0040.00417.14
8.0.200.0000.00717.09
8.0.190.0030.00617.06
8.0.180.0040.00417.03
8.0.170.0040.00417.07
8.0.160.0100.00017.08
8.0.150.0050.00217.10
8.0.140.0040.00717.13
8.0.130.0000.00613.54
8.0.120.0040.00417.10
8.0.110.0000.00717.08
8.0.100.0050.00316.93
8.0.90.0050.00316.94
8.0.80.0080.00917.02
8.0.70.0030.00617.13
8.0.60.0030.00517.11
8.0.50.0000.00916.98
8.0.30.0090.01117.19
8.0.20.0120.00617.40
8.0.10.0080.00017.18
8.0.00.0030.01616.91
7.4.330.0000.00615.00
7.4.320.0060.00016.71
7.4.300.0030.00316.57
7.4.290.0050.00216.61
7.4.280.0030.00616.59
7.4.270.0050.00216.68
7.4.260.0000.00816.66
7.4.250.0060.00316.58
7.4.240.0010.00616.62
7.4.230.0030.00316.42
7.4.220.0090.00916.67
7.4.210.0000.01416.68
7.4.200.0030.00316.69
7.4.160.0060.01016.55
7.4.150.0040.01417.40
7.4.140.0120.00917.86
7.4.130.0060.01316.62
7.4.120.0100.00816.71
7.4.110.0070.01116.55
7.4.100.0110.00716.70
7.4.90.0090.00916.77
7.4.80.0070.01119.39
7.4.70.0120.00616.53
7.4.60.0000.01716.68
7.4.50.0060.00316.63
7.4.40.0130.01316.77
7.4.30.0130.00616.70
7.4.00.0100.00714.95
7.3.330.0060.00013.26
7.3.320.0030.00313.47
7.3.310.0030.00316.48
7.3.300.0030.00316.48
7.3.290.0150.00016.43
7.3.280.0080.00916.43
7.3.270.0060.01217.40
7.3.260.0060.01116.66
7.3.250.0080.01016.62
7.3.240.0070.01016.41
7.3.230.0150.00316.52
7.3.210.0090.01016.63
7.3.200.0130.00319.39
7.3.190.0120.00616.43
7.3.180.0160.00016.50
7.3.170.0060.00916.58
7.3.160.0090.00916.59
7.2.330.0100.00716.61
7.2.320.0090.00916.62
7.2.310.0140.01016.64
7.2.300.0030.01516.83
7.2.290.0120.00616.57
7.2.00.0000.01119.39
7.1.100.0000.01117.80
7.1.70.0030.00616.84
7.1.60.0140.01019.61
7.1.50.0000.02016.95
7.1.00.0070.07022.46
7.0.200.0540.00314.70
7.0.140.0070.07021.95
7.0.70.0130.07021.72
7.0.60.0070.06021.79
7.0.50.0100.07722.11
7.0.40.0070.06720.10
7.0.30.0000.09020.19
7.0.20.0100.07320.00
7.0.10.0100.06020.00
7.0.00.0030.08019.91
5.6.280.0070.07021.23
5.6.220.0030.08320.75
5.6.210.0000.08720.82
5.6.200.0030.09321.12
5.6.190.0030.08021.11
5.6.180.0030.08321.19
5.6.170.0100.08021.12
5.6.160.0070.07721.09
5.6.150.0070.07321.11
5.6.140.0000.09321.16
5.6.130.0100.07721.18
5.6.120.0070.05721.01
5.6.110.0030.05721.00
5.6.100.0000.08721.01
5.6.90.0070.07721.08
5.6.80.0100.06720.50
5.6.70.0100.04320.46
5.6.60.0070.07720.58
5.6.50.0070.04320.54
5.6.40.0030.04720.38
5.6.30.0100.07020.48
5.6.20.0100.07720.36
5.6.10.0070.08020.46
5.6.00.0070.08020.38
5.5.360.0100.07320.43
5.5.350.0070.05020.52
5.5.340.0070.06021.00
5.5.330.0070.06720.97
5.5.320.0100.04720.99
5.5.310.0030.08320.98
5.5.300.0070.08321.00
5.5.290.0100.07720.87
5.5.280.0030.08020.87
5.5.270.0030.08320.71
5.5.260.0100.07020.98
5.5.250.0100.07720.77
5.5.240.0070.05320.23
5.5.230.0100.07020.34
5.5.220.0070.06020.26
5.5.210.0070.08020.21
5.5.200.0070.04320.32
5.5.190.0030.08320.36
5.5.180.0030.06720.20
5.5.160.0000.08320.35
5.5.150.0000.08320.29
5.5.140.0070.08020.25
5.5.130.0070.07720.27
5.5.120.0030.08320.24
5.5.110.0130.07020.21
5.5.100.0030.07720.18
5.5.90.0070.06720.18
5.5.80.0070.08020.24
5.5.70.0000.05020.14
5.5.60.0100.07020.22
5.5.50.0100.03720.14
5.5.40.0030.03720.13
5.5.30.0000.04020.17
5.5.20.0030.04020.13
5.5.10.0000.04320.21
5.5.00.0000.04020.11
5.4.450.0030.06019.46
5.4.440.0070.05319.49
5.4.430.0030.08319.48
5.4.420.0070.08019.41
5.4.410.0070.06719.25
5.4.400.0070.07018.89
5.4.390.0070.05318.91
5.4.380.0000.07319.09
5.4.370.0030.05018.87
5.4.360.0030.07719.05
5.4.350.0030.06019.23
5.4.340.0030.05719.13
5.4.320.0030.06018.87
5.4.310.0030.07319.20
5.4.300.0100.04319.03
5.4.290.0100.07319.06
5.4.280.0000.07319.05
5.4.270.0000.06718.90
5.4.260.0070.07319.14
5.4.250.0030.08019.26
5.4.240.0000.07719.21
5.4.230.0100.07019.19
5.4.220.0030.05019.03
5.4.210.0030.05318.87
5.4.200.0070.06019.22
5.4.190.0030.04018.96
5.4.180.0000.04018.86
5.4.170.0070.04719.07
5.4.160.0000.04018.86
5.4.150.0030.03718.89
5.4.140.0070.03016.32
5.4.130.0030.03316.48
5.4.120.0000.05016.40
5.4.110.0000.03716.45
5.4.100.0000.04016.39
5.4.90.0100.03016.45
5.4.80.0000.03716.50
5.4.70.0070.03016.48
5.4.60.0000.03716.46
5.4.50.0030.03316.54
5.4.40.0000.03716.36
5.4.30.0000.06716.49
5.4.20.0130.03016.50
5.4.10.0000.03716.52
5.4.00.0030.03315.83

preferences:
42.37 ms | 400 KiB | 5 Q