27 May 2009

MapGuide 2.1beta

Eager to try beta version, here I log the problem I face and how I solve.

Error msg:

httpd.exe: Syntax error on line 127 of C:/Program Files/OSGeo/MapGuide/Web/Apache2/conf/httpd.conf: Cannot load ../Php/MgHttpHandler.dll into server: The specified procedure could not be found.

Fix by move directive 'MgHttpHandler.dll' up

LoadFile ../Php/php5ts.dll
LoadFile ../Php/ACE.dll
LoadFile ../Php/MgHttpHandler.dll # put this directive here, the first one and restart apache service
LoadFile ../Php/MgFoundation.dll
LoadFile ../Php/MgGeometry.dll
LoadFile ../Php/MgMapGuideCommon.dll
LoadFile ../Php/MgMdfModel.dll
LoadFile ../Php/MgMdfParser.dll
LoadFile ../Php/MgPlatformBase.dll
LoadFile ../Php/MgWebApp.dll

http://n2.nabble.com/MGOS-2.0.0-Beta-2-loadfile-error-td1816710.html

26 May 2009

13 May 2009

schema.table to "schema"."table"

PHP

<?php
$st = 'schema.table';
print join('.', array_map(create_function('$a', 'return \'"\'.$a.\'"\';'), explode('.', $st)));

Selected 3 of 3 Lines; 11 of 11 Words; 124 of 124 Bytes

Python

st = 'schema.table'
print '.'.join('"'+i+'"' for i in st.split('.'))

Selected 2 of 2 Lines; 9 of 9 Words; 69 of 69 Bytes

Python=PHP/2

05 May 2009

Trac error - database disk image is malformed

One of my trac page got the error message "database disk image is malformed"

"underlying Trac DB is in serious trouble, may corrupted" - Ticket #6347

How I fix:
sqlite3 trac.db .dump | sqlite3 trac2.db
cp trac.db trac.broken.db
cp trac2.db trac.db

04 May 2009

Find files, php5 way

Find files, implemented using iterator.

class RegexFilter extends FilterIterator {
protected $regex;

public function __construct(Iterator $it, $regex) {
parent::__construct($it);
$this->regex = $regex;
}

public function accept() {
return preg_match($this->regex, $this->current());
}
}

function find_files($path, $pattern, $include_dir=FALSE) {
$objects = new RecursiveDirectoryIterator($path);
$objects = new RecursiveIteratorIterator($objects, RecursiveIteratorIterator::SELF_FIRST);
$objects = new RegexFilter($objects, $pattern);
return $objects;
}


Example usage:

$files = find_files('/tmp', '/.py$/');
foreach ($files as $file) {
echo $file;
}


Previously using php 4:

function find_files4($path, $pattern, $callback=null) {
$path = rtrim(str_replace("\\", "/", $path), '/') . '/';
$matches = Array();
$entries = Array();
$dir = dir($path);
while (false !== ($entry = $dir->read())) {
$entries[] = $entry;
}
$dir->close();
$files = array();
foreach ($entries as $entry) {
$fullname = $path . $entry;
if ($entry != '.' && $entry != '..' && is_dir($fullname)) {
$this->find_files($fullname, $pattern, $callback);
} else if (is_file($fullname) && preg_match($pattern, $entry)) {
if (!$callback) {
$files[] = $fullname;
} else {
call_user_func($callback, $fullname);
}
}
}
if (!$callback) {
return $files;
}
}

import data from csv into postgresql

We can use sql command, COPY. But programmer still need to create the table before copy the data. Simple python script to help


#!/usr/bin/python
fn = '/home/helmi/world.csv'
columns = file(fn).readline()
print 'create table world (%s text);' % " text,".join(columns.split(','))
print "copy world from '%s' with csv header;" % fn


then

python csv2psql.py | psql mydb