Ejemplo n.º 1
0
        /// <summary>
        /// Performs a search in the WeBlog search index, with a sort
        /// </summary>
        /// <typeparam name="T">The type of the items to be returned from the search</typeparam>
        /// <param name="query">The query to execute</param>
        /// <param name="maximumResults">The maximum number of results</param>
        /// <param name="sortField">The index field to sort on</param>
        /// <returns>An array of search results, or an empty array if there was an issue</returns>
        public T[] Execute <T>(QueryBase query, int maximumResults, Action <List <T>, Item> func, string sortField, bool reverseSort)
        {
            if (query is CombinedQuery)
            {
                // Add on database
                (query as CombinedQuery).Add(new FieldQuery(Sitecore.Search.BuiltinFields.Database, Sitecore.Context.Database.Name), QueryOccurance.Must);

                // Add language
                var langCode = DatabaseCrawler.TransformLanguageCode(Sitecore.Context.Language.Name);
                (query as CombinedQuery).Add(new FieldQuery(Constants.Index.Fields.Language, langCode), QueryOccurance.Must);
            }

            // I have to use Action<T> cause the compiler can't work out how to use implicit operators when T is one of the items classes (generated by CIG)
            var items = new List <T>();

            if (maximumResults > 0)
            {
                var index = GetSearchIndex();
                if (index != null && index.GetDocumentCount() > 0)
                {
                    using (var searchContext = new SortableIndexSearchContext(index))
                    {
                        SearchHits hits;
                        if (!string.IsNullOrEmpty(sortField))
                        {
#if FEATURE_CONTENT_SEARCH
                            var sort = new Lucene.Net.Search.Sort(new SortField(sortField, SortField.STRING, reverseSort));
#else
                            var sort = new Lucene.Net.Search.Sort(sortField, reverseSort);
#endif
                            hits = searchContext.Search(query, sort);
                        }
                        else
                        {
                            hits = searchContext.Search(query);
                        }

                        if (hits != null)
                        {
                            foreach (var result in hits.FetchResults(0, maximumResults))
                            {
                                var item = SearchManager.GetObject(result);
                                if (item != null)
                                {
                                    func(items, (Item)item);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Log.Warn("WeBlog index was not found or didn't contain any documents", this);
                }
            }

            return(items.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets blog entries for the given blog up to the maximum number given
        /// </summary>
        /// <param name="blog">The blog item to get the entries for</param>
        /// <param name="maxNumber">The maximum number of entries to retrieve</param>
        /// <param name="tag">A tag the entry must contain</param>
        /// <param name="category">A category the entry must contain</param>
        /// <returns></returns>
        public EntryItem[] GetBlogEntries(Item blog, int maxNumber, string tag, string category, string datePrefix = null)
        {
            if (blog == null || maxNumber <= 0)
            {
                return(new EntryItem[0]);
            }

            BlogHomeItem customBlogItem = null;

            if (blog.TemplateIsOrBasedOn(Settings.BlogTemplateID))
            {
                customBlogItem = blog;
            }
            else
            {
                customBlogItem = blog.GetCurrentItem(Settings.BlogTemplateIDString);
            }
            if (customBlogItem == null)
            {
                return(new EntryItem[0]);
            }

            var query = new CombinedQuery();

            //query.Add(new FieldQuery(Constants.Index.Fields.BlogID, blog.ID.ToShortID().ToString().ToLower()), QueryOccurance.Must);
            query.Add(new FieldQuery(Sitecore.Search.BuiltinFields.Path, customBlogItem.ID.ToShortID().ToString()), QueryOccurance.Must);
            query.Add(new FieldQuery(Constants.Index.Fields.Template, customBlogItem.BlogSettings.EntryTemplateID.ToShortID().ToString().ToLower()), QueryOccurance.Must);

            if (!string.IsNullOrEmpty(tag))
            {
                query.Add(new FieldQuery(Constants.Index.Fields.Tags, DatabaseCrawler.TransformCSV(tag)), QueryOccurance.Must);
            }

            if (!string.IsNullOrEmpty(category))
            {
                var categoryItem = ManagerFactory.CategoryManagerInstance.GetCategory(blog, category);
                ID  id           = ID.Null;

                if (categoryItem != null)
                {
                    id = categoryItem.ID;
                }

                query.Add(new FieldQuery(Constants.Index.Fields.Category, id.ToShortID().ToString().ToLower()), QueryOccurance.Must);
            }

            if (!string.IsNullOrEmpty(datePrefix))
            {
                query.Add(new FieldQuery(Constants.Index.Fields.Created, datePrefix + "*"), QueryOccurance.Must);
            }

            var searcher = new Searcher();
            var result   = searcher.Execute <EntryItem>(query, maxNumber, (list, item) => list.Add((EntryItem)item), Constants.Index.Fields.EntryDate, true);

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets blog entries for the given blog up to the maximum number given
        /// </summary>
        /// <param name="blog">The blog item to get the entries for</param>
        /// <param name="maxNumber">The maximum number of entries to retrieve</param>
        /// <param name="tag">A tag the entry must contain</param>
        /// <param name="category">A category the entry must contain</param>
        /// <returns></returns>
        public EntryItem[] GetBlogEntries(Item blog, int maxNumber, string tag, string category, string datePrefix = null)
        {
            if (blog == null || maxNumber <= 0)
            {
                return(new EntryItem[0]);
            }

            BlogHomeItem customBlogItem = null;

            if (blog.TemplateIsOrBasedOn(Settings.BlogTemplateID))
            {
                customBlogItem = blog;
            }
            else
            {
                customBlogItem = blog.GetCurrentItem(Settings.BlogTemplateIDString);
            }
            if (customBlogItem == null)
            {
                return(new EntryItem[0]);
            }


            List <EntryItem> result = new List <EntryItem>();

#if FEATURE_CONTENT_SEARCH
            var indexName = Settings.SearchIndexName;
            if (!string.IsNullOrEmpty(indexName))
            {
                using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
                {
                    var builder = PredicateBuilder.True <EntryResultItem>();
                    var id      = Settings.EntryTemplateID;
                    builder = builder.And(i => i.TemplateId == id);
                    // Tag
                    if (!String.IsNullOrEmpty(tag))
                    {
                        builder = builder.And(PredicateController.BlogTags(tag));
                    }
                    // Categories
                    if (!string.IsNullOrEmpty(category))
                    {
                        builder = builder.And(PredicateController.BlogCategory(category));
                    }
                    var indexresults = context.GetQueryable <EntryResultItem>().Where(builder);
                    if (indexresults.Any())
                    {
                        result = indexresults.Select(i => new EntryItem(i.GetItem())).ToList();
                        result = result.OrderByDescending(post => post.Created).Take(maxNumber).ToList();
                    }
                }
            }
#else
            var query = new CombinedQuery();
            //query.Add(new FieldQuery(Constants.Index.Fields.BlogID, blog.ID.ToShortID().ToString().ToLower()), QueryOccurance.Must);
            query.Add(new FieldQuery(Sitecore.Search.BuiltinFields.Path, customBlogItem.ID.ToShortID().ToString()), QueryOccurance.Must);
            query.Add(new FieldQuery(Constants.Index.Fields.Template, customBlogItem.BlogSettings.EntryTemplateID.ToShortID().ToString().ToLower()), QueryOccurance.Must);

            if (!string.IsNullOrEmpty(tag))
            {
                query.Add(new FieldQuery(Constants.Index.Fields.Tags, DatabaseCrawler.TransformCSV(tag)), QueryOccurance.Must);
            }

            if (!string.IsNullOrEmpty(category))
            {
                var categoryItem = ManagerFactory.CategoryManagerInstance.GetCategory(blog, category);
                ID  id           = ID.Null;

                if (categoryItem != null)
                {
                    id = categoryItem.ID;
                }

                query.Add(new FieldQuery(Constants.Index.Fields.Category, id.ToShortID().ToString().ToLower()), QueryOccurance.Must);
            }

            if (!string.IsNullOrEmpty(datePrefix))
            {
                query.Add(new FieldQuery(Constants.Index.Fields.Created, datePrefix + "*"), QueryOccurance.Must);
            }

            var searcher = new Searcher();
            result = searcher.Execute <EntryItem>(query, maxNumber, (list, item) => list.Add((EntryItem)item), Constants.Index.Fields.EntryDate, true).ToList();
#endif
            return(result.ToArray());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets blog entries for the given blog up to the maximum number given
        /// </summary>
        /// <param name="blog">The blog item to get the entries for</param>
        /// <param name="maxNumber">The maximum number of entries to retrieve</param>
        /// <param name="tag">A tag the entry must contain</param>
        /// <param name="category">A category the entry must contain</param>
        /// <param name="minimumDate">The minimum date for entries</param>
        /// <param name="maximumDate">The maximum date for the entries</param>
        /// <returns></returns>
        public EntryItem[] GetBlogEntries(Item blog, int maxNumber, string tag, string category, DateTime?minimumDate = null, DateTime?maximumDate = null)
        {
            if (blog == null || maxNumber <= 0)
            {
                return(new EntryItem[0]);
            }

            BlogHomeItem customBlogItem = null;

            if (blog.TemplateIsOrBasedOn(Settings.BlogTemplateID))
            {
                customBlogItem = blog;
            }
            else
            {
                customBlogItem = blog.GetCurrentItem(Settings.BlogTemplateIDString);
            }
            if (customBlogItem == null)
            {
                return(new EntryItem[0]);
            }


            List <EntryItem> result = new List <EntryItem>();

#if FEATURE_CONTENT_SEARCH
            var indexName = Settings.SearchIndexName;
            if (!string.IsNullOrEmpty(indexName))
            {
                using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext(SearchSecurityOptions.DisableSecurityCheck))
                {
                    var builder = PredicateBuilder.True <EntryResultItem>();

                    var id = customBlogItem.BlogSettings.EntryTemplateID;
                    builder = builder.And(i => i.TemplateId == id);
                    builder = builder.And(i => i.Paths.Contains(customBlogItem.ID));
                    builder = builder.And(i => i.Language.Equals(customBlogItem.InnerItem.Language.Name, StringComparison.InvariantCulture));
                    builder = builder.And(item => item.DatabaseName.Equals(Context.Database.Name, StringComparison.InvariantCulture));

                    // Tag
                    if (!string.IsNullOrEmpty(tag))
                    {
                        builder = builder.And(i => i.Tags.Contains(tag));
                    }

                    // Categories
                    if (!string.IsNullOrEmpty(category))
                    {
                        var categoryItem = ManagerFactory.CategoryManagerInstance.GetCategory(customBlogItem, category);

                        // If the category is unknown, don't return any results.
                        if (categoryItem == null)
                        {
                            return(new EntryItem[0]);
                        }
#if SC70
                        var normalizedID = Sitecore.ContentSearch.Utilities.IdHelper.NormalizeGuid(categoryItem.ID);
                        builder = builder.And(i => i.Category.Contains(normalizedID));
#else
                        builder = builder.And(i => i.Category.Contains(categoryItem.ID));
#endif
                    }

                    if (minimumDate != null)
                    {
                        builder = builder.And(i => i.CreatedDate >= minimumDate);
                    }

                    if (maximumDate != null)
                    {
                        builder = builder.And(i => i.CreatedDate < maximumDate);
                    }

                    var indexresults = context.GetQueryable <EntryResultItem>().Where(builder);

                    if (indexresults.Any())
                    {
                        result = indexresults.Select(i => new EntryItem(i.GetItem())).ToList();
                        result = result.OrderByDescending(post => post.EntryDate.DateTime).Take(maxNumber).ToList();
                    }
                }
            }
#else
            var query = new CombinedQuery();
            //query.Add(new FieldQuery(Constants.Index.Fields.BlogID, blog.ID.ToShortID().ToString().ToLower()), QueryOccurance.Must);
            query.Add(new FieldQuery(Sitecore.Search.BuiltinFields.Path, customBlogItem.ID.ToShortID().ToString()), QueryOccurance.Must);
            query.Add(new FieldQuery(Constants.Index.Fields.Template, customBlogItem.BlogSettings.EntryTemplateID.ToShortID().ToString().ToLower()), QueryOccurance.Must);

            if (!string.IsNullOrEmpty(tag))
            {
                query.Add(new FieldQuery(Constants.Index.Fields.Tags, DatabaseCrawler.TransformCSV(tag)), QueryOccurance.Must);
            }

            if (!string.IsNullOrEmpty(category))
            {
                var categoryItem = ManagerFactory.CategoryManagerInstance.GetCategory(blog, category);
                ID  id           = ID.Null;

                // If the category is unknown, don't return any results.
                if (categoryItem == null)
                {
                    return(new EntryItem[0]);
                }

                id = categoryItem.ID;

                query.Add(new FieldQuery(Constants.Index.Fields.Category, id.ToShortID().ToString().ToLower()), QueryOccurance.Must);
            }

            if (minimumDate != null)
            {
                query.Add(new FieldQuery(Constants.Index.Fields.Created, minimumDate.Value.Year.ToString() + minimumDate.Value.Month.ToString() + "*"), QueryOccurance.Must);
            }

            var searcher = new Searcher();
            result = searcher.Execute <EntryItem>(query, blog.Language, maxNumber, (list, item) => list.Add((EntryItem)item), Constants.Index.Fields.EntryDate, false).ToList();
#endif
            return(result.ToArray());
        }
 public static bool IsMatch(DatabaseCrawler crawler, Item item)
 {
     IEnumerable<MethodInfo> methods = crawler.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
     MethodInfo isMatchMethod = methods.First(x => x.Name == "IsMatch");
     return (bool)isMatchMethod.Invoke(crawler, new object[] { item });
 }
        private void Update(DatabaseCrawler crawler, Item current)
        {
            _foundItems[current.Uri] = true;

            // check to see if the current item is a match
            crawler.UpdateItem(current);
        }
        private void Refresh(DatabaseCrawler crawler, Item current)
        {
            if (ReflectionUtil.IsMatch(crawler, current))
            {
                Update(crawler, current);
            }

            // iterate all children and call this same method with each child
            foreach (Item child in current.Children)
            {
                Refresh(crawler, child);
            }
        }