Example #1
0
        public IList<ulong> Pages_GetParentIds(PageBE page) {

            //MaxM: This query returns 10 segments of a hierarchy without using a loop or titles and can be query cached. Please don't submit to the dailywtf. :)
            string query = @" /* Pages_GetParentIds */
select p0.page_id, p0.page_parent, p1.page_parent, p2.page_parent, p3.page_parent, p4.page_parent, p5.page_parent, p6.page_parent, p7.page_parent, p8.page_parent, p9.page_parent
from pages p0
left join pages p1 on p0.page_parent = p1.page_id
left join pages p2 on p1.page_parent = p2.page_id
left join pages p3 on p2.page_parent = p3.page_id
left join pages p4 on p3.page_parent = p4.page_id
left join pages p5 on p4.page_parent = p5.page_id
left join pages p6 on p5.page_parent = p6.page_id
left join pages p7 on p6.page_parent = p7.page_id
left join pages p8 on p7.page_parent = p8.page_id
left join pages p9 on p8.page_parent = p9.page_id
where p0.page_id = ?PAGEID;";

            ulong homepageid = Head.Pages_HomePageId;
            int counter = 0;
            ulong id = page.ID;
            bool first = true;
            Dictionary<ulong, object> dupeChecker = new Dictionary<ulong, object>();
            List<ulong> result = new List<ulong>(16);

            // loop until we've found all the parents
            while(counter++ < 20) {

                // execute query for given page
                bool done = false;
                Catalog.NewQuery(query)
                    .With("PAGEID", id)
                    .Execute(delegate(IDataReader dr) {
                    if(dr.Read()) {
                        for(int i = first ? 0 : 1; i < dr.FieldCount; i++) {
                            ulong parentPageId = DbUtils.Convert.To<ulong>(dr.GetValue(i)) ?? 0;
                            if(parentPageId == 0) {

                                // no valid parent found
                                if(page.Title.Namespace == NS.MAIN) {

                                    // add the homepage for pages in the MAIN namespace
                                    result.Add(homepageid);
                                }
                                done = true;
                                break;
                            } else {
                                if(!dupeChecker.ContainsKey(parentPageId)) {
                                    result.Add(parentPageId);
                                    dupeChecker[parentPageId] = null;
                                } else {

                                    //Cycle detected
                                    done = true;
                                }
                            }
                        }
                    } else {
                        done = true;
                    }
                });

                // check if done
                if(done) {
                    return result;
                }

                // continue with last parent id
                id = result[result.Count - 1];
                first = false;
            }
            throw new Exception(string.Format("GetParentPages failed: too many iterations. Starting id: {0} Parent chain: {1}", page.ID, result.ToCommaDelimitedString()));
        }
Example #2
0
 private void WriteLinks(int page, List<string> tags, List<string> searchTerms, string sortBy)
 {
     if (sortBy == null)
         sortBy = "DATE";
     this.CommandResult.ClearScreen = true;
     this.CommandResult.ScrollToBottom = true;
     var linksPage = _linkRepository.GetLinks(page, AppSettings.LinksPerPage, tags, searchTerms, sortBy);
     if (page > linksPage.TotalPages)
         page = linksPage.TotalPages;
     else if (page < 1)
         page = 1;
     this.CommandResult.WriteLine(DisplayMode.Inverted | DisplayMode.Bold, "Welcome to the links board!");
     this.CommandResult.WriteLine();
     this.CommandResult.WriteLine(DisplayMode.Bold, "Active Filters");
     this.CommandResult.WriteLine("Tags: {0}", tags.ToCommaDelimitedString());
     this.CommandResult.WriteLine("Search Terms: {0}", searchTerms.ToCommaDelimitedString());
     this.CommandResult.WriteLine("Sorted By: {0}", sortBy.ToUpper());
     this.CommandResult.WriteLine();
     this.CommandResult.WriteLine(DisplayMode.DontType, "Page {0}/{1}", page, linksPage.TotalPages);
     this.CommandResult.WriteLine();
     this.CommandResult.WriteLine(DisplayMode.Dim | DisplayMode.DontType, new string('-', AppSettings.DividerLength));
     foreach (var link in linksPage.Items)
     {
         var displayMode = DisplayMode.DontType;
         var lastLinkComment = link.LinkComments.LastOrDefault();
         this.CommandResult.WriteLine(displayMode | DisplayMode.Bold, "{{[transmit=LINK]{0}[/transmit]}} {1}", link.LinkID, link.Title);
         this.CommandResult.WriteLine(displayMode, "   by [transmit=USER]{0}[/transmit] {1} | {2} comments", link.Username, link.Date.TimePassed(), link.LinkComments.Count);
         if (lastLinkComment != null)
             this.CommandResult.WriteLine(displayMode, "   last comment by [transmit=USER]{0}[/transmit] {1}", lastLinkComment.Username, lastLinkComment.Date.TimePassed());
         this.CommandResult.WriteLine(DisplayMode.Dim | DisplayMode.DontType, new string('-', AppSettings.DividerLength));
     }
     if (linksPage.TotalItems == 0)
     {
         this.CommandResult.WriteLine("There are no links on the links board at this time.");
         this.CommandResult.WriteLine();
         this.CommandResult.WriteLine(DisplayMode.Dim | DisplayMode.DontType, new string('-', AppSettings.DividerLength));
         this.CommandResult.WriteLine();
     }
     this.CommandResult.WriteLine();
     this.CommandResult.WriteLine(DisplayMode.DontType, "Page {0}/{1}", page, linksPage.TotalPages);
     this.CommandResult.CommandContext.CurrentPage = page;
     this.CommandResult.CommandContext.CurrentLinkTags = tags;
     this.CommandResult.CommandContext.CurrentSearchTerms = searchTerms;
     this.CommandResult.CommandContext.CurrentSortOrder = sortBy;
     this.CommandResult.CommandContext.Set(ContextStatus.Passive, this.Name, null, null);
 }