Skip to content

Instantly share code, notes, and snippets.

@makoni makoni/gist:4345440
Last active Dec 9, 2015

Embed
What would you like to do?
simple php-script for couchdb views building. all views in all databases.
<?php
set_time_limit( 0 );
// all dbs
$ch = prepareCurlResource();
curl_setopt( $ch, CURLOPT_URL, 'http://localhost:5984/_all_dbs' );
$data = curl_exec( $ch );
$dbs = json_decode( $data );
foreach ( $dbs as $db ) {
// skip _users and _replicator
if ( substr( $db, 0, 1 ) == '_' )
continue;
// getting all databases
$chCC = prepareCurlResource();
curl_setopt( $chCC, CURLOPT_URL, 'http://localhost:5984/' . $db . '/_all_docs?startkey="_design/"&endkey="_design0"&include_docs=true' );
$data = json_decode( curl_exec( $chCC ) );
if ( false === empty( $data->rows ) ) {
foreach( $data->rows as $design ) {
// creating views of design
if ( false === empty( $design->doc->views ) ) {
foreach( $design->doc->views as $viewName => $tmp ) {
echo 'Creating view: ' . $db . '/' . $design->id . '/_view/' . $viewName . "\n";
$chCC = prepareCurlResource();
curl_setopt( $chCC, CURLOPT_URL, 'http://localhost:5984/' . $db . '/' . $design->id . '/_view/' . $viewName . '?limit=0' );
curl_exec( $chCC );
}
}
}
}
}
function prepareCurlResource() {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_PORT, 5984 );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) );
return $ch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.