/** * Search through a single contenttype * * Search, weigh and return the results. */ private function searchSingleContentType($query, $contenttype, $fields, array $filter = null) { // This could be even more configurable // (see also Content->getFieldWeights) $searchable_types = array('text', 'textarea', 'html', 'markdown'); $table = $this->getTablename($contenttype); // Build fields 'WHERE' $fields_where = array(); foreach ($fields as $field => $fieldconfig) { if (in_array($fieldconfig['type'], $searchable_types)) { foreach ($query['words'] as $word) { $fields_where[] = sprintf('%s.%s LIKE %s', $table, $field, $this->app['db']->quote('%' . $word . '%')); } } } // make taxonomies work $taxonomytable = $this->getTablename('taxonomy'); $taxonomies = $this->getContentTypeTaxonomy($contenttype); $tags_where = array(); $tags_query = ''; foreach ($taxonomies as $taxonomy) { if ($taxonomy['behaves_like'] == 'tags') { foreach ($query['words'] as $word) { $tags_where[] = sprintf('%s.slug LIKE %s', $taxonomytable, $this->app['db']->quote('%' . $word . '%')); } } } // only add taxonomies if they exist if (!empty($taxonomies) && !empty($tags_where)) { $tags_query_1 = sprintf('%s.contenttype = "%s"', $taxonomytable, $contenttype); $tags_query_2 = implode(' OR ', $tags_where); $tags_query = sprintf(' OR (%s AND (%s))', $tags_query_1, $tags_query_2); } // Build filter 'WHERE" // @todo make relations work as well $filter_where = array(); if (!is_null($filter)) { foreach ($fields as $field => $fieldconfig) { if (isset($filter[$field])) { $filter_where[] = $this->parseWhereParameter($table . '.' . $field, $filter[$field]); } } } // Build actual where $where = array(); $where[] = sprintf('%s.status = "published"', $table); $where[] = '(( ' . implode(' OR ', $fields_where) . ' ) ' . $tags_query . ' )'; $where = array_merge($where, $filter_where); // Build SQL query $select = sprintf('SELECT %s.id', $table); $select .= ' FROM ' . $table; $select .= ' LEFT JOIN ' . $taxonomytable; $select .= sprintf(' ON %s.id = %s.content_id', $table, $taxonomytable); $select .= ' WHERE ' . implode(' AND ', $where); // Run Query $results = $this->app['db']->fetchAll($select); if (!empty($results)) { $ids = implode(' || ', \util::array_pluck($results, 'id')); $results = $this->getContent($contenttype, array('id' => $ids, 'returnsingle' => false)); // Convert and weight foreach ($results as $result) { $result->weighSearchResult($query); } } return $results; }
/** * Get the taxonomy for one or more units of content, return the array with the taxonomy attached. * * @param array $content * * @return array $content */ protected function getTaxonomy($content) { $tablename = $this->prefix . "taxonomy"; $ids = util::array_pluck($content, 'id'); if (empty($ids)) { return $content; } // Get the contenttype from first $content $contenttype = $content[util::array_first_key($content)]->contenttype['slug']; $taxonomytypes = array_keys($this->config['taxonomy']); $query = sprintf("SELECT * FROM {$tablename} WHERE content_id IN (%s) AND contenttype=%s AND taxonomytype IN ('%s')", implode(", ", $ids), $this->db->quote($contenttype), implode("', '", $taxonomytypes)); $rows = $this->db->fetchAll($query); foreach ($rows as $key => $row) { $content[$row['content_id']]->setTaxonomy($row['taxonomytype'], $row['slug']); } }
/** * @author Xiao-Hu Tai * @param \Bolt\Content $record The record to search similar content for. * @param array $options Options for custom queries. * * @return array Returns an array with the elements sorted by similarity. */ function relatedContentByTags($record, $options = array()) { $startTime = microtime(true); $app = $this->app; $limit = isset($options['limit']) ? $options['limit'] : $this->config['limit']; $tablePrefix = $app['config']->get('general/database/prefix', 'bolt_'); $taxonomyTable = sprintf('%staxonomy', $tablePrefix); $contenttypes = $app['config']->get('contenttypes'); $filter = isset($options['contenttypes']) ? $options['contenttypes'] : false; // if set, filter contenttypes if ($filter) { //\util::var_dump($filter); $filterContenttypes = array(); foreach ($filter as $contenttypeName) { if (isset($contenttypes[$contenttypeName])) { $filterContenttypes[$contenttypeName] = $contenttypes[$contenttypeName]; } } if ($filterContenttypes) { $contenttypes = $filterContenttypes; } } // Get all taxonomies that behave like tags and their values from $record. $tagsValues = array(); $tagsTaxonomies = array(); // If no taxonomies exist, then no matching items exist if (!isset($record->contenttype['taxonomy'])) { return array(); } foreach ($record->contenttype['taxonomy'] as $key) { if ($app['config']->get('taxonomy/' . $key . '/behaves_like') == 'tags') { // only useful if values exist, otherwise just skip this taxonomy if ($record->taxonomy[$key]) { $tagsValues[$key] = array_values($record->taxonomy[$key]); $tagsTaxonomies[] = $key; } } } // Make the basic WHERE query for all behaves-like-tags values in $record. $queryWhere = array(); foreach ($tagsValues as $tagName => $values) { $subqueryWhere = array(); foreach ($values as $word) { $subqueryWhere[] = sprintf('%s.slug = "%s"', $taxonomyTable, $word); } $temp = sprintf('%s.taxonomytype = "%s"', $taxonomyTable, $tagName); $temp .= sprintf(' AND (%s)', implode(' OR ', $subqueryWhere)); $queryWhere[] = $temp; } $queryWhere = implode(' OR ', $queryWhere); // Get all contenttypes (database tables) that have a similar behaves-like-tags taxonomies like $record $tables = array(); foreach ($contenttypes as $key => $contenttype) { foreach ($contenttype['taxonomy'] as $taxonomyKey) { if (in_array($taxonomyKey, $tagsTaxonomies)) { $tables[] = $contenttype['slug']; break; } } } // Fetch results for every proper contenttype $results = array(); foreach ($tables as $name) { $table = sprintf('%s%s', $tablePrefix, $name); $querySelect = ''; $querySelect .= sprintf('SELECT %s.id FROM %s', $table, $table); $querySelect .= sprintf(' LEFT JOIN %s', $taxonomyTable); $querySelect .= sprintf(' ON %s.id = %s.content_id', $table, $taxonomyTable); $querySelect .= sprintf(' WHERE %s.status = "published"', $table); if ($name == $record->contenttype['slug']) { $querySelect .= sprintf('AND %s.id != ' . $record->id, $table); } $querySelect .= sprintf(' AND %s.contenttype = "%s"', $taxonomyTable, $name); $querySelect .= sprintf(' AND (%s)', $queryWhere); $queryResults = $app['db']->fetchAll($querySelect); //\util::var_dump($querySelect); // print the query //\util::var_dump($queryResults); if (!empty($queryResults)) { $ids = implode(' || ', \util::array_pluck($queryResults, 'id')); $contents = $app['storage']->getContent($name, array('id' => $ids, 'returnsingle' => false)); $results = array_merge($results, $contents); } } // Add similarities by tags and difference in publication dates. foreach ($results as $result) { $similarity = $this->calculateTaxonomySimilarity($record, $result, $tagsTaxonomies, $tagsValues, $options); $diff = $this->calculatePublicationDiff($record, $result); $result->similarity = $similarity; $result->diff = $diff; } // Sort results usort($results, array($this, 'compareSimilarity')); // Limit results $results = array_slice($results, 0, $limit); $totalTime = microtime(true) - $startTime; // \util::var_dump( sprintf('%.03f seconds', $totalTime) ); return $results; }
public function test_array_pluck() { $array = array(array('name' => 'Bob', 'age' => 37), array('name' => 'Fred', 'age' => 37), array('name' => 'Jane', 'age' => 29), array('name' => 'Brandon', 'age' => 20), array('age' => 41)); $obj_array = array('bob' => (object) array('name' => 'Bob', 'age' => 37), 'fred' => (object) array('name' => 'Fred', 'age' => 37), 'jane' => (object) array('name' => 'Jane', 'age' => 29), 'brandon' => (object) array('name' => 'Brandon', 'age' => 20), 'invalid' => (object) array('age' => 41)); $obj_array_expect = array('bob' => 'Bob', 'fred' => 'Fred', 'jane' => 'Jane', 'brandon' => 'Brandon'); $this->assertEquals(array('Bob', 'Fred', 'Jane', 'Brandon'), util::array_pluck($array, 'name')); $this->assertEquals(array('Bob', 'Fred', 'Jane', 'Brandon', array('age' => 41)), util::array_pluck($array, 'name', TRUE, FALSE)); $this->assertEquals($obj_array_expect, util::array_pluck($obj_array, 'name')); $this->assertEquals(array('Bob', 'Fred', 'Jane', 'Brandon'), util::array_pluck($obj_array, 'name', FALSE)); }