Get MySQL Table Sizes

Posted March 19th, 2013 in Database

Here is a super simple SQL query to determine the size of all tables in a given database:

SELECT TABLE_NAME AS "Table",
	round(((data_length + index_length) / 1024 / 1024), 2) AS Size_in_MB
FROM information_schema.TABLES 
WHERE table_schema = 'my_db_name'
ORDER BY Size_in_MB DESC

This can be very useful when debugging for instance in figuring out which rogue plugin/module on your site is dumping hundreds of megs of data into your database.

Read More »

Fix Home and End Keys on Mac OS X

Posted March 18th, 2013 in Uncategorized

Disclaimer: The details of this post are shamelessly ripped from phatness.com. All credit to those guys, I just wanted a copy for myself for future reference.


On every OS ever, Home, End, Pg Up and Pg Down keys work like so:

- Home -> move the cursor to the beginning of the line
- End -> move the cursor to the end of the line
- Pg-Up -> move the cursor up the length of the viewport
- Pg-Dn -> move the cursor down the length of the viewport

But OSX in all its infinate wisdom decided that doesn’t make sense. Instead they should work like so:

- Home -> move (nothing, not even the cursor, just your view) to the beginning of the DOCUMENT
- End -> move (nothing, not even the cursor, just your view) to the end of the DOCUMENT
- Pg-Up -> move (nothing, not even the cursor, just your view) up the length of the viewport
- Pg-Dn -> move (nothing, not even the cursor, just your view) down the length of the viewport

Now I’m not one to criticise… but that’s stupid and anyone who likes it is stupid ;)

 

How to Fix

There’s a really useful open source utility out there called DoubleCommand which is highly recommended and I’m sure broadly used. If you want a non-application solution there are also some config files to edit to get the job done.

Most Applications

Create a file
/home/<username>/Library/KeyBindings/DefaultKeyBinding.dict and add the following:

{
    "UF729"  = "moveToBeginningOfLine:";
    "$UF729" = "moveToBeginningOfLineAndModifySelection:";
    "UF72B"  = "moveToEndOfLine:";
    "$UF72B" = "moveToEndOfLineAndModifySelection:";
    "UF72C"  = "pageUp:";
    "UF72D"  = "pageDown:";
}

Terminal

Terminal preferences

Terminal preferences

Open Terminal – Preferences – Settings – Keyboard. Edit each of the following setting their action to ‘send string to shell’.

Key		Escape Sequence
 
Home		33[1~
End		33[4~
Page Up		33[5~
Page Down	33[6~

Now open /home/<username>/.inputrc and add:

"e[1~": beginning-of-line
"e[4~": end-of-line
"e[5~": history-search-backward
"e[6~": history-search-forward
"e[3~": delete-char
"e[2~": quoted-insert
"e[5C": forward-word
"e[5D": backward-word
"ee[C": forward-word
"ee[D": backward-word
set completion-ignore-case On

Other notes

Restart your machine for changes to take affect. Restarting individual applications also works if you can’t restart for whatever reason. I hear Firefox needs its own changes – if you happen to know what those are comment below and I’ll add them in.

Read More »

How to Drastically Speed Up WordPress with Redis

Posted March 16th, 2013 (Updated 25 Mar 2013) in Database, PHP

I recently came across a tutorial on sitting Redis infront of WordPress allowing for insanely fast page generation. I gave it a try and it really works, in fact I’m now using it on this very site! The best part however is the fact that the script requires absolutely no modification to your existing WordPress site save for 1 line of htaccess. Truly amazing.

Below I’ll detail my slightly modified version of Jim’s script along with some metrics.

 

Firstly, What is Redis and what will it do for me?

The Redis website describes Redis as

… an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain stringshasheslistssets and sorted sets.

What does this mean? Essentially it’s Memcached but more useful. Redis stores key-value pairs in memory and spits them out when requested. Unlike Memcached it has built in persistence but what’s most important to us is that it’s fast – very fast.

We’ll be using Redis to speed up our site by loading cached pages from it directly without even booting up WordPress. This will save a large amount of page generation time and get out site infront of our users’ eyeballs faster.

 

Exactly how much faster are we talking?

In my very unscientific tests, loading www.flynsarmy.com a bunch of times resulted in the following:

Before (Secs) After (Secs)
1.556
0.468
0.494
0.498
0.492
0.514
0.499
0.511
0.499
0.02001
0.00896
0.00883
0.00959
0.01472
0.00916
0.00915
0.00756
0.01989

As you can see from the table above this equates to a 20x to 50x speed increase and that was WITH W3 Total Cache installed! Results of course may vary but I think you get the picture.

Read More »

Edit WordPress Posts from the Front End

Posted March 5th, 2013 (Updated 13 Mar 2013) in PHP

I have a project coming up involving editing WordPress posts from the front end of the site. There are a bunch of plugins that let you do this the coolest of which seems to be Front-end Editor but I wanted to come up with my own solution. Luckily it turned out to be surprisingly quick and painless!

For this tutorial I’ll be editing the cafe custom post type from my last post.

Read More »

Adding Custom Post Types, Fields and Taxonomies in WordPress

Posted March 5th, 2013 (Updated 13 Mar 2013) in PHP

This information is pretty readily available on the internet but I figured I’d make my own post for safe keeping. I’ll be adding a custom post type (Cafes) with 3 custom fields (Website, Address and Phone) and a custom taxonomy (Countries).

The following code all goes in your functions.php file.

 

Custom Post Types

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * Add Cafe post type
 */
function create_cafe_post_type() {
	register_post_type( 'cafes',
		array(
			'labels' => array(
				'name' => 'Cafes',
				'singular_name' => 'Cafe',
				'add_new' => 'Add New',
				'add_new_item' => 'Add New Cafe',
				'edit_item' => 'Edit Cafe',
				'new_item' => 'New Cafe',
				'view_item' => 'View Cafe',
				'search_items' => 'Search Cafes',
				'not_found' =>  'Nothing Found',
				'not_found_in_trash' => 'Nothing found in the Trash',
				'parent_item_colon' => ''
			),
			'public' => true,
			'publicly_queryable' => true,
			'show_ui' => true,
			'query_var' => true,
			//'menu_icon' => get_stylesheet_directory_uri() . '/yourimage.png',
			'rewrite' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'menu_position' => null,
			'supports' => array('title','editor','thumbnail')
		)
	);
}
add_action( 'init', 'create_cafe_post_type' );

 

Custom Taxonomies

Taxonomies are basically tags for your post types. Add the following below the register_post_type call above:

1
2
3
4
5
6
7
//Cafe countries taxonomy
register_taxonomy("countries", array("cafes"), array(
	"hierarchical" => false,
	"label" => "Countries",
	"singular_label" => "Country",
	"rewrite" => true
));

 

Custom Fields

This is the hardest of the lot. It’s not really hard though, just alot of HTML to make the meta boxes. You can put any HTML you like in meta boxes, I just like to keep mine looking like standard WP fields:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/**
 * Add cafe custom fields
 */
function add_cafe_meta_boxes() {
	add_meta_box("cafe_contact_meta", "Contact Details", "add_contact_details_cafe_meta_box", "cafes", "normal", "low");
}
function add_contact_details_cafe_meta_box()
{
	global $post;
	$custom = get_post_custom( $post->ID );
 
	?>
	<style>.width99 {width:99%;}</style>
	<p>
		<label>Address:</label><br />
		<textarea rows="5" name="address" class="width99"><?= @$custom["address"][0] ?></textarea>
	</p>
	<p>
		<label>Website:</label><br />
		<input type="text" name="website" value="<?= @$custom["website"][0] ?>" class="width99" />
	</p>
	<p>
		<label>Phone:</label><br />
		<input type="text" name="phone" value="<?= @$custom["phone"][0] ?>" class="width99" />
	</p>
	<?php
}
/**
 * Save custom field data when creating/updating posts
 */
function save_cafe_custom_fields(){
  global $post;
 
  update_post_meta($post->ID, "address", @$_POST["address"]);
  update_post_meta($post->ID, "website", @$_POST["website"]);
  update_post_meta($post->ID, "phone", @$_POST["phone"]);
}
add_action( 'admin_init', 'add_cafe_meta_boxes' );
add_action( 'save_post', 'save_cafe_custom_fields' );

 

Custom Templates

You may want your new post type to look different from standard posts. This can be done using custom templates. The WordPress codex cover custom post type templates nicely but I’ll quickly go over what I did for completeness. I’m using twentytwelve theme in my demo but this should apply for most themes.

  1. Duplicate single.php into single-<post type>.php
  2. Replace
    <?php get_template_part( 'content', get_post_format() ); ?>

    with

    <?php get_template_part( 'content', '<post type>' ); ?>
  3. Duplicate content.php into content-<post type>.php and modify however you like

 

Conclusion

And we’re done! Always remember to check the WordPress documentation for the various functions involved if you want to customize. Lots of useful information in there.

Read More »

How to Load WordPress Multi-Site in a script

Posted February 12th, 2013 (Updated 14 Mar 2013) in PHP

This morning I was loading WPMU in a custom script and all get_option() lookups were returning the details for the main blog despite being in a switch_to_blog() block. After a bit of debugging it turns out this was due to get_option() caching its values from the main blog and never expiring its cache when switching. There’s a pretty easy action you can use to fix this:

1
2
3
4
5
6
7
8
function switch_to_blog_cache_clear( $blog_id, $prev_blog_id = 0 ) {
    if ( $blog_id === $prev_blog_id )
        return;
 
	wp_cache_delete( 'notoptions', 'options' );
	wp_cache_delete( 'alloptions', 'options' );
}
add_action( 'switch_blog', 'switch_to_blog_cache_clear', 10, 2 );

Below is a script in its entirety for loading WPMU, switching to blog 31 and grabbing its name:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
 
$_SERVER['HTTP_HOST'] = 'mypolicedev.com';
 
ini_set('display_errors', true);
 
/**
 * BEGIN LOAD WORDPRESS
 */
function find_wordpress_base_path() {
	$dir = dirname(__FILE__);
	do {
		//it is possible to check for other files here
		if( file_exists($dir."/wp-config.php") ) {
			return $dir;
		}
	} while( $dir = realpath("$dir/..") );
	return null;
}
 
define( 'BASE_PATH', find_wordpress_base_path()."/" );
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');
 
function switch_to_blog_cache_clear( $blog_id, $prev_blog_id = 0 ) {
	if ( $blog_id === $prev_blog_id )
		return;
 
	wp_cache_delete( 'notoptions', 'options' );
	wp_cache_delete( 'alloptions', 'options' );
}
add_action( 'switch_blog', 'switch_to_blog_cache_clear', 10, 2 );
 
/**
 * END LOAD WORDPRESS
 */
 
switch_to_blog( 31 );
var_dump(get_bloginfo('name'));
restore_current_blog();

Read More »

Create and Manage a RAID Array in Ubuntu

Posted December 30th, 2012 (Updated 31 Dec 2012) in Linux

The below is a collaboration of useful information I’ve found while attempting to build and maintain a RAID5 array consisting of 4 HDDs. This tutorial is a work in progress and I’m learning everything as I go. I’ve left relevant links in each section for more information and if you spot anything that could be done better or that I’m missing let me know in the comments below!

Read More »

How to get CodeIntel working in Sublime Text on OSX

Posted November 24th, 2012 in Uncategorized

SublimeCodeIntel is perhaps the most useful and heavily installed package for Sublime Text but it’s also very difficult to get working. The most common issue people run into is the dreaded endless

Info: processing `PHP’: Please wait…

This can be fixed by navigating to the SublimeCodeIntel package directory and running build.sh. For build.sh to compile we first need to install two things: pcre and XCode Command Line Tools. Annoyingly this is around 1.6GB of files!

 

Installing XCode Command Line Tools

Head over here and click View in Mac App Store. Hit install and watch your progress in the Purchases tab.

Once installed open it up and go to XCode – Preferences – Downloads – Components and install Command Line Tools.

 

Installing pcre

This little script is required by the scintilla package of SublimeCodeIntel. First install MacPorts then run:

sudo port install autoconf
sudo port install pcre
sudo cp /opt/local/include/pcre.h /usr/include/

You’re done! Navigate to the SublimeCodeIntel package directory and run sh src/build.sh. If all goes well at the end of compilation you’ll see a done! message. Restart Sublime Text and you’re good to go.

Read More »

Installing RocketRaid 2760a drivers on Ubuntu 12.10

Posted November 8th, 2012 (Updated 30 Dec 2012) in Linux

So I don’t forget, here’s a tutorial on installing the RocketRaid 2760a drivers and management utilities on Ubuntu 12.10. I’m using 64-bit but 32-bit should be the same – just substitute where appropriate.

Installing the Driver

The installation disc comes with an out of date version of the driver and kernel module. The way the installation works, we need to download and compile the kernel module then download the driver installer, drop the module into place and run the installer. Make sure you have appropriate compiler tools installed – to be safe just run

sudo apt-get install ubuntu-dev-tools

Download and extract the ones with kernel 3.x support from the HighPoint website:

mkdir ~/driver
cd ~/driver
# driver
wget http://www.highpoint-tech.com/BIOS_Driver/RR276x/linux/Ubuntu/rr276x-ubuntu-11.10-x86_64-v1.1.12.0502.tgz
tar -xvzf rr276x-ubuntu-11.10-x86_64-v1.1.12.0502.tgz
# kernel module
mkdir module
cd module
wget http://www.highpoint-tech.com/BIOS_Driver/RR276x/linux/RR276x-Linux-Src-v1.1-120424-1734.tar.gz
tar -xvzf  RR276x-Linux-Src-v1.1-120424-1734.tar.gz

Build and compress the kernel module for our driver installer:

cd rr276x-linux-src-v1.1/product/rr276x/linux/
make
# Ignore the warning about not being able to find him_rr2760x.o...doesn't seem to matter
gzip rr276x.ko
mv rr276x.ko.gz ~/driver/boot/rr276x$(uname -r)$(uname -m).ko.gz

And finally install the driver

cd ~/driver
sudo bash preinst.sh

This step succeeded! Now you can press ALT+F1 to switch back to the installation screen!

sudo bash install.sh

Update initrd file /boot/initrd.img-3.5.0-17-generic for 3.5.0-17-generic
Please reboot the system to use the new driver module.

sudo shutdown -r now

That should be everything! You can now test with

cat /proc/scsi/rr276x/*

 

Installing the RAID Management Software

Annoyingly the RAID management console is difficult to install to say the least. The GUI deb packages error when trying to install and HighPoint don’t even provide a deb for the command line version and to top it off the version of the command line utility on the driver CD is newer than the version on their site! For this reason I’ve provided the newer command line RPMs here.

Let’s get this thing installed.

Web version:

mkdir ~/driver/utility
mkdir ~/driver/utility/console
mkdir ~/driver/utility/web
cd ~/driver/utility/web
echo "rr276x" | sudo tee -a /etc/hptcfg > /dev/null
wget http://www.highpoint-tech.com/BIOS_Driver/GUI/linux/WebGui/WebGUI-Linux-v2.1-120419.tgz
tar -xzvf WebGUI-Linux-v2.1-120419.tgz
sudo apt-get -y install alien
sudo alien -d hptsvr-https-2.1-12.0419.$(uname -m).rpm
sudo dpkg -i hptsvr-https_2.1-13.0419_amd64.deb

 

Command Line version:

cd ~/driver/utility/console
wget http://www.flynsarmy.com/wp-content/uploads/2012/11/LinuxRaidUtilityConsole.tar.gz
sudo alien -d hptraidconf-3.5-1.$(uname -m).rpm
sudo alien -d hptsvr-3.13-7.$(uname -m).rpm
sudo dpkg -i hptraidconf_3.5-2_amd64.deb

Run the web server:

sudo hptsvr

You should now be able to connect to it from your browser by navigating to http://localhost:7402 with username RAID and password hpt.

 

Further Reading

HOWTO: Get GUI hptsvr & hptraid 3.13 working on Ubuntu
HighPoint driver download page for RocketRaid 2760a

Read More »

Partitioning and Formatting New Disks in Linux

Posted November 4th, 2012 in Linux

The below is a quick guide to creating partitions on a newly purchased, unformatted disk. For this guide I’ll be formatting a new WD 2TB Black.

 

Find the disk you want to partition

# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 55.9G 0 disk
├─sda1 8:1 0 53.6G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 2.3G 0 part [SWAP]
sdb 8:16 0 1.8T 0 disk
sdc 8:32 0 1.8T 0 disk

Partitions appear as subitems. Notice sdb and sdc have no partitions – those are the disks I want to format.

 

Create the partition

# sudo fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd3e43840.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)

Command (m for help):

Below I’m using the default values to create one large partition for the whole disk.

n

Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended

Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-3907029167, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-3907029167, default 3907029167):
Using default value 3907029167

Command (m for help):

All done? Write!

w

The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

That’s all for partitioning! Take a gander at your fancy new partitions:

# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 55.9G 0 disk
├─sda1 8:1 0 53.6G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 2.3G 0 part [SWAP]
sdb 8:16 0 1.8T 0 disk
└─sdb1 8:17 0 1.8T 0 part
sdc 8:32 0 1.8T 0 disk
└─sdc1 8:33 0 1.8T 0 part

 

Format to NTFS

I’m choosing a quick format. You may choose instead to remove the -f argument for a proper one instead.

# sudo mkntfs -f /dev/sdb1

Cluster size has been automatically set to 4096 bytes.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.

 

Optional: Name the drives

Because I’m running NTFS I can give the drives labels. Make sure you have ntfsprogs installed then enter:

# sudo ntfslabel -f /dev/sdb1 YourLabel

 

More Information

For more information on partitioning check out Chris Wakefield’s post here.
For a short and sweet howto on  formatting as NTFS check here.
Information on NTFS labels here.

Read More »

Page 3 of 1312345...10...Last »