Oct 23

getting php bindings for subversion

Fri, 10/23/2009 - 19:04 — peter

Let's say you want to access a svn repo or perhaps even a working copy programatically through php without escaping to the shell, you need svn functions setup in php. Here is how you do it.

First off, you need to have a few packages installed. I assume you already got a web server and php up, in addition to this you will also need the following:

  • php development libraries
  • php pear/pecl
  • subversion
  • subversion development libraries

On a modern *buntu distro, the packages you need are:

sudo apt-get install php5-dev subversion libsvn-dev php-pear

And on Fedora,

sudo yum install php-devel subversion-devel php-pear

Then you need to install the svn beta package from pecl (pecl comes with pear).

pecl install svn-beta

If the setup complains about phpize, make sure you got php5-dev/php-devel packages installed.

After the packages are installed, make sure you add

extension=svn.so

to a php.ini file. On my ubuntu server I used the following command:

sudo echo "extension=svn.so"> /etc/php5/conf.d/svn.ini

and reloaded apache.

That is it! You are done. To test your work, create a short php program named, say, svn.php, like the following code that reads the repo info from the awesome Code Swarm project:

<?php
print_r( svn_ls('http://codeswarm.googlecode.com/svn/trunk/') );
?>

Running it with php svn.php and you should get something like this:

# php svn.php    
Array            
(                
    [COPYING] => Array
        (             
            [created_rev] => 34
            [last_author] => michael.ogawa
            [size] => 35148               
            [time] => Jun 22 2008         
            [time_t] => 1214113154        
            [name] => COPYING             
            [type] => file                
        )                                 
 
    [README] => Array
        (            
            [created_rev] => 284
            [last_author] => michael.ogawa
            [size] => 4019                
            [time] => Sep 26 04:08        
            [time_t] => 1253956139        
            [name] => README              
            [type] => file                
        )                                 
 
    [build.xml] => Array
        (               
            [created_rev] => 284
            [last_author] => michael.ogawa
            [size] => 2599                
            [time] => Sep 26 04:08        
            [time_t] => 1253956139        
            [name] => build.xml           
            [type] => file                
        )                                 
 
    [convert_logs] => Array
        (                  
            [created_rev] => 259
            [last_author] => michael.ogawa
            [size] => 0                   
            [time] => Feb 19 2009         
            [time_t] => 1235096578        
            [name] => convert_logs        
            [type] => dir                 
        )                                 
 
    [data] => Array
        (          
            [created_rev] => 275
            [last_author] => michael.ogawa
            [size] => 0                   
            [time] => May 02 02:56        
            [time_t] => 1241250998        
            [name] => data                
            [type] => dir                 
        )                                 
 
    [doc] => Array
        (         
            [created_rev] => 36
            [last_author] => michael.ogawa
            [size] => 0                   
            [time] => Jun 22 2008         
            [time_t] => 1214116028        
            [name] => doc                 
            [type] => dir                 
        )                                 
 
    [eclipse] => Array
        (             
            [created_rev] => 165
            [last_author] => nawglan
            [size] => 0             
            [time] => Jul 13 2008   
            [time_t] => 1215993587  
            [name] => eclipse       
            [type] => dir           
        )                           
 
    [lib] => Array
        (         
            [created_rev] => 235
            [last_author] => kraeusen
            [size] => 0              
            [time] => Aug 07 2008    
            [time_t] => 1218126048   
            [name] => lib            
            [type] => dir            
        )                            
 
    [physics_engine] => Array
        (                    
            [created_rev] => 231
            [last_author] => nawglan
            [size] => 0             
            [time] => Aug 01 2008   
            [time_t] => 1217639319  
            [name] => physics_engine
            [type] => dir           
        )                           
 
    [run.bat] => Array
        (             
            [created_rev] => 210
            [last_author] => sebastien.rombauts
            [size] => 1193                     
            [time] => Jul 23 2008              
            [time_t] => 1216791580             
            [name] => run.bat                  
            [type] => file                     
        )                                      
 
    [run.sh] => Array
        (            
            [created_rev] => 209
            [last_author] => sebastien.rombauts
            [size] => 1539                     
            [time] => Jul 23 2008              
            [time_t] => 1216791205
            [name] => run.sh
            [type] => file
        )
 
    [runrepositoryfetch.bat] => Array
        (
            [created_rev] => 235
            [last_author] => kraeusen
            [size] => 1227
            [time] => Aug 07 2008
            [time_t] => 1218126048
            [name] => runrepositoryfetch.bat
            [type] => file
        )
 
    [runrepositoryfetch.sh] => Array
        (
            [created_rev] => 239
            [last_author] => nawglan
            [size] => 1584
            [time] => Aug 10 2008
            [time_t] => 1218395386
            [name] => runrepositoryfetch.sh
            [type] => file
        )
 
    [src] => Array
        (
            [created_rev] => 276
            [last_author] => Dudley.Fox
            [size] => 0
            [time] => May 04 23:27
            [time_t] => 1241497627
            [name] => src
            [type] => dir
        )
 
)

Now you can go forth and build interfaces that manipulate your subversion repos without resorting to escaping to the command-line or setting up xml services!

Easy Peasy!
-PCP