Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2020-04-01 11:40:52 +0300
committerJoas Schilling <coding@schilljs.com>2020-04-01 11:40:52 +0300
commit3b42ea2008b1745805572a3cf74162cc534a18fd (patch)
treebdc7dde3c910a562c7c4ea5a2d95ea76ac8e8a32 /sample-commands
parent38734148979e0a5b3465d14df41f2b815a415a92 (diff)
Fix the wikipedia example
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'sample-commands')
-rw-r--r--sample-commands/wikipedia.php29
1 files changed, 13 insertions, 16 deletions
diff --git a/sample-commands/wikipedia.php b/sample-commands/wikipedia.php
index 1fcccf1e3..646582330 100644
--- a/sample-commands/wikipedia.php
+++ b/sample-commands/wikipedia.php
@@ -42,42 +42,39 @@ if ($searchTerm === '--help') {
$endpoint = 'https://en.wikipedia.org/w/api.php';
$parameters = [
- 'action' => 'opensearch',
+ 'action' => 'query',
+ 'generator' => 'prefixsearch',
+ 'gpssearch' => $searchTerm,
+ 'prop' => 'description|info',
'format' => 'json',
'formatversion' => 2,
- 'search' => $searchTerm,
+ 'inprop' => 'url',
];
$content = file_get_contents($endpoint . '?' . http_build_query($parameters));
$results = json_decode($content, true);
-[, $titles, $descriptions, $links] = $results;
+$pages = $results['query']['pages'];
-$numArticles = count($titles);
+$numArticles = count($pages);
if ($numArticles === 0) {
echo 'Wikipedia did not have any results for "' . $searchTerm . '" :(' . "\n";
return;
}
-if ($numArticles !== count($descriptions) || $numArticles !== count($links)) {
- echo 'Result returned from wikipedia is maleformed';
- return 1;
-}
-
$response = 'Wikipedia search results for "' . $searchTerm . '":' . "\n";
-
$maxArticles = $numArticles > 7 ? 5 : $numArticles;
$length = (int) ((800 - strlen($response)) / $maxArticles);
-foreach ($titles as $key => $title) {
+foreach ($pages as $key => $page) {
if ($key >= $maxArticles) {
break;
}
- $link = " - {$links[$key]}\n";
- $remainingLength = max(strlen($title),$length - strlen($link));
- if ($remainingLength < strlen("* $title - {$descriptions[$key]}")) {
- $response .= substr("* $title - {$descriptions[$key]}", 0, $remainingLength) . '…' . $link;
+ $link = " - {$page['canonicalurl']}\n";
+ $remainingLength = max(strlen($page['title']),$length - strlen($link));
+ if ($remainingLength < strlen("* {$page['title']} - {$page['description']}")) {
+ $response .= substr("* {$page['title']} - {$page['description']}", 0, $remainingLength) . '…' . $link;
} else {
- $response .= "* $title - {$descriptions[$key]}" . $link;
+ $response .= "* {$page['title']} - {$page['description']}" . $link;
}
}