1
0

Added new %parentname% category-permalink option

This commit is contained in:
Garvin Hicking
2008-11-26 12:33:29 +00:00
parent 0dc95ae371
commit b979267c4c
59 changed files with 101 additions and 65 deletions
docs
include
lang
UTF-8
serendipity_lang_bg.inc.phpserendipity_lang_cn.inc.phpserendipity_lang_cs.inc.phpserendipity_lang_cz.inc.phpserendipity_lang_da.inc.phpserendipity_lang_de.inc.phpserendipity_lang_en.inc.phpserendipity_lang_es.inc.phpserendipity_lang_fi.inc.phpserendipity_lang_fr.inc.phpserendipity_lang_hu.inc.phpserendipity_lang_is.inc.phpserendipity_lang_it.inc.phpserendipity_lang_ja.inc.phpserendipity_lang_ko.inc.phpserendipity_lang_nl.inc.phpserendipity_lang_no.inc.phpserendipity_lang_pl.inc.phpserendipity_lang_pt.inc.phpserendipity_lang_pt_PT.inc.phpserendipity_lang_ro.inc.phpserendipity_lang_sa.inc.phpserendipity_lang_se.inc.phpserendipity_lang_ta.inc.phpserendipity_lang_tn.inc.phpserendipity_lang_tr.inc.phpserendipity_lang_tw.inc.phpserendipity_lang_zh.inc.php

@ -1626,3 +1626,22 @@ function serendipity_getTotalCount($what) {
}
}
/**
* Get a path of all parent categories to a given category.
*
* @access public
* @param string The id of the category, whose parents you want to fetch
* @return array An Array with all category information, ordered from root to the ID you supplied.
*/
function serendipity_getCategoryRoot($id) {
global $serendipity;
$r = serendipity_db_query("SELECT p.*
FROM {$serendipity['dbPrefix']}category n,
{$serendipity['dbPrefix']}category p
WHERE n.category_left BETWEEN p.category_left AND p.category_right
AND n.categoryid = " . (int)$id . "
ORDER BY n.category_left DESC, p.category_left ASC");
return $r;
}

@ -255,6 +255,7 @@ function serendipity_initPermalinks() {
*/
@define('PAT_FILENAME', '0-9a-z\.\_!;,\+\-\%');
@define('PAT_FILENAME_MATCH', '[' . PAT_FILENAME . ']+');
@define('PAT_DIRNAME_MATCH', '[' . PAT_FILENAME . '/]*');
@define('PAT_CSS', '@/(serendipity\.css|serendipity_admin\.css)@');
@define('PAT_FEED', '@/(index|atom[0-9]*|rss|b2rss|b2rdf).(rss|rdf|rss2|xml)@');
@define('PAT_COMMENTSUB', '@/([0-9]+)[_\-][' . PAT_FILENAME . ']*\.html@i');
@ -503,7 +504,7 @@ function serendipity_makePermalink($format, $data, $type = 'entry') {
global $serendipity;
static $entryKeys = array('%id%', '%lowertitle%', '%title%', '%day%', '%month%', '%year%');
static $authorKeys = array('%id%', '%username%', '%realname%', '%email%');
static $categoryKeys = array('%id%', '%name%', '%description%');
static $categoryKeys = array('%id%', '%name%', '%parentname%', '%description%');
switch($type) {
case 'entry':
@ -546,10 +547,23 @@ function serendipity_makePermalink($format, $data, $type = 'entry') {
break;
case 'category':
$parent_path = array();
// This is expensive. Only lookup if required.
if (strstr($format, '%parentname%')) {
$parents = serendipity_getCategoryRoot($data['categoryid']);
if (is_array($parents)) {
foreach($parents AS $parent) {
$parent_path[] = serendipity_makeFilename($parent['category_name'], true);
}
}
}
$replacements =
array(
(int)$data['categoryid'],
serendipity_makeFilename($data['category_name'], true),
implode('/', $parent_path),
serendipity_makeFilename($data['category_description'], true)
);
return str_replace($categoryKeys, $replacements, $format);
@ -571,11 +585,11 @@ function serendipity_makePermalinkRegex($format, $type = 'entry') {
static $entryKeys = array('%id%', '%lowertitle%', '%title%', '%day%', '%month%', '%year%');
static $entryRegexValues = array('([0-9]+)', PAT_FILENAME_MATCH, PAT_FILENAME_MATCH, '[0-9]{1,2}', '[0-9]{1,2}', '[0-9]{4}');
static $authorKeys = array('%id%', '%username%', '%realname%', '%email%');
static $authorKeys = array('%id%', '%username%', '%realname%', '%email%');
static $authorRegexValues = array('([0-9]+)', PAT_FILENAME_MATCH, PAT_FILENAME_MATCH, PAT_FILENAME_MATCH);
static $categoryKeys = array('%id%', '%name%', '%description%');
static $categoryRegexValues = array('([0-9;]+)', PAT_FILENAME_MATCH, PAT_FILENAME_MATCH);
static $categoryKeys = array('%id%', '%name%', '%parentname%', '%description%');
static $categoryRegexValues = array('([0-9;]+)', PAT_FILENAME_MATCH, PAT_DIRNAME_MATCH, PAT_FILENAME_MATCH);
switch($type) {
case 'entry':