10 November 2012

Riak - phpmyadmin like web interface

Now this is a super-lite admin web interface for Riak. Not sure who would use something like this, but here it is free and opensource. This version allows you to see all buckets, all keys of a bucket and the values from one key. Also, you can remove each key individually.

P.S. Sorry about the indent, blogger lost it.

<?php

/*****************************************************
RiakMyAdmin - v0.1
created by Stefan Silasi => stefan@silasi.ro
*****************************************************/

$host = '127.0.0.1:8098'; // <= change this !

/**********************************************************
NO CHANGES FROM HERE ON UNLESS YOU KNOW WHAT YOU'RE DOING
**********************************************************/

function request_riak($bucket = NULL, $key = NULL)
{
global $host, $port, $options;
$curl = curl_init ();

if (!$bucket)
{
// list all buckets
$host .= '/buckets?buckets=true';
}
elseif (($bucket) && (!$key))
{
// list all keys
$host .= '/riak/'.$bucket.'?keys=true';
}
else
{
// list the current key
if ($_GET['cmd']=='DELETE'){curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_GET['cmd']);}
$host .= '/riak/'.$bucket.'/'.$key.'/';
}

curl_setopt($curl, CURLOPT_URL, $host); // host:port/request
curl_setopt($curl, CURLOPT_HEADER, 0); // no additional headers for CURL
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // returns the curl response in the $RET instead of screen
$ret = curl_exec($curl);
curl_close($curl);

return $ret;
}

$x = request_riak($_GET['bucket'], $_GET['key']);
$a = json_decode($x);

if (!$_GET['bucket']){$arr=$a->{'buckets'};}
elseif ((($_GET['bucket']) && (!$_GET['key'])) || (($_GET['cmd'] == 'DELETE') && ($_GET['bucket']) && ($_GET['key'])))
{
$arr=$a->{'keys'};
echo '<a href="?">Bucket list</a><h1>Bucket: '.$_GET['bucket'].' ('.count($arr).' keys)</h1>';
}
else
{
echo '<a href="?">Bucket list</a><h1>Bucket: <a href="?bucket='.$_GET['bucket'].'">'.$_GET['bucket'].'</a></h1><h2>Key: <a href="?bucket='.$_GET['bucket'].'&key='.$_GET['key'].'">'.$_GET['key'].'</a></h2>';
echo '<textarea rows="25" cols="75">'.$x.'</textarea><br>';

echo '<a href="?bucket='.$_GET['bucket'].'&key='.$_GET['key'].'&cmd=DELETE">Delete</a>';
}

if ($_GET['cmd'] == 'DELETE') { echo 'key '.$_GET['key'].' was deleted. <a href="?bucket='.$_GET['bucket'].'">continue</a>'; exit; }

for ($i=0;$i<count($arr);$i++)
{
if (!$_GET['bucket'])
{
echo 'bucket name: <a href="?bucket='.$arr[$i].'">'.$arr[$i].'</a><br>';
}
elseif (($_GET['bucket']) && (!$_GET['key']))
{
echo 'key name: <a href="?bucket='.$_GET['bucket'].'&key='.$arr[$i].'">'.$arr[$i].'</a><br>';
}
}

?>

No comments:

Post a Comment