Recipes for using SVN library in PHP

The SVN library in PHP allows to work with SVN through a working copy or directly to the SVN repository files: http://us.php.net/manual/en/ref.svn.php

The problem is there is really few documentation for the direct access functions to repository (svn_repos_* and svn_fs_*) the code is almost the same to working in C the libsvn, but also there are few documentation, most of the svn command.

The good is that is really easy to work with SVN, basically you only need to:

  1. call to svn_repos_open($path) for getting a $repo handler
  2. call to svn_repos_fs($repo) to get a $repo_fs handler
  3. get the latest revision through:  svn_fs_youngest_rev($repo_fs);
  4. Get a $transaction through svn_repos_fs_begin_txn_for_commit($repo, $latest_revision, $commiter, $log_message);
  5. Get a $repo_root resource with svn_fs_txn_root($repo_txn);
    With $repo_root will be the most of operations and the $repo_txn will just work for do a commit or abort
  6. do all the operations (create a file/dir, remove, set contents)
  7. Call to svn_repos_fs_commit_txn($repo_txn); if you want to commit, or svn_fs_abort_txn ( $repo_txn ); if you want to abort the transaction

If you read the part of API for SVN: http://svnbook.red-bean.com/en/1.5/svn.developer.usingapi.html you will see an example for creating a new dir; this code can be easily changed to PHP since the calls are almost the same (replace  svn_repos_fs_begin_txn_for_commit2 for svn_repos_fs_begin_txn_for_commit) I will show you a script for init the repository creating the common paths (branches, tags, trunk) into an empty repository:

<?php
//the path to your repository
$svn_path = "/var/lib/svn/repos/testrepo";

//open the repo
$svn_repo = svn_repos_open($svn_path);

//get a fs handler
$repo_fs = svn_repos_fs($svn_repo);

//get latest revision
$latest_revision = svn_fs_youngest_rev($repo_fs);

//generate a transaction, with user and log message
$repo_txn = svn_repos_fs_begin_txn_for_commit($svn_repo, $latest_revision, 'user-repo-label', 'Creating paths');

//get the root respource to this transaction
$repo_root = svn_fs_txn_root($repo_txn);

//create the dirs
svn_fs_make_dir($repo_root, "trunk");
svn_fs_make_dir($repo_root, "tags");
svn_fs_make_dir($repo_root, "branches");

//finally commit the transaction
svn_repos_fs_commit_txn($repo_txn);

This script is called: create-repository-paths.php

You will see at end the list of scripts, the order to use is: (with svn commands in the console to check if worked):

#create SVN repository
danguer@rukia:~$ svnadmin create /var/lib/svn/repos/testrepo
#checking is empty
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo

#create paths
danguer@rukia:~$ php create-repository-paths.php
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo
branches/
tags/
trunk/

#creating dirs
danguer@rukia:~$ php create-dirs-recursive.php
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test
subdir1/
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1
subdir2/
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1/subdir2
subdir3/
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1/subdir2/subdir3

#create a new file
danguer@rukia:~$ php write-contents.php
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/myfile.txt
myfile.txt
danguer@rukia:~$ svn cat file:///var/lib/svn/repos/testrepo/test/myfile.txt
Lorem Ipsum, file generated with svn

#remove file
danguer@rukia:~$ php remove-object.php
danguer@rukia:~$ svn cat file:///var/lib/svn/repos/testrepo/test/myfile.txt
svn: File not found: revision 4, path '/test/myfile.txt'

Scripts:

Share

NFS client “Permission Denied” on Debian unstable

While I was trying to access to a master NFS server in lenny from a squeeze client, I was having “Permission denied” when I tried to write any file; as there are other NFS clients with the same configuration this didn’t seem to be a problem of NFS configuration but rather from new client version.

After some googling time, I found this bug:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=492970

If you want to know if your version is having this problem, you can check doing a “cat /proc/mounts” and should see the NFS mount with a “sec=null” and that is giving you the problem.

A suggestion from bug report is to downgrade, but that could not be an option in a fresh installed squeeze so the other workaround is easy:

  1. Unmount all nfs (important as I only unmount a exported filesystem from a server and won’t work until you unmount all)
  2. On /etc/fstab (or when mounting if you are doing manual) add the option sec=sys to all the mounts for that server; for example in fstab:
    example.com:/shared /tmp/shared nfs rw,sec=sys 0 0

    or mounting:

    mount -t nfs  -o "rw,sec=sys" example.com:/shared /tmp/shared

  3. Mount all nfs from that server

If you check the /proc/mounts you should see the sec=sys instead null and be able to write to remote server

Share

Flex 4: Getting bitmap data on a spark clipped content

Recently I was working on a project where I need to get the bitmap data from a container which had a scroller. From Flex 3 I tried to simply get the content directly from container; but I noticed that I only got the exact image of the scroller (Viewport) instead the full content.

With some playing, I got to the same easy solution; to draw the entire content of the container is to simply disable the clipAndEnableScrolling property over the container I was going to fetch the bitmap data and after drawing, enable again; as internally it just adjust the view properties, you won’t notice the change.

Here is the snippet:

this.containerDraw.clipAndEnableScrolling = false;
bmSave.draw(this.containerDraw);
this.containerDraw.clipAndEnableScrolling = true;

Where bmSave it’s a BitmapData, you can see an example here:

http://code.danguer.com/flex/clip-container

Share

Yahoo Widgets 4.5

A lot of time ago (about a year and a half) I worked briefly with Yahoo Widgets (previously named Konfabulator), as result I published a Small Widget: FangIT Server Stats. Unfortunately due lack of time I couldn’t finish some features I wanted to make on Yahoo Widgets.

In that time I remember some people were trying to allow Video (mainly FLV) on Widgets or even HTML Content. I left without checking Yahoo Widgets for some time, recently I remember this and checked to know what improvements were done it. When I see the ability to draw on widgets without thinking I downloaded the new Versio: 4.5

I have to say that the features are amazing, among them: Flash Embed, HTML Content, include of XMLHttpRequest, Drawing and even SQLite. I started ‘porting’ the old widget to a new version using this great features.

The widget is almost done and I hope on the next weekend I will release with great features, but meanwhile you surely will want to check Yahoo Widgets and if you’re a developer you will really like it more.

Share

Mysqli small fix for allowing socket in Zend Framework

As this site is running on ICDSoft hosting, they allow to use MySQL 5 or MySQL 4, but they provide different sockets for each version.

In MySQLi extension, the following host string doesn’t works: localhost:/tmp/mysql5.sock (or localhost:/tmp/mysql.sock), this because they separate the socket from the server host string.

In Zend Framework in the MySQLi Adapter, they don’t use the socket optional parameter, so I made a small fix to allow this [Zend/Db/Adapter/Mysqli.php]:

if (isset($this->_config['port'])) {$port = (integer) $this->_config['port'];

} else {

$port = null;

}/* new block */

if (isset($this->_config['socket'])) {

$socket = $this->_config['socket'];

} else {

$socket = null;

}

/* end of new block */

// Suppress connection warnings here.

// Throw an exception instead.

@$this->_connection = new mysqli(

$this->_config['host'],

$this->_config['username'],

$this->_config['password'],

$this->_config['dbname'],

$port,

$socket //new

);

This allow you to use the socket name, this was for enabling my new showcase example: Google Maps and GeoIP Location, you can see the full code here: http://blog.danguer.com/files/Zend_Db_Adapter/Mysqli.phps

Share