Posts Tagged ‘tips’

5 Really Useful Trac Reports

March 5th, 2010

For those of you who are not familiar with the project management software called The Trac Project please proceed to the Trac Guide. I’ve been working with this project management tool for quite some time now and the latest upgrades are just awesome. I believe the solution for private projects’ RSS feeds has been found a few months ago somewhere here. The iCalendar issue remains, but it’s probably solved the exact same way – http authentication, although not all iCalendar clients and RSS aggregators support that yet. My favourite Feedly doesn’t.

Effective Project Management with Trac

Effective Project Management with Trac

Before showing off the snipets that I wrote for Trac Reports I’d like to give you one little hint if you’re running Trac on an old (but stable) OS version, such as Fedora Core 8. Amazon EC2 still ships the Fedora 8 operating system as default for Linux-based EC2 instances, and a simple yum install trac will get you version 0.10.x, which is outdated. The trick here is to install:

1
2
yum install python-setuptools
yum install python-setuptools-devel

Which include a package called easy_install. Since you’ve got an old version of Trac installed, use easy_install to update it, like this:

1
easy_install -U trac

Bam! Wasn’t that easy? One more situation where you can use easy_install is to install Trac plugins directly from their repositories (instead of the old way by uploading source files).

Okay, now back to the reports. I made these with a little experimenting with the Trac database. It’s structure is pretty much transparent and obvious, so if you’re an SQL genius then go ahead and write your own. Here’s my most useful list …

Read the rest of this entry »

Permalink, comment (0) or share:
  • Twitter
  • Digg
  • Facebook
  • del.icio.us
  • FriendFeed
  • Technorati
  • Google Bookmarks
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • StumbleUpon
  • Print
  • email

WordPress: The wp_update_post Dates in Drafts

December 1st, 2009

This one’s pretty tricky. If you’ve ever tried to update a post using the wp_update_post function with drafts or pending posts, you might have noticed that the post_date argument is ignored, instead the current date/time comes up. The post_date argument works only for published posts or during the publishing process. I looked into the WordPress core code, the wp_update_post function has an additional argument called edit_date, which is not mentioned in the codex, so if you’re trying to update an existing post which is a draft or a pending one, use the following method:

1
2
3
4
5
6
7
$post_data = array(
	"ID" => 123,
	"post_date" => $post_date,
	"edit_date" => true
);
 
$post_id = wp_update_post($post_data);

This will update the post 123 and set it’s time to $post_date (which is in format Y-d-m H:i:s if you know what I mean ;). The other possible parameters for wp_update_post are described in the docs for wp_insert_post in the Codex.

Permalink, comment (4) or share:
  • Twitter
  • Digg
  • Facebook
  • del.icio.us
  • FriendFeed
  • Technorati
  • Google Bookmarks
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • StumbleUpon
  • Print
  • email

Adding mod_rewrite Rules to .htaccess in WordPress

November 2nd, 2009

This is all about the Twitter Friendly Links plugin I’ve been working on lately. You might have noticed some 404 issues in the comments section on the plugin page, this in most cases is due to caching fail. I wrote about this a while ago when I was working on W3 Total Cache compatibility where all I had to do is set the wp_query object’s is_404 variable to false. Then again, the other caching plugins didn’t understand that, so I came up with my own links caching mechanism – direct cache with .htaccess ;)

Hacking WordPress Rewrite Rules

Hacking WordPress Rewrite Rules

The WP_Rewrite article in the Codex pretty much explains everything, except that it’s pretty difficult to add custom rewrite rules to .htaccess via WordPress, as even wp_query object’s non_wp_rules is handled like this:

RewriteRule ^Pattern /Substitution [QSA,L]

With the trailing slash and “Query String Append + Last Rule” hard-coded in the WordPress core. Well here’s how I managed to do Permanent 301 redirects to absolute URLs.

First of all add an action and a filter. My plugin code is wrapped up in a class (and I encourage you to do the same for maximum compatibility):

add_action('generate_rewrite_rules', array(&$this, 'generate_rewrite_rules'));
add_filter('mod_rewrite_rules', array(&$this, 'mod_rewrite_rules'));

The generate_rewrite_rules action is the place where we will add some rules to non_wp_rules, then modify (or customize) in the mod_rewrite_rules filter. Here’s the generate_rewrite_rules function which I’m hooking to:

1
2
3
4
5
6
7
8
9
10
function generate_rewrite_rules()
{
	global $wp_rewrite;
	$non_wp_rules = array(
		'simple-redirect/?$plugin_name' => 'http://google.com',
		'one-more-redirect/?$plugin_name' => 'http://yahoo.com'
	);
 
	$wp_rewrite->non_wp_rules = $non_wp_rules + $wp_rewrite->non_wp_rules;
}

I used the string ‘plugin_name’ in every rule just to make sure that I don’t edit anyone else’s rewrite rules further on. You’ll understand what I mean once you read the next part of the code. Also make sure you use single quotes as double quotes would make $plugin_name a variable.

1
2
3
4
5
function mod_rewrite_rules($rules)
{
	$rules = preg_replace('/^(RewriteRule \^.*+\/\?\$)plugin_name (\/)(.*) (\[QSA,L\])$/im', '\1 \3 [R=301,L]', $rules);
	return $rules;
}

There you go, so the rules are modified using a simple regular expression, which in .htaccess will turn out to be:

RewriteRule ^simple-redirect/?$ http://google.com [R=301,L]
RewriteRule ^one-more-redirect/?$ http://yahoo.com [R=301,L]

The regex is not perfect though, just a sketch. I believe it will not work if the RewriteBase is not set to / but you get my point ;) The rules are written to .htaccess when you access or save changes in the Permalinks section in the admin panel or whenever wp_rewrite object’s flush_rules is called. Now be careful, you cannot call flush_rules anywhere outside the admin panel (although you may try to link with a few includes – wp-admin/includes/misc.php and wp-admin/includes/files.php before flushing) but in my case I’ll just go with the publish_post hook.

Permalink, comment (3) or share:
  • Twitter
  • Digg
  • Facebook
  • del.icio.us
  • FriendFeed
  • Technorati
  • Google Bookmarks
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • StumbleUpon
  • Print
  • email

Linux Shell: Host to IP in Bulk

September 8th, 2009

I’m very busy this week setting up a new router here at the office, but I do find some interesting stuff that might be somehow useful to you. Like this shell script I wrote a few hours ago that reads a text file with different hosts on each line (google.com, yahoo.com, etc) and returns a new file with their resolved, i.e. ip addresses on each line. I’m not sure where you’d want to use this, but I’ve setup a server with two internet connections. One is pretty fast but expensive, the second one is slower but free. I made the most useful websites (google.com, etc) go fast and expensive (eth0), and less useful ones (facebook.com, etc) go slower and free of charge (wimax0). I store the useful hosts in a file called special and I use this script (which I called parsehosts.sh) to resolve it to special.resolved:

#!/bin/sh
filename=$1;
echo Parsing ${filename}
output_filename="${filename}.resolved";
echo > ${output_filename}
while read -r LINE ; do
        host $LINE | grep "has address" | sed -e "s/.*has address //" >> ${output_filename} ;
done < ${filename}
echo Done

To invoke the script use:

[root@localhost ~] ./parsehosts.sh special

This generates special.resolved, which I then use in my iptables script to route certain directions. Note that you should chmod +x parsehosts.sh before trying to execute it.

Permalink, comment (0) or share:
  • Twitter
  • Digg
  • Facebook
  • del.icio.us
  • FriendFeed
  • Technorati
  • Google Bookmarks
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • StumbleUpon
  • Print
  • email

Multiblog WordPress: Eeek! No Database Connection

August 5th, 2009

I tried browsing to www.kovshenin.com this morning and was pretty sure I’ll get the usual redirect, but no. I got an “Error establishing a database connection”. Right, it seems that www.kovshenin.com is not defined in the wp-config.php file we created earlier this week (Multiple Sites Driven By One WordPress Installation Part II) so here’s a workaround (and we’re switching back to example.org and example-two.org):

$wp_multi = array(
	"example.org" => array(
		"DB_NAME" => "example",
		"DB_USER" => "example-user",
		"DB_PASSWORD" => "example-pass",
		"DB_HOST" => "example-host"
	),
 
	"example-two.org" => array(
		"DB_NAME" => "example-two",
		"DB_USER" => "example-two-user",
		"DB_PASSWORD" => "example-two-pass",
		"DB_HOST" => "example-two-host"
	)
);
$server_name = str_replace("www." , "", strtolower($_SERVER["SERVER_NAME"]));
$wp_settings = $wp_multi[$server_name];

There. And we also got rid of an error we’d get if we typed eXamPle.org in the address bar.

I’m thinking of a way to wrap this up in some plugin or a little hack, or perhaps a super-short step-by-step tutorial, so that setting up multiple websites one a single wordpress bundle would be easier than ever. If you have any suggestions feel free to speak ;)

Permalink, comment (0) or share:
  • Twitter
  • Digg
  • Facebook
  • del.icio.us
  • FriendFeed
  • Technorati
  • Google Bookmarks
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • StumbleUpon
  • Print
  • email