feature 657: permalinks for categories

git-svn-id: http://piwigo.org/svn/trunk@1866 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
rvelices
2007-02-28 03:07:12 +00:00
parent 30e259904c
commit ea56d7b2ac
22 changed files with 650 additions and 89 deletions
+2 -2
View File
@@ -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'].'
+2 -2
View File
@@ -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');
?>
+20
View File
@@ -1174,6 +1174,26 @@ function simple_hash_from_query($query, $keyname, $valuename)
return $array;
}
/**
* 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
+71 -18
View File
@@ -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']);
+7 -11
View File
@@ -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 = '';
+15 -3
View File
@@ -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.= '-'.str2url($params['category']['name']);
$section_string.= $params['category']['id'];
if ( $conf['category_url_style']=='id-name' )
{
$section_string.= '-'.str2url($params['category']['name']);
}
}
else
{
$section_string.= $params['category']['permalink'];
}
}
+68 -5
View File
@@ -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');
?>
+3 -3
View File
@@ -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'].'