Esempio n. 1
0
    /**
     * Creates a new alias<->id relation in database lookup table.
     *
     * WARNING! This function is internal to RealURL. It is made public for
     * backwards compatibility but its behavior and parameters may change as
     * necessary for RealURL. No guaranties at all!
     *
     * @param array $cfg Configuration array of lookup table
     * @param string $newAliasValue Preferred new alias (final alias might be different if duplicates were found in the cache)
     * @param integer $idValue ID associated with alias
     * @param int $lang sys_language_uid to store with record
     * @return string Final alias string
     * @see lookUpTranslation()
     */
    protected function lookUp_newAlias($cfg, $newAliasValue, $idValue, $lang)
    {
        // Clean preferred alias
        $newAliasValue = $this->lookUp_cleanAlias($cfg, $newAliasValue);
        // If autoupdate is true we might be here even if an alias exists. Therefore we check if that alias is the $newAliasValue and if so, we return that instead of making a new, unique one.
        if ($cfg['autoUpdate'] && $this->lookUp_idToUniqAlias($cfg, $idValue, $lang, $newAliasValue)) {
            return $newAliasValue;
        }
        // Now, go create a unique alias
        $uniqueAlias = '';
        $counter = 0;
        $maxTry = 100;
        $test_newAliasValue = $newAliasValue;
        while ($counter < $maxTry) {
            // If the test-alias did NOT exist, it must be unique and we break out
            $foundId = $this->lookUp_uniqAliasToId($cfg, $test_newAliasValue, true);
            if (!$foundId || $foundId == $idValue) {
                $uniqueAlias = $test_newAliasValue;
                break;
            }
            // Otherwise, increment counter and test again...
            $counter++;
            $test_newAliasValue = $newAliasValue . '-' . $counter;
        }
        // if no unique alias was found in the process above, just suffix a hash string and assume that is unique...
        if (!$uniqueAlias) {
            $newAliasValue .= '-' . $this->apiWrapper->shortMD5(microtime());
            $uniqueAlias = $newAliasValue;
        }
        // Insert the new id<->alias relation
        $insertArray = array('tstamp' => time(), 'tablename' => $cfg['table'], 'field_alias' => $cfg['alias_field'], 'field_id' => $cfg['id_field'], 'value_alias' => $uniqueAlias, 'value_id' => $idValue, 'lang' => $lang);
        // Checking that this alias hasn't been stored since we looked last time
        $returnAlias = $this->lookUp_idToUniqAlias($cfg, $idValue, $lang, $uniqueAlias);
        if ($returnAlias) {
            // If we are here it is because another process managed to create this alias in the time between we looked the first time and now when we want to put it in database.
            $uniqueAlias = $returnAlias;
        } else {
            // Expire all other aliases
            // Look for an alias based on ID
            /** @noinspection PhpUndefinedMethodInspection */
            $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_realurl_uniqalias', 'value_id=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($idValue, 'tx_realurl_uniqalias') . '
					AND field_alias=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($cfg['alias_field'], 'tx_realurl_uniqalias') . '
					AND field_id=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($cfg['id_field'], 'tx_realurl_uniqalias') . '
					AND tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($cfg['table'], 'tx_realurl_uniqalias') . '
					AND lang=' . intval($lang) . '
					AND expire=0', array('expire' => time() + 24 * 3600 * ($cfg['expireDays'] ? $cfg['expireDays'] : 60)));
            // Store new alias
            /** @noinspection PhpUndefinedMethodInspection */
            $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_realurl_uniqalias', $insertArray);
        }
        // Return new unique alias
        return $uniqueAlias;
    }