Uncategorized (4)


concrete5 – package installer based on XML file

If you ever built a concrete5 package before, you’ll probably have had to write some code to handle the installation process where you check if an object is already installed and if not, add it. Especially with packages that are in use, this can take a while and the code gets messy very quickly. However, there’s an alternative which is not yet well known.

Creating installation XML

Before we start looking at some code, how do we get our XML content? There are a ton of different objects, but luckily, we can generate an XML from an existing site very easily. To do that, you’ll have to install the Sampel Content Generator you can find here http://www.concrete5.org/marketplace/addons/sample-content-generator/. Please note that you currently have to install it manually as it’s marked as incompatible with version 5.6.

Once you’ve installed the add-on, you can find a new single page in your dashboard. If you open it, you’ll find two buttons, the first one “Archive Files” to create a complete zip file of the site and the second one “Generate content.xml from current website” to get the output we’re looking for. If you click on the second button, you’ll see a screen like this:

installation-data

The output you can see in this screen contains everything you’ve got in your site. That’s way too much for a package but it’s pretty easy to find the elements you need. Most elements are pretty self-explanatory. In my example, I’ve needed two elements, one I had to add using a previous version of my package controller (attributetypes) and one I’ve added in the dashboard (attributetypes). The complete content of my XML looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0"?>
<concrete5-cif version="1.0">    
    <attributetypes>
        <attributetype handle="remo_phasher" package="remo_phasher">
            <categories>
                <category handle="file"/>
            </categories>
        </attributetype>
    </attributetypes>    
    <attributekeys>
        <attributekey handle="image_hash" name="Image Hash" package="" searchable="1" indexed="1" type="remo_phasher" category="file"/>
    </attributekeys>
</concrete5-cif>

I’ll put this content in a file called install.xml right in the root of the package. It looks like this:

file-structure

Install XML content from package controller

In our package controller, all we need is this:

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
<?php
 
defined('C5_EXECUTE') or die('Access Denied.');
 
class RemoPhasherPackage extends Package {
 
    protected $pkgHandle = 'remo_phasher';
    protected $appVersionRequired = '5.6.2.1';
    protected $pkgVersion = '0.9';
 
    public function getPackageDescription() {
        return t("Adds perceptual hash to images.");
    }
 
    public function getPackageName() {
        return t("Image pHasher");
    }
 
    public function install() {
        $pkg = parent::install();
 
        $ci = new ContentImporter();
        $ci->importContentFile($pkg->getPackagePath() . '/install.xml');
    }
 
    public function upgrade() {
        $pkg = Package::getByHandle($this->pkgHandle);
        $ci = new ContentImporter();
        $ci->importContentFile($pkg->getPackagePath() . '/install.xml');
 
        parent::upgrade();
    }
}

We simply get an instance of ContentImporter and import the content of our XML file in both methods, install as well as upgrade. If you need a new item, just add it to install.xml and you’re good to go. You can find a complete example on github: https://github.com/Remo/concrete5-phasher.




My answer to the most difficult Sudoku

A coder likes riddles like coding. Unlike my post about the number of zeros at the end of the result of 1000! this post does not even have any code in it. I found a post about a sudoku which is supposed to be the most difficult of its kind, check this link for more information: http://www.chinaabout.net/69-year-old-chinese-farmer-answer-difficult-sudoku-world/.

There’s one solution in the article, the second one is missing. Suspicious how I am, I had to check if there’s a second solution. It turns out there is, here’s my solution:

8 1 2 | 7 5 3 | 6 4 9
9 4 3 | 6 8 2 | 1 7 5
6 7 5 | 4 9 1 | 2 8 3
------+-------+------
1 5 4 | 2 3 7 | 8 9 6
3 6 9 | 8 4 5 | 7 2 1
2 8 7 | 1 6 9 | 5 3 4
------+-------+------
5 2 1 | 9 7 4 | 3 6 8
4 3 8 | 5 2 6 | 9 1 7
7 9 6 | 3 1 8 | 4 5 2

What I don’t understand is how one knows whether it’s the most difficult sudoku. Anyone knows more than I do?




Maximum Length of a website address / URL

Short story about maximum URL length

Make sure your URLs aren’t longer than 2’000 characters

Long Story about the maximum URL length

As a web developer you’ve probably seen a GET method request a few times. If you have a form and submit it, all the parameters including their values are appended to the browser URL. The more parameters you have to longer the address gets. You can use a POST method if you want to transfer more data but a GET request has the benefit of being link-able – you can copy the whole address and send it to anyone.

While you might want to avoid that in certain cases due to aesthetic reasons, there’s also a technical limit.

The standard RFC2616 specified that your URL should not exceed 255 bytes, check 3.2.1 in this document: http://www.faqs.org/rfcs/rfc2616.html.

It turns out that you can use up to 2’000 characters and it also turns out that someone had a closer look. Check this page more information http://www.boutell.com/newfaq/misc/urllength.html




Smart text trimming with PHP

Have you ever tried to create a short preview text using PHP? Did you just count the characters and ended up having texts with a completely different width like shown on this picture?

text-trimming

This tutorial describes a small method which calculates the real width of a string and lets you create better looking preview texts!