Ever wanted to change http://site/item/1234 into e.g. http://site/nieuws/1234?
The hack presented in this article explains how to change the item, archive, archives, blog and member keywords that are used in fancy URL mode. Being able to change this could be useful for people running a non-english Nucleus website.
Besides renaming the stub files, four other files need to be changed: nucleus/libs/globalfunctions.php, fancyurls.config.php, .htaccess and index.php
Fancyurls.config.php
In fancyurls.config.php, we're going to add a set of new values to the $CONF array. These will define which keywords will be used when URLs are generated or parsed.
The updated file looks like this:
<?php
// remember: this URL should _NOT_ end with a slash.
$CONF['Self'] = 'http://yoursite.com/path';
// keywords to use in fancy URLs
$CONF['ItemKey'] = 'nieuws';
$CONF['ArchiveKey'] = 'archief';
$CONF['ArchivesKey'] = 'archieven';
$CONF['MemberKey'] = 'medewerker';
$CONF['BlogKey'] = 'blog';
$CONF['CategoryKey'] = 'categorie';
?>
Renaming the stub files
When using the fancy URLs in a default configuration, you'll have a bunch of stub files sitting in your root directory: archive, archives, blog, category, item and member. All of these need to be renamed.
In our example, we're renaming these to archief, archieven, blog, categorie, nieuws and medewerker.
.htaccess
Simply renaming the stub files is not enough, however. You'll also need to update the .htaccess file to reflect the name changes:
<FilesMatch "^nieuws$">
ForceType application/x-httpd-php
</FilesMatch>
<FilesMatch "^archief$">
ForceType application/x-httpd-php
</FilesMatch>
<FilesMatch "^medewerker$">
ForceType application/x-httpd-php
</FilesMatch>
<FilesMatch "^archieven$">
ForceType application/x-httpd-php
</FilesMatch>
<FilesMatch "^categorie$">
ForceType application/x-httpd-php
</FilesMatch>
<FilesMatch "^blog$">
ForceType application/x-httpd-php
</FilesMatch>
Globalfunctions.php
There's only one missing piece in the puzzle: parsing the incoming URLs and generating URLs to be inserted in the output. That's where globalfunctions.php comes in.
Parsing URLs
Around line 259, you'll find the part of code responsible for parsing incoming URLs. edit it as follows:
// decode path_info
if ($CONF['URLMode'] == 'pathinfo')
{
// initialize keywords if this hasn't been done before
if ($CONF['ItemKey'] == '') $CONF['ItemKey'] = 'item';
if ($CONF['ArchiveKey'] == '') $CONF['ArchiveKey'] = 'archive';
if ($CONF['ArchivesKey'] == '') $CONF['ArchivesKey'] = 'archives';
if ($CONF['MemberKey'] == '') $CONF['MemberKey'] = 'member';
if ($CONF['BlogKey'] == '') $CONF['BlogKey'] = 'blog';
if ($CONF['CategoryKey'] == '') $CONF['CategoryKey'] = 'category';
$data = explode("/",serverVar('PATH_INFO'));
for ($i=0;$i<sizeof($data);$i++) {
switch ($data[$i]) {
case $CONF['ItemKey']: // item/1 (blogid)
$i++;
if ($i<sizeof($data)) $itemid = intval($data[$i]);
break;
case $CONF['ArchivesKey']: // archives/1 (blogid)
$i++;
if ($i<sizeof($data)) $archivelist = intval($data[$i]);
break;
case $CONF['ArchiveKey']: // two possibilities: archive/yyyy-mm or archive/1/yyyy-mm (with blogid)
if ((($i+1)<sizeof($data)) && (!strstr($data[$i+1],'-')) ){
$blogid = intval($data[++$i]);
}
$i++;
if ($i<sizeof($data)) $archive = $data[$i];
break;
case 'blogid': // blogid/1
case $CONF['BlogKey']: // blog/1
$i++;
if ($i<sizeof($data)) $blogid = intval($data[$i]);
break;
case $CONF['CategoryKey']: // category/1 (catid)
case 'catid':
$i++;
if ($i<sizeof($data)) $catid = intval($data[$i]);
break;
case $CONF['MemberKey']:
$i++;
if ($i<sizeof($data)) $memberid = intval($data[$i]);
break;
default:
// skip...
}
}
}
In this code, all hardcoded keyword occurrences (e.g. 'item') have been replaced by the matching keyword variable ($CONF['ItemKey'])
generating URLs
The code which generates URLs is a little further down the code, around line 920. Update this code in the same way as the URL-parsing code: by replacing keywords by the matching $CONF variable:
/**
* Centralisation of the functions that generate links
*/
function createItemLink($itemid, $extra = '') {
global $CONF;
if ($CONF['URLMode'] == 'pathinfo')
$link = $CONF['ItemURL'] . '/' . $CONF['ItemKey'] . '/' . $itemid;
else
$link = $CONF['ItemURL'] . '?itemid=' . $itemid;
return addLinkParams($link, $extra);
}
function createMemberLink($memberid, $extra = '') {
global $CONF;
if ($CONF['URLMode'] == 'pathinfo')
$link = $CONF['MemberURL'] . '/' . $CONF['MemberKey'] . '/' . $memberid;
else
$link = $CONF['MemberURL'] . '?memberid=' . $memberid;
return addLinkParams($link, $extra);
}
function createCategoryLink($catid, $extra = '') {
global $CONF;
if ($CONF['URLMode'] == 'pathinfo')
$link = $CONF['CategoryURL'] . '/' . $CONF['CategoryKey'] . '/' . $catid;
else
$link = $CONF['CategoryURL'] . '?catid=' . $catid;
return addLinkParams($link, $extra);
}
function createArchiveListLink($blogid = '', $extra = '') {
global $CONF;
if (!$blogid)
$blogid = $CONF['DefaultBlog'];
if ($CONF['URLMode'] == 'pathinfo')
$link = $CONF['ArchiveListURL'] . '/' . $CONF['ArchivesKey'] . '/' . $blogid;
else
$link = $CONF['ArchiveListURL'] . '?archivelist=' . $blogid;
return addLinkParams($link, $extra);
}
function createArchiveLink($blogid, $archive, $extra = '') {
global $CONF;
if ($CONF['URLMode'] == 'pathinfo')
$link = $CONF['ArchiveURL'] . '/' . $CONF['ArchiveKey'] . '/'.$blogid.'/' . $archive;
else
$link = $CONF['ArchiveURL'] . '?blogid='.$blogid.'&archive=' . $archive;
return addLinkParams($link, $extra);
}
function createBlogLink($url, $params) {
return addLinkParams($url . '?', $params);
}
function createBlogidLink($blogid, $params = '') {
global $CONF;
if ($CONF['URLMode'] == 'pathinfo')
$link = $CONF['BlogURL'] . '/' . $CONF['BlogKey'] . '/' . $blogid;
else
$link = $CONF['BlogURL'] . '?blogid=' . $blogid;
return addLinkParams($link, $params);
}
Further extending
The URL generation/parsing system could be even further extended by adding plugin events (see Task 12: generate-url-event on the bug tracker). With such a system, super-fancy URLs like http://site/item/My_Item_Title could be supported without having to replace each <%itemlink%> by <%NP_FancierURL%>.
Posted by karma at 14:37:39. Filed under: Nucleus Hacks

Comments
Add Comment