Changeset 1866 for trunk/include


Ignore:
Timestamp:
Feb 28, 2007, 4:07:12 AM (17 years ago)
Author:
rvelices
Message:

feature 657: permalinks for categories

Location:
trunk/include
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/category_cats.inc.php

    r1861 r1866  
    3636  $query = '
    3737SELECT
    38   id, name, representative_picture_id, comment, nb_images, uppercats,
     38  id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
    3939  date_last, max_date_last, count_images, count_categories, global_rank
    4040  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
     
    5858  $query = '
    5959SELECT
    60   id, name, representative_picture_id, comment, nb_images,
     60  id, name, permalink, representative_picture_id, comment, nb_images,
    6161  date_last, max_date_last, count_images, count_categories
    6262  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
  • trunk/include/constants.php

    r1727 r1866  
    55// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
    66// +-----------------------------------------------------------------------+
    7 // | branch        : BSF (Best So Far)
    8 // | file          : $RCSfile$
     7// | file          : $Id$
    98// | last update   : $Date$
    109// | last modifier : $Author$
     
    7675define('PLUGINS_TABLE', $prefixeTable.'plugins');
    7776define('WEB_SERVICES_ACCESS_TABLE', $prefixeTable.'ws_access');
     77define('OLD_PERMALINKS_TABLE', $prefixeTable.'old_permalinks');
    7878?>
  • trunk/include/functions.inc.php

    r1864 r1866  
    11761176
    11771177/**
     1178 * creates an hashed based on a query, this function is a very common
     1179 * pattern used here. The key is given as parameter, the value is an associative
     1180 * array.
     1181 *
     1182 * @param string $query
     1183 * @param string $keyname
     1184 * @return array
     1185 */
     1186function hash_from_query($query, $keyname)
     1187{
     1188  $array = array();
     1189  $result = pwg_query($query);
     1190  while ($row = mysql_fetch_assoc($result))
     1191  {
     1192    $array[ $row[$keyname] ] = $row;
     1193  }
     1194  return $array;
     1195}
     1196
     1197/**
    11781198 * Return basename of the current script
    11791199 * Lower case convertion is applied on return value
  • trunk/include/functions_category.inc.php

    r1861 r1866  
    6060  // From CATEGORIES_TABLE
    6161  $query.= '
    62   name, id, nb_images, global_rank,';
     62  id, name, permalink, nb_images, global_rank,';
    6363  // From USER_CACHE_CATEGORIES_TABLE
    6464  $query.= '
     
    160160  }
    161161
    162   $names = array();
    163   $query = '
    164 SELECT id, name
    165   FROM '.CATEGORIES_TABLE.'
    166   WHERE id IN ('.$cat['uppercats'].')
    167 ;';
    168   $result = pwg_query($query);
    169   while($row = mysql_fetch_assoc($result))
    170   {
    171     $names[$row['id']] = $row;
    172   }
    173 
    174   // category names must be in the same order than uppercats list
    175   $cat['upper_names'] = array();
    176   foreach (explode(',', $cat['uppercats']) as $cat_id)
    177   {
    178     $cat['upper_names'][$cat_id] = $names[$cat_id];
    179   }
    180 
     162  $upper_ids = explode(',', $cat['uppercats']);
     163  if ( count($upper_ids)==1 )
     164  {// no need to make a query for level 1
     165    $cat['upper_names'] = array(
     166        array(
     167          'id' => $cat['id'],
     168          'name' => $cat['name'],
     169          'permalink' => $cat['permalink'],
     170          )
     171      );
     172  }
     173  else
     174  {
     175    $names = array();
     176    $query = '
     177  SELECT id, name, permalink
     178    FROM '.CATEGORIES_TABLE.'
     179    WHERE id IN ('.$cat['uppercats'].')
     180  ;';
     181    $names = hash_from_query($query, 'id');
     182
     183    // category names must be in the same order than uppercats list
     184    $cat['upper_names'] = array();
     185    foreach ($upper_ids as $cat_id)
     186    {
     187      array_push( $cat['upper_names'], $names[$cat_id]);
     188    }
     189  }
    181190  return $cat;
    182191}
     
    309318  if (!empty($result))
    310319  {
    311     while ($row = mysql_fetch_array($result))
     320    while ($row = mysql_fetch_assoc($result))
    312321    {
    313322      array_push($categories, $row);
     
    356365}
    357366
     367/** returns a category id that corresponds to the given permalink (or null)
     368 * @param string permalink
     369 */
     370function get_cat_id_from_permalink( $permalink )
     371{
     372  $query ='
     373SELECT id FROM '.CATEGORIES_TABLE.'
     374  WHERE permalink="'.$permalink.'"';
     375  $ids = array_from_query($query, 'id');
     376  if (!empty($ids))
     377  {
     378    return $ids[0];
     379  }
     380  return null;
     381}
     382
     383/** returns a category id that has used before this permalink (or null)
     384 * @param string permalink
     385 * @param boolean is_hit if true update the usage counters on the old permalinks
     386 */
     387function get_cat_id_from_old_permalink($permalink, $is_hit)
     388{
     389  $query='
     390SELECT c.id
     391  FROM '.OLD_PERMALINKS_TABLE.' op INNER JOIN '.CATEGORIES_TABLE.' c
     392    ON op.cat_id=c.id
     393  WHERE op.permalink="'.$permalink.'"
     394  LIMIT 1';
     395  $result = pwg_query($query);
     396  $cat_id = null;
     397  if ( mysql_num_rows($result) )
     398    list( $cat_id ) = mysql_fetch_array($result);
     399
     400  if ( isset($cat_id) and $is_hit )
     401  {
     402    $query='
     403UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
     404  WHERE permalink="'.$permalink.'" AND cat_id='.$cat_id.'
     405  LIMIT 1';
     406    pwg_query($query);
     407  }
     408  return $cat_id;
     409}
     410
    358411function global_rank_compare($a, $b)
    359412{
  • trunk/include/functions_html.inc.php

    r1861 r1866  
    235235 *
    236236 * categories string returned contains categories as given in the input
    237  * array $cat_informations. $cat_informations array must be an association
    238  * of {category_id => array( id, name) }. If url input parameter is null,
     237 * array $cat_informations. $cat_informations array must be an array
     238 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
    239239 * returns only the categories name without links.
    240240 *
     
    252252  $output = '';
    253253  $is_first = true;
    254   foreach ($cat_informations as $id => $cat)
     254  foreach ($cat_informations as $cat)
    255255  {
    256256    is_array($cat) or trigger_error(
    257         'get_cat_display_name wrong type for cat '.$id, E_USER_WARNING
     257        'get_cat_display_name wrong type for category ', E_USER_WARNING
    258258      );
    259259    if ($is_first)
     
    283283    else
    284284    {
    285       $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$id.'">';
     285      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
    286286      $output.= $cat['name'].'</a>';
    287287    }
     
    319319  {
    320320    $query = '
    321 SELECT id, name
     321SELECT id, name, permalink
    322322  FROM '.CATEGORIES_TABLE.'
    323323;';
    324     $result = pwg_query($query);
    325     while ($row = mysql_fetch_assoc($result))
    326     {
    327       $cache['cat_names'][$row['id']] = $row;
    328     }
     324    $cache['cat_names'] = hash_from_query($query, 'id');
    329325  }
    330326
  • trunk/include/functions_url.inc.php

    r1861 r1866  
    337337            );
    338338
    339         $section_string.= '/category/'.$params['category']['id'];
    340         if ( $conf['category_url_style']=='id-name' )
     339        array_key_exists('permalink', $params['category']) or trigger_error(
     340            'make_section_in_url category permalink not set', E_USER_WARNING
     341            );
     342
     343        $section_string.= '/category/';
     344        if ( empty($params['category']['permalink']) )
    341345        {
    342           $section_string.= '-'.str2url($params['category']['name']);
     346          $section_string.= $params['category']['id'];
     347          if ( $conf['category_url_style']=='id-name' )
     348          {
     349            $section_string.= '-'.str2url($params['category']['name']);
     350          }
     351        }
     352        else
     353        {
     354          $section_string.= $params['category']['permalink'];
    343355        }
    344356      }
  • trunk/include/section_init.inc.php

    r1861 r1866  
    108108        $page['image_file'] = $matches[2];
    109109      }
    110 
    111110    }
    112111    else
     
    129128  $next_token++;
    130129
    131   if (isset($tokens[$next_token])
    132       and preg_match('/^(\d+)/', $tokens[$next_token], $matches))
    133   {
    134     $page['category'] = $matches[1];
    135     $next_token++;
     130  if (isset($tokens[$next_token]) )
     131  {
     132    if (preg_match('/^(\d+)(?:-(.+))?$/', $tokens[$next_token], $matches))
     133    {
     134      if ( isset($matches[2]) )
     135        $page['hit_by']['cat_url_name'] = $matches[2];
     136      $page['category'] = $matches[1];
     137      $next_token++;
     138    }
     139    else
     140    {
     141      if ( strpos($tokens[$next_token], 'created-')!==0
     142          and strpos($tokens[$next_token], 'posted-')!==0
     143          and $tokens[$next_token] != 'flat')
     144      {// try a permalink
     145        $cat_id = get_cat_id_from_permalink($tokens[$next_token]);
     146        if ( !isset($cat_id) )
     147        {//try old permalink
     148          $cat_id = get_cat_id_from_old_permalink($tokens[$next_token], true);
     149        }
     150        if ( isset($cat_id) )
     151        {
     152          $page['category'] = $cat_id;
     153          $page['hit_by']['cat_permalink'] = $tokens[$next_token];
     154        }
     155        else
     156        {
     157          page_not_found('Permalink for album not found');
     158        }
     159        unset($cat_id);
     160        $next_token++;
     161      }
     162    }
    136163  }
    137164}
     
    691718}
    692719
     720// see if we need a redirect because of a permalink
     721if ( 'categories'==$page['section'] and isset($page['category']) )
     722{
     723  $need_redirect=false;
     724  if ( empty($page['category']['permalink']) )
     725  {
     726    if ( $conf['category_url_style'] == 'id-name' and
     727        @$page['hit_by']['cat_url_name'] !== str2url($page['category']['name']) )
     728    {
     729      $need_redirect=true;
     730    }
     731  }
     732  else
     733  {
     734    if ( $page['category']['permalink'] !== @$page['hit_by']['cat_permalink'] )
     735    {
     736      $need_redirect=true;
     737    }
     738  }
     739
     740  if ($need_redirect)
     741  {
     742    $redirect_url = ( script_basename()=='picture'
     743        ? duplicate_picture_url()
     744          : duplicate_index_url()
     745      );
     746    if (!headers_sent())
     747    { // this is a permanent redirection
     748      set_status_header(302);
     749      redirect_http( $redirect_url );
     750    }
     751    redirect( $redirect_url );
     752  }
     753  unset( $need_redirect, $page['hit_by'] );
     754}
     755
    693756trigger_action('loc_end_section_init');
    694757?>
  • trunk/include/ws_functions.inc.php

    r1861 r1866  
    320320
    321321  $query = '
    322 SELECT id, name, image_order
     322SELECT id, name, permalink, image_order
    323323  FROM '.CATEGORIES_TABLE.'
    324324  WHERE '. implode('
     
    466466
    467467  $query = '
    468 SELECT id, name, uppercats, global_rank,
     468SELECT id, name, permalink, uppercats, global_rank,
    469469    nb_images, count_images AS total_nb_images,
    470470    date_last, max_date_last, count_categories AS nb_categories
     
    597597  //-------------------------------------------------------- related categories
    598598  $query = '
    599 SELECT id, name, uppercats, global_rank, commentable
     599SELECT id, name, permalink, uppercats, global_rank, commentable
    600600  FROM '.IMAGE_CATEGORY_TABLE.'
    601601    INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id
Note: See TracChangeset for help on using the changeset viewer.