Skip to content

Instantly share code, notes, and snippets.

@dvaisman
Created March 31, 2015 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvaisman/721de1c899ddf29c0229 to your computer and use it in GitHub Desktop.
Save dvaisman/721de1c899ddf29c0229 to your computer and use it in GitHub Desktop.
<!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.
// 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"]
// 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
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment