From patchwork Sat Jul 4 21:14:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Morris X-Patchwork-Id: 1707 Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id 604C519BC1301 for ; Sat, 4 Jul 2020 21:14:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on apollo.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_ADSP_CUSTOM_MED=0.001, DKIM_INVALID=1,DKIM_SIGNED=0.1,FREEMAIL_FROM=0.5,MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3,SPF_HELO_NONE=0.001,T_DMARC_POLICY_NONE=0.01, T_DMARC_SIMPLE_DKIM=0.01 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-BL-Results: [127.0.9.2] Received: from orion.archlinux.org (orion.archlinux.org [IPv6:2a01:4f8:160:6087::1]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Sat, 4 Jul 2020 21:14:52 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 6BECF1D3584292; Sat, 4 Jul 2020 21:14:50 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [5.9.250.164]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: luna) by orion.archlinux.org (Postfix) with ESMTPSA id 12B591D358428C; Sat, 4 Jul 2020 21:14:50 +0000 (UTC) Authentication-Results: orion.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=vgNk+rG/ Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 05861210F6; Sat, 4 Jul 2020 21:14:50 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=vgNk+rG/ Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 898AD20F85 for ; Sat, 4 Jul 2020 21:14:47 +0000 (UTC) Received: from orion.archlinux.org (orion.archlinux.org [IPv6:2a01:4f8:160:6087::1]) by luna.archlinux.org (Postfix) with ESMTPS for ; Sat, 4 Jul 2020 21:14:47 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 3755E1D358428A for ; Sat, 4 Jul 2020 21:14:43 +0000 (UTC) Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by orion.archlinux.org (Postfix) with ESMTPS for ; Sat, 4 Jul 2020 21:14:43 +0000 (UTC) Received: by mail-pg1-x541.google.com with SMTP id d4so16612816pgk.4 for ; Sat, 04 Jul 2020 14:14:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7Y9cFKaV9rxophasfRJ/xkBUwWs0mGOxB5MpLOanB0A=; b=vgNk+rG/GhmCcHevri7aALb1o2ZTHrhJnPZIHz6cjOyRYC1B84jWoaL9Aflhh4tQF5 nyql0/YhS9gGGcvkG54GO/x8Wh33AuadE+7QdGV/vYW2xFlHJGPgWpxNQ3rcaprQKuoq I03mvSaa2uGNYfAEwhrKzofERydXW/ENKkDBXmo/7YiUaO28li0W9GytJb3w5P4TV8Bu OFJol9jA8Yh9RkHbZkFAwBvtR9BVbYWZqyX7kBX9gKHqRZKe5du6AycVIHGbHoIUJ94C tfD16g7miyG6r0UbZXxn6VMV9z7hdiYQTh/qWj3I7YkTi53g/M5adlwLj9+PYHkJ4X/I EGGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7Y9cFKaV9rxophasfRJ/xkBUwWs0mGOxB5MpLOanB0A=; b=qEnijYsT1ES0xuSckSCGgDepuj2bOg7KdByiyEmNoUbO+2uMgNBTjX7zRDMQPovY33 p3jNt6hrrNJyEpP0JjNqaaGsqrn7eca7TwZbU32QKUfpgNT0y3G0imEembl/9Epk8usQ j8/Nt3pfG+VId7AYUQ3zoaBQ+NTin2Fsb+I/g1midqDBe1trj9/ahZ0IFJiueengsivM a5TOJuWRdwsyP6v8kd6S9jF8svkQSTrZyuut97uowO4TXIldAO4MpXV3z/OozAAJCsea FjscZbQzEGx22pcrUFPsrjb+XiSdiubwwYzBSoMg5N581qnBD2Kqw8X9WGBEI1ulfrq+ qu0Q== X-Gm-Message-State: AOAM5325GhxlaRJxcenxEFkesV8ln/tjPvMzQiGKmw+PBmSE8tLd+VqB cZE3rGGVH+3wRrKNxAq3ZL6ip9l5pJk= X-Google-Smtp-Source: ABdhPJzHco4jsacVXzK1akidyGtljgKE36P+ub+ONznGNHcArfkCAhbSNEbRdq1EyrzdSwZfb2jK0Q== X-Received: by 2002:a63:308:: with SMTP id 8mr36225744pgd.112.1593897280958; Sat, 04 Jul 2020 14:14:40 -0700 (PDT) Received: from localhost.localdomain ([2600:1:9a26:fe89:78d3:8a2f:514b:d2b7]) by smtp.gmail.com with ESMTPSA id d37sm15534966pgd.18.2020.07.04.14.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jul 2020 14:14:40 -0700 (PDT) From: Kevin Morris To: aur-dev@archlinux.org Subject: [PATCH] Bump RPC API Version 6 Date: Sat, 4 Jul 2020 14:14:26 -0700 Message-Id: <20200704211427.20535-1-kevr.gtalk@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: aur-dev@archlinux.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "Arch User Repository \(AUR\) Development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: aur-dev-bounces@archlinux.org Sender: "aur-dev" API Version 6 modifies `type=search` functionality: Split `arg` into keywords separated by spaces (quoted keywords are preserved). Search for packages containing the literal keyword `blah blah` AND `haha`: https://aur.archlinux.org/rpc/?v=6&type=search&arg="blah blah"%20haha Search for packages containing the literal keyword `abc 123`: https://aur.archlinux.org/rpc/?v=6&type=search&arg="abc 123" The following example searches for packages that contain `blah` AND `abc`: https://aur.archlinux.org/rpc/?v=6&type=search&arg=blah%20abc The legacy method still searches for packages that contain 'blah abc': https://aur.archlinux.org/rpc/?v=5&type=search&arg=blah%20abc https://aur.archlinux.org/rpc/?v=5&type=search&arg=blah%20abc API Version 6 is only considered during a `search` of `name` or `name-desc`. Note: This change was written as a solution to https://bugs.archlinux.org/task/49133. PS: + Some spacing issues fixed in comments. Signed-off-by: Kevin Morris --- doc/rpc.txt | 4 +++ web/lib/aurjson.class.php | 60 +++++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/doc/rpc.txt b/doc/rpc.txt index 3148ebea..b0f5c4e1 100644 --- a/doc/rpc.txt +++ b/doc/rpc.txt @@ -39,6 +39,10 @@ Examples `/rpc/?v=5&type=search&by=makedepends&arg=boost` `search` with callback:: `/rpc/?v=5&type=search&arg=foobar&callback=jsonp1192244621103` +`search` with API Version 6 for packages containing `cookie` AND `milk`:: + `/rpc/?v=6&type=search&arg=cookie%20milk` +`search` with API Version 6 for packages containing `cookie milk`:: + `/rpc/?v=6&type=search&arg="cookie milk"` `info`:: `/rpc/?v=5&type=info&arg[]=foobar` `info` with multiple packages:: diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 0ac586fe..b639653f 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -80,7 +80,7 @@ class AurJSON { if (isset($http_data['v'])) { $this->version = intval($http_data['v']); } - if ($this->version < 1 || $this->version > 5) { + if ($this->version < 1 || $this->version > 6) { return $this->json_error('Invalid version specified.'); } @@ -140,7 +140,7 @@ class AurJSON { } /* - * Check if an IP needs to be rate limited. + * Check if an IP needs to be rate limited. * * @param $ip IP of the current request * @@ -192,7 +192,7 @@ class AurJSON { $value = get_cache_value('ratelimit-ws:' . $ip, $status); if (!$status || ($status && $value < $deletion_time)) { if (set_cache_value('ratelimit-ws:' . $ip, $time, $window_length) && - set_cache_value('ratelimit:' . $ip, 1, $window_length)) { + set_cache_value('ratelimit:' . $ip, 1, $window_length)) { return; } } else { @@ -370,7 +370,7 @@ class AurJSON { } elseif ($this->version >= 2) { if ($this->version == 2 || $this->version == 3) { $fields = implode(',', self::$fields_v2); - } else if ($this->version == 4 || $this->version == 5) { + } else if ($this->version >= 4 && $this->version <= 6) { $fields = implode(',', self::$fields_v4); } $query = "SELECT {$fields} " . @@ -472,6 +472,27 @@ class AurJSON { return array('ids' => $id_args, 'names' => $name_args); } + /* + * Prepare a WHERE statement for each $transform($keyword), separated + * by $delim. + * + * @param $delim Delimiter used to join several keywords. + * @param $keywords Array of keywords. + * @param $transform Transformation function that receives a keyword + * and returns a transformed string. + * + * @return string WHERE condition statement of transformed keywords + * separated by $delim. + */ + private function join_where($delim, $keywords, $transform) { + $reduce_func = function($carry, $item) use(&$transform) { + array_push($carry, $transform($item)); + return $carry; + }; + $result = array_reduce($keywords, $reduce_func, array()); + return join($delim, $result); + } + /* * Performs a fulltext mysql search of the package database. * @@ -492,13 +513,36 @@ class AurJSON { if (strlen($keyword_string) < 2) { return $this->json_error('Query arg too small.'); } - $keyword_string = $this->dbh->quote("%" . addcslashes($keyword_string, '%_') . "%"); if ($search_by === 'name') { - $where_condition = "(Packages.Name LIKE $keyword_string)"; + if ($this->version >= 6) { + $keywords = preg_split('/"([^"]*)"|\h+/', $keyword_string, -1, + PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); + $func = function($keyword) { + $str = $this->dbh->quote("%" . addcslashes($keyword, '%_') . "%"); + return "(Packages.Name LIKE $str)"; + }; + $where_condition = $this->join_where(" AND ", $keywords, $func); + } else { + $keyword_string = $this->dbh->quote( + "%" . addcslashes($keyword_string, '%_') . "%"); + $where_condition = "(Packages.Name LIKE $keyword_string)"; + } } else if ($search_by === 'name-desc') { - $where_condition = "(Packages.Name LIKE $keyword_string OR "; - $where_condition .= "Description LIKE $keyword_string)"; + if ($this->version >= 6) { + $keywords = preg_split('/"([^"]*)"|\h+/', $keyword_string, -1, + PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); + $func = function($keyword) { + $str = $this->dbh->quote("%" . addcslashes($keyword, '%_') . "%"); + return "(Packages.Name LIKE $str OR Description LIKE $str)"; + }; + $where_condition = $this->join_where(" AND ", $keywords, $func); + } else { + $keyword_string = $this->dbh->quote( + "%" . addcslashes($keyword_string, '%_') . "%"); + $where_condition = "(Packages.Name LIKE $keyword_string "; + $where_condition .= "OR Description LIKE $keyword_string)"; + } } } else if ($search_by === 'maintainer') { if (empty($keyword_string)) {