function r_implode($glue, $pieces) { $retVal = array(); foreach ($pieces as $r_pieces) { if (is_array($r_pieces)) { $retVal[] = r_implode($glue, $r_pieces); } else { $retVal[] = $r_pieces; } } return implode($glue, $retVal); }
/** * Generates an array of related posts * * @param int $limit. (default: 10) * @param array $post_types. (default: 'post') * @param array $taxonomies. (default: 'post_tag') This is not the taxonomies which are used to compare, it is the taxonomies for the post * @param array $terms_not_in. (default: empty array) * @return array - post IDs */ function hm_get_related_posts($limit = 10, $post_types = array('post'), $taxonomies = array('post_tag', 'category'), $terms_not_in = array(), $args = array()) { global $wpdb; $default_args = array('post_id' => get_the_id(), 'terms' => array(), 'related_post_taxonomies' => $taxonomies); $args = wp_parse_args($args, $default_args); extract($args); foreach ($related_post_taxonomies as &$related_post_taxonomy) { $related_post_taxonomy = "'" . $related_post_taxonomy . "'"; } if (empty($post_id)) { return; } $func_args = func_get_args(); if (!($related_posts = wp_cache_get($post_id . '_' . r_implode('_', $func_args), 'hm_related_posts'))) { if (empty($terms)) { $term_objects = wp_get_object_terms($post_id, $taxonomies); } else { $term_objects = $terms; } $terms = array(); foreach ($term_objects as $term) { $terms[] = $term->term_id; } $terms = array_unique(array_diff($terms, $terms_not_in)); sort($terms); $post_type_sql = ''; foreach ($post_types as $post_type) { $post_type_sql .= " OR p.post_type = '{$post_type}'"; } $post_type_sql = substr($post_type_sql, 4); $query[] = "SELECT p.ID"; $query[] = "FROM {$wpdb->term_taxonomy} t_t, {$wpdb->term_relationships} t_r, {$wpdb->posts} p"; $query[] = "WHERE t_t.term_taxonomy_id = t_r.term_taxonomy_id"; $query[] = "AND t_r.object_id = p.ID"; if (!empty($terms)) { $query[] = "AND t_t.term_id IN ( " . implode(', ', $terms) . " ) AND t_t.taxonomy IN ( " . implode(', ', $related_post_taxonomies) . " )"; } if (!empty($terms_not_in)) { $query[] = "AND p.ID NOT IN ( SELECT tr.object_id FROM wp_term_relationships AS tr INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.term_id IN (" . implode(', ', $terms_not_in) . ") )"; } $query[] = "AND ( {$post_type_sql} )"; $query[] = "AND p.ID != {$post_id}"; $query[] = "AND p.post_status = 'publish'"; $query[] = "GROUP BY t_r.object_id"; $query[] = "ORDER BY count(t_r.object_id) DESC, p.post_date_gmt DESC LIMIT 0, {$limit}"; $query = implode(' ', $query); $related_posts = $wpdb->get_col($query); wp_cache_add($post_id . '_' . r_implode('_', $func_args), $related_posts, 'hm_related_posts'); } return $related_posts; }
public function service($name) { $start = AetherTimer::getMicroTime(); $memcache = new Memcache(); $memcache->connect('localhost', 11211); $db = Config::getDb(); $routeSearch = new RouteSearch(); $cacheKey = "stops_"; $filters = array(); if (isset($_GET['term'])) { $query = toLower($_GET['term']); $filters['search'] = new MongoRegex("/^{$query}/i"); $cacheKey .= $query; } elseif (isset($_GET['lat']) && isset($_GET['long'])) { $lat = $_GET['lat']; $long = $_GET['long']; $regex = '/^[0-9]+\\.[0-9]*$/'; $cacheKey .= $lat . "," . $long; if (preg_match($regex, $lat) && preg_match($regex, $long)) { $filters['location'] = array('$near' => array((double) $lat, (double) $long)); } } if (isset($_GET['from']) && !empty($_GET['from'])) { $from = toLower($_GET['from']); $filters['connectsFrom'] = $from; } $cacheKey .= r_implode(":", $filters); $result = $memcache->get($cacheKey); if ($result) { $result = unserialize($result); } else { // Find stops near me!! $db->routes->ensureIndex(array('search' => 1)); $filters['active'] = true; $stops = $db->stops->find($filters); $result = array('stops' => array()); while ($stop = $stops->getNext()) { $result['stops'][] = array('name' => $stop['name']); } $result['generated'] = time(); $result['length'] = count($result['stops']); $memcache->set($cacheKey, serialize($result), false, 7200); } $time = AetherTimer::getMicroTime() - $start; $result['time'] = $time; if (isset($_GET['callback'])) { return new AetherJSONPResponse($result, $_GET['callback']); } else { return new AetherJSONResponse($result); } }
public function display($file) { global $THEME; $this->file = $file; $this->template_dir = $THEME->getTemplatePath(); if ($this->cache && $GLOBALS['CONF']['debug'] == 0) { $this->cachefile = $this->cachedir . md5(filemtime($this->template_dir . $this->file) . r_implode('', $this->vars)) . '.tpl.php'; if (file_exists($this->cachefile)) { echo file_get_contents($this->cachefile); } else { echo $this->render(); } } else { echo $this->render(); } }
function r_implode($glue, $pieces) { $retVal = array(); foreach ($pieces as $r_pieces) { $retVal[] = is_array($r_pieces) ? r_implode($glue, $r_pieces) : $r_pieces; } return implode($glue, $retVal); }
/** * r_implode function. a recursive version of implode * * @access public * @param string $glue * @param array $pieces * @return string */ function r_implode($glue, $pieces) { foreach ($pieces as $piece) { if (is_array($piece)) { $return[] = r_implode($glue, $piece); } else { $return[] = $piece; } } return implode($glue, $return); }
function viaPointNames($tripid, $stop_sequence = "") { $viaPointNames = array(); foreach (viaPoints($tripid, $stop_sequence) as $point) { if (strstr($point['stop_name'], "Station") || strstr($point['stop_name'], "Shops") || strstr($point['stop_name'], "CIT") || strstr($point['stop_name'], "School") || strstr($point['stop_name'], "University")) { $viaPointNames[] = $point['stop_name']; } } if (sizeof($viaPointNames) > 0) { return r_implode(", ", $viaPointNames); } else { return ""; } }
function generateFrom($from, &$conditions, $join_type = 'LEFT', $existing_joins = null) { # $joins will contain a list of all the necessary joins which will be combined at the end to generate the FROM $joins = array(); // setup a default $from if (!$from) { $from = "FROM " . $this->table_name . " " . $this->table_shortname; } $original_conditions = $conditions; foreach ($original_conditions as $field => $value) { // conditions come in the format // table_alias.field:operator $linked_table = null; $operator = null; $computed_field = null; if (preg_match("/^(.*?)\\,(.*)/", $field, $matches)) { $field = $matches[1]; $computed_field = $matches[2]; if (preg_match("/(.*?)\\:(.*)/", $computed_field, $matches)) { $computed_field = $matches[1]; $field .= ":" . $matches[2]; } } // split table alias from fieldname if (preg_match("/^(.*?)\\.(.*)/", $field, $matches)) { $linked_table = $matches[1]; $field = $matches[2]; $operator = null; if (preg_match("/(.*?)\\:(.*)/", $field, $matches)) { $field = $matches[1]; $operator = ":" . $matches[2]; } } else { if (preg_match("/^(.*?):(.*)/", $field, $matches)) { $linked_table = $this->table_shortname; $field = $matches[1]; $operator = ":" . $matches[2]; } else { // no operator $linked_table = $this->table_shortname; } } if (!$computed_field) { $computed_field = $field; } // now we know what field we want // what table the field is in // and what operator we're going to call on it. //echo "Handling $field from $linked_table<br />"; // handle sub-clauses if (preg_match("/^or(\\d+)?/i", $field)) { // handled later } else { if (preg_match("/^and(\\d+)?/i", $field)) { // handled later } else { if (preg_match("/^\\!or(\\d+)?/i", $field)) { // handled later } else { if (preg_match("/^\\!and(\\d+)?/i", $field)) { // handled later } else { if ($linked_table == 'flag') { // link in the flag tables in the proper way // if we are querying users... do something special. if (isset($original_conditions['flag.itemId']) && $this->TYPE == 'user') { if (!isset($joins['flags'])) { if (isset($original_conditions['flag.id']) && $original_conditions['flag.id'] == "null") { $joins['flags'] = "left join flags flag on flag.itemId=" . $this->table_shortname . ".id and flag.type='" . $this->TYPE . "'"; } else { $joins['flags'] = "inner join flags flag on flag.userId=" . $this->table_shortname . ".id"; } if (isset($original_conditions['flag.name'])) { $joins['flags'] .= " and flag.name='" . mysql_real_escape_string($original_conditions['flag.name']) . "'"; unset($conditions['flag.name']); } if (isset($original_conditions['flag.userId'])) { $joins['flags'] .= " and flag.userId='" . mysql_real_escape_string($original_conditions['flag.userId']) . "'"; unset($conditions['flag.userId']); } } } else { // if we are querying another type if (!isset($joins['flags'])) { if (isset($original_conditions['flag.id']) && $original_conditions['flag.id'] == "null") { $joins['flags'] = "left join flags flag on flag.itemId=" . $this->table_shortname . ".id and flag.type='" . $this->TYPE . "'"; } else { $joins['flags'] = "inner join flags flag on flag.itemId=" . $this->table_shortname . ".id and flag.type='" . $this->TYPE . "'"; } if (isset($original_conditions['flag.name'])) { $joins['flags'] .= " and flag.name='" . mysql_real_escape_string($original_conditions['flag.name']) . "'"; unset($conditions['flag.name']); } if (isset($original_conditions['flag.userId'])) { $joins['flags'] .= " and flag.userId='" . mysql_real_escape_string($original_conditions['flag.userId']) . "'"; unset($conditions['flag.userId']); } } } } else { if ($linked_table != $this->table_shortname) { // handle fields in remote tables // find the join statement for this table. if ($this->JOINS[$linked_table]) { $obj = $this->referenceObject($linked_table); if (!$obj || $obj->isRealField($field)) { $joins[$linked_table] = $this->JOINS[$linked_table]; } else { $joins[$linked_table] = $this->JOINS[$linked_table]; if (!$existing_joins['meta_' . $linked_table]["{$linked_table}_m_{$field}"]) { $joins['meta_' . $linked_table]["{$linked_table}_m_{$field}"] = "{$join_type} JOIN meta {$linked_table}_m_{$field} on {$linked_table}_m_{$field}.itemId={$linked_table}.id and {$linked_table}_m_{$field}.type='" . $obj->TYPE . "' and {$linked_table}_m_{$field}.name='{$field}'"; } unset($conditions["{$linked_table}.{$field}{$operator}"]); if ($value != 'null') { $conditions["{$linked_table}_m_{$field}.value{$operator}"] = $value; } else { $conditions["{$linked_table}_m_{$field}.id{$operator}"] = 'null'; } } } else { $this->throwError("Not sure how to find the field {$field} from {$linked_table} when joining with {$this->table_shortname}"); } } else { // handle fields in the local table // skip fields where we just passed in a 1 if (preg_match("/^\\d+/", $field)) { continue; } if (!$this->isRealField($field)) { if (!$existing_joins['meta_' . $linked_table]["{$linked_table}_m_{$field}"]) { $joins['meta_' . $linked_table]["{$linked_table}_m_{$field}"] = "{$join_type} JOIN meta {$linked_table}_m_{$field} on {$linked_table}_m_{$field}.itemId={$linked_table}.id and {$linked_table}_m_{$field}.type='" . $this->TYPE . "' and {$linked_table}_m_{$field}.name='{$field}'"; } if ($computed_field != $field) { unset($conditions["{$field},{$computed_field}{$operator}"]); if ($value != 'null') { $conditions["{$linked_table}_m_{$field}.value,{$computed_field}{$operator}"] = $value; } else { $conditions["{$linked_table}_m_{$field}.id,{$computed_field}{$operator}"] = 'null'; } } else { unset($conditions["{$field}{$operator}"]); if ($value != 'null') { $conditions["{$linked_table}_m_{$field}.value{$operator}"] = $value; } else { $conditions["{$linked_table}_m_{$field}.id{$operator}"] = 'null'; } } } else { if ($computed_field != $field) { // add table name to unlabeled fields so field names don't bash into one another unset($conditions["{$field},{$computed_field}{$operator}"]); $conditions["{$linked_table}.{$field},{$computed_field}{$operator}"] = $value; } else { // add table name to unlabeled fields so field names don't bash into one another unset($conditions["{$field}{$operator}"]); $conditions["{$linked_table}.{$field}{$operator}"] = $value; } } } } } } } } } // end foreach condition // do this all again, but ONLY for the or and and clauses foreach ($original_conditions as $field => $value) { // conditions come in the format // table_alias.field:operator $linked_table = null; $operator = null; // split table alias from fieldname if (preg_match("/^(.*?)\\.(.*)/", $field, $matches)) { $linked_table = $matches[1]; $field = $matches[2]; $operator = null; if (preg_match("/(.*?)\\:(.*)/", $field, $matches)) { $field = $matches[1]; $operator = ":" . $matches[2]; } } else { if (preg_match("/^(.*?):(.*)/", $field, $matches)) { $linked_table = $this->table_shortname; $field = $matches[1]; $operator = ":" . $matches[2]; } else { // no operator $linked_table = $this->table_shortname; } } // now we know what field we want // what table the field is in // and what operator we're going to call on it. //echo "Handling $field from $linked_table<br />"; // handle sub-clauses if (preg_match("/^or(\\d+)?/i", $field)) { // generate joins based on this OR //echo "Processing an OR<br />"; $this->generateFrom($from, $value, 'LEFT', $joins); $conditions[$field] = $value; } else { if (preg_match("/^and(\\d+)?/i", $field)) { // generate joins based on this AND //echo "Processing an AND<br />"; $this->generateFrom($from, $value, 'LEFT', $joins); $conditions[$field] = $value; } else { if (preg_match("/^\\!or(\\d+)?/i", $field)) { // generate joins based on this OR //echo "Processing an OR<br />"; $this->generateFrom($from, $value, 'LEFT', $joins); $conditions[$field] = $value; } else { if (preg_match("/^\\!and(\\d+)?/i", $field)) { // generate joins based on this AND //echo "Processing an AND<br />"; $this->generateFrom($from, $value, 'LEFT', $joins); $conditions[$field] = $value; } } } } } if ($this->TYPE == "user") { if (isset($joins['g'])) { $joins['mem'] = null; } } if ($this->TYPE == "tags") { if (isset($joins['d'])) { $joins['tr'] = null; } if (isset($joins['g'])) { $joins['tr'] = null; } if (isset($joins['f'])) { $joins['tr'] = null; } if (isset($joins['c'])) { $joins['tr'] = null; } if (isset($joins['u'])) { $joins['tr'] = null; } } $from = $from . " " . r_implode(" ", $joins); // update conditions array return $from; }