Allow tel: hyperlinks in MediaWiki

In MediaWiki you can already make email hyperlinks ([mailto:someone@example.com Mail someone]), but telephone number hyperlinks aren’t allowed for some reason by default ([tel:0123456789 Call someone]). Turns out to be an easy fix though.

Simply add the following to LocalSettings.php.

array_push($wgUrlProtocols, 'tel:');

Make it prettier

The links will get same icon as external links, which is a bit annoying, but that can be fixed as well. Find the main CSS file for your theme, in our case /skins/monobook/main.css, and look for a section which looks like the following.

#bodyContent a.external[href^="mailto:"], .link-mailto {
    background: url("mail_icon.gif") no-repeat scroll right center transparent;
    padding: 0 18px;
}

Simply make a copy of that section and replace all instances of ‘mailto’ with ‘tel’ and set the URL to the icon you want. The icon should of course be uploaded to the same location as the CSS for that to work :)

PHP: Catch all exceptions and errors in a normalized and consistent matter

For my small website myhymnal.net I wanted a nice error handler that caught all errors and presented them as nice error pages. The problem is that you have at least three kinds of errors which you have to handle in PHP. Uncaught exceptions, regular errors and fatal errors. After some experimenting I think I’ve found a way to catch them all in a close to consistent matter so that they are easy to deal with.

error_reporting(ENV === 'dev' ? E_ALL : 0);

set_error_handler("error_handler");
set_exception_handler("error_handler");
register_shutdown_function("error_handler");

function error_handler()
{
    // Check for unhandled errors (fatal shutdown)
    $e = error_get_last();

    // If none, check function args (error handler)
    if($e === null)
        $e = func_get_args();

    // Return if no error
    if(empty($e))
        return;

    // "Normalize" exceptions (exception handler)
    if($e[0] instanceof Exception)
    {
        call_user_func_array(__FUNCTION__, array(
            $e[0]->getCode(),
            $e[0]->getMessage(),
            $e[0]->getFile(),
            $e[0]->getLine(),
            $e[0]));
        return;
    }

    $e = array_combine(array('number', 'message', 'file', 'line', 'context'), array_pad($e, 5, null));
   
    var_dump($e);
    exit;
}

This way the only irregularity should be the last property, context. In cases of errors this will be an array with variables in the context where the error occurred, while for exceptions I’m setting it to the Exception itself. Normally you’d then pass $e to something that would format the information nicely to the user, do some logging or whatever.

External tool definitions for mRemoteNG

Here are some external tool definitions I have defined in mRemoteNG.

WinSCP

Display name

WinSCP (sftp)

Filename

C:\Program Files (x86)\WinSCP\WinSCP.exe

Arguments

sftp://%username%:%password%@%hostname%:%port%/

Ping

Display name

Ping

Filename

cmd

Arguments

/c ping -t %HostName%

Traceroute

Display name

Ping

Filename

cmd

Arguments

/c set /P = | tracert %HostName%

More can be found at the mRemoteNG forum.

If you haven’t tried mRemoteNG, you should. It’s very handy if you’re on Windows and need to connect to various servers through for example SSH and RDP. Sure beats plain PuTTY at least…

MediaWiki: Promote user to sysop when sysops are gone

Working for a client which has documentation on a MediaWiki installation which was set up before we got there. Was able to create users for ourselves through the regular site, but certain things like deleting pages can only be done by sysops. None of our newly registered users are of course sysops and the only existing sysop user is from whoever installed the wiki to begin with.

Here’s how to promote a user to sysop the hackish way, directly in the database.

Find database details

Note: This can of course be skipped if you already know how to access your mediawiki database, but I didn’t :)
  1. SSH into the system where MediaWiki is installed and find its location.
    Was /var/www/html/foowiki in our case.

  2. Open LocalSettings.php and everything should be under the heading ## Database settings

Promote user

  1. Connect with a mysql client.
    mysql -h localhost -u wikiuser -p wikidb
  2. Find the user id.
    SELECT user_id, CONVERT(user_name USING utf8) FROM `user`;
  3. Add user n to sysop (and bureaucrat) group.
    INSERT INTO `user_groups` VALUES (n, 'sysop'),(n, 'bureaucrat');

The user should now be sysop, which can be double checked on the Special:ListUsers wiki page.

Simple automated website deployment via GitHub

Here is a very simple way to add automatic deployment via GitHub. This assumes

  • You have shell access to your web host
  • Your website is in a repository on GitHub
  • CGI scripts are enabled for your website
  • That git (and optionally composer if you use that) is installed

Initial pull

Log on to your web host and do the initial pull.

$ cd path/to/website
$ git clone https://github.com/<you>/<website>.git .

Deployment script

Put the following script in a file named for example deploy.cgi in your website root.

#!/bin/bash
echo Content-type: text/plain
echo

export PATH=~/bin/:$PATH

git pull
composer.phar install
echo DONE
I’m on Dreamhost and the export line is so the cgi script will use the composer.phar and php symlink located in ~/bin. See this post for more info.

Add execute rights to the script.

$ chmod u+x deploy.cgi

You should now be able to visit http://<your-website>/deploy.cgi in your browser and see output like this:

Already up-to-date.
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Generating autoload files
DONE

Set up GitHub service hook

Go to https://github.com/<you>/<website>/settings/hooks, click on WebHook URLs and enter the full URL to your script, http://<your-website>/deploy.cgi

Test it out

With this set up you should be able to make a change at your dev machine, commit it and push it to GitHub. The script should then be executed shortly after and your website should be updated automatically. Pretty cool :)

This is of course ultra simple, and if this is a critical site you should probably add some security of some sort. A simple version could be to use a cryptic name like a guid for the script and of course not have this script checked in at GitHub. You might also perhaps want to only pull from a certain branch or things like that, but for a small simple site this works pretty well as long as you remember to only push to GitHub when you have a working set of commits ;)

Java: Simple check to see if a server is listening on a port

Here is a simple method to check if a server is listening on a certain port. I used it to ignore certain non-critical SFTP related integration tests in a project when I hadn’t bothered starting the SFTP server.

public static boolean serverListening(String host, int port)
{
    Socket s = null;
    try
    {
        s = new Socket(host, port);
        return true;
    }
    catch (Exception e)
    {
        return false;
    }
    finally
    {
        if(s != null)
            try {s.close();}
            catch(Exception e){}
    }
}

To ignore a test (assuming JUnit) you can then do the following:

@Test
public void some_test()
{
    assumeTrue(serverListening("localhost", 22));

    // Rest of test...
}

How to install PHPUnit on Windows

Note to self in case I need to do this again…

Commands assumes the PHP install is in C:\wamp\bin\php\php5.3.13 and that this path is already added to the PATH environment variable.

Install PEAR

  1. Download go-pear.phar and Save it as C:\wamp\bin\php\php5.3.13\pear\go-pear.phar.
  2. Open an elevated command prompt.
  3. Go to the PHP directory.
    > cd \wamp\bin\php\php5.3.13
  4. Install PEAR, pressing enter to accept defaults and Y to alter php.ini.
    > php .\pear\go-pear.phar
  5. Add environment variables.
    > start PEAR_ENV.reg

Install PhpUnit

  1. Make sure pear is fully up to date (should be if we just installed it)
    > pear upgrade-all
  2. Add pear channels.
    > pear channel-discover components.ez.no
    > pear channel-discover pear.phpunit.de
    > pear channel-discover pear.symfony.com
  3. Install PHPUnit with all dependencies.
    > pear install --alldeps phpunit/PHPUnit
  4. Verify installation by checking version.
    > phpunit --version