aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorrvelices <rv-github@modusoptimus.com>2007-02-28 03:07:12 +0000
committerrvelices <rv-github@modusoptimus.com>2007-02-28 03:07:12 +0000
commitea56d7b2ac1d41ea19b5fb45843c839e30a0b37b (patch)
tree5f55c108b1a808867d44db2b6ebcf0ad573874b6 /include
parent30e259904cc38172b2b730455009455675f0d8f5 (diff)
feature 657: permalinks for categories
git-svn-id: http://piwigo.org/svn/trunk@1866 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'include')
-rw-r--r--include/category_cats.inc.php4
-rw-r--r--include/constants.php4
-rw-r--r--include/functions.inc.php20
-rw-r--r--include/functions_category.inc.php89
-rw-r--r--include/functions_html.inc.php18
-rw-r--r--include/functions_url.inc.php18
-rw-r--r--include/section_init.inc.php73
-rw-r--r--include/ws_functions.inc.php6
8 files changed, 188 insertions, 44 deletions
diff --git a/include/category_cats.inc.php b/include/category_cats.inc.php
index e9549ba44..ccdfa5949 100644
--- a/include/category_cats.inc.php
+++ b/include/category_cats.inc.php
@@ -35,7 +35,7 @@ if ($page['section']=='recent_cats')
// $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
$query = '
SELECT
- id, name, representative_picture_id, comment, nb_images, uppercats,
+ id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
date_last, max_date_last, count_images, count_categories, global_rank
FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
ON id = cat_id and user_id = '.$user['id'].'
@@ -57,7 +57,7 @@ else
// $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
$query = '
SELECT
- id, name, representative_picture_id, comment, nb_images,
+ id, name, permalink, representative_picture_id, comment, nb_images,
date_last, max_date_last, count_images, count_categories
FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
ON id = cat_id and user_id = '.$user['id'].'
diff --git a/include/constants.php b/include/constants.php
index 0d1b6f9ae..f442bac99 100644
--- a/include/constants.php
+++ b/include/constants.php
@@ -4,8 +4,7 @@
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
-// | branch : BSF (Best So Far)
-// | file : $RCSfile$
+// | file : $Id$
// | last update : $Date$
// | last modifier : $Author$
// | revision : $Revision$
@@ -75,4 +74,5 @@ define('TAGS_TABLE', $prefixeTable.'tags');
define('IMAGE_TAG_TABLE', $prefixeTable.'image_tag');
define('PLUGINS_TABLE', $prefixeTable.'plugins');
define('WEB_SERVICES_ACCESS_TABLE', $prefixeTable.'ws_access');
+define('OLD_PERMALINKS_TABLE', $prefixeTable.'old_permalinks');
?>
diff --git a/include/functions.inc.php b/include/functions.inc.php
index 6b44c81dc..eb706e6ba 100644
--- a/include/functions.inc.php
+++ b/include/functions.inc.php
@@ -1175,6 +1175,26 @@ function simple_hash_from_query($query, $keyname, $valuename)
}
/**
+ * creates an hashed based on a query, this function is a very common
+ * pattern used here. The key is given as parameter, the value is an associative
+ * array.
+ *
+ * @param string $query
+ * @param string $keyname
+ * @return array
+ */
+function hash_from_query($query, $keyname)
+{
+ $array = array();
+ $result = pwg_query($query);
+ while ($row = mysql_fetch_assoc($result))
+ {
+ $array[ $row[$keyname] ] = $row;
+ }
+ return $array;
+}
+
+/**
* Return basename of the current script
* Lower case convertion is applied on return value
* Return value is without file extention ".php"
diff --git a/include/functions_category.inc.php b/include/functions_category.inc.php
index 66b8865da..5d1679e5c 100644
--- a/include/functions_category.inc.php
+++ b/include/functions_category.inc.php
@@ -59,7 +59,7 @@ function get_categories_menu()
SELECT ';
// From CATEGORIES_TABLE
$query.= '
- name, id, nb_images, global_rank,';
+ id, name, permalink, nb_images, global_rank,';
// From USER_CACHE_CATEGORIES_TABLE
$query.= '
date_last, max_date_last, count_images, count_categories';
@@ -159,25 +159,34 @@ SELECT *
$cat['comment'] = nl2br(@$cat['comment']);
}
- $names = array();
- $query = '
-SELECT id, name
- FROM '.CATEGORIES_TABLE.'
- WHERE id IN ('.$cat['uppercats'].')
-;';
- $result = pwg_query($query);
- while($row = mysql_fetch_assoc($result))
- {
- $names[$row['id']] = $row;
+ $upper_ids = explode(',', $cat['uppercats']);
+ if ( count($upper_ids)==1 )
+ {// no need to make a query for level 1
+ $cat['upper_names'] = array(
+ array(
+ 'id' => $cat['id'],
+ 'name' => $cat['name'],
+ 'permalink' => $cat['permalink'],
+ )
+ );
}
-
- // category names must be in the same order than uppercats list
- $cat['upper_names'] = array();
- foreach (explode(',', $cat['uppercats']) as $cat_id)
+ else
{
- $cat['upper_names'][$cat_id] = $names[$cat_id];
+ $names = array();
+ $query = '
+ SELECT id, name, permalink
+ FROM '.CATEGORIES_TABLE.'
+ WHERE id IN ('.$cat['uppercats'].')
+ ;';
+ $names = hash_from_query($query, 'id');
+
+ // category names must be in the same order than uppercats list
+ $cat['upper_names'] = array();
+ foreach ($upper_ids as $cat_id)
+ {
+ array_push( $cat['upper_names'], $names[$cat_id]);
+ }
}
-
return $cat;
}
@@ -308,7 +317,7 @@ function display_select_cat_wrapper($query, $selecteds, $blockname,
$categories = array();
if (!empty($result))
{
- while ($row = mysql_fetch_array($result))
+ while ($row = mysql_fetch_assoc($result))
{
array_push($categories, $row);
}
@@ -355,6 +364,50 @@ SELECT DISTINCT(id)
return $subcats;
}
+/** 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, $is_hit)
+{
+ $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);
+
+ if ( isset($cat_id) and $is_hit )
+ {
+ $query='
+UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
+ WHERE permalink="'.$permalink.'" AND cat_id='.$cat_id.'
+ LIMIT 1';
+ pwg_query($query);
+ }
+ return $cat_id;
+}
+
function global_rank_compare($a, $b)
{
return strnatcasecmp($a['global_rank'], $b['global_rank']);
diff --git a/include/functions_html.inc.php b/include/functions_html.inc.php
index dcb42cdb1..5612454b0 100644
--- a/include/functions_html.inc.php
+++ b/include/functions_html.inc.php
@@ -234,8 +234,8 @@ function create_navigation_bar(
* returns the list of categories as a HTML string
*
* categories string returned contains categories as given in the input
- * array $cat_informations. $cat_informations array must be an association
- * of {category_id => array( id, name) }. If url input parameter is null,
+ * array $cat_informations. $cat_informations array must be an array
+ * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
* returns only the categories name without links.
*
* @param array cat_informations
@@ -251,10 +251,10 @@ function get_cat_display_name($cat_informations,
$output = '';
$is_first = true;
- foreach ($cat_informations as $id => $cat)
+ foreach ($cat_informations as $cat)
{
is_array($cat) or trigger_error(
- 'get_cat_display_name wrong type for cat '.$id, E_USER_WARNING
+ 'get_cat_display_name wrong type for category ', E_USER_WARNING
);
if ($is_first)
{
@@ -282,7 +282,7 @@ function get_cat_display_name($cat_informations,
}
else
{
- $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$id.'">';
+ $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
$output.= $cat['name'].'</a>';
}
}
@@ -318,14 +318,10 @@ function get_cat_display_name_cache($uppercats,
if (!isset($cache['cat_names']))
{
$query = '
-SELECT id, name
+SELECT id, name, permalink
FROM '.CATEGORIES_TABLE.'
;';
- $result = pwg_query($query);
- while ($row = mysql_fetch_assoc($result))
- {
- $cache['cat_names'][$row['id']] = $row;
- }
+ $cache['cat_names'] = hash_from_query($query, 'id');
}
$output = '';
diff --git a/include/functions_url.inc.php b/include/functions_url.inc.php
index b96ed82b7..04e0b32d1 100644
--- a/include/functions_url.inc.php
+++ b/include/functions_url.inc.php
@@ -336,10 +336,22 @@ function make_section_in_url($params)
'make_section_in_url category name not set', E_USER_WARNING
);
- $section_string.= '/category/'.$params['category']['id'];
- if ( $conf['category_url_style']=='id-name' )
+ array_key_exists('permalink', $params['category']) or trigger_error(
+ 'make_section_in_url category permalink not set', E_USER_WARNING
+ );
+
+ $section_string.= '/category/';
+ if ( empty($params['category']['permalink']) )
+ {
+ $section_string.= $params['category']['id'];
+ if ( $conf['category_url_style']=='id-name' )
+ {
+ $section_string.= '-'.str2url($params['category']['name']);
+ }
+ }
+ else
{
- $section_string.= '-'.str2url($params['category']['name']);
+ $section_string.= $params['category']['permalink'];
}
}
diff --git a/include/section_init.inc.php b/include/section_init.inc.php
index 4434ec542..af633571a 100644
--- a/include/section_init.inc.php
+++ b/include/section_init.inc.php
@@ -107,7 +107,6 @@ if (script_basename() == 'picture') // basename without file extention
{
$page['image_file'] = $matches[2];
}
-
}
else
{
@@ -128,11 +127,39 @@ if (0 === strpos(@$tokens[$next_token], 'categor'))
$page['section'] = 'categories';
$next_token++;
- if (isset($tokens[$next_token])
- and preg_match('/^(\d+)/', $tokens[$next_token], $matches))
+ if (isset($tokens[$next_token]) )
{
- $page['category'] = $matches[1];
- $next_token++;
+ if (preg_match('/^(\d+)(?:-(.+))?$/', $tokens[$next_token], $matches))
+ {
+ if ( isset($matches[2]) )
+ $page['hit_by']['cat_url_name'] = $matches[2];
+ $page['category'] = $matches[1];
+ $next_token++;
+ }
+ else
+ {
+ if ( strpos($tokens[$next_token], 'created-')!==0
+ and strpos($tokens[$next_token], 'posted-')!==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);
+ }
+ if ( isset($cat_id) )
+ {
+ $page['category'] = $cat_id;
+ $page['hit_by']['cat_permalink'] = $tokens[$next_token];
+ }
+ else
+ {
+ page_not_found('Permalink for album not found');
+ }
+ unset($cat_id);
+ $next_token++;
+ }
+ }
}
}
else if (0 === strpos(@$tokens[$next_token], 'tag'))
@@ -690,5 +717,41 @@ if ( $filter['enabled'] )
$page['meta_robots']['noindex']=1;
}
+// see if we need a redirect because of a permalink
+if ( 'categories'==$page['section'] and isset($page['category']) )
+{
+ $need_redirect=false;
+ if ( empty($page['category']['permalink']) )
+ {
+ if ( $conf['category_url_style'] == 'id-name' and
+ @$page['hit_by']['cat_url_name'] !== str2url($page['category']['name']) )
+ {
+ $need_redirect=true;
+ }
+ }
+ else
+ {
+ if ( $page['category']['permalink'] !== @$page['hit_by']['cat_permalink'] )
+ {
+ $need_redirect=true;
+ }
+ }
+
+ if ($need_redirect)
+ {
+ $redirect_url = ( script_basename()=='picture'
+ ? duplicate_picture_url()
+ : duplicate_index_url()
+ );
+ if (!headers_sent())
+ { // this is a permanent redirection
+ set_status_header(302);
+ redirect_http( $redirect_url );
+ }
+ redirect( $redirect_url );
+ }
+ unset( $need_redirect, $page['hit_by'] );
+}
+
trigger_action('loc_end_section_init');
?> \ No newline at end of file
diff --git a/include/ws_functions.inc.php b/include/ws_functions.inc.php
index c8e2a0fe0..f6653a457 100644
--- a/include/ws_functions.inc.php
+++ b/include/ws_functions.inc.php
@@ -319,7 +319,7 @@ function ws_categories_getImages($params, &$service)
$where_clauses[] = 'id NOT IN ('.$user['forbidden_categories'].')';
$query = '
-SELECT id, name, image_order
+SELECT id, name, permalink, image_order
FROM '.CATEGORIES_TABLE.'
WHERE '. implode('
AND ', $where_clauses);
@@ -465,7 +465,7 @@ function ws_categories_getList($params, &$service)
}
$query = '
-SELECT id, name, uppercats, global_rank,
+SELECT id, name, permalink, uppercats, global_rank,
nb_images, count_images AS total_nb_images,
date_last, max_date_last, count_categories AS nb_categories
FROM '.CATEGORIES_TABLE.'
@@ -596,7 +596,7 @@ LIMIT 1;';
//-------------------------------------------------------- related categories
$query = '
-SELECT id, name, uppercats, global_rank, commentable
+SELECT id, name, permalink, uppercats, global_rank, commentable
FROM '.IMAGE_CATEGORY_TABLE.'
INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id
WHERE image_id = '.$image_row['id'].'