public function createPageId($sefUrl, $nonSefUrl) { $shURL = ''; if (!$this->_mustCreatePageid($nonSefUrl)) { return $shURL; } jimport('joomla.utilities.string'); $sefUrl = JString::ltrim($sefUrl, '/'); try { if (!empty($sefUrl)) { // check that we don't already have a shURL for the same SEF url, even if non-sef differ $result = (int) ShlDbHelper::count('#__sh404sef_urls', '*', $this->_db->quoteName('oldurl') . ' = ? and ' . $this->_db->quoteName('newurl') . ' <> ?', array($sefUrl, '')); if (!empty($result) && $result > 1) { // we already have a SEF URL, so we must already have a shURL as well return $shURL; } } // check this nonsef url does not already have a shURL $existingShurl = ShlDbHelper::selectResult('#__sh404sef_pageids', 'pageid', array('newurl' => $nonSefUrl)); // there already is a shurl for the same non-sef if (!empty($existingShurl)) { return $existingShurl; } // if we don't already have a shURL, create the new one $shURL = $this->_buildPageId(); if (!empty($shURL)) { // insert in db ShlDbHelper::insert('#__sh404sef_pageids', array('newurl' => $nonSefUrl, 'pageid' => $shURL, 'type' => Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_PAGEID, 'hits' => 0)); } } catch (Exception $e) { ShlSystem_Log::error('sh404sef', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, $e->getMessage()); } // don't need to add the pageid to cache, won't be needed when building up the page, //only when decoding incoming url return $shURL; }
/** * * @param string $url * @param array $title * @param string $task * @param int $limit * @param int $limitstart * @return sefurl */ public static function sefGetLocation($nonSefUrl, &$title, $task = null, $limit = null, $limitstart = null, $langParam = null, $showall = null, $suppressPagination = false) { try { $shPageInfo =& Sh404sefFactory::getPageInfo(); $sefConfig =& Sh404sefFactory::getConfig(); $lang = empty($langParam) ? $shPageInfo->currentLanguageTag : $langParam; // shumisha : try to avoid duplicate content on multilingual sites by always adding &lang=xx to url (stored in DB). // warning : must add &lang=xx only if it does not exists already if (!strpos($nonSefUrl, 'lang=')) { $shSepString = substr($nonSefUrl, -9) == 'index.php' ? '?' : '&'; $nonSefUrl .= $shSepString . 'lang=' . shGetIsoCodeFromName($lang); } // make sure url is consistent $nonSefUrl = str_replace('&', '&', $nonSefUrl); // detect multipage homepage $shMultiPageHomePageFlag = shIsHomepage($nonSefUrl); // get all the slugs ready for being urls bits $tempSefUrl = array(); foreach ($title as $titlestring) { $decodedTitletring = urldecode($titlestring); $tempSefUrl[] = titleToLocation($decodedTitletring); } // now build the URL $tempSefUrl = implode("/", $tempSefUrl); // remove duplicate / $tempSefUrl = ShlSystem_Strings::pr('/\\/{2,}/u', '/', $tempSefUrl); // and truncate to max length, according to param $tempSefUrl = JString::substr($tempSefUrl, 0, sh404SEF_MAX_SEF_URL_LENGTH); // trim to max length V 1.2.4.t // if URL is empty, and unless this is a paginated home page, or home page in non-default language, stop there if (empty($tempSefUrl)) { if ((!shIsMultilingual() || shIsMultilingual() && shIsDefaultlang($lang)) && !$sefConfig->addFile && !$shMultiPageHomePageFlag) { // return ''; } // if location is empty, and not multilingual site, or multilingual, but this is default language, then there is nothing to add to url } // we have a valid SEF url, built with the data ($title) sent // by plugin. Now we want to check if it's already in the db // and add it if not // first, we search the memory cache for the non-sef url // as it is faster than looking up the db $finalSefUrl = ''; $sefUrlType = Sh404sefHelperCache::getSefUrlFromCache($nonSefUrl, $finalSefUrl); // if non-sef was not found in cache - or found, but it was a 404 last time we saw it - // we should continue and try adding it if ($sefUrlType == sh404SEF_URLTYPE_NONE || $sefUrlType == sh404SEF_URLTYPE_404) { $finalSefUrl = false; // non-sef was not found in cache, let's look up the database if ($sefUrlType == sh404SEF_URLTYPE_NONE) { $finalSefUrl = ShlDbHelper::selectResult('#__sh404sef_urls', 'oldurl', array('newurl' => $nonSefUrl)); } // we found the sef url in database, we're done if (!empty($finalSefUrl)) { return $finalSefUrl; } // the non-sef url is not in memory cache, nor in database // that's a new one, we need to finalize its sef (add pagination and language information) // After finalizing it, we'll also check that sef is not in the db // as it can already be there, associated with another non-sef (ie: a duplicate) // Either way we'll add it in the db, but mark it as a duplicate if needed // add pagination information, unless we were instructed by extension plugin not to // find if we should separate pagination info from sef with a / or not if (!empty($tempSefUrl)) { $shSeparator = JString::substr($tempSefUrl, -1) == '/' ? '' : '/'; } else { $shSeparator = ''; } $finalSefUrl = $suppressPagination ? $tempSefUrl : shAddPaginationInfo($limit, $limitstart, $showall, 1, $nonSefUrl, $tempSefUrl, $shSeparator); // v 1.2.4.t // if home page, we don't record anything, just return "home page" if ($shMultiPageHomePageFlag && '/' . $finalSefUrl == $tempSefUrl && (!shIsMultilingual() || shIsMultilingual() && shIsDefaultLang($lang))) { // but this is default language // this is start page of multipage homepage, return home or forced home if (!empty($sefConfig->shForcedHomePage)) { return str_replace($shPageInfo->getDefaultFrontLiveSite() . '/', '', $sefConfig->shForcedHomePage); } else { return ''; } } // add language information // first, remove languages in non-sef, to see if we're on homepage // as handling is sligthly different for homepage $v1 = shCleanUpLang($nonSefUrl); $v2 = shCleanUpLang($shPageInfo->homeLink); if ($v1 == $v2 || $v1 == 'index.php') { // check if this is homepage if (shIsMultilingual() && !shIsDefaultLang($lang)) { // if homepage in not-default-language, then add language code regardless of user settings // as we otherwise would not be able to switch language on the frontpage $finalSefUrl = shGetIsoCodeFromName($lang) . '/'; } else { $finalSefUrl = ''; } } else { // not on homepage, insert lang code based on user setting $option = shGetURLVar($nonSefUrl, 'option', ''); if (shInsertIsoCodeInUrl($option, $lang)) { // insert language code based on param // pass URL lang info, as may not be current lang $finalSefUrl = shGetIsoCodeFromName($lang) . '/' . $finalSefUrl; // must be forced lang, not default } } // after adding pagination part of SEF, and adding language code // the new SEF url is now complete and we can try adding to it cache and db if ($finalSefUrl != '') { $dburl = null; $dbUrlId = null; $nonSefUrlType = sh404SEF_URLTYPE_NONE; // search the memory cache for this new sef if ($sefConfig->shUseURLCache) { $nonSefUrlType = Sh404sefHelperCache::getNonSefUrlFromCache($finalSefUrl, $dburl); } $newMaxRank = 0; // if the new SEF was not found in memory cache, or if it was found but // we're set to record duplicates, we search for it in the database if ($sefConfig->shRecordDuplicates || $nonSefUrlType == sh404SEF_URLTYPE_NONE) { $dbUrlList = ShlDbHelper::selectObjectList('#__sh404sef_urls', array('id', 'newurl', 'rank', 'dateadd'), array('oldurl' => $finalSefUrl), $aWhereData = array(), $orderBy = array('rank')); if (count($dbUrlList) > 0) { $dburl = $dbUrlList[0]->newurl; $dbUrlId = $dbUrlList[0]->id; if (empty($dburl)) { // V 1.2.4.t url was found in DB, but was a 404 $nonSefUrlType = sh404SEF_URLTYPE_404; } else { $newMaxRank = $dbUrlList[count($dbUrlList) - 1]->rank + 1; $nonSefUrlType = $dbUrlList[0]->dateadd == '0000-00-00' ? sh404SEF_URLTYPE_AUTO : sh404SEF_URLTYPE_CUSTOM; } } } if ($nonSefUrlType != sh404SEF_URLTYPE_NONE && $nonSefUrlType != sh404SEF_URLTYPE_404) { // we found the SEF, one or more times in the db, in records which do have a non-sef attached $isDuplicate = $dburl != $nonSefUrl; // This is a duplicate so we must indert it with incremented rank; if (is_null($dburl) || $isDuplicate && $sefConfig->shRecordDuplicates) { // shAddSefUrlToDBAndCache( $nonSefUrl, $finalSefUrl, ($isDuplicate ? $newMaxRank : 0), $nonSefUrlType); $dateAdd = $nonSefUrlType == sh404SEF_URLTYPE_AUTO ? '0000-00-00' : date("Y-m-d"); ShlDbHelper::insert('#__sh404sef_urls', array('oldurl' => $finalSefUrl, 'newurl' => $nonSefUrl, 'rank' => $isDuplicate ? $newMaxRank : 0, 'dateadd' => $dateAdd)); // store new sef/non-sef pair in memory cache Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $finalSefUrl, $nonSefUrlType); // create shURL : get a shURL model, and ask url creation $model = ShlMvcModel_Base::getInstance('pageids', 'Sh404sefModel'); $model->createPageId($finalSefUrl, $nonSefUrl); } } else { // we haven't found the non-sef/sef pair, but maybe there is a record for // a 404 with that SEF. If so, we will "upgrade" the 404 record to a // normal non-sef/sef pair $dbUrlId = empty($dbUrlId) ? 0 : intval($dbUrlId); if ($sefConfig->shLog404Errors) { if ($nonSefUrlType == sh404SEF_URLTYPE_404 && !empty($dbUrlId)) { // we already have seen that it is a 404 $id = $dbUrlId; } elseif ($nonSefUrlType == sh404SEF_URLTYPE_404) { $id = ShlDbHelper::selectResult('#__sh404sef_urls', 'id', array('oldurl' => $finalSefUrl, 'newurl' => '')); } else { $id = null; } } else { $id = null; // if we are not logging 404 errors, then no need to check for } // previous hit of this page. if (!empty($id)) { // we found a 404 record matching the SEF url just created. We'll update that record // instead of creating a new one // need to update dateadd to 0, as otherwise this sef/non-sef pair will be seen as custom // this makes all such 404 errors 'disappear' from the 404 log, but no other solution ShlDbHelper::updateIn('#__sh404sef_urls', array('newurl' => $nonSefUrl, 'dateadd' => '0000-00-00'), 'id', array($id)); Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $finalSefUrl, sh404SEF_URLTYPE_AUTO); } else { // standard case: creation of a totally new sef/non-sef pair ShlDbHelper::insert('#__sh404sef_urls', array('oldurl' => $finalSefUrl, 'newurl' => $nonSefUrl, 'rank' => 0, 'dateadd' => '0000-00-00')); // store new sef/non-sef pair in memory cache Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $finalSefUrl, sh404SEF_URLTYPE_AUTO); // create shURL : get a shURL model, and ask url creation $model = ShlMvcModel_Base::getInstance('pageids', 'Sh404sefModel'); $model->createPageId($finalSefUrl, $nonSefUrl); } } } } } catch (Exception $e) { $finalSefUrl = ''; ShlSystem_Log::error('sh404sef', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, $e->getMessage()); } return $finalSefUrl; }
function shAddSefUrlToDBAndCache($nonSefUrl, $sefString, $rank, $urlType) { $db = ShlDbHelper::getDb(); $sefString = JString::ltrim($sefString, '/'); // V 1.2.4.t just in case you forgot to remove leading slash switch ($urlType) { case sh404SEF_URLTYPE_AUTO: $dateAdd = '0000-00-00'; break; case sh404SEF_URLTYPE_CUSTOM: $dateAdd = date("Y-m-d"); break; case sh404SEF_URLTYPE_NONE: return null; break; } try { $query = ''; if ($urlType == sh404SEF_URLTYPE_AUTO) { $result = ShlDbHelper::quoteQuery('select ??, ?? from ?? where ?? = ? and (?? = ? or ?? = ?)', array('id', 'newurl', '#__sh404sef_urls', 'oldurl', 'newurl', 'newurl'), array($sefString, '', addslashes(urldecode($nonSefUrl))))->shlLoadObject(); if (!empty($result)) { // sef urls was found either as a 404 or as already existing, with also the same non-sef if ($result->newurl == $nonSefUrl) { // url already in db, nothing to do ShlSystem_Log::debug('sh404sef', 'url already in db, nothing to do'); return true; } ShlDbHelper::update('#__sh404sef_urls', array('newurl' => addslashes(urldecode($nonSefUrl)), 'rank' => $rank, 'dateadd' => $dateAdd), array('oldurl' => $sefString)); } else { // another option: sef exists, but with another non-sef: that's a duplicate // need to check that $result = ShlDbHelper::selectObject('#__sh404sef_urls', array('id', 'newurl', 'rank'), $db->quoteName('oldurl') . ' = ? and ' . $db->quoteName('newurl') . ' <> ?', array($sefString, addslashes(urldecode($nonSefUrl))), array('rank' => 'desc')); if (!empty($result)) { // we found at least one identical SEF url, with another non-sef. Mark the new one as duplicate of the old one $rank = $result->rank + 1; } ShlDbHelper::insert('#__sh404sef_urls', array('oldurl' => $sefString, 'newurl' => $nonSefUrl, 'rank' => $rank, 'dateadd' => $dateAdd)); } } // store new sef/non-sef pair in memory cache Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $sefString, $urlType); // create shURL : get a shURL model, and ask url creation $model = ShlMvcModel_Base::getInstance('pageids', 'Sh404sefModel'); $model->createPageId($sefString, $nonSefUrl); } catch (Exception $e) { ShlSystem_Log::error('sh404sef', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, $e->getMessage()); } }