From 12af0a00794d4624c4804eeef74fd207f474b002 Mon Sep 17 00:00:00 2001 From: rvelices Date: Thu, 28 Jun 2007 02:16:03 +0000 Subject: feature 713: allow permalinks to contain the slash ("/") character git-svn-id: http://piwigo.org/svn/trunk@2047 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/include/functions_permalinks.php | 44 +++++++++++++++++++-- include/functions_category.inc.php | 65 +++++++++++++++++--------------- include/functions_url.inc.php | 39 +++++++++++++------ language/en_UK.iso-8859-1/admin.lang.php | 2 +- language/fr_FR.iso-8859-1/admin.lang.php | 2 +- 5 files changed, 104 insertions(+), 48 deletions(-) diff --git a/admin/include/functions_permalinks.php b/admin/include/functions_permalinks.php index 1f10b4379..a9d576256 100644 --- a/admin/include/functions_permalinks.php +++ b/admin/include/functions_permalinks.php @@ -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 @@ -48,7 +84,7 @@ SELECT permalink } 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'][] = @@ -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) ) { @@ -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'][] = diff --git a/include/functions_category.inc.php b/include/functions_category.inc.php index 5d1679e5c..2c1150321 100644 --- a/include/functions_category.inc.php +++ b/include/functions_category.inc.php @@ -364,48 +364,51 @@ SELECT DISTINCT(id) 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) diff --git a/include/functions_url.inc.php b/include/functions_url.inc.php index 3c0138136..9e81a72cf 100644 --- a/include/functions_url.inc.php +++ b/include/functions_url.inc.php @@ -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'])) diff --git a/language/en_UK.iso-8859-1/admin.lang.php b/language/en_UK.iso-8859-1/admin.lang.php index 1b026ae72..d73c92c5e 100644 --- a/language/en_UK.iso-8859-1/admin.lang.php +++ b/language/en_UK.iso-8859-1/admin.lang.php @@ -148,7 +148,7 @@ $lang['Parent category'] = 'Parent category'; $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'; diff --git a/language/fr_FR.iso-8859-1/admin.lang.php b/language/fr_FR.iso-8859-1/admin.lang.php index ae88c0403..6a47b6398 100644 --- a/language/fr_FR.iso-8859-1/admin.lang.php +++ b/language/fr_FR.iso-8859-1/admin.lang.php @@ -148,7 +148,7 @@ $lang['Parent category'] = 'Cat $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'; -- cgit v1.2.3