Skip to content

Commit

Permalink
REST API for purging tweets (digitalmethodsinitiative#205)
Browse files Browse the repository at this point in the history
REST API for purging tweets, thanks to @hoylen
  • Loading branch information
hoylen authored and dentoir committed Jul 27, 2016
1 parent e5b44b0 commit 993f4fd
Show file tree
Hide file tree
Showing 9 changed files with 1,857 additions and 7 deletions.
20 changes: 18 additions & 2 deletions analysis/common/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

db_connect($hostname, $dbuser, $dbpass, $database);

$datasets = get_all_datasets();

// catch parameters
if (isset($_GET['dataset']) && !empty($_GET['dataset']))
$dataset = urldecode($_GET['dataset']);
else {
else if (count($datasets) != 0) {
// Default to earliest created not-deleted query bin
$sql = "SELECT querybin FROM tcat_query_bins ORDER BY id LIMIT 1";
$rec = mysql_query($sql);
if ($res = mysql_fetch_assoc($rec))
$dataset = $res['querybin'];
} else {
$dataset = NULL; // No query bins exist
}
$datasets = get_all_datasets();

if (isset($_GET['query']) && !empty($_GET['query']))
$query = urldecode($_GET['query']);
else
Expand Down Expand Up @@ -818,6 +823,7 @@ function get_all_datasets() {
$dbh = pdo_connect();
$rec = $dbh->prepare("SELECT id, querybin, type, active, comments FROM tcat_query_bins WHERE access = " . TCAT_QUERYBIN_ACCESS_OK . " OR access = " . TCAT_QUERYBIN_ACCESS_READONLY . " ORDER BY LOWER(querybin)");
$datasets = array();
try {
if ($rec->execute() && $rec->rowCount() > 0) {
while ($res = $rec->fetch()) {
$row = array();
Expand Down Expand Up @@ -853,6 +859,16 @@ function get_all_datasets() {
$datasets[$row['bin']] = $row;
}
}

} catch (PDOException $e) {
if ($e->errorInfo[0] == '42S02') {
// Base table or view not found
// Tables not yet created: just return empty $datasets
} else {
throw $e;
}
}

return $datasets;
}

Expand Down
112 changes: 112 additions & 0 deletions api/api.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* TCAT API */

/* COMMON */

body {
font-family: sans-serif;
font-size: 12px;
}

h1 {
float: left;
margin: 0;
padding: 1em 0 1ex 0;

font-size: 150%;
}
h2 { font-size: 130%; }
h3 { font-size: 115%; }



/* ids */

#content {
max-width: 1000px;
padding: 1ex 0 1ex 0;
}

/* Top links */

#if_links {
float: right;
margin: 2ex 0 1ex 0;
}

#if_links a {
display: inline-block;
margin-left: 1em;
text-decoration: none;
color: #000;
}
#if_links a:before { content: "» "; }
#if_links a:hover { text-decoration: underline; }

/* Breadcrumb navigation */

ul.breadcrumbs {
display: block;
clear: both;
margin: 0;
padding: 1ex 0 1ex 0;
}

ul.breadcrumbs > li {
display: inline-block;
color: #999;
}
ul.breadcrumbs > li:first-child:before { content: ""; }
ul.breadcrumbs > li:before { content: " » "; }

ul.breadcrumbs > li > a {
display: inline-block;
color: #999;
text-decoration: none;
}
ul.breadcrumbs > li > a:hover {
color: #000;
text-decoration: underline;
}

/* Main contents of page */

div#content { clear: both; }

/* Tables */

table {
border-collapse: collapse;
}
th {
padding-right: 0.5em;
text-align: right;
}
th:after {
content: ":";
}
td { padding: 0.5ex; }

/* SPECIFIC */

/* Text input fields on querybin tweet view page */

input#startdate, input#enddate {
min-width: 16em; /* to accommodate "YYYY-MM-DD HH:MM:SS +hh:mm" */
}
#export-tweets-form {
float: left;
margin-top: 2ex;
}
#purge-tweets-form {
float: right;
margin-top: 2ex;
text-align: center;
}

#purge-time-warning {
clear: both;
padding: 1ex 0;
text-align: right;
color: #666;
}
/* EOF */
24 changes: 24 additions & 0 deletions api/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
require_once __DIR__ . '/lib/common.php';
require_once __DIR__ . '/lib/http_util.php';

switch (choose_mediatype(['text/html'])) {
case 'text/html':
html_begin("API", []);
echo <<<END
<ul>
<li><a href="querybin.php">List query bins</a></li>
</ul>
END;
echo "<p>Version: ", API_VERSION, "</p>\n";
html_end();
break;
default:
echo "DMI-TCAT API (version: ", API_VERSION, ")\n";
echo " Running this script from the command line does nothing useful!\n";
echo " Run \"php querybin.php --help\" instead.\n";
exit(1);
break;
}
36 changes: 36 additions & 0 deletions api/lib/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// Common code that is loaded by all the API PHP scripts.

define('API_VERSION', '1.0');

// Speed multiplier for estimating purge times.
//
// Set this to a number greater than 1.0 for slower machines.
// Set this to a number less than 1.0 for faster machines.

define('PURGE_TIME_FACTOR', 1.0);

// Default timezone
//
// Set this to a string indicating the timezone to use for displaying
// times and for parsing strings (when no explicit timezone is in the
// string).
//
// Valid values can be found at: <http://php.net/manual/timezones.php>
//
// Strings of the form "+HH:MM" DO NOT WORK correctly. if you must
// specify an offset, try the values under the "Other" section of the
// above page. Note: those values are incomplete and the sign is
// reversed (e.g. for UTC+10, the name is "Etc/GMT-10"). PHP!
//
// If not set (or set to NULL), times will be displayed in UTC and strings
// being parsed must explicitly include a timezone.
//
// Examples:
// $api_timezone = date_default_timezone_get(); // PHP's default
// $api_timezone = 'UTC';
// $api_timezone = 'Europe/Amsterdam';
// $api_timezone = 'Australia/Brisbane';

$api_timezone = NULL;

Loading

0 comments on commit 993f4fd

Please sign in to comment.