Category Archives: DevTool

Pruning the Ubuntu MRU

Imagine a situation, e.g. a kitchen review, where you don’t want your customer or colleague to see a list of you recently opened files, especially those files you opened for recreational purposes. In Ubuntu, this list is a XML file recently-used.xbel, located in the user directory under ~/.local/share.

This file consists of bookmarks:

  <bookmark href="file:///home/ron/Downloads/liferay-portal-6.1.1-ce-ga2/readme.html" added="2013-02-23T19:04:53Z" modified="2013-02-23T19:04:53Z" visited="2013-02-23T19:04:53Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="text/html"/>
        <bookmark:applications>
          <bookmark:application name="nautilus" exec="'epiphany-browser %U'" modified="2013-02-23T19:04:53Z" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>

Pruning here is done by analyzing the file extension of the href attribute, let’s get rid of .flv and .mp4 entries:

<?php
/**
 * Prunes the Ubuntu MRU file.
 * 
 */
class MRUPruner
{

    /**
     * Dom object
     * @var DOMobject
     */
    private $DOM;

    /**
     * XML file name
     * @var String
     */
    private $XML;

    /**
     * Loads XML into the DOM
     *
     * @param string $XML   XML File Name
     */
    function __construct($XML) {
        $this->XML = $XML;
        $this->DOM = new DOMDocument();
        $this->DOM->load($XML);
    } // __construct()

    /**
     * Prunes MRU by applying filters
     * @uses DOM
     */
    public function prune() {

        $elements = $this->DOM->getElementsByTagName('bookmark');
        $pruned = false;

        do {
            $pruned = true;
            foreach($elements as $element) {

                $tempDocument = new DOMDocument();
                $clonedElement = $element->cloneNode(true);
                $tempDocument->appendChild($tempDocument->importNode($clonedElement, true));

                $_href = $element->getAttribute('href');
                print $_href . PHP_EOL;

                // Delete:
                $pattern = '~.*\.(flv|mp4)$~i';
                if(preg_match($pattern, $_href) == 1) {
                    print '-->delete' . PHP_EOL;
                    $element->parentNode->removeChild($element);
                    reset($elements);
                    $pruned = false;
                    break;
                } // endif

                // another pattern block here...

            } // nexteach
        } while(!$pruned);

    $this->DOM->save($this->XML);
    } // prune()

} // class

$pruner = new MRUPruner('recently-used.xbel');
$pruner->prune();

All nodes with these file extensions in the href attribute are removed from the document, the document is written back to the MRU file. The pattern block can be replaced or enhanced by another. This code does not handle the blank lines coming from deleting elements.

Remember: Use this at your own risk; Backup your data before you manipulate it.

P.S.: Being a dyed-in-the-wool data collector, I do not simply delete entries, but save them in a database for further analysis…

Installing PHPDepend

When installing PHPDepend thru PEAR on Ubuntu, I ran into an error caused by a missing MagickWand-config:

$ pear channel-discover pear.pdepend.org
$ pear install --alldeps pdepend/PHP_Depend-beta

checking ImageMagick MagickWand API configuration program…
configure: error: not found. Please provide a path to MagickWand-config or Wand-config program.
ERROR: `/tmp/pear/temp/imagick/configure –with-imagick’ failed

Installing imagick thru PECL results in the same error message:

$ pecl install --alldeps imagick

The MagickWand API provides interfaces between high level programming languages and the ImageMagick image processing libraries.

Install the MagickWand libraries:

$ apt-get install libmagickwand-dev libmagickcore-dev

This finally enables the installation of the aforementioned PEAR & PECL libraries. After that, test the installation:

$ MagickWand-config --version
6.6.0 Q16
$ pdepend --version
PHP_Depend 1.0.7 by Manuel Pichler

A simple TCP/IP Notification Tool

Since I had some lightweight tools working on different data located on different machines in my TCP/IP LAN, I wanted to be informed about states or errors of the processes without logging into these machines.

The Notifier package provides a Java server that can be connected to a selected port on a selected machine; once it receives a message on this port, it pops up a small Java JFrame, showing the dispatcher and the message body indicating the urgency by its background color.

The Java package consists of two classes, Notifier and NotifierServer. With the Ant file, a Java JAR file can be compiled; start this manually or with autostart support. IP and Port are hardcoded, since they are not subject to frequent changes.

A PHP class Notifier provides a method fireNotification() that can send notifications to the server. The Sample.php shows how to do that.

Download package here