Skip to content

Commit

Permalink
fix: use RW API URLs for the OCHA feeds
Browse files Browse the repository at this point in the history
Refs: UNO-777
  • Loading branch information
orakili committed Aug 17, 2023
1 parent 1cb77ec commit 8c3c6d1
Showing 1 changed file with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -145,18 +144,35 @@ public function execute(ViewExecutable $view) {

$view->result = [];

if (isset($feeds[$this->options['feed']]['url'])) {
// Initialize the pager. We only handle offset and limit.
$view->initPager();
$limit = $view->pager->getItemsPerPage() ?: 100;
$offset = $view->pager->current_page * $limit;

// Get the data from the RW API.
$total = 0;
if (!empty($feeds[$this->options['feed']]['url'])) {
// Request settings.
$url = $feeds[$this->options['feed']]['url'];
$method = 'GET';
$options = [
'headers' => [
// Really unfortunate but the JSON data returned by the unocha.org
// feeds has the "text/javascript" mimetype...
'Accept' => 'application/json, text/javascript',
'Accept' => 'application/json',
],
];

// Add the pagination to the query and ensure we have the required fields.
$url .= (strpos($url, '?') ? '&' : '?') . http_build_query([
'offset' => $offset,
'limit' => $limit,
'fields' => [
'include' => [
'file',
'url_alias',
],
],
]);

try {
$response = $this->httpClient->request($method, $url, $options);
$code = $response->getStatusCode();
Expand All @@ -165,8 +181,26 @@ public function execute(ViewExecutable $view) {
$body = $response->getBody()->getContents();
$data = Json::decode($body);

$total = $data['totalCount'] ?? 0;

$index = 0;
foreach ($data as $item) {
$unocha_url = rtrim(static::getOchaFeedBaseUrl(), '/') . '/';
foreach ($data['data'] ?? [] as $item) {
// Convert the RW API data to what was returned by the OCHA feeds.
if (empty($item['fields'])) {
continue;
}
$fields = $item['fields'];
$file = !empty($fields['file']) ? reset($fields['file']) : NULL;
$item = [
'nid' => $item['id'],
'title' => $fields['title'],
'changed' => $fields['date']['created'],
'path' => '/publications/' . ltrim(parse_url($item['fields']['url_alias'], \PHP_URL_PATH), '/'),
'uri' => isset($file['preview']['url-small']) ? preg_replace('#^https://[^/]+/#', $unocha_url, $file['preview']['url-small']) : '',
'field_publication_document' => isset($file['url']) ? preg_replace('#^https://[^/]+/#', $unocha_url, $file['url']) : '',
];

$item = $this->validateDocument($item);
// Only add the item if valid. The index property is required.
if (!empty($item)) {
Expand All @@ -176,21 +210,16 @@ public function execute(ViewExecutable $view) {
}
}
}
catch (RequestException $exception) {
catch (\Exception $exception) {
watchdog_exception('odsg_ocha', $exception);
}
}

// Initialize the pager. We only handle offset and limit.
$view->initPager();
$limit = $view->pager->getItemsPerPage();
$offset = $view->pager->getOffset();
$view->pager->current_page = 0;
$view->pager->total_items = count($view->result);

// Apply the offset and limit on the result set as the feed service doesn't
// handle them.
$view->result = array_slice($view->result, $offset, !empty($limit) ? $limit : NULL);
// Set the maximum number of results for the pager.
$view->pager->total_items = $total;
$view->pager->updatePageInfo();

// Explicitly set the number of rows and the execution time.
$view->total_rows = count($view->result);
$view->execute_time = time() - REQUEST_TIME;
}
Expand Down Expand Up @@ -355,6 +384,11 @@ public static function getOchaFeedData() {
static $feeds;
if (!isset($feeds)) {
$feeds = \Drupal::config('odsg_ocha.settings')->get('feeds') ?? [];

// Use the overriden feeds in state if any.
foreach ($feeds as $feed => $info) {
$feeds[$feed]['url'] = \Drupal::state()->get('odsg_ocha.feeds.' . $feed, '');
}
}
return $feeds;
}
Expand Down

0 comments on commit 8c3c6d1

Please sign in to comment.