function run($constraints) { #set limit list($limit, $w) = ApiBase::setLimit($_GET['limit']); $warning = isset($w) ? $w : null; #set offset list($offset, $w) = ApiBase::setOffset($_GET['offset']); $warning = isset($w) ? $warning . $w : $warning; #set view list($target_table, $w) = self::setView($_GET['view']); $warning = isset($w) ? $warning . $w : $warning; #load list of parameters to select list($select, $w) = self::setSelect($_GET['show']); $warning = isset($w) ? $warning . $w : $warning; #load constraints #isset($_GET['muni']) ? $constraints['muni'] = $_GET['muni'] : null; #isset($_GET['county']) ? $constraints['county'] = $_GET['county'] : null; #construct query $query = ' SELECT SQL_CALC_FOUND_ROWS ' . $select . ' FROM `' . mysql_real_escape_string($target_table) . '` '; $query = isset($constraints) ? ApiBase::addConstraints($query . 'WHERE ', $constraints) : $query; $query .= 'LIMIT ' . mysql_real_escape_string($offset) . ', ' . mysql_real_escape_string($limit) . ' '; #run query try { $response = ApiBase::doQuery($query); $hits = ApiBase::doQuery('SELECT FOUND_ROWS()'); } catch (Exception $e) { return ApiBase::makeErrorResult('610', 'Select Failed. ' . 'Probably wrong name supplied.[' . $e->getMessage() . ']', $warning); } $body = array(); foreach ($response as $r) { array_push($body, array('hit' => ApiBase::sanitizeBit1($r))); } # so that xml plays nice #Did we get all? $hits = $hits[0]['FOUND_ROWS()']; $head = array('hits' => $offset . '–' . ($offset + count($body)) . ' of ' . $hits, 'limit' => $limit); if ($hits > $offset + $limit) { $head['continue'] = $offset + $limit; } if (!empty($warning)) { $head['warning'] = $warning; } //~ $head = Array( //~ 'hits' => $hits, //~ 'limit' => $limit, //~ 'warning' => $warning //~ ); $results = ApiBase::makeSuccessResultHead($head, $body); return $results; }
function run($constraints) { /* * use options to choose a query generating function * then */ $table = isset($_GET['table']) ? strtolower($_GET['table']) : null; $column = isset($_GET['column']) ? strtolower($_GET['column']) : null; $split = isset($_GET['split']) ? strtolower($_GET['split']) : null; #choose function based on given parameters try { if (isset($table) and isset($column)) { $warning = 'You cannot specify both table and column; ignoring table parameter. '; list($response, $warning) = self::splitSelector($split, 'main', $column, $constraints, $warning); } elseif (isset($table)) { list($response, $warning) = self::splitSelector($split, $table, NULL, $constraints, $warning); } elseif (isset($column)) { list($response, $warning) = self::splitSelector($split, 'main', $column, $constraints, $warning); } elseif (isset($split)) { #only split is set $warning = 'No table specified for split; assuming table = "main". '; list($response, $warning) = self::splitSelector($split, 'main', NULL, $constraints, $warning); } else { $warning = 'Neither column, table nor split parameter defined; assuming table = "all". '; $response = self::countTable('all', $constraints); } } catch (Exception $e) { return ApiBase::makeErrorResult('620', 'Count Failed. ' . 'Probably wrong parameter name supplied. [' . $e->getMessage() . ']', $warning); } return ApiBase::makeSuccessResult($warning, $response); }
function run($constraints) { #set limit list($limit, $w) = ApiBase::setLimit($_GET['limit']); $warning = isset($w) ? $w : null; #set offset list($offset, $w) = ApiBase::setOffset($_GET['offset']); $warning .= isset($w) ? $w : ''; try { switch (strtolower($_GET['function'])) { case strtolower('diff'): list($response, $hits) = self::getChanges($limit, $offset, $constraints); break; case strtolower('lists'): list($response, $hits) = self::getLists(); break; case strtolower('objectlessArtist'): #list of artists that have no objects list($response, $hits) = self::getObjectlessArtist($limit, $offset); break; case strtolower('yearlessArtist'): list($response, $hits) = self::getYearlessArtist($limit, $offset); break; case strtolower('artistlessObject'): list($response, $hits) = self::getArtistlessObject($limit, $offset); break; case 'info': #e.g. funtion=info&table=source&id=4 #Displays all known info for a given id of a given table $table = $_GET['table']; $id = $_GET['id']; if (isset($table) and isset($id)) { $response = self::getInfo($table, $id); } else { return ApiBase::makeErrorResult('631', 'Admin Failed. ' . 'Function "info" must be used together with a "table" and "id" parameter.', $warning); } break; default: return ApiBase::makeErrorResult('632', 'Admin Failed. ' . 'Sorry but [' . $_GET['function'] . '] is not a valid function for the "admin" action.', $warning); break; } } catch (Exception $e) { return ApiBase::makeErrorResult('630', 'Admin Failed. ' . 'Probably error in one of the constraints. [' . $e->getMessage() . ']', $warning); } $head = array(); if (isset($hits)) { $hits = $hits[0]['FOUND_ROWS()']; $head['hits'] = $offset . '–' . ($offset + count($response)) . ' of ' . $hits; $head['limit'] = $limit; if ($hits > $offset + $limit) { $head['continue'] = $offset + $limit; } } if (!empty($warning)) { $head['warning'] = $warning; } return ApiBase::makeSuccessResultHead($head, $response); }
function search() { # Database info (including username+pass) in external file if (file_exists('/home/andre/config.php')) { require_once '/home/andre/config.php'; } elseif (file_exists('config.php')) { require_once 'config.php'; } else { die('Couldn\'t find config file '); } include 'Format.php'; #formats the output include 'ApiBase.php'; #functions used by multiple modules include 'ApiGet.php'; #standard sql query stuff include 'ApiStats.php'; #stats about the database include 'ApiAdmin.php'; #various functions that the average user wouldn't care about include 'ApiArtist.php'; #hook into the artist_table #include('ApiHelp.php'); #help file/documentation global $helpurl; /* * Trying to connect to mysql server and database * Output Temporary error if unable to. */ if (!@mysql_connect($dbServer, $dbUser, $dbPassword)) { $results = ApiBase::makeErrorResult('500', 'Temporary Error. ' . 'Our server might be down, please try again later.[' . mysql_error() . ']', null); $errors = 1; } if (!@mysql_select_db($dbDatabase)) { $results = ApiBase::makeErrorResult('500', 'Temporary Error. ' . 'Our server might be down, please try again later.[' . mysql_error() . ']', null); $errors = 1; } /* * Set up jsonp compatibility */ if (strtolower($_GET['format']) == 'jsonp') { if (!isset($_GET['callback'])) { $results = ApiBase::makeErrorResult('640', 'JSONP Error. ' . 'Cannot request JSONP without a callback', null); $errors = 1; } } /* * If no errors were found during connection * let's proceed with our queries */ if (!$errors) { mysql_query("SET CHARACTER SET utf8"); #deal with general constraints try { $constraints = ApiBase::readConstraints(); } catch (ValueLimitException $e) { $results = ApiBase::makeErrorResult('602', $e->getMessage(), null); $errors = 1; } catch (CharacterLimitException $e) { $results = ApiBase::makeErrorResult('603', $e->getMessage(), null); $errors = 1; } catch (Exception $e) { $results = ApiBase::makeErrorResult('600', $e->getMessage(), null); $errors = 1; } #if kml/geojson output format then make sure has_coords is set if (($_GET['format'] == 'kml' or $_GET['format'] == 'geojson') and !isset($_GET['has_coords'])) { $constraints['coords'] = ApiBase::requireCoords(); } } if (!$errors) { #switch by action; return results array switch (strtolower($_GET['action'])) { case 'get': $results = ApiGet::run($constraints); break; case 'artist': $results = ApiArtist::run($constraints); break; case 'statistics': /* * Outputs counts per table/muni/county/artist/withCoords/withPics */ $results = ApiStats::run($constraints); break; case 'admin': /* * should generate a file with changes (since a certain date) and/or * list all entries (with changes) from a given source which has ugc=1. * Possibly use this to create an rss feed? */ $results = ApiAdmin::run($constraints); break; case 'help': header('Location: ' . $helpurl); break; default: $results = ApiBase::makeErrorResult('601', 'Action Failed. ' . 'Sorry but "' . $_GET['action'] . '" is not a valid action for this api.', $warning); break; } } /* Switch between output formats */ if (isset($_GET['format'])) { switch (strtolower($_GET['format'])) { case 'xml': Format::outputXml($results); break; case 'json': Format::outputJson($results); break; case 'php': Format::outputPhp($results); break; case 'kml': Format::outputKml($results); break; case 'xsl': Format::outputXsl($results); break; case 'wiki': Format::outputWiki($results); break; case 'geojson': Format::outputGeojson($results); break; case 'jsonp': Format::outputJsonp($results, $_GET['callback']); break; default: $results['head']['warning'] .= 'You chose an output format [' . $_GET['format'] . '] which does not exist ; defaulting to xml. '; Format::outputDefault($results); } } else { if (isset($_GET['callback'])) { Format::outputJsonp($results, $_GET['callback']); } else { Format::outputDefault($results); } } mysql_close(); }
function run($constraints) { #either look up on artwork_id or one of the others $prefix = null; $w = null; $otherSelectors = array('wiki', 'id', 'first_name', 'last_name', 'name', 'birth_year', 'death_year', 'is_dead', 'lifespan'); if (isset($_GET['artwork'])) { $prefix = 'at'; if (count(array_intersect($otherSelectors, array_keys($_GET))) > 0) { # if any of $otherSelectors were provided $w = 'The artwork parameter cannot be combined with any other selectors, these are therefore disregarded. '; } } $warning = isset($w) ? $w : null; #set limit list($limit, $w) = ApiBase::setLimit($_GET['limit']); $warning = isset($w) ? $warning . $w : $warning; #set offset list($offset, $w) = ApiBase::setOffset($_GET['offset']); $warning = isset($w) ? $warning . $w : $warning; #load list of parameters to select list($select, $getWorks, $w) = self::setSelect($_GET['show'], $prefix); $warning = isset($w) ? $warning . $w : $warning; #Needs support for # name, first_name, last_name as well (with name=Concatenate(first, ' ', last)) # as a constraint # Look up on artwork_id or other $query = null; if (isset($_GET['artwork'])) { $query = ' SELECT SQL_CALC_FOUND_ROWS ' . $select . ' FROM `artist_table` at INNER JOIN `artist_links` al ON al.`artist` = at.`id` WHERE al.`object` in ( "' . implode('", "', array_map('mysql_real_escape_string', explode('|', $_GET['artwork']))) . '" ) '; } else { #add available constraints $query = ' SELECT SQL_CALC_FOUND_ROWS ' . $select . ' FROM `artist_table` '; $query = isset($constraints) ? ApiBase::addConstraints($query . 'WHERE ', $constraints) : $query; } # add limit $query .= 'LIMIT ' . mysql_real_escape_string($offset) . ', ' . mysql_real_escape_string($limit) . ' '; #run query try { $response = ApiBase::doQuery($query); $hits = ApiBase::doQuery('SELECT FOUND_ROWS()'); } catch (Exception $e) { return ApiBase::makeErrorResult('610', 'Select Failed. ' . 'Probably wrong name supplied.[' . $e->getMessage() . ']', $warning); } # look up works for each artist $works = null; if ($getWorks) { $artists = array(); foreach ($response as $r) { array_push($artists, $r['id']); } list($works, $w) = self::getWorks($artists, $warning); $warning = isset($w) ? $warning . $w : $warning; } #collect results $body = array(); foreach ($response as $r) { if ($getWorks) { $r['works'] = array(); if (isset($works[$r['id']])) { foreach ($works[$r['id']] as $work) { array_push($r['works'], array('work' => $work)); # so that xml plays nice } } } array_push($body, array('hit' => ApiBase::sanitizeBit1($r))); # so that xml plays nice } #Did we get all? $hits = $hits[0]['FOUND_ROWS()']; $head = array('hits' => $offset . '–' . ($offset + count($body)) . ' of ' . $hits, 'limit' => $limit); if ($hits > $offset + $limit) { $head['continue'] = $offset + $limit; } if (!empty($warning)) { $head['warning'] = $warning; } $results = ApiBase::makeSuccessResultHead($head, $body); return $results; }