You are here

function qpcache_qp in QueryPath 6

Same name and namespace in other branches
  1. 7.3 qpcache/qpcache.module \qpcache_qp()
  2. 7.2 qpcache/qpcache.module \qpcache_qp()

Factory for creating a QueryPath object.

This should be used only when retrieving remote files. Do not use it on local content or DOM/SimpleXML objects.

This factory builds the QueryPath using a key. If the key exists in the database and has not expired, then the document in the database is used. Otherwise, the source is retrieved and parsed.

Parameters

$key: The key to retrieve. Typically this is a URL. It can be any data type but a resource (which is unserializable).

$ttl: Time to live. This will be run through strtotime().

$query: CSS query to run on the object. Defaults to NULL.

$options: Array of options to pass to qp(). Defaults to NULL.

File

qpcache/qpcache.module, line 161
The main file for qpcache.

Code

function qpcache_qp($key, $ttl = '+2 weeks', $query = NULL, $options = array()) {
  $data = QPCache::get($key);
  if (empty($data)) {
    $qp = qp($key, $query, $options);
    QPCache::set($key, $qp
      ->xml(), strtotime($ttl));
  }
  else {
    try {
      $xml = $data->xml;
      $dom = new DOMDocument();
      $dom
        ->loadXML($xml);

      // TODO: Find out why QP will not load a DB file directly.
      $qp = qp($dom);
    } catch (Exception $e) {
      drupal_set_message('Error: ' . check_plain($e
        ->getMessage()), 'status');
    }
  }
  return $qp;
}