Add this blog to Del.icio.us, Digg or Furl | Create Watchlist for this blog

Archive for the ‘programming’ Category

Proposal for a CSS ancestor grouping syntax

Wednesday, July 23rd, 2008

What we need

I am missing a syntax to apply a common ancestor to a set of rules. As far as I know, there’s no such concept in CSS selector syntax.

With the increasing usage of CSS definitions to style HTML markup, CSS files grow bigger and bigger. With this it’s getting more and more complex to find rules that apply the a special element and/or to have one’s code structured properly. When ie8 will support the more sophisticated locators like adjacent siblings or attribute selectors the locators and files likely will continue to grow.
Also CSS gets used in situations where you import content from several providers to display it inside one “mashup” site or (like in iGoogle or the Vista desktop) as “snippet”. In this situation one needs to separate the CSS rules of the different providers from each other and from the mashup’s home style.

Basically I want to grab remote content then wrap the grabbed sourcecode into a i.e. a <div id=”#GG”> and integrate that code together with it’s CSS in my site (or fridge display or whatever).

How it could work

The solution to this would be a ancestor grouping concept, i.e.:

ancestor "#GG" {
a { color: black; }
p { margin-top: 3em; }
}

This syntax should then expand to:

#GG a { color: black; }
#GG p { margin-top: 3em; }

Far more added value would be provided by the following construction:
ancestor "#GG" { @import(google.com/styles.css); }

or inside the HTML markup:
<link rel="StyleSheet" href="http://google.com/style.css" type="text/css" ancestor="#GG">

I think the goal should be clear now. (more…)

CSS Selectors - Short Reference

Monday, June 23rd, 2008

This is a “short as possible” reference for CSS selector syntax. Intended audience should be very familiar with XHTML, DOM and CSS Styling.

Basics

Type selector
Select all HTML elements of a special type.
Example: em {font-weight: bold;}
Class selector
Select all elements with a given class.
Example: .classname {font-weight: normal;}
ID selector
Select the one element with a given id.
Example: #idname {font-weight: normal;}
Concatenator
Select HTML elements of special type only with given class/id.
Example: em.classname, em#idname {font-weight: normal;}
Universal selector
Select all elements.
Example: * {padding:0;}
Descendent selector
Select all elements that are lower down in the DOM tree (descendants).
Example: p em {font-weight: normal;}
-> Applies to all em's within p's.

Advanced

Child selector
Select all elements that are children in the DOM tree (direct descendant).
Example: p > em {font-weight: normal;}
-> Applies to all em's directly within p's, not supported by ie6 and below.

Adjacent sibling selectors
Select all elements that are on the same DOM level in the same DOM subtree
Example: h3 + p {font-size:80%;}
-> Applies to all p's that "belong" to a h3

(more…)

Favorite current project

Wednesday, June 4th, 2008

My favorite current project is opentimesheet.org.
It’s about efficient online time registration and recording.
Last 14 years as freelancer I was using a horrible number of time capturing systems. Actually I am using about three at a time now. There are so many of them, and most companies invent their own - starting with excel - ending up in a very special time consumptive “solution”.

Break the rule - join the new open source project!

Feeding “static” sites from Drupal nodes

Tuesday, February 26th, 2008

This is about how I manage to create content for domains without CMS from a Drupal enabled domain without any manual work.

On address www.immobilien-spot.de I am running a community for real estate agents. It’s primary focused on Immobilien in München (that’s German for real estate in Munich).

Estate agents can become members, can show 10 of their best buildings and act like usual in communities (Blog, Forum, Contact…). Additionally the agents can book a special site to promote one of their offers, i.e. an office loft for rent. This office loft is shown in the offer list and also on a static site called officeloft.de (this is an example, I don’t own that domain). To feed the content of officeloft.de from the Drupal content on immobilien-spot.de, I’ve established a special node type called a “promotion”.

A promotion node consists of a node-ref (pointing to the estate offer node), an entry place (office-loft.de) and a booking period (March 2008). To render the promotion node completely different from all other nodes, I have installed the sections and pathauto modules for Drupal. With pathauto one can automatically create special paths for nodetypes, and with sections.module one can assign special page.tpl.php for this path.

What’s left is to have a nightly cronjob that copies the rendered promotion node to the special site’s http-root directory (what should be a subdirectory of the main domains http-root - else you have to struggle with your OS). And finally transpose all of the relative path’s inside the copy to be absolute.

Have fun!

New MIMEMailPHP Version 2.1.3 released

Tuesday, January 8th, 2008

Users told me they had problems with some online mail clients (like gmx and gmail) viewing mails sent with my MIMEMailxPHP4_V2 PHP class when cid (base64 coded inline) images are contained.
There was a bug initially found by Patrick Kränzlin. This bug is fixed now, you find the current (2.1.3) Version for download (it’s open source) here.

According to the RFC2111:

Note: in Internet mail messages, the addr-spec in a Content-ID [MIME] or Message-ID [822] header are enclosed in angle brackets (<>). Since addr-spec in a Message-ID or Content-ID might contain characters not allowed within a URL; any such character (including “/”, which is reserved within the “mid” scheme) must be hex-encoded using the %hh escape mechanism in [URL].

I forgot the angle brackets in version 2.1.2

Sorry for that!

Drupal: Using node and user information in block code

Monday, January 7th, 2008

This is for Drupal 5, don’t know if it works also in version 6.

Usually block content is not related to the currently displayed node. I.e. the block that displays the recent blog posts always displays the same content, regardless of on what page you are currently looking.
I often had situations where I wanted the block content to be related to the currently viewed node. To keep the example: When viewing a blog post, I wanted a block to display the most recent post of the author of the currently displayed post, and additionally print some profile fields like age and a gravatar.

The key to achieve this goal is the Drupal arg() function. This function returns the components of the currently viewed path before any URL aliasing. So if you view the node 5, that is yourdomain.com/node/5 (and sometimes a trailing “view”), you will get following results from function arg():

arg(0) -> "node"
arg(1) -> "5"
arg(2) -> "" or "view"

Now, if you want to get some information about node 5, you use node_load(). And to get information about the user that is the author of node 5, you use user_load().

In PHP code it works out like:

if (arg(0) == 'node'
    && is_numeric(arg(1))
    && (arg(2) == '' || arg(2) == 'view')) {
 $node = node_load(arg(1));
 if (is_numeric($node->uid))
   $user = user_load(array('uid' => "$node->uid"));
 ... do something with $node and $user here
}

BTW: If you need this a lot, you should consider writing a module for your content type :-)

To make a useful example out of it…
I use the following “debug-block” while developing to view my node and user objects:

if (arg(0) == 'node' && is_numeric(arg(1))
    && (arg(2) == '' || arg(2) == 'view')) {
 $node = node_load(arg(1));
 if (is_numeric($node->uid))
   $user = user_load(array('uid' => "$node->uid"));
 print "node object<br>";
 foreach ($node as $k => $v) {
  print "[$k] = $v<br>";
 }
 print "<br>user object<br>";
 if ($user) foreach ($user as $k => $v) {
  print "[$k] = $v<br>";
 }
} else {
print 'not a node';
}

Intel C++ 10, OpenMP & OpenSuSE Linux

Friday, August 31st, 2007

OpenMP on Linux with Intel iccAs promised yesterday, I’ll try the OpenMP features of Intel C++ 10.0 for Linux today. Cause I’m lazy, I use a wide spread “Hello World” for OpenMP example, it is very simple and it’s only purpose is to demonstrate the basics of how to parallelize a code with OpenMP.

/***********************************************************
* FILE: omp_hello.c
* DESCRIPTION:
*   OpenMP Example - Hello World - C/C++ Version
*   In this simple example, the master thread
*   forks a parallel region. All threads in the team obtain
*   their unique thread number and print it. The master
*   thread only prints the total number of threads.  Two OpenMP
*   library routines are used to obtain the number of threads
*   and each thread's number.
* AUTHOR: Blaise Barney  5/99
* LAST REVISED: 04/06/05
************************************************************/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv) {

int nthreads, tid;

/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
  {

  /* Obtain thread number */
  tid = omp_get_thread_num();
  printf("Hello World from thread = %dn", tid);

  /* Only master thread does this */
  if (tid == 0)
    {
    nthreads = omp_get_num_threads();
    printf("Number of threads = %dn", nthreads);
    }

  }  /* All threads join master thread and disband */

}

To compile this example with icc you need the OpenMP switch turned on:

rainer@novotop:/tmp> icc -openmp omp_hello.c
omp_hello.c(22): (col. 1) remark: OpenMP DEFINED REGION
WAS PARALLELIZED.
rainer@novotop:/tmp> ./a.out
Hello World from thread = 0
Number of threads = 2
Hello World from thread = 1

Ok, that’s pretty cool. We saw how to parallelize a region and giving private variables to it. By the way, you can specify the number of threads OpenMP starts with the OMP_NUM_THREADS environment variable (export OMP_NUM_THREADS=4, then start a.out -> you get four thread outputs).
Next post will handle mutexes, semaphores, thread shared memory etc. in more depth.

About installing Intel C++ 10.0 on OpenSuSE 10.2
A good tutorial

Intel C++ 10.0 for linux

Thursday, August 30th, 2007

Get the most out of it

I like gcc very much but now I have a Intel Core 2 Duo notebook, and I think it’s time to get the most out of it. That’s why I go for the Intel C++ 10.0 compiler today.

Intel provides a “free for non-commercial usage” download, that’s pretty nice and one more reason to give’em a try!

The other thing I am really interested in is the openMP integration. If you had troubles with pthreads you know why!

Start of my report

Go to the download page, confirm non-commercial agreement, register with email address, then start downloading the 45 MB package (in the IA32 version).

Untar the package and as root type
./install.sh

The installation went smooth, had to agree to several license agreements, answer some question (always took the default). Until suddenly:


Your platform :
architecture = i686
kernel = 2.6.18
glibc = glibc-2.5-25
operating system = unknown

This product is supported for use with the following combinations :

Machine Type Kernel glibc

IA-32/Intel(R) 64 2.4.x 2.2.5
IA-32/Intel(R) 64 2.4.x 2.2.93
IA-32/Intel(R) 64 2.4.x 2.3.2
IA-32/Intel(R) 64 2.6.x 2.3.x
IA-32/Intel(R) 64 2.6.x 2.4.x
IA-32/Intel(R) 64 2.6.x 2.5.x

Would you like to perform an unsupported install of this product [yes/no] (no) ? :

At this point I face my fear but typed YES.
After some other questions (always answer with default) I finally got the “successful” message:


Successfully installed:

Substitute Headers for Intel(R) C++ Compiler for applications running on IA-32, Version 10.0 (10.0.023)
intel-isubh100023-10.0.023-1.i386.rpm

Intel(R) C++ Compiler for applications running on IA-32, Version 10.0 (10.0.023)
intel-icc100023-10.0.023-1.i386.rpm

Intel(R) Debugger for applications running on IA-32, Version 10.0 (10.0.023)
intel-iidb100023-10.0.023-1.i386.rpm

Intel(R) C++ Compiler features and plugins for integration into Eclipse* CDT development environment, Version 10.0 (10.0.023)
intel-icc_ide100023-10.0.023-1.i386.rpm

Intel(R) Debugger features and plugins for integration into Eclipse* CDT development environment, Version 10.0 (10.0.023)
intel-idb_ide100023-10.0.023-1.i386.rpm

First compilation

My first compilation will be a “Hello World” as it is good tradition in the C business:

#include <stdio.h>

int main(int argc, char **argv)
{
        fprintf(stdout, "Hello ICC Worldn");

        return 0;
}

To get the icc environment up, Intel delivers a Shell-Script. Source it then compile like this:
source /opt/intel/cc/10.0.023/bin/iccvars.sh
icc hello.c

Then run the result:
./a.out
Watch the output and be happy :-)
Hello ICC World

This is so great! Tomorrow I’ll report on first experiences with openMP!
See you soon!

Related Entries Plugin with WP 2.2

Wednesday, July 18th, 2007

When I tried to install WASABI’s nice related entries plugin on my new Wordpress site geekland it failed with the following error:

#1214 - The used table type doesn’t support FULLTEXT indexes

I am not sure if this is WP2.2 related - in the schema creation source I found no difference to 2.1 in the posts table - but I never had that problem with 2.1.

The solution is to convert the wp_posts table to ISAM (MyISAM) by issuing

ALTER TABLE `wp_posts` ENGINE = MYISAM

and after that re-issue the installation script of WASABI what does not more than:

alter table wp_posts add fulltext post_related (post_name, post_content)

Well, on my development system, the WP2.2 database was created in InnoDB format. and InnoDB does not support fulltext-search columns.
That’s all.

ajax script tutorial - quick technology intro

Wednesday, June 20th, 2007

I love short stories to get started in a new technology. So here is one about ajax - the current hype on web platforms.

What is it for

Send requests asynchronous to your webserver and process the results within the current client-page-DOM without re-loading the whole page!

If you need special data in your webpage as response to a user action then use ajax. It is not meant for a whole Web GUI. If you use ajax for too much user navigation you will explore the two major drawbacks soon: The user can’t use the browser’s back button and search engines will ignore you (well, there are workarounds but those are bad browser dependent hacks).
The typical situation where ajax is useful is when you have two drop-down lists and the content of the second list depends on the selected entry of the first. I.e. “choose your cpu:” - first list contains manufacturers “Intel”, “AMD”, “IBM”, “Motorola”, second list contains processor model.

Technology, Syntax, Objects

On client side your users need a javascript enabled webbrowser of a “newer” generation. On server side you need nothing special, just use your favorite technique (there’s more importance on the selected protocol format - like just string or xml or json). On webdesigner’s side you need some DHTML and javascript.

A typical javascript function that initiates an ajax call with an url as parameter looks like this:

var request = false;  // you need this variable globally 

function initiateAjaxCall(url)
{
  request  = new XMLHttpRequest();
  request.open("get", url, true);
  request.onreadystatechange = async_event;
  request.send(NULL);
}

Where the XMLHttpRequest is the javascript-ajax object to focus on (for IE6 you need some workarounds well known in the web). ajax is “Asynchronous Javascript and XML” so it’s asynchronous and so you need a callback function (a function that gets called when an event occurs). Above “async_event” is declared to be your event handling function to handle the webserver’s asynchronous response.

There are four events your callback will be called with:

ajax state table:

0 = uninitialized
1 = loading
2 = loaded
3 = interactiv
4 = complete

For this quick intro, only state 4 is of interest. Maybe I’ll get back on this later in another post (can program nice progress bars with this infos).
Your event handler could look like this:

function async_event() {
  switch(request.readyState) {
  case 4:
    if (request.status != 200) {
      alert("HTTP Response not OK : Code:"+request.status);
    } else {
      myProcessing(request.responseText);
    }
  break;
  default:  break;
  }
}

In the “myProcessing” function the final response processing is done (usualy it’s any DHTML manipulation of the current DOM).

Full Code Example
It’s not that easy to invent a simple example that makes at least little sense, so don’t be to critical with this.

ajax script tutorial - example 1
Initial view of the example

To show up the technology in short code we build a website that displays the current client time (javascript) and a button to request the current server time. When the button is pressed, the current server time get’s fetched from the server by magical AJAX and is finally display via a DOM manipulation in - again - javascript.

We start with the server side (in php - to keep it simple)
File getTime.php

<?php echo date("H:i:s"); ?>

That was pretty simple, now we do the html page that uses ajax:
File getTime.html


<html>
<head><title>AJAX Test 1</title>
<script type="text/javascript">
   <!--
   // our global request variable
   var request = false; 

   // function to initiate the ajax call
   function initiateAjaxCall(url)
   {
     request  = new XMLHttpRequest();
     request.open("get", url, true);
     request.onreadystatechange = async_event;
     request.send(null);
   }

   // ajax callback function
   function async_event() {
     switch(request.readyState) {
     case 4:
       if (request.status != 200) {
         myProc("Error Response Code: "+request.status);
       } else {
         myProc(request.responseText);
       }
     break;
     default:  break;
     }
   }

   // function to print the ajax result
   function myProc(text) {
      var now = new Date();
      var ct = document.getElementById("CTIME");
      var st = document.getElementById("STIME");
      ct.innerHTML = ""+now.getHours()+":"+
                               now.getMinutes()+":"+
                               now.getSeconds();
      st.innerHTML = text;
   }
   //-->
</script>
</head>
<body onLoad="myProc('---');">
   <h1>Ajax Test 1</h1>
   Client Time: <span id="CTIME"></span><br>
   Server Time: <span id="STIME"></span>
   <br><br>
   <input type="submit" value="fetch server time via ajax now"
                          onClick="initiateAjaxCall('getTime.php');">
</body>
</html>

ajax script tutorial - example 2
After pressing the button, server’s time is displayed