Skip to content

Commit

Permalink
feature 713: allow permalinks to contain the slash ("/") character
Browse files Browse the repository at this point in the history
git-svn-id: http://piwigo.org/svn/trunk@2047 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
rvelices committed Jun 28, 2007
1 parent 009970b commit 12af0a0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 48 deletions.
44 changes: 41 additions & 3 deletions admin/include/functions_permalinks.php
Expand Up @@ -23,6 +23,42 @@
// | USA. |
// +-----------------------------------------------------------------------+

/** returns a category id that corresponds to the given permalink (or null)
* @param string permalink
*/
function get_cat_id_from_permalink( $permalink )
{
$query ='
SELECT id FROM '.CATEGORIES_TABLE.'
WHERE permalink="'.$permalink.'"';
$ids = array_from_query($query, 'id');
if (!empty($ids))
{
return $ids[0];
}
return null;
}

/** returns a category id that has used before this permalink (or null)
* @param string permalink
* @param boolean is_hit if true update the usage counters on the old permalinks
*/
function get_cat_id_from_old_permalink($permalink)
{
$query='
SELECT c.id
FROM '.OLD_PERMALINKS_TABLE.' op INNER JOIN '.CATEGORIES_TABLE.' c
ON op.cat_id=c.id
WHERE op.permalink="'.$permalink.'"
LIMIT 1';
$result = pwg_query($query);
$cat_id = null;
if ( mysql_num_rows($result) )
list( $cat_id ) = mysql_fetch_array($result);
return $cat_id;
}


/** deletes the permalink associated with a category
* returns true on success
* @param int cat_id the target category id
Expand All @@ -48,7 +84,7 @@ function delete_cat_permalink( $cat_id, $save )
}
if ($save)
{
$old_cat_id = get_cat_id_from_old_permalink($permalink, false);
$old_cat_id = get_cat_id_from_old_permalink($permalink);
if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
{
$page['errors'][] =
Expand Down Expand Up @@ -100,7 +136,9 @@ function set_cat_permalink( $cat_id, $permalink, $save )
{
global $page, $cache;

$sanitized_permalink = preg_replace( '#[^a-zA-Z0-9_-]#', '' ,$permalink);
$sanitized_permalink = preg_replace( '#[^a-zA-Z0-9_/-]#', '' ,$permalink);
$sanitized_permalink = trim($sanitized_permalink, '/');
$sanitized_permalink = str_replace('//', '/', $sanitized_permalink);
if ( $sanitized_permalink != $permalink
or preg_match( '#^(\d)+(-.*)?$#', $permalink) )
{
Expand Down Expand Up @@ -128,7 +166,7 @@ function set_cat_permalink( $cat_id, $permalink, $save )
}

// check if the new permalink was historically used
$old_cat_id = get_cat_id_from_old_permalink($permalink, false);
$old_cat_id = get_cat_id_from_old_permalink($permalink);
if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
{
$page['errors'][] =
Expand Down
65 changes: 34 additions & 31 deletions include/functions_category.inc.php
Expand Up @@ -364,48 +364,51 @@ function get_subcat_ids($ids)
return $subcats;
}

/** returns a category id that corresponds to the given permalink (or null)
* @param string permalink
/** finds a matching category id from a potential list of permalinks
* @param array permalinks example: holiday holiday/france holiday/france/paris
* @param int idx - output of the index in $permalinks that matches
* return category id or null if no match
*/
function get_cat_id_from_permalink( $permalink )
function get_cat_id_from_permalinks( $permalinks, &$idx )
{
$query ='
SELECT id FROM '.CATEGORIES_TABLE.'
WHERE permalink="'.$permalink.'"';
$ids = array_from_query($query, 'id');
if (!empty($ids))
$in = '';
foreach($permalinks as $permalink)
{
return $ids[0];
if ( !empty($in) ) $in.=', ';
$in .= '"'.$permalink.'"';
}
return null;
}

/** returns a category id that has used before this permalink (or null)
* @param string permalink
* @param boolean is_hit if true update the usage counters on the old permalinks
*/
function get_cat_id_from_old_permalink($permalink, $is_hit)
{
$query='
SELECT c.id
$query ='
SELECT c.id, op.permalink, 1 AS is_old
FROM '.OLD_PERMALINKS_TABLE.' op INNER JOIN '.CATEGORIES_TABLE.' c
ON op.cat_id=c.id
WHERE op.permalink="'.$permalink.'"
LIMIT 1';
$result = pwg_query($query);
$cat_id = null;
if ( mysql_num_rows($result) )
list( $cat_id ) = mysql_fetch_array($result);
WHERE op.permalink IN ('.$in.')
UNION
SELECT id, permalink, 0 AS is_old
FROM '.CATEGORIES_TABLE.'
WHERE permalink IN ('.$in.')
;';
$perma_hash = hash_from_query($query, 'permalink');

if ( isset($cat_id) and $is_hit )
if ( empty($perma_hash) )
return null;
for ($i=count($permalinks)-1; $i>=0; $i--)
{
$query='
if ( isset( $perma_hash[ $permalinks[$i] ] ) )
{
$idx = $i;
$cat_id = $perma_hash[ $permalinks[$i] ]['id'];
if ($perma_hash[ $permalinks[$i] ]['is_old'])
{
$query='
UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
WHERE permalink="'.$permalink.'" AND cat_id='.$cat_id.'
WHERE permalink="'.$permalinks[$i].'" AND cat_id='.$cat_id.'
LIMIT 1';
pwg_query($query);
pwg_query($query);
}
return $cat_id;
}
}
return $cat_id;
return null;
}

function global_rank_compare($a, $b)
Expand Down
39 changes: 27 additions & 12 deletions include/functions_url.inc.php
Expand Up @@ -457,29 +457,44 @@ function parse_section_url( $tokens, &$next_token)
$next_token++;
}
else
{
if ( strpos($tokens[$next_token], 'created-')!==0
and strpos($tokens[$next_token], 'posted-')!==0
{// try a permalink
$maybe_permalinks = array();
$current_token = $next_token;
while ( isset($tokens[$current_token])
and strpos($tokens[$current_token], 'created-')!==0
and strpos($tokens[$current_token], 'posted-')!==0
and strpos($tokens[$next_token], 'start-')!==0
and $tokens[$next_token] != 'flat')
{// try a permalink
$cat_id = get_cat_id_from_permalink($tokens[$next_token]);
if ( !isset($cat_id) )
{//try old permalink
$cat_id = get_cat_id_from_old_permalink($tokens[$next_token], true);
and $tokens[$current_token] != 'flat')
{
if (empty($maybe_permalinks))
{
array_push($maybe_permalinks, $tokens[$current_token]);
}
else
{
array_push($maybe_permalinks,
$maybe_permalinks[count($maybe_permalinks)-1]
. '/' . $tokens[$current_token]
);
}
$current_token++;
}

if ( count($maybe_permalinks) )
{
$cat_id = get_cat_id_from_permalinks($maybe_permalinks, $perma_index);
if ( isset($cat_id) )
{
$next_token += $perma_index+1;
$page['category'] = $cat_id;
$page['hit_by']['cat_permalink'] = $tokens[$next_token];
$page['hit_by']['cat_permalink'] = $maybe_permalinks[$perma_index];
}
else
{
page_not_found('Permalink for album not found');
}
$next_token++;
}
}
}
}

if (isset($page['category']))
Expand Down
2 changes: 1 addition & 1 deletion language/en_UK.iso-8859-1/admin.lang.php
Expand Up @@ -148,7 +148,7 @@
$lang['Path'] = 'Path';
$lang['Permalink'] = 'Permalink';
$lang['Permalink_%s_histo_used_by_%s'] = 'Permalink %s has been previously used by category %s. Delete from the permalink history first';
$lang['Permalink_name_rule'] = 'The permalink name must be composed of a-z, A-Z, 0-9, "-" or "_". It must not be numeric or start with number followed by "-"';
$lang['Permalink_name_rule'] = 'The permalink name must be composed of a-z, A-Z, 0-9, "-", "_" or "/". It must not be numeric or start with number followed by "-"';
$lang['Permalink %s is already used by category %s'] = 'Permalink %s is already used by category %s';
$lang['Permalink history'] = 'Permalink history';
$lang['Permalinks'] = 'Permalinks';
Expand Down
2 changes: 1 addition & 1 deletion language/fr_FR.iso-8859-1/admin.lang.php
Expand Up @@ -148,7 +148,7 @@
$lang['Path'] = 'Chemin';
$lang['Permalink'] = 'Lien permanent';
$lang['Permalink_%s_histo_used_by_%s'] = 'Le lien permanent %s a été utilisé précédemment par la catégorie %s. Veuillez l\'effacer de l\'historique des liens permanents';
$lang['Permalink_name_rule'] = 'Le lien permanent ne doit contenir des caractères que parmi "a-zA-Z0-9", "-" ou "_". Il ne doit pas être numérique ou commencer par un nombre suivi par "-"';
$lang['Permalink_name_rule'] = 'Le lien permanent ne doit contenir des caractères que parmi "a-zA-Z0-9", "-", "_" ou "/". Il ne doit pas être numérique ou commencer par un nombre suivi par "-"';
$lang['Permalink %s is already used by category %s'] = 'Le lien permanent %s est dèja utilisé par la catégorie %s';
$lang['Permalink history'] = 'Historique des liens permanents';
$lang['Permalinks'] = 'Liens permanents';
Expand Down

0 comments on commit 12af0a0

Please sign in to comment.