LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

wikipedia.php (1678B)

      1 <?php
      2     class WikipediaRequest extends EngineRequest {
      3         public function get_request_url() {
      4                 $this->wikipedia_domain = "wikipedia.org";
      5                 $query_encoded = urlencode($this->query);
      6 
      7                 $languages = json_decode(file_get_contents("static/misc/languages.json"), true);
      8 
      9                 if (array_key_exists($this->opts->language, $languages))
     10                     $this->wikipedia_domain = $languages[$this->opts->language]["wikipedia"] . ".wikipedia.org";
     11 
     12                 return "https://$this->wikipedia_domain/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro&explaintext&redirects=1&pithumbsize=500&titles=$query_encoded";
     13         }
     14 
     15         public function parse_results($response) {
     16             $json_response = json_decode($response, true);
     17             if (!$json_response)
     18                 return array();
     19 
     20             $first_page = array_values($json_response["query"]["pages"])[0];
     21 
     22             if (array_key_exists("missing", $first_page))
     23                 return array();
     24 
     25             $description = substr($first_page["extract"], 0, 250) . "...";
     26 
     27             $source = "https://$this->wikipedia_domain/wiki/$this->query";
     28             $response = array(
     29                 "special_response" => array(
     30                     "response" => htmlspecialchars($description),
     31                     "source" => $source
     32                 )
     33             );
     34 
     35             if (array_key_exists("thumbnail",  $first_page)) {
     36                 $image_url = $first_page["thumbnail"]["source"];
     37                 $response["special_response"]["image"] = $image_url;
     38             }
     39 
     40             return $response;
     41         }
     42     }
     43 ?>