Skip to content

Instantly share code, notes, and snippets.

@awjrichards-zz
Created July 12, 2012 23:29
Show Gist options
  • Save awjrichards-zz/3101814 to your computer and use it in GitHub Desktop.
Save awjrichards-zz/3101814 to your computer and use it in GitHub Desktop.
First pass at admin_tree api - diffs from r741
Index: api/includes/ApiBase.php
===================================================================
--- api/includes/ApiBase.php (revision 741)
+++ api/includes/ApiBase.php (working copy)
@@ -62,7 +62,7 @@
}
}
} elseif ( $p == 'integer' ) {
- $i = intval( $_GET[$name] );
+ $i = intval( $_GET[$name] ); // @fixme this will return 0 on failure, or 1 if it's a non-empty array... is this desired?
$cache[$name] = min( max( $i, $allowed[$name][ApiBase::PARAM_MIN] ),
$allowed[$name][ApiBase::PARAM_MAX]);
} elseif ( $p == 'boolean' ) {
Index: api/includes/ApiMonuments.php
===================================================================
--- api/includes/ApiMonuments.php (revision 741)
+++ api/includes/ApiMonuments.php (working copy)
@@ -7,19 +7,22 @@
/**
* Definition of Monuments API
* @author Platonides
+ * @author awjrichards
*/
class ApiMonuments extends ApiBase {
const MAX_GEOSEARCH_AREA = 0.04;// 0.2 * 0.2 degrees
const GRANULARITY = 20;
+ private $isComplex = false;
+
protected function getParamDescription() {
return array(
/* FIXME: Copy from http://etherpad.wikimedia.org/WLM-tech*/
);
}
-
- function getAllowedParams() {
+
+ public function getAllowedParams() {
$params = array(
'props' => array( ApiBase::PARAM_DFLT => Monuments::$dbFields,
ApiBase::PARAM_TYPE => Monuments::$dbFields, ApiBase::PARAM_ISMULTI => true ),
@@ -28,14 +31,26 @@
'callback' => array( ApiBase::PARAM_DFLT => false, ApiBase::PARAM_TYPE => 'callback' ),
'limit' => array( ApiBase::PARAM_MIN => 0, ApiBase::PARAM_MAX => 5000,
ApiBase::PARAM_DFLT => 100, ApiBase::PARAM_TYPE => 'integer' ),
-
+
'action' => array( ApiBase::PARAM_DFLT => 'help',
ApiBase::PARAM_TYPE => array( 'help', 'search', 'statistics' ) ),
-
+
'srquery' => array( ApiBase::PARAM_DFLT => false, ApiBase::PARAM_TYPE => 'string' ),
'bbox' => array( ApiBase::PARAM_DFLT => false, ApiBase::PARAM_TYPE => 'string' ),
'BBOX' => array( ApiBase::PARAM_DFLT => false, ApiBase::PARAM_TYPE => 'string' ),
'srcontinue' => array( ApiBase::PARAM_DFLT => false, ApiBase::PARAM_TYPE => 'string' ),
+
+ // adminlevels
+ 'admlevel' => array(
+ ApiBase::PARAM_MIN => 0,
+ ApiBase::PARAM_MAX => 4,
+ ApiBase::PARAM_DFLT => 0,
+ ApiBase::PARAM_TYPE => 'integer'
+ ),
+ 'admval' => array(
+ ApiBase::PARAM_DFLT => false,
+ ApiBase::PARAM_TYPE => 'string'
+ ),
);
foreach ( Monuments::$dbFields as $field ) {
@@ -50,29 +65,33 @@
return $params;
}
-
- function executeModule() {
+
+ public function executeModule() {
switch ( $this->getParam( 'action' ) ) {
- case 'help':
- $this->help();
- break;
case 'search':
$this->search();
break;
case 'statistics':
$this->statistics();
+ break;
+ case 'adminlevels':
+ $this->adminlevels();
+ break;
+ case 'help':
+ default:
+ $this->help();
+ break;
}
}
- private $isComplex = false;
private function complexQuery() {
if ( $this->isComplex ) {
$this->error( 'Only one pattern matching (%) or full-text (~) condition allowed' );
}
$this->isComplex = true;
}
-
- function search() {
+
+ public function search() {
$fulltextColumns = array( 'name' => 1 );
if ( $this->getParam('format') == 'dynamickml' ) {
@@ -87,7 +106,7 @@
$forceIndex = false;
$orderby = Monuments::$dbPrimaryKey;
$db = Database::getDb();
-
+
foreach ( Monuments::$dbFields as $field ) {
if ( $this->getParam( "srwith$field" ) ) {
$where[] = $db->escapeIdentifier( $field ) . " <> ''";
@@ -96,6 +115,7 @@
$where[] = $db->escapeIdentifier( $field ) . " = ''";
}
}
+
foreach ( Monuments::$dbFields as $field ) {
$value = $this->getParam( "sr$field" );
if ( $value === false ) continue;
@@ -122,18 +142,18 @@
$where[$field] = $value;
}
}
-
- if ( $this->getParam('bbox') or $this->getParam('BBOX') ) {
+
+ if ( $this->getParam('bbox') or $this->getParam('BBOX') ) {
if ( $this->getParam('bbox') ) {
- $bbox = $this->getParam('bbox');
- } else {
- $bbox = $this->getParam('BBOX');
- }
- $coords = preg_split('/,|\s/', $bbox);
- $bl_lon = floatval( $coords[0] );
- $bl_lat = floatval( $coords[1] );
- $tr_lon = floatval( $coords[2] );
- $tr_lat = floatval( $coords[3] );
+ $bbox = $this->getParam('bbox');
+ } else {
+ $bbox = $this->getParam('BBOX');
+ }
+ $coords = preg_split('/,|\s/', $bbox);
+ $bl_lon = floatval( $coords[0] );
+ $bl_lat = floatval( $coords[1] );
+ $tr_lon = floatval( $coords[2] );
+ $tr_lat = floatval( $coords[3] );
if ( $bl_lat > $tr_lat || $bl_lon > $tr_lon ) {
$this->error( 'Invalid bounding box' );
}
@@ -144,9 +164,9 @@
$where['lon_int'] = self::intRange( $bl_lon, $tr_lon );
$where[] = "`lat` BETWEEN $bl_lat AND $tr_lat";
$where[] = "`lon` BETWEEN $bl_lon AND $tr_lon";
- }
+ }
- /* FIXME: User should be able to set sort fields and order */
+ /* FIXME: User should be able to set sort fields and order */
if ( $this->getParam('format') == 'kml' ) {
$orderby = array('monument_random');
} elseif ( $this->getParam('format') == 'html' ) {
@@ -165,23 +185,47 @@
}
$limit = $this->getParam( 'limit' );
-
+
$res = $db->select( array_merge( Monuments::$dbPrimaryKey, $this->getParam( 'props' ) ), Monuments::$dbTable, $where,
$orderby, $limit + 1, $forceIndex );
$this->getFormatter()->output( $res, $limit, 'srcontinue', $this->getParam( 'props' ), $orderby );
}
-
- function statistics() {
+
+ public function statistics() {
$st = new Statistics( $db = Database::getDb() );
- $items = $this->getParam( 'stitem' );
- $filters = explode('|', $this->getParam('stcountry'));
- $limit = $this->getParam( 'limit' );
+ $items = $this->getParam( 'stitem' );
+ $filters = explode('|', $this->getParam('stcountry'));
+ $limit = $this->getParam( 'limit' );
- $r = $st->retrieveReport($items, $filters, $limit);
- $this->getFormatter()->output($r, 9999999, 'stcontinue', array_merge(array('country', 'municipality'), $st->getAxis('columns')), Monuments::$dbPrimaryKey );
+ $r = $st->retrieveReport($items, $filters, $limit);
+ $this->getFormatter()->output($r, 9999999, 'stcontinue', array_merge(array('country', 'municipality'), $st->getAxis('columns')), Monuments::$dbPrimaryKey );
}
-
- function help() {
+
+ public function adminlevels() {
+ $admval = $this->getParam( 'admval' );
+ $admlevel = $this->getParam( 'admlevel' );
+ if ( $admval === false ) {
+ $this->error( 'You must specify a value for admval' );
+ }
+
+ // grab the db object
+ $db = Database::getDb();
+
+ // SELECT level, country, name FROM admin_tree WHERE country = $admval AND level > $admlevel;
+ // @TODO dbl check indices
+ $fields = array( 'level', 'country', 'name' );
+ $where = array(
+ 'country' => $db->sanitize( $admval ),
+ 'level > ' . $admlevel,
+ );
+ // @TODO support orderby/limit? no orderby param...
+ $limit = $this->getParam( 'limit' );
+ $res = $db->select( $fields, 'admin_tree', $where, false, $limit );
+ $this->getFormatter()->output( $res, $limit, 'srcontinue', $fields, false );
+ }
+
+ public function help() {
/* TODO: Expand me! */
echo '
<html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment