You are here

function views_oai_pmh_plugin_display::build_oai_resumption_token in Views OAI-PMH 7.2

Same name and namespace in other branches
  1. 6.2 plugins/views_oai_pmh_plugin_display.inc \views_oai_pmh_plugin_display::build_oai_resumption_token()
  2. 6 plugins/views_oai_pmh_plugin_display.inc \views_oai_pmh_plugin_display::build_oai_resumption_token()
  3. 7 plugins/views_oai_pmh_plugin_display.inc \views_oai_pmh_plugin_display::build_oai_resumption_token()

File

plugins/views_oai_pmh_plugin_display.inc, line 416
Contains the OAI-PMH display plugin.

Class

views_oai_pmh_plugin_display
We are based on a feed display for compatibility.

Code

function build_oai_resumption_token() {
  if (count($this->oai_errors)) {
    return;
  }
  $items_per_page = $this->view->query->pager
    ->get_items_per_page();
  $current_page = $this->view->query->pager
    ->get_current_page();
  $total_items = $this->view->query->pager
    ->get_total_items();
  $total_pages = $this->view->query->pager
    ->get_pager_total();
  $cursor = $items_per_page * $current_page;
  $token = $current_page == $total_pages - 1 ? NULL : md5(uniqid());
  $token_expire_time = gmstrftime('%Y-%m-%dT%H:%M:%SZ', time() + 24 * 3600);
  $token_attrs = array(
    'completeListSize' => $total_items,
    'cursor' => $cursor,
  );
  if ($token) {
    $token_attrs['expirationDate'] = $token_expire_time;
    $data = array(
      'cursor' => $cursor,
      'total_items' => $total_items,
      'current_page' => $current_page,
      'metadataPrefix' => $this->oai_args['metadataPrefix'],
      'query' => $this->view->query,
    );
    $fields = array(
      'serialized' => 1,
      'created' => REQUEST_TIME,
      'expire' => time() + OAI_TOKEN_LIFETIME,
      'data' => serialize($data),
      'cid' => $token,
    );

    // doing my own insert here because cache_set was silently eating
    // max_allowed_packet errors and dropping cache inserts.  http://drupal.org/node/542874
    // changing the my.ini setting fixed the error, but I would like users to know about the error.
    //
    // cache_set($token, $cache, 'cache_views_oai_pmh', time()+24*3600);
    db_insert('cache_views_oai_pmh')
      ->fields($fields)
      ->execute();
  }
  return format_xml_elements(array(
    array(
      'key' => 'resumptionToken',
      'attributes' => $token_attrs,
      'value' => $token,
    ),
  ));
}