RationalSpace

Archive for the ‘Drupal’ Category

Drupal Page Caching

leave a comment »

Drupal as a CMS is pretty slow in rendering pages unless its caching is turned on.

The way caching works is that any page request that comes to the system gets logged in database table called cache_page. Any subsequent request for the same page gets rendered from there. You can set this from Administration->Configuration->Performance.

You can set from here how long would you want the cache to be alive. Basically, when should the cache expire.

Now, this does make pages render faster but then it comes with drawbacks. You really can’t render anything that is dynamic. If cache exists your page will get rendered from there.

Now how does one make one’s site load faster? – Have everything loaded in this table cache_page for the first time user.

You can do this by simply setting up a crawler that goes through your sitemap and fetches all the URLs. This does not sound as very good plan cause even for a site of lets say =~ 2K pages – this will take a couple of hours to finish ! But then it works:) Before running the crawler you may want to manually flush the cache first.


include_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
cache_clear_all( '*', 'cache_page', TRUE );


Now , another problem with Drupal is that page caching can be set to maximum lifetime of 1 day! Now this is again not desired at least for static pages. You might want your cache to be rebuild only once a month or something!

I hacked this by changing the database setting directly:

update variable set value='s:6:"604800";' where name = 'cache_lifetime';

Also, you can set expire column to be 0 – which will mean its a permanent cache unless you want to flush it manually.

update cache_page set expire=0

So by combining these two techniques, you can over-ride Drupal’s basic settings and create page cache when you desire.

Written by rationalspace

April 11, 2013 at 4:57 pm

Posted in Drupal

Tagged with , , ,

Creating hierarchical structure in Drupal content pages

leave a comment »

In a typical website structure , it is often found that there is  a main content type and several other content-types  linked to that content in a parent-child relationship. So you may want to store a unique identifier in the main content type and make this accessible to all its children content-types. You may also want  the urls of children to be defined with a proper structure linking them to the parent.

A typical example would be  movie page being your main content and synopsis of the movie as child page.

You can do this in drupal by using a module called node-hierarchy. Now if you enable this module you can go the page and set its’ parents/children. But my problem was a little complicated. I had a data set of around 8K pages i.e.  almost 8000 nodes and I wanted to create 3 child nodes of each node using a script. It is not possible to do this manually.

Here’s the code that I came up with:

<?php

//get all the nodes that you wish to be the parent nodes

foreach($nodes as $nid){

$plid = _nodehierarchy_get_node_mlid($nid,true); //parent node

//create a new node
$node = new StdClass();//creating a bare node
$node->type = 'whatever';
$node->status = 1;//give it a published staus
$node->title = "whatever";
$node->created = time();
$node->comment = 1;
$node->promote = 0;
$node->sticky = 0;
$node->uid = 1;// the user id who owns the node
$node->language = 'und';

node_submit($node);
node_save($node);

$nodehierarchy_menu_links = _nodehierarchy_default_menu_link($nid, $plid);
$nodehierarchy_menu_links['pnid']=$nid; // <----- Give the menu link the parent id
$nodehierarchy_menu_links['hidden'] = FALSE;
$nodehierarchy_menu_links['enabled'] = TRUE;

$nodehierarchy_old_menu_links = $nodehierarchy_menu_links;
$nodehierarchy_old_menu_links['enabled'] = TRUE;
unset($nodehierarchy_old_menu_links['hidden']);
unset($nodehierarchy_old_menu_links['link_path']);
$nodehierarchy_old_menu_links['link_title'] = '';
$nodehierarchy_old_menu_links['expanded'] = '';
$nodehierarchy_old_menu_links['description'] = '';
$nodehierarchy_old_menu_links['customized'] = '';

$node->nodehierarchy_menu_links[] = $nodehierarchy_menu_links;
$node->nodehierarchy_old_menu_links[] = $nodehierarchy_old_menu_links;

_nodehierarchy_save_node($node); // <---- Let nodeheirarchy do it's thing
$newnodeid = $node->nid;

}

?>

Now if you go to your parent node page, you should be able to see the children as well . You can also add a piece of code that will create the url_alias as you wish to be for each child node.

For example:

<?php

$path = array ();
$path['source'] = 'node/'.$newnodeid;
$path['alias'] = '/parenturl/content-type/childurl';
path_save($path);

?>

Written by rationalspace

April 8, 2013 at 11:39 am

Posted in Drupal

Redirecting www to non-www URL in Drupal

leave a comment »

Well, this is a pretty common requirement for any website. You may want only one URL to be your primary URL – either with www or without it and the other one should get redirected to the first.

This is fairly straightforward to do by putting a redirect rule in apache config.

But in case you are using Drupal or some other CMS, make sure you check its .htaccess file and then do a change there. Mostly you will have to look for these lines and make sure they are not commented.

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

Written by rationalspace

April 8, 2013 at 10:55 am

Posted in Apache, Drupal

Drupal module not being loaded/called ?

leave a comment »

This is quite frustrating for anybody who is new to drupal/CMS. You have just written a new module and its simply not getting called. The “_init” hook is not working. One can spend hours and hours on debugging this and not arrive at a conclusion.

Well, I did so too and figured that its cause of caching. To make sure while you are working, your code is not getting cached, enable fake Cache in sites/default/settings.php

Here is the piece of code you need to add:

include_once(DRUPAL_ROOT . ‘/includes/cache.inc’);
include_once(DRUPAL_ROOT . ‘/includes/cache-install.inc’);
$conf[‘cache_default_class’] = ‘DrupalFakeCache’;

Once this is set, the page will load with no cache and hence any change you make in the code will get reflected.

Once you are ready for production, you can enable caching in Admin->Configuration->Development->Performance->Caching

Another place to check is the database cache tables:

cache_page  – caches each page request

cache_block – caches modules

You may simply look in this table, delete the entry that you have modified and load it again to check results.

But be careful about cache_form table. It is not really a cache table and emptying it will invalidate all forms on the site.

Written by rationalspace

February 13, 2013 at 4:32 am

Posted in Drupal

Tagged with , ,

%d bloggers like this: