Category Archives: code

WordPress GeSHi plugin optimization and modification.

Download link at the bottom.

Yesterdays research has pointed me one important issue – the WP-syntax plugin was taking way too long to process the homepage. And it wasn’t only the WP-Syntax but also other syntax-highlighting plugins including WP-GeSHi-highlight and Better WordPress Syntax Highlighter.
From few tested I found that WP-GeSHi-Highlight has the best performance but still – it wasn’t so great.
I was thinking – is there any way to speed up this plugin (or at least minimize the “damage”)?

The plugin itself is very well commented and is very simple, this is the simplified flow of the script:

  1. Plugin init
  2. Loop through posts to display and parse full text looking for <pre lang=”> tag
  3. If found – mark the place with unique identifier
  4. Replace every unique identifier with the GeSHi output

What if one of the posts doesn’t contain <pre lang=”> tag? The plugin still has to parse whole text to look for it. This doesn’t sound good if you want to highlight code only in a fraction of your posts.

Before I did anything else I wanted to run a couple of tests:

WP-GeSHi-Highlight disabled, didn’t parse anything – only initialization.


WP-GeSHi-Highlight enabled, parsed 3 posts.


WP-GeSHi-Highlight enabled, parsed all 10 posts.

After done with the profiling I decided it is worth to modify the plugin.
The new feature works in a very simple way, go to your dashboard and under Settings->WP-GeSHi-Highlight you can set the tag with which posts will be parsed.

The last thing you need to do is to tag all the posts with the code snippets and the plugin will do all the rest for you.

Keep your load times low and your wordpress will love you forever!

You can download the modified plugin here.

Conky setup with custom gauges, twitter and wlan status.

This is a follow-up of previous posts about conky and a summary.
Full source of the conky setup can be downloaded from here.

The full twitter script can be found in the previous post and along other important files in the archive.

The directory structure I am using is the following:

~/conky
     |- left
     |- right
     |- /scripts
         |- twit.py
         |- conky_lunatico.lua

It’s a simple structure using two conky instances for both sides of the screen with separate configuration files.
The most important bits of the left part are the following:

...
maximum_width 270
own_window yes
own_window_type override
own_window_transparent yes
own_window_hints undecorate,sticky,skip_taskbar,skip_pager,below
border_inner_margin 0
border_outer_margin 0
alignment top_left
...
 
//to set up the window properly and
...
${color1}${execi 60 python ~/conky/scripts/twit.py | fold -w45}
...

to include the twitter script.

Configuration of the right side has the following key items:

...
minimum_size 180 500
maximum_width 180
own_window yes
own_window_type override
own_window_transparent yes
own_window_hints undecorate,sticky,skip_taskbar,skip_pager,below
border_inner_margin 0
border_outer_margin 0
alignment tr
...
 
//as previously - setting up the window on the top-right side of the screen and
 
...
lua_load ~/conky/scripts/conky_lunatico.lua
lua_draw_hook_post main
...

To display the nice looking graphs.

And that’s pretty much of it!
For the graph position you will have to edit conky_lunatico.lua script to change values or change the data you are trying to display (I’m showing 7 separate graphs for each value, probably you will want to remove second WLAN graph from the script and config or change the name of your wireless adapter, it’s up to you.

To see it in action simply run conky -c ~/conky/right && conky -c ~/conky/left. For the how-to run this no your system startup and the last post of the overall desktop setup please take a look at the next post explaining Compiz and terminal setup.

Latest tweets in your conky setup.

After getting a clean Ubuntu installation I decided to make few tweaks including a custom conky script which included a Twitter feed.

I made a quick research I found out the easiest solution using RSS feed is outdated so I have had to find the other way – and I did here.
As you can read there the only requirement is python-twitter package (and ofcourse python itself).

I have updated the code as it went obsolete when Twitter staff decided to complicate their API even more.

To make this script work you need a few of things:

1. Twitter account (duh)

2. Create Twitter application

3. Receive access tokens

After you will receive all required tokens you have to put them in marked place in the code below.

'''
Original author: Travis Moore (@travist120) from http://travist120.wordpress.com/
Updated by: Peter Kasperski (peter@kasperskiweb.pl) from http://kasperskiweb.pl
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see 
 
'''
import os
import string
import twitter
 
#set your API access details here.
consumer_key='CONSUMER KEY'
consumer_secret='CONSUMER SECRET'
access_token = 'ACCESS TOKEN'
access_token_secret = 'ACCESS TOKEN SECRET'
 
#how many tweets you want displayed at a time
twitterSize = 9;
 
#do not edit beyond this line.
api = twitter.Api(consumer_key, consumer_secret, access_token, access_token_secret)
status = api.GetFriendsTimeline()
i = 0
for s in status:
	i = i+1
	print s.user.name.encode("utf-8"), "(@"+s.user.screen_name.encode("utf-8")+"):"
 
	print s.text.encode("utf-8")
	print
	if i == twitterSize:
		break

The easy way to include it in your conky script is the following:

${execi 60 python ~/conky/scripts/twit.py | fold -w45}

If you will get any error try to run the script alone and make sure you have all required python libraries installed.

T_PAAMAYIM_NEKUDOTAYIM error in PHP

Today I’ve encountered a very strange error which was quite similar to Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in….
At the first look I thought it’s a joke from the second programmer I’ve been working with but I was wrong.

This error is addressing to Scope Resolution Operator (::).
The problem is that this error can happen in quite unpredictable places – like in my example:

define('IPHONE',true);
 
[...]
 
if(defined(IPHONE)){
	$mediumlimit = " AND (Media=0 OR Media=2)";
} else {
	$mediumlimit = " AND (Media=0 OR Media=1)";
}

Line if(defined(IPHONE)){ was throwing an error.
When I’ve added quotes around IPHONE constant name the error was gone!

Sometimes error messages can cause headache when they are weird and completely not related to actual code – god bless google for letting me fix it in no time!

UTF-8 filenames under Windows / PHP

If you read this, probably you have problems with files with names containing unicode characters.

My scenario was simple:

  1. User uploads own file with unicode characters (ex. my_unicode_ąść.jpg)
  2. PHP handles the upload and moves uploaded file to a directory on your server and creates a database record with original filename(ex. my_unicode_ąść.jpg)
  3. Because lack of unicode support in PHP filename is saved with wrong encoding (multibyte string is split into one-byte characters, which in case of unicode multibyte encoding renders wrong, ex. my_unicode_ść.jpg)
  4. Any future call using database stored name fails because two names are different.

To get rid of this problem I have used PHP UTF8 library.

It is as simple as that:

include("../utf8toascii/utf8_to_ascii.php");
utf8_to_ascii($_FILES['Filedata']['name'])

From now on, before working with filename, first we change any unicode character to it’s closest ASCII version.

We have to do this and patiently wait for PHP6 release, which promises native UFT-8 support.

Flash dropping session cookies

If you have ever used SWFupload with PHP application using session authentication you could run into similar problem.

SWFUpload is a small JavaScript/Flash library to get the best of both worlds. It features the great upload capabilities of Flash and the accessibility and ease of HTML/CSS.
 

Your users are able to upload multiple files using flash uploader after they log into your website. While their browser have no problem with cookie / session handling, flash application ignores session id and in the result, instead of typical ajax result – your reply from server will contain a page with a login form.

This is a very old bug(?), reported back in 2007 (or even before), still available in newest flash player.

Bug affects following browsers / versions:

  • Firefox / Opera / Chrome
  • Flash player 9
  • SWFUpload v2.2.0.1
I have no information on other versions but it seems like bug affects every non-IE browser and every flash player version

There are few workarounds out there, according to Evert Pot and his blog.

In case you are working on a bigger project, where you cannot simply override authorisation I suggest simply upgrading SWFUpload to SWFUpload v2.5.0 Beta 3 – it should do the trick, but beware that it is still beta, unstable version!

I’ve spent too much time to debug this, so for all of you going the same way – please follow my guidelines so you can spend additional hours with your family.