pub / serci

Search the web with !keywords
git clone https://src.jayvii.de/pub/serci.git
Home | Home | Log | Files | Exports | Exports | Refs | README | RSS | RSS

commit 4a990c43d256913923eed1acc1c206e470b54df2
parent 31b493dc36dc71857ee0988892b395375f544702
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Mon, 27 Jan 2025 21:23:12 +0100

feat: add keyword options

Implements keyword options via the !key:option
syntax. This typically relates to languages, in
particular on wikipedia or translation searches,
such as deepl.

Diffstat:
Mconfig/config.php | 37+++++++++++++++++--------------------
Mindex.php | 23+++++++++++++++++++++--
2 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/config/config.php b/config/config.php @@ -47,12 +47,13 @@ $searches = array( "description" => "Manuals and documentation for the Debian Linux distribution" ), "deepl" => array( - "name" => "DeepL (en-de)", + "name" => "DeepL", "website" => "https://deepl.com", - "query" => "https://www.deepl.com/translator#de/en/%s", + "query" => "https://www.deepl.com/translator#%opt/%s", + "default_opts" => "de/en", "categories" => array("lang"), "keywords" => array("deepl"), - "description" => "DeepL Translator is a neural machine translation service that is owned by Cologne-based DeepL SE and offers translations for over 33 languages" + "description" => "DeepL Translator is a neural machine translation service that is owned by Cologne-based DeepL SE and offers translations for over 33 languages. Translation languages can be choosen via !deepl:de/en" ), "dictcc" => array( "name" => "dict.cc", @@ -215,12 +216,13 @@ $searches = array( "description" => "An American nonprofit digital library website founded in 1996, that provides free access to collections of digitized materials including websites, software applications, music, audiovisual, and print materials" ), "leo" => array( - "name" => "LEO (en-de)", + "name" => "LEO", "website" => "https://leo.org", - "query" => "https://dict.leo.org/englisch-deutsch/%s", + "query" => "https://dict.leo.org/%opt/%s", + "default_query" => "englisch-deutsch", "categories" => array("lang", "community"), "keywords" => array("leo"), - "description" => "An Internet-based and community-driven translation dictionary originating from the computer science department of the Technical University of Munich in Germany and run by the Leo GmbH since 2006" + "description" => "An Internet-based and community-driven translation dictionary originating from the computer science department of the Technical University of Munich in Germany and run by the Leo GmbH since 2006. Translation languages can be choosen via !leo:englisch-deutsch" ), "libretranslate" => array( "name" => "LibreTranslate", @@ -257,7 +259,8 @@ $searches = array( "metagermap" => array( "name" => "MetaGer Maps", "website" => "https://metager.de/?fokus=maps", - "query" => "https://maps.metager.de/en/%s/guess", + "query" => "https://maps.metager.de/%opt/%s/guess", + "default_opts" => "en", "categories" => array("map", "foss"), "keywords" => array( "metagermap", "metagermaps", "mgmap", "metamap" @@ -357,7 +360,7 @@ $searches = array( "openverse" => array( "name" => "Openverse", "website" => "https://openverse.org", - "query" => "https://openverse.org/de/search?q=%s", + "query" => "https://openverse.org/search?q=%s", "categories" => array("img"), "keywords" => array("openverse", "creativecommons", "ov", "cc"), "description" => "An extensive library of free stock photos, images, and audio, available for free use under a Creative Commons license or is in the public domain" @@ -471,10 +474,11 @@ $searches = array( "symbl" => array( "name" => "SYMBL", "website" => "https://symbl.cc", - "query" => "https://symbl.cc/en/search/?q=%s", + "query" => "https://symbl.cc/%opt/search/?q=%s", + "default_opts" => "en", "categories" => array("misc"), "keywords" => array("symbl", "symbol", "unicode"), - "description" => "A search, table and description repository for (almost) all Unicode characters and Emojis" + "description" => "A search, table and description repository for (almost) all Unicode characters and Emojis. Language can be choosen via !symbl:en" ), "thedocumentfoundation" => array( "name" => "The Document Fdn.", @@ -551,18 +555,11 @@ $searches = array( "wikipedia" => array( "name" => "Wikipedia", "website" => "https://wikipedia.org", - "query" => "https://wikipedia.org/w/index.php?title=Special%3ASearch&search=%s", + "query" => "https://%opt.wikipedia.org/w/index.php?title=Special%3ASearch&search=%s", + "default_opts" => "en", "categories" => array("wiki", "foss", "edu", "community"), "keywords" => array("wikipedia", "wiki", "wp", "wk"), - "description" => "A free content online encyclopedia written and maintained by a community of volunteers through open collaboration. It is the largest and most-read reference work in history, and is consistently ranked among the ten most visited websites world-wide" - ), - "wikipediade" => array( - "name" => "Wikipedia (DE)", - "website" => "https://de.wikipedia.org", - "query" => "https://de.wikipedia.org/w/index.php?title=Special%3ASearch&search=%s", - "categories" => array("wiki", "foss", "edu", "community"), - "keywords" => array("wikipediade", "wikide", "wpde", "wkde"), - "description" => "A free content online encyclopedia written and maintained by a community of volunteers through open collaboration. It is the largest and most-read reference work in history, and is consistently ranked among the ten most visited websites world-wide. Specifically accesses the German Version of Wikipedia" + "description" => "A free content online encyclopedia written and maintained by a community of volunteers through open collaboration. It is the largest and most-read reference work in history, and is consistently ranked among the ten most visited websites world-wide. Language can be choosen via !wikipedia:en" ), "wikisource" => array( "name" => "Wikisource", diff --git a/index.php b/index.php @@ -54,9 +54,23 @@ $search = $fallback; } + /* Get options from keyword (syntax: !key:opt)*/ + if (preg_match('/\![A-Za-z0-9_\-\.]+:[A-Za-z0-9_\-\.\/]+/', $query) === 1) { + $keyoption = preg_replace( + '/^.*?\![A-Za-z0-9_\-\.]+:([A-Za-z0-9_\-\.\/]+).*$/', + '${1}', + $query + ); + $keyoption = strtolower($keyoption); + } elseif (array_key_exists("default_opts", $searches[$search])) { + $keyoption = $searches[$search]["default_opts"]; + } else { + $keyoption = ""; + } + /* Strip keyword from search term */ $search_term = preg_replace( - '/\![A-Za-z0-9_\-\.]+/', + '/\![A-Za-z0-9_\-\.]+:{0,1}[A-Za-z0-9_\-\.\/]*/', '', $query ); @@ -72,7 +86,12 @@ $target = str_replace( '%s', rawurlencode($search_term), - $searches[$search]["query"] + /* insert keyoption */ + str_replace( + '%opt', + $keyoption, + $searches[$search]["query"] + ) ); /* Redirect to target search engine: 307 - Temporary Redirect */