Skip to content

Instantly share code, notes, and snippets.

@dvaisman
Created March 28, 2015 20:55
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/641b70a567e0efed53ef to your computer and use it in GitHub Desktop.
Save dvaisman/641b70a567e0efed53ef to your computer and use it in GitHub Desktop.
pseudocode
#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
<!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 program will allow users to retrieve key pairs by specific value
// This program will not return entire categories of searches (e.g., friendly/unfriendly)
// http://stackoverflow.com/questions/19253753/javascript-find-json-value
// name/value pairs: "firstname": "Walter"
// The following below is an example of a JSON array
// JSON arrays contain objects (what's written in {}
{"witnesses":[
{"firstname": "Walter", "lastname": "Disney"},
{"firstname": "Ayn", "lastname": "Rand"},
{"firstname": "Peter", "lastname": "Seeger"},
{"firstname": "Bertolt", "lastname": "Brecht"}
]
##// Function below to convert key-value pairs to dictionary object.
## //Needs to be correctly formatted as json or .js
//
dict = {}
json.forEach(function(x) {
dict[x.code] = x.name
})
countryName = dict[countryCode]
// JSON reads data from a web serve and displays data in web page
// First, we create a JS string with JSON syntax
var text = '{ "witnesses" : [' +
'{"firstname": "Walter" , "lastname": "Disney"}, +
'{"firstname": "Ayn", "lastname": "Rand"}, +
' {"firstname": "Peter", "lastname": "Seeger"}, +
' {"firstname": "Bertolt", "lastname": "Brecht"} ]}';
// Then we use JS built-in function JSON.parse() to convert string
// to JavaScript Object
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
// This filter method [filter()] will identify an element by value
function getCountryByCode(code) {
return data.filter(
function(data){return data.code == code}
);
}
var found = getCountryByCode('DZ');
document.getElementById('output').innerHTML=found[0].name;
// 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"]
<?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']);
// This is only really required if you plan on validating an api key before you
// proceed. How you validate this key is totally up to you.
$key = htmlentities($_GET['key']);
// If no format is passed in, pick a default. Valid options are php, xml, json or test
if ($format == '') $format = 'xml';
// If you do any error checking above, set `$error` to true and uncomment below
// to stop the script execution if an error was found.
/*
if ($error) {
$out['status'] = 'Error';
$out['message'] = 'Bad Request';
require_once('display_output.php');
exit;
}
*/
// Setup the response header
$out = array();
$out['status'] = '200';
$out['message'] = 'This is a sample api call. Change this to a description of what this API call does.';
$out['link'] = 'http://yourdomain.com/path-to-your-api-documentation';
$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;
}
// 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');
<?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;
}
// Setup 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