You are here

protected function views_oai_pmh_plugin_style::save_resumption_token in Views OAI-PMH 7.3

Saves a resumption token to the database and appends it to the XML.

2 calls to views_oai_pmh_plugin_style::save_resumption_token()
views_oai_pmh_plugin_style::list_identifiers_render in plugins/views_oai_pmh_plugin_style.inc
Renders a response to the 'ListIdentifiers' OAI-PMH request.
views_oai_pmh_plugin_style::list_records_render in plugins/views_oai_pmh_plugin_style.inc
Renders a response to the 'ListRecords' OAI-PMH request.

File

plugins/views_oai_pmh_plugin_style.inc, line 689
Contains the base OAI-PMH style plugin.

Class

views_oai_pmh_plugin_style
Views OAI-PMH_plugin style.

Code

protected function save_resumption_token() {
  if (get_class($this->view->query->pager) != 'views_plugin_pager_full') {
    watchdog('views_oai_pmh', 'Unexpected pager type. A full pager is required for harvesters to be able to retrieve all the data.', NULL, WATCHDOG_ERROR);
    return;
  }
  $items_per_page = $this->view->query->pager
    ->get_items_per_page();
  $total_items = $this->view->query->pager
    ->get_total_items();

  // Only generate the token if the list is incomplete.
  if ($total_items > $items_per_page) {
    $current_page = $this->view->query->pager
      ->get_current_page();
    $total_pages = $this->view->query->pager
      ->get_pager_total();
    $cursor = $items_per_page * $current_page;

    // When the list is complete, standard requires that an empty token is
    // output.
    $token = $current_page == $total_pages - 1 ? '' : md5(uniqid());

    // Add token output.
    $token_element = $this->xml
      ->createElement('resumptionToken', $token);
    $this->xml_verb
      ->appendChild($token_element);
    $token_element
      ->setAttribute('completeListSize', $total_items);
    $token_element
      ->setAttribute('cursor', $cursor);
    if ($token) {
      $expire = time() + VIEWS_OAI_PMH_TOKEN_LIFETIME;
      $token_element
        ->setAttribute('expirationDate', gmstrftime('%Y-%m-%dT%H:%M:%SZ', $expire));

      // Save token to database.
      $data = array(
        'verb' => $this->request->verb,
        'metadata_format' => $this->request->metadata_format->id,
        'query' => $this->view->query,
        'current_page' => $current_page,
      );
      $fields = array(
        'serialized' => 1,
        'created' => $this->request->response_timestamp,
        'expire' => $expire,
        'data' => serialize($data),
        'cid' => $token,
      );

      // Doing our own insert here because cache_set() is silently eating
      // max_allowed_packet errors and dropping cache inserts. Changing the
      // database configuration fixes the error, so let's make users aware of
      // it. Reference: http://drupal.org/node/542874.
      //
      // cache_set($token, $cache, 'cache_views_oai_pmh', time() +
      // VIEWS_OAI_PMH_TOKEN_LIFETIME);.
      db_insert('cache_views_oai_pmh')
        ->fields($fields)
        ->execute();
    }
  }
}