get_storage_total()))); case 'collections': exit(json_encode($db->get_collection_list_with_timestamps())); case 'collection_counts': exit(json_encode($db->get_collection_list_with_counts())); case 'collection_usage': $results = $db->get_collection_storage_totals(); foreach (array_keys($results) as $collection) { $results[$collection] = ceil($results[$collection] / 1024); #converting to k from bytes } exit(json_encode($results)); default: report_problem(WEAVE_ERROR_INVALID_PROTOCOL, 400); } } elseif ($function == 'storage') { if ($id) #retrieve a single record { $wbo = $db->retrieve_objects($collection, $id, 1); #get the full contents of one record if (count($wbo) > 0) { $item = array_shift($wbo); echo $item->json(); } else report_problem("record not found", 404); } else #retrieve a batch of records. Sadly, due to potential record sizes, have the storage object stream the output... { $full = array_key_exists('full', $_GET) && $_GET['full']; $outputter = new WBOJsonOutput($full); $params = validate_search_params(); $ids = $db->retrieve_objects($collection, null, $full, $outputter, $params['parentid'], $params['predecessorid'], $params['newer'], $params['older'], $params['sort'], $params['limit'], $params['offset'], $params['ids'], $params['index_above'], $params['index_below'], $params['depth'] ); } } } else if ($_SERVER['REQUEST_METHOD'] == 'PUT') #add a single record to the server { $wbo = new wbo(); if (!$wbo->extract_json(get_json())) report_problem(WEAVE_ERROR_JSON_PARSE, 400); check_quota($db); check_timestamp($collection, $db); #use the url if the json object doesn't have an id if (!$wbo->id() && $id) { $wbo->id($id); } $wbo->collection($collection); $wbo->modified($server_time); #current microtime if ($wbo->validate()) { #if there's no payload (as opposed to blank), then update the metadata if ($wbo->payload_exists()) $db->store_object($wbo); else $db->update_object($wbo); } else { report_problem(WEAVE_ERROR_INVALID_WBO, 400); } echo json_encode($server_time); } else if ($_SERVER['REQUEST_METHOD'] == 'POST') { $json = get_json(); check_quota($db); check_timestamp($collection, $db); $success_ids = array(); $failed_ids = array(); $db->begin_transaction(); foreach ($json as $wbo_data) { $wbo = new wbo(); if (!$wbo->extract_json($wbo_data)) { $failed_ids[$wbo->id()] = $wbo->get_error(); continue; } $wbo->collection($collection); $wbo->modified($server_time); if ($wbo->validate()) { #if there's no payload (as opposed to blank), then update the metadata if ($wbo->payload_exists()) { $db->store_object($wbo); } else { $db->update_object($wbo); } $success_ids[] = $wbo->id(); } else { $failed_ids[$wbo->id()] = $wbo->get_error(); } } $db->commit_transaction(); echo json_encode(array('success' => $success_ids, 'failed' => $failed_ids)); } else if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { check_timestamp($collection, $db); if ($id) { $db->delete_object($collection, $id); } else if ($collection) { $params = validate_search_params(); $db->delete_objects($collection, null, $params['parentid'], $params['predecessorid'], $params['newer'], $params['older'], $params['sort'], $params['limit'], $params['offset'], $params['ids'], $params['index_above'], $params['index_below'] ); } else { if (!array_key_exists('HTTP_X_CONFIRM_DELETE', $_SERVER)) report_problem(WEAVE_ERROR_NO_OVERWRITE, 412); $db->delete_user($username); } echo json_encode($server_time); } else { #bad protocol. There are protocols left? HEAD, I guess. report_problem(1, 400); } } catch(Exception $e) { report_problem($e->getMessage(), $e->getCode()); } #The datasets we might be dealing with here are too large for sticking it all into an array, so #we need to define a direct-output method for the storage class to use. If we start producing multiples #(unlikely), we can put them in their own class. class WBOJsonOutput { private $_full = null; private $_comma_flag = 0; private $_output_format = 'json'; function __construct ($full = false) { $this->_full = $full; if (array_key_exists('HTTP_ACCEPT', $_SERVER) && !preg_match('/\*\/\*/', $_SERVER['HTTP_ACCEPT']) && !preg_match('/application\/json/', $_SERVER['HTTP_ACCEPT'])) { if (preg_match('/application\/whoisi/', $_SERVER['HTTP_ACCEPT'])) { header("Content-type: application/whoisi"); $this->_output_format = 'whoisi'; } elseif (preg_match('/application\/newlines/', $_SERVER['HTTP_ACCEPT'])) { header("Content-type: application/newlines"); $this->_output_format = 'newlines'; } } } function set_format($format) { $this->_output_format = $format; } function output($sth) { if (($rowcount = $sth->rowCount()) > 0) { header('X-Weave-Records: ' . $rowcount); } if ($this->_output_format == 'newlines') { return $this->output_newlines($sth); } elseif ($this->_output_format == 'whoisi') { return $this->output_whoisi($sth); } else { return $this->output_json($sth); } } function output_json($sth) { echo '['; while ($result = $sth->fetch(PDO::FETCH_ASSOC)) { if ($this->_comma_flag) { echo ','; } else { $this->_comma_flag = 1; } if ($this->_full) { $wbo = new wbo(); $wbo->populate($result); echo $wbo->json(); } else echo json_encode($result{'id'}); } echo ']'; return 1; } function output_whoisi($sth) { while ($result = $sth->fetch(PDO::FETCH_ASSOC)) { if ($this->_full) { $wbo = new wbo(); $wbo->populate($result); $output = $wbo->json(); } else $output = json_encode($result{'id'}); echo pack('N', mb_strlen($output, '8bit')) . $output; } return 1; } function output_newlines($sth) { while ($result = $sth->fetch(PDO::FETCH_ASSOC)) { if ($this->_full) { $wbo = new wbo(); $wbo->populate($result); echo preg_replace('/\n/', '\u000a', $wbo->json()); } else echo json_encode($result{'id'}); echo "\n"; } return 1; } } ?>