From 0488e8597c83ce9dfc75398db197249196157e6d Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 12 Apr 2011 00:15:49 -0500 Subject: rpc: introduce multiinfo query (fixes FS#17583) The majority of "real world" info requests [1] come in hefty batches. We would be better served to handle these in one request rather than multiple by allowing AUR clients to send multiple arguments. This enables things like this to work: http://aur.test/rpc.php?type=multiinfo&arg[]=cups-xerox&arg[]=cups-mc2430dl&arg[]=10673 Note to RPC users: unfortunately due to the asinine design of PHP, you unfortunately have to use the 'arg[]' syntax if you want more than one query argument, or you will only get the package satisfying the last arg you pass. [1] Rough data from April 11, 2011, with a total hit count of 1,109,163: 12 /login.php 13 /rpc.php?type=sarch 15 /rpc.php?type=msearch 16 /pingserver.php 16 /rpc.php 22 /logout.php 163 /passreset.php 335 /account.php 530 /pkgsubmit.php 916 /rss2.php 3838 /index.php 6752 /rss.php 9699 / 42478 /rpc.php?type=search 184737 /packages.php 681725 /rpc.php?type=info That means a whopping 61.5% of our requests were for info over the RPC interface; package pages are a distant second at only 16.7%. Lukas: Introduce "multiinfo" query instead of extending "info" (for the sake of backward compatibility). Signed-off-by: Dan McGee Signed-off-by: Lukas Fleischer --- web/html/rpc.php | 1 + web/lib/aurjson.class.php | 67 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) (limited to 'web') diff --git a/web/html/rpc.php b/web/html/rpc.php index 240cad1..1a9ca34 100644 --- a/web/html/rpc.php +++ b/web/html/rpc.php @@ -18,6 +18,7 @@ if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) { echo '
    '; echo '
  • search
  • '; echo '
  • info
  • '; + echo '
  • multiinfo
  • '; echo '
  • msearch
  • '; echo '

'; echo 'Each method requires the following HTTP GET syntax:
'; diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 2521948..bb7344a 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -14,7 +14,9 @@ include_once("aur.inc"); **/ class AurJSON { private $dbh = false; - private static $exposed_methods = array('search', 'info', 'msearch'); + private static $exposed_methods = array( + 'search', 'info', 'multiinfo', 'msearch' + ); private static $fields = array( 'Packages.ID', 'Name', 'Version', 'CategoryID', 'Description', 'URL', 'License', @@ -107,6 +109,36 @@ class AurJSON { } } + /** + * Parse the args to the multiinfo function. We may have a string or an + * array, so do the appropriate thing. Within the elements, both * package + * IDs and package names are valid; sort them into the relevant arrays and + * escape/quote the names. + * @param $args the arg string or array to parse. + * @return mixed An array containing 'ids' and 'names'. + **/ + private function parse_multiinfo_args($args) { + if (!is_array($args)) { + $args = array($args); + } + + $id_args = array(); + $name_args = array(); + foreach ($args as $arg) { + if (!$arg) { + continue; + } + if (is_numeric($arg)) { + $id_args[] = intval($arg); + } else { + $escaped = mysql_real_escape_string($arg, $this->dbh); + $name_args[] = "'" . $escaped . "'"; + } + } + + return array('ids' => $id_args, 'names' => $name_args); + } + /** * Performs a fulltext mysql search of the package database. * @param $keyword_string A string of keywords to search with. @@ -155,6 +187,39 @@ class AurJSON { return $this->process_query('info', $query); } + /** + * Returns the info on multiple packages. + * @param $pqdata A comma-separated list of IDs or names of the packages. + * @return mixed Returns an array of results containing the package data + **/ + private function multiinfo($pqdata) { + $fields = implode(',', self::$fields); + $args = $this->parse_multiinfo_args($pqdata); + $ids = $args['ids']; + $names = $args['names']; + + if (!$ids && !$names) { + return $this->json_error('Invalid query arguments'); + } + + $query = "SELECT {$fields} " . + " FROM Packages WHERE "; + if ($ids) { + $ids_value = implode(',', $args['ids']); + $query .= "ID IN ({$ids_value})"; + } + if ($ids && $names) { + $query .= " OR "; + } + if ($names) { + // individual names were quoted in parse_multiinfo_args() + $names_value = implode(',', $args['names']); + $query .= "Name IN ({$names_value})"; + } + + return $this->process_query('multiinfo', $query); + } + /** * Returns all the packages for a specific maintainer. * @param $maintainer The name of the maintainer. -- cgit v1.2.3-70-g09d2