Rewrite Rules in Habari

Morgante Pell is currently working on a plugin to manage rewrite rules in Habari. After looking at the code he suggested to change an entry’s permalink, I realized Habari never defined what are rewrite rules, this is my personal definition.

public function action_init() {
$rule= RewriteRules::by_name('display_entry');
$rule= $rule[0];

$rule->parse_regex= '%(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[^/]+)(?:/page/(?P\d+))?/?$%i';
$rule->build_str= '{$year}/{$mon0}/{$mday0}/{$slug}(/page/{$page})';
}

Rewrite rules are a library of supported requests. Rules shouldn’t be altered. We try to force people to use plugin hooks to modify Habari’s behavior. Simply put, if one wants to change Habari’s permalink, he should filter the Post::get_permalink() method. Of course, the developers of Habari tried to foresee most hooks people would need. This one was miss.

Therefore, Habari should first add the filter hook “post_permalink_rules”. Afterwards, we could alter the array of default rules to use for permalink generation.

private function get_permalink()
{
$content_type = Post::type_name( $this->content_type );
$permalink_rules = array(
"display_{$content_type}",
"display_post"
)
$permalink_rules = Plugins::filter( "post_permalink_rules", $permalink_rules, $this );

return URL::get(
$permalink_rules,
$this,
false
);
}

In the end, I could compare this to Gmail’s premise: “you’ll never need to delete another message.” We should never delete or alter existing core rules. Rewrite rules use a name string, but that string does not indicate its function, just a human-readable identifier. Their function is defined by the “handler” and the “action” parameters.


Proudly powered by Habari.