Changeset 1866


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

feature 657: permalinks for categories

Location:
trunk
Files:
4 added
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin.php

    r1865 r1866  
    55// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
    66// +-----------------------------------------------------------------------+
    7 // | branch        : BSF (Best So Far)
    87// | file          : $Id$
    98// | last update   : $Date$
     
    108107    'U_USERS'=> $link_start.'user_list',
    109108    'U_GROUPS'=> $link_start.'group_list',
     109    'U_PERMALINKS'=> $link_start.'permalinks',
    110110    'U_RETURN'=> make_index_url(),
    111111    'U_ADMIN'=> PHPWG_ROOT_PATH.'admin.php',
    112     'L_ADMIN' => $lang['admin'],
    113     'L_ADMIN_HINT' => $lang['hint_admin']
    114112    )
    115113  );
  • trunk/admin/cat_list.php

    r1861 r1866  
    139139
    140140// +-----------------------------------------------------------------------+
    141 // |                           Cache management                            |
    142 // +-----------------------------------------------------------------------+
    143 $query = '
    144 SELECT *
    145   FROM '.CATEGORIES_TABLE;
    146 if (!isset($_GET['parent_id']))
    147 {
    148   $query.= '
    149   WHERE id_uppercat IS NULL';
    150 }
    151 else
    152 {
    153   $query.= '
    154   WHERE id_uppercat = '.$_GET['parent_id'];
    155 }
    156 $query.= '
    157   ORDER BY rank ASC
    158 ;';
    159 $result = pwg_query($query);
    160 while ($row = mysql_fetch_assoc($result))
    161 {
    162   $categories[$row['rank']] = $row;
    163   $categories[$row['rank']]['nb_subcats'] = 0;
    164 }
    165 
    166 // +-----------------------------------------------------------------------+
    167141// |                            Navigation path                            |
    168142// +-----------------------------------------------------------------------+
     
    210184
    211185$query = '
    212 SELECT id, name, dir, rank, nb_images, status
     186SELECT id, name, permalink, dir, rank, nb_images, status
    213187  FROM '.CATEGORIES_TABLE;
    214188if (!isset($_GET['parent_id']))
  • trunk/admin/include/functions.php

    r1863 r1866  
    119119'.wordwrap(implode(', ', $ids), 80, "\n").')
    120120;';
     121  pwg_query($query);
     122
     123   $query='
     124DELETE FROM '.OLD_PERMALINKS_TABLE.'
     125  WHERE cat_id IN ('.implode(',',$cat_ids).')';
    121126  pwg_query($query);
    122127
  • trunk/admin/plugin.php

    r1731 r1866  
    5050
    5151$plugin_id = $sections[0];
    52 $check_db_plugin = get_db_plugins('active', $plugin_id );
    53 if (empty($check_db_plugin))
     52if ( !isset($pwg_loaded_plugins[$plugin_id]) )
    5453{
    5554  die('Invalid URL - plugin '.$plugin_id.' not active');
  • trunk/comments.php

    r1861 r1866  
    370370
    371371  // retrieving category informations
    372   $categories = array();
    373372  $query = '
    374 SELECT id, name, uppercats
     373SELECT id, name, permalink, uppercats
    375374  FROM '.CATEGORIES_TABLE.'
    376375  WHERE id IN ('.implode(',', $category_ids).')
    377376;';
    378   $result = pwg_query($query);
    379   while ($row = mysql_fetch_assoc($result))
    380   {
    381     $categories[$row['id']] = $row;
    382   }
     377  $categories = hash_from_query($query, 'id');
    383378
    384379  foreach ($comments as $comment)
  • 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
  • trunk/install/phpwebgallery_structure.sql

    r1844 r1866  
    3939  `global_rank` varchar(255) default NULL,
    4040  `image_order` varchar(128) default NULL,
    41   PRIMARY KEY  (`id`),
    42   KEY `categories_i2` (`id_uppercat`)
     41  `permalink` VARCHAR(64) default NULL,
     42  PRIMARY KEY  (`id`),
     43  KEY `categories_i2` (`id_uppercat`),
     44  UNIQUE KEY `categories_i3` (`permalink`)
    4345) TYPE=MyISAM;
    4446
     
    204206
    205207--
     208-- Table structure for table `phpwebgallery_old_permalinks`
     209--
     210
     211DROP TABLE IF EXISTS `phpwebgallery_old_permalinks`;
     212CREATE TABLE `phpwebgallery_old_permalinks` (
     213  `cat_id` smallint(5) unsigned NOT NULL,
     214  `permalink` VARCHAR(64) NOT NULL,
     215  `date_deleted` datetime NOT NULL,
     216  `last_hit` datetime default NULL,
     217  `hit` int(10) unsigned NOT NULL default '0',
     218  PRIMARY KEY (`permalink`)
     219) TYPE=MyISAM;
     220
     221--
    206222-- Table structure for table `phpwebgallery_plugins`
    207223--
  • trunk/language/en_UK.iso-8859-1/admin.lang.php

    r1853 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$
     
    4443$lang['Actions'] = 'Actions';
    4544$lang['Activate'] = 'Activate';
     45$lang['Add/delete a permalink'] = 'Add/delete a permalink';
    4646$lang['Add a tag'] = 'Add a tag';
    4747$lang['Add a user'] = 'Add a user';
     
    138138$lang['Parent category'] = 'Parent category';
    139139$lang['Path'] = 'Path';
     140$lang['Permalink'] = 'Permalink';
     141$lang['Permalink_%s_histo_used_by_%s'] = 'Permalink %s has been previously used by category %s. Delete from the permalink history first';
     142$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 "-"';
     143$lang['Permalink %s is already used by category %s'] = 'Permalink %s is already used by category %s';
     144$lang['Permalink history'] = 'Permalink history';
     145$lang['Permalinks'] = 'Permalinks';
    140146$lang['Permission denied'] = 'Permission denied';
    141147$lang['Permission granted thanks to a group'] = 'Permission granted thanks to a group';
     
    160166$lang['Represents'] = 'Represents';
    161167$lang['Save order'] = 'Save order';
     168$lang['Save to permalink history'] = 'Save to permalink history';
    162169$lang['Select at least one category'] = 'Select at least one category';
    163170$lang['Select at least one picture'] = 'Select at least one picture';
  • trunk/language/fr_FR.iso-8859-1/admin.lang.php

    r1853 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$
     
    4443$lang['Actions'] = 'Actions';
    4544$lang['Activate'] = 'Activer';
     45$lang['Add/delete a permalink'] = 'Ajouter/effacer un lien permanent';
    4646$lang['Add a tag'] = 'Ajouter un tag';
    4747$lang['Add a user'] = 'Ajouter un utilisateur';
     
    138138$lang['Parent category'] = 'Catégorie parente';
    139139$lang['Path'] = 'Chemin';
     140$lang['Permalink'] = 'Lien permanent';
     141$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';
     142$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 "-"';
     143$lang['Permalink %s is already used by category %s'] = 'Le lien permanent %s est dèja utilisé par la catégorie %s';
     144$lang['Permalink history'] = 'Historique des liens permanents';
     145$lang['Permalinks'] = 'Liens permanents';
    140146$lang['Permission denied'] = 'Accès interdit';
    141147$lang['Permission granted thanks to a group'] = 'Accès autorisé grâce à l\'appartenance aux groupes';
     
    160166$lang['Represents'] = 'Représente';
    161167$lang['Save order'] = 'Sauvegarder l\'ordre';
     168$lang['Save to permalink history'] = 'Sauvegarder dans l\'historique des liens permanents';
    162169$lang['Select at least one category'] = 'Sélectionner au moins une catégorie';
    163170$lang['Select at least one picture'] = 'Sélectionner au moins une image';
  • trunk/template/yoga/admin.tpl

    r1865 r1866  
    77        <li><a href="{U_RETURN}">{lang:home}</a></li>
    88        <li><a href="{U_FAQ}">{lang:instructions}</a></li>
    9         <li><a href="{U_ADMIN}" title="{L_ADMIN_HINT}">{L_ADMIN}</a></li>
     9        <li><a href="{U_ADMIN}" title="{lang:hint_admin}">{lang:admin}</a></li>
    1010      </ul>
    1111    </dd>
     
    3535        <li><a href="{representative.URL}">{lang:Representative}</a></li>
    3636        <!-- END representative -->
     37        <li><a href="{U_PERMALINKS}">{lang:Permalinks}</a></li>
    3738      </ul>
    3839    </dd>
  • trunk/template/yoga/default-layout.css

    r1776 r1866  
    8181        margin-bottom: 2em;
    8282}
     83
     84FORM FIELDSET P {
     85        margin-top: 1em;
     86        margin-bottom: 0;
     87}
     88
    8389.small {
    8490        font-size: 80%;
Note: See TracChangeset for help on using the changeset viewer.