Created
March 31, 2015 20:21
-
-
Save dvaisman/721de1c899ddf29c0229 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
## This function will read the contents of a text. However, needs to | |
## be passed through DocumentCloud first | |
<?php | |
$myfile = fopen("docCloudText.txt", "r") or die("Unable to open file!"); | |
echo fread($myfile,filesize("docCloudText.txt")); | |
fclose($myfile); | |
?> | |
</body> | |
</html> | |
## This is an example of form action to display contents to screen | |
<html> | |
<body> | |
<form action="welcome_get.php" method="get"> | |
Name: <input type="text" name="name"><br> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> | |
## Before this step, query needs to be passed to DocumentCloud to | |
## retrieve results of query search | |
## The GET method creates an array: key => value, key2 => value2, etc. | |
## keys are the names of form controls; values are the input data from user | |
## $_GET is an array of variables passed to the current script via URL parameters | |
## Developers prefer the POST method. Simply replace GET with POST | |
<html> | |
<body> | |
Welcome <?php echo $_GET["name"]; ?> | |
</body> | |
</html> | |
## The filter_list() function can be used to retrieve database query results | |
## List of PHP 5 Filter Functions | |
filter_has_var() Checks if a variable of a specified input type exist | |
filter_id() Returns the filter ID of a specified filter name | |
filter_input() Gets an external variable (e.g. from form input) and optionally filters it | |
filter_input_array() Gets external variables (e.g. from form input) and optionally filters them | |
filter_list() Returns a list of all supported filters | |
filter_var_array() Gets multiple variables and filter them | |
filter_var() Filters a variable with a specified filter | |
## Also: | |
filter.default | |
## Filter all $_GET, $_POST, $_COOKIE, $_REQUEST and $_SERVER data by this filter. | |
## Accepts the name of the filter you like to use by default. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// These are two examples of the canonical JSON representation of particular documents (Rand and Seeger) | |
// as specified by the document id. This is retrieved using the GET function on json | |
// The format is GET/api/documents/[id].json | |
{ | |
"id": "1684587-seegertranscript", | |
"title": "SeegerTranscript", | |
"access": "public", | |
"pages": 15, | |
"description": null, | |
"source": null, | |
"created_at": "Tue, 10 Mar 2015 21:38:16 +0000", | |
"updated_at": "Tue, 17 Mar 2015 21:15:13 +0000", | |
"canonical_url": "http://www.documentcloud.org/documents/1684587-seegertranscript.html", | |
"language": "eng", | |
"file_hash": "3d15ea7da2fb392422060f80f827942380d6a61f", | |
"display_language": "eng", | |
"resources": { | |
"pdf": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684587/seegertranscript.pdf", | |
"text": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684587/seegertranscript.txt", | |
"thumbnail": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684587/pages/seegertranscript-p1-thumbnail.gif", | |
"search": "https://www.documentcloud.org/documents/1684587/search.json?q={query}", | |
"print_annotations": "https://www.documentcloud.org/notes/print?docs[]=1684587", | |
"translations_url": "https://www.documentcloud.org/translations/{realm}/{language}", | |
"page": { | |
"image": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684587/pages/seegertranscript-p{page}-{size}.gif", | |
"text": "https://www.documentcloud.org/documents/1684587/pages/seegertranscript-p{page}.txt" | |
} | |
} | |
}, | |
{ | |
"id": "1684586-randtestimony", | |
"title": "RandTestimony", | |
"access": "public", | |
"pages": 11, | |
"description": null, | |
"source": null, | |
"created_at": "Tue, 10 Mar 2015 21:38:15 +0000", | |
"updated_at": "Tue, 17 Mar 2015 21:15:21 +0000", | |
"canonical_url": "http://www.documentcloud.org/documents/1684586-randtestimony.html", | |
"language": "eng", | |
"file_hash": "61b3ffcf117a27fa18bf2c9dd7f570d37b37b482", | |
"display_language": "eng", | |
"resources": { | |
"pdf": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684586/randtestimony.pdf", | |
"text": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684586/randtestimony.txt", | |
"thumbnail": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684586/pages/randtestimony-p1-thumbnail.gif", | |
"search": "https://www.documentcloud.org/documents/1684586/search.json?q={query}", | |
"print_annotations": "https://www.documentcloud.org/notes/print?docs[]=1684586", | |
"translations_url": "https://www.documentcloud.org/translations/{realm}/{language}", | |
"page": { | |
"image": "https://s3.amazonaws.com/s3.documentcloud.org/documents/1684586/pages/randtestimony-p{page}-{size}.gif", | |
"text": "https://www.documentcloud.org/documents/1684586/pages/randtestimony-p{page}.txt" | |
} | |
} | |
}, | |
// In addition, we can use the GET function to retrieve entitities within documents using the following statement: | |
// GET /api/documents/[id]/entities.json | |
// This is the JSON search results from DocumentCloud API. | |
// The JSON string is parsed into a JavaScript object using this format: | |
// JSON.parse(json-string). This will loop through the JavaScript objects to | |
// find the key specified by the HUAC search. | |
// getObjects function: | |
// Program to get objects that have a particular key in JSON | |
// Program will also retrieve an array of objects that match a particular value | |
//return an array of objects according to key, value, or key and value matching | |
function getObjects(obj, key, val) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { | |
objects = objects.concat(getObjects(obj[i], key, val)); | |
} else | |
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not) | |
if (i == key && obj[i] == val || i == key && val == '') { // | |
objects.push(obj); | |
} else if (obj[i] == val && key == ''){ | |
//only add if the object is not already in the array | |
if (objects.lastIndexOf(obj) == -1){ | |
objects.push(obj); | |
} | |
} | |
} | |
return objects; | |
} | |
//return an array of values that match on a certain key | |
function getValues(obj, key) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { | |
objects = objects.concat(getValues(obj[i], key)); | |
} else if (i == key) { | |
objects.push(obj[i]); | |
} | |
} | |
return objects; | |
} | |
//return an array of keys that match on a certain value | |
function getKeys(obj, val) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { | |
objects = objects.concat(getKeys(obj[i], val)); | |
} else if (obj[i] == val) { | |
objects.push(i); | |
} | |
} | |
return objects; | |
} | |
// var json = Here we need to enter a glossary | |
/// --- | |
{"witnesses": [ | |
{"firstname": | |
// '{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","ID":"44","str":"SGML","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}'; | |
var js = JSON.parse(json); | |
//example of grabbing objects that match some key and value in JSON | |
console.log(getObjects(js,'ID','SGML')); | |
//returns 1 object where a key names ID has the value SGML | |
//example of grabbing objects that match some key in JSON | |
console.log(getObjects(js,'ID','')); | |
//returns 2 objects since keys with name ID are found in 2 objects | |
//example of grabbing obejcts that match some value in JSON | |
console.log(getObjects(js,'','SGML')); | |
//returns 2 object since 2 obects have keys with the value SGML | |
//example of grabbing objects that match some key in JSON | |
console.log(getObjects(js,'ID','')); | |
//returns 2 objects since keys with name ID are found in 2 objects | |
//example of grabbing values from any key passed in JSON | |
console.log(getValues(js,'ID')); | |
//returns array ["SGML", "44"] | |
//example of grabbing keys by searching via values in JSON | |
console.log(getKeys(js,'SGML')); | |
//returns array ["ID", "SortAs", "Acronym", "str"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Read JSON data from web server using PHP | |
// JSON syntax: | |
// data represented in name/value pairs | |
//array [] --> object {} --> name-value pairs | |
// 2 data structures: collection of name/value pairs or ordered list | |
// Example: Array containing multiple objects | |
{ | |
"document": [ | |
{ | |
"id": "01", | |
"witness type": "friendly", | |
"witness occupation": "writer" | |
}, | |
{ | |
"id": "02", | |
"witness type": "unfriendly", | |
"witness occupation": "actor" | |
}] | |
} | |
//Datatypes: string (double-quoted unicode with backlash); value (string, boolean) | |
// String syntax | |
var json-object-name = { string : "string value", .......} | |
var obj = {witness occupation: 'writer'} | |
//PHP code | |
// read JSON data and send to PHP | |
// PHP conditionals to display data | |
//Creating and parsing a JSON object in PHP: | |
//PHP array encoded in JSON format | |
<?php | |
$array = array(1 => 'witness type', 2 => 'witness occupation' | |
echo json_encode($array); | |
?> | |
//Above code generates JSON data in following format: | |
"1": "witness type", "2": "witness occupation" | |
//Next, decode JSON data using PHP | |
<?php | |
//json string to decode | |
$json_string = '{"1": "witness type", "2": "witness occupation"} | |
//decode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Pseudocode-simple search | |
# If user enters "string", retrieve all instances of "string" from text | |
# Display results to screen | |
# Pseudocode: advanced search | |
# Allows users to search by keywords | |
# Keyword value pairs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Sample REST API Call | |
* | |
*/ | |
include('needed_functions.php'); | |
ob_start("ob_gzhandler"); | |
// Variable initialization | |
$error = false; | |
$format = strtolower(htmlentities($_GET['format'])); | |
$callback = htmlentities($_GET['callback']); | |
// Default format to pass in. Two versions are presented here--php and json | |
if ($format == '') $format = 'php'; | |
## if ($format == '') $format = 'json'; | |
## Adapted script for error checking. Check if php language for setting error to true is correct. | |
## Currently written as $error = true | |
if ($error) = true | |
$out['status'] = 'Error'; | |
$out['message'] = 'Bad Request'; | |
require_once('display_output.php'); | |
exit; | |
} | |
// Set up the response header | |
$out = array(); | |
$out['status'] = '200'; | |
$out['message'] = 'This is the API for advanced searches of the HUAC database.'; | |
$out['link'] = 'http://digitalhuac.com/## need API documentation path here: documentcloud.org?'; | |
$out['request']['format'] = $format; | |
$out['request']['key'] = $key; | |
$out['request']['date'] = date('Y-m-d h:i:s'); | |
// If the format is json and there is a callback passed in, set this call up for a JSONP response. | |
if ($format == 'json') { | |
if ($callback != '') $out['request']['callback'] = $callback; | |
} | |
## Here is where we need to lookup data in DocumentCloud. The following script is for MySQL lookups. | |
## It needs to be replaced with DocumentCloud lookup. However, PHP calls MySQL while DocumentCloud's | |
## API is called in json. Json and PHP can be combined, however it requires additional functions and is | |
## not as intuitive as working with one or the other. | |
// Perform the lookup of data in MySQL. This requires you to have an already setup mysql | |
// connection available. You can also accept in different $_GET variables in order to | |
// fine-tune your sql call and provide the appropriate data back. | |
// This is just a sample sql statement: | |
$sql = " | |
SELECT * | |
FROM table | |
WHERE field = '".$_GET['field']."' | |
order by field ASC | |
"; | |
// Make the SQL call | |
$result = mysql_query($sql); | |
if (! mysql_num_rows($result)) { | |
// There were no results found, come back with a 500 error status | |
$out['status'] = '500'; | |
$out['message'] = 'No Results Found'; | |
} else { | |
// Put all the resulting data into an array | |
while($set = mysql_fetch_assoc($result)) { | |
$out['items'][] = $set; | |
} | |
} | |
// Display output will take whatever is in the `$out` array and transform it into | |
// the format that has been requested. | |
require_once('display_output.php'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment