/** * Fetches streams from the database. * @method fetch * @static * @param {string} $asUserId * Set this to the user for which you are fetching the streams. * If this matches the publisherId, just returns the streams. * If this is '', only returns the streams anybody can see. * Otherwise, return the streams joined with the calculated access settings. * If you pass null here, then either the logged-in user's id or '' will be used. * @param {string} $publisherId * The id of the user publishing these streams * @param {string|array|Db_Range} $name * The name of the stream to fetch. Can end in "/" for template streams. * Also it can be an array of stream names, or a custom Db_Range for stream names * @param {string} [$fields='*'] * Comma delimited list of fields to retrieve in the stream. * Must include at least "publisherId" and "name". * since make up the primary key of the stream table. * @param {array} [$options=array()] * Provide additional query options like 'limit', 'offset', 'orderBy', 'where' etc. * See Db_Query_Mysql::options(). * @param {boolean} [$options.refetch] Ignore cache of previous calls to fetch, * and save a new cache if necessary. * @param {boolean} [$options.dontCache] Do not cache the results of * fetching the streams * @return {array} * Returns an array of Streams_Stream objects with access info calculated * specifically for $asUserId . Make sure to call the methods * testReadLevel(), testWriteLevel() and testAdminLevel() * on these streams before using them on the user's behalf. */ static function fetch($asUserId, $publisherId, $name, $fields = '*', $options = array()) { if (!isset($asUserId)) { $asUserId = Users::loggedInUser(); if (!$asUserId) { $asUserId = ""; } } if ($asUserId instanceof Users_User) { $asUserId = $asUserId->id; } if ($publisherId instanceof Users_User) { $publisherId = $publisherId->id; } if (empty($publisherId) or empty($name)) { return array(); } if (is_array($fields)) { $options = $fields; $fields = '*'; } $allCached = array(); if (empty($options['refetch'])) { $arr = is_array($name) ? $name : array($name); $namesToFetch = array(); foreach ($arr as $n) { if (isset(self::$fetch[$asUserId][$publisherId][$n][$fields])) { $allCached[$n] = self::$fetch[$asUserId][$publisherId][$n][$fields]; } else { $namesToFetch[] = $n; } } if (!is_array($name)) { $namesToFetch = $namesToFetch ? $namesToFetch[0] : null; } } else { $namesToFetch = $name; } $criteria = array('publisherId' => $publisherId, 'name' => $namesToFetch); // Get streams and set their default access info $allRetrieved = $namesToFetch ? Streams_Stream::select($fields)->where($criteria)->ignoreCache()->options($options)->fetchDbRows(null, '', 'name') : array(); $streams = $allCached ? array_merge($allCached, $allRetrieved) : $allRetrieved; Streams::calculateAccess($asUserId, $publisherId, $streams, false); if (is_array($name) and count($name) > 1) { // put the streams back in the same internal PHP array order // and in the process honor any duplicate names that might have been passed $temp = $streams; $streams = array(); foreach ($name as $n) { $streams[$n] = isset($temp[$n]) ? $temp[$n] : null; } } $types = array(); foreach ($streams as $stream) { if ($stream) { $types[$stream->type] = true; } } $types = array_keys($types); self::afterFetchExtended($publisherId, $streams); foreach ($types as $type) { $cached = array(); $retrieved = array(); foreach ($allCached as $n => $s) { if ($s->type === $type) { $cached[$n] = $s; } } foreach ($allRetrieved as $n => $s) { if ($s->type === $type) { $retrieved[$n] = $s; } } $params = array('streams' => &$streams, 'cached' => $cached, 'retrieved' => $retrieved, 'allCached' => $allCached, 'allRetrieved' => $allRetrieved, 'asUserId' => $asUserId, 'publisherId' => $publisherId, 'name' => $name, 'criteria' => $criteria, 'fields' => $fields, 'options' => $options, 'type' => $type); /** * @event Streams/fetch/$streamType {after} * @param {&array} streams * @param {string} asUserId * @param {string} publisherId * @param {string} name * @param {array} criteria * @param {string} fields * @param {array} options */ Q::event("Streams/fetch/{$type}", $params, 'after', false, $streams); } if (!empty($option['dontCache'])) { foreach ($streams as $n => $stream) { self::$fetch[$asUserId][$publisherId][$n][$fields] = $stream; } } return $streams; }
/** * Look up stream by types and title filter * @method lookup * @static * @param {string} $publisherId * The id of the publisher whose streams to look through * @param {string|array} $types * The possible stream type, or an array of types * @param {string} $title * A string to compare titles by using SQL's "LIKE" statement */ static function lookup($publisherId, $types, $title) { $fc = $title[0]; if ($fc === '%' and strlen($title) > 1 and Q_Config::get('Streams', 'lookup', 'requireTitleIndex', true)) { throw new Q_Exception_WrongValue(array('field' => 'title', 'range' => "something that doesn't start with %")); } $limit = Q_Config::get('Streams', 'lookup', 'limit', 10); return Streams_Stream::select('*')->where(array('publisherId' => $publisherId, 'type' => $types, 'title LIKE ' => $title))->limit($limit)->fetchDbRows(); }