You are here

function revisioning_get_revisions in Revisioning 7

Same name and namespace in other branches
  1. 8 revisioning_api.inc \revisioning_get_revisions()

Get list of revisions accessible to the logged-in user via the operation.

@todo This code may need to be reviewed if used for purposes other than the Pending Revisions block.

Parameters

string $op: Revision operation, eg 'view revision list' (as used by Pending Revisions block)

int $is_published: (optional) 1 to return only published content 0 to return only content that isn't published -1 (default) no filter, return content regardles of publication status

int $creator_uid: (optional) Only return content created by the user with the supplied id. Defaults to -1, which means don't care who the creator is.

int $modifier_uid: (optional) Only return content last modified by user with the supplied id. Defaults to -1, which means don't care who last modifed the node.

bool|int $is_moderated: (optional) TRUE to return only content of types subject to moderation FALSE to return only content that isn't subject to moderation -1 (default) no filter, return content regardles of moderation flag

bool $is_pending: (optional) bool indicating whether only nodes pending publication should be returned; a pending node is defined as a node that has a revision newer than the current OR a node with a single revision that is not published.

int $max: (optional) Maximum number of nodes to be returned, defaults to 1000

string $order_by_override: (optional) "ORDER BY ..." clause to be added, defaults to "timestamp DESC".

Return value

array An array of revision objects each containing nid, content type, published flag, creator-id, title+vid+modifier-id+timestamp of the current revision, plus tags and taxonomy terms.

1 call to revisioning_get_revisions()
revisioning_block_view in ./revisioning.pages.inc
Implements hook_block_view().

File

./revisioning_api.inc, line 799
API functions of Revisioning module

Code

function revisioning_get_revisions($op, $is_published = -1, $creator_uid = -1, $modifier_uid = -1, $is_moderated = -1, $is_pending = FALSE, $max = 1000, $order_by_override = NULL) {
  $sql_select = 'SELECT n.nid, r.vid, n.uid AS creator_uid, r.uid, n.type, n.status, r.title, r.timestamp';

  // Join on current revision (vid) except when looking for pending revisions.
  $sql_from = ' FROM {node} n INNER JOIN {node_revision} r ' . ($is_pending ? 'ON n.nid=r.nid' : 'ON n.vid=r.vid');
  $sql_where = $is_published < 0 ? '' : " WHERE n.status={$is_published}";
  if ($creator_uid >= 0) {
    $sql_where = empty($sql_where) ? " WHERE n.uid={$creator_uid}" : $sql_where . " AND n.uid={$creator_uid}";
  }
  if ($modifier_uid >= 0) {
    $sql_where = empty($sql_where) ? " WHERE r.uid={$modifier_uid}" : $sql_where . " AND r.uid={$modifier_uid}";
  }
  if ($is_pending) {
    $sql_where = empty($sql_where) ? ' WHERE' : $sql_where . ' AND';
    $sql_where .= ' (r.vid>n.vid OR (n.status=0 AND (SELECT COUNT(vid) FROM {node_revision} WHERE nid=n.nid)=1))';
  }
  $sql_order = " ORDER BY " . (empty($order_by_override) ? _revisioning_extract_order_clause_from_URI() : $order_by_override);
  $include_taxonomy_terms = module_exists('taxonomy') && variable_get('revisioning_show_taxonomy_terms', TRUE) && count(taxonomy_get_vocabularies()) > 0;
  if ($include_taxonomy_terms) {
    $conditions = array(
      'type' => 'taxonomy_term_reference',
    );
    $fields = field_read_fields($conditions);
    foreach ($fields as $field => $data) {
      $sql_select .= ", ttd_{$field}.name AS " . ($field == 'field_tags' ? 'tags' : 'term');
      $sql_from .= " LEFT JOIN {field_revision_{$field}} r_{$field} ON r.vid = r_{$field}.revision_id LEFT JOIN {taxonomy_term_data} ttd_{$field} ON r_{$field}.{$field}_tid=ttd_{$field}.tid";
    }
  }
  $sql = $sql_select . $sql_from . $sql_where . $sql_order;
  $node_query_result = db_query_range($sql, 0, $max);
  $revisions = array();
  foreach ($node_query_result as $revision) {

    // Need to set revision_moderation for revisioning_node_access() to work
    // properly.
    $revision->revision_moderation = revisioning_content_is_moderated($revision->type);
    $filter = $is_moderated < 0 || $is_moderated == $revision->revision_moderation;
    if ($filter && _revisioning_access_node_revision($op, $revision)) {
      if (empty($revisions[$revision->vid])) {
        $revisions[$revision->vid] = $revision;
      }
      elseif ($include_taxonomy_terms) {
        $existing_revision = $revisions[$revision->vid];
        if (!empty($revision->term)) {
          if (strpos($existing_revision->term, $revision->term) === FALSE) {

            // Bit of a quick & dirty -- goes wrong if a term is substr of
            // another.
            $existing_revision->term .= ", {$revision->term}";
          }
        }
        if (!empty($revision->tags)) {
          if (strpos($existing_revision->tags, $revision->tags) === FALSE) {
            $existing_revision->tags .= ", {$revision->tags}";
          }
        }
      }
    }
  }
  return $revisions;
}