コード例 #1
0
        } // RunBackgroundTask

        public bool FetchAndSaveRemoteRSSGlossaryData()
        {
            string dataCacheKey    = GlossaryPlaceholderData.getRssDataPersistentVariableName();
            string lastRunCacheKey = dataCacheKey + "_LastRun";

            string rssUrl = GlossaryPlaceholderData.getRssDataSourceUrl();

            Rss.RssFeed glossaryRss = Rss.RssFeed.Read(rssUrl);
            if (glossaryRss.Channels.Count == 0)
            {
                // html.Append("<em>Error: could not retrieve Glossary from "+rssUrl+"</em>");
            }
            else
            {
                GlossaryData[]        items         = GlossaryData.FromRSSItems(glossaryRss.Channels[0].Items);
                CmsPersistentVariable persistedData = CmsPersistentVariable.Fetch(dataCacheKey);
                persistedData.Name           = dataCacheKey;
                persistedData.PersistedValue = new List <GlossaryData>(items);
                bool b = persistedData.SaveToDatabase();

                if (b)
                {
                    CmsPersistentVariable persistedLastRun = CmsPersistentVariable.Fetch(lastRunCacheKey);
                    persistedLastRun.PersistedValue = DateTime.Now;
                    persistedLastRun.Name           = lastRunCacheKey;
                    return(persistedLastRun.SaveToDatabase());
                }
            }

            return(false);
        } // FetchAndSaveRemoteRSSGlossaryData
コード例 #2
0
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder html = new StringBuilder();

            string swh = "http://www.sadcwaterhub.org/glossary/feed?lang_tid[0]=2";
            string url = CmsControlUtils.getControlParameterKeyValue(CmsContext.currentPage, this, "rssurl", swh);

            int cacheDuration_hours = CmsControlUtils.getControlParameterKeyValue(CmsContext.currentPage, this, "cacheduration_hours", 12);

            System.Web.Caching.Cache Cache = System.Web.Hosting.HostingEnvironment.Cache;

            Rss.RssFeed glossaryRss;
            // the RSS feed is cached to improve performance.
            if (cacheDuration_hours >= 0 && Cache[url] != null)
            {
                glossaryRss = (Rss.RssFeed)Cache[url];
            }
            else
            {
                glossaryRss = Rss.RssFeed.Read(url);

                // add it to the cache
                if (cacheDuration_hours >= 0)
                {
                    Cache.Insert(url, glossaryRss, null, DateTime.Now.AddHours(cacheDuration_hours), System.Web.Caching.Cache.NoSlidingExpiration);
                }
            }

            if (glossaryRss.Channels.Count == 0)
            {
                html.Append("<em>Error: could not retrieve Glossary from SADC Water Hub</em>");
            }
            else
            {
                GlossaryData[]          items  = ToGlossaryData(glossaryRss.Channels[0].Items);
                GlossaryPlaceholderData phData = new GlossaryPlaceholderData();
                phData.SortOrder = GlossaryPlaceholderData.GlossarySortOrder.byWord;
                phData.ViewMode  = GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter;

                string[] charsWithData   = getCharsWithData(items);
                string   letterToDisplay = Glossary.getLetterToDisplay(phData);

                if (letterToDisplay != "")
                {
                    items = GlossaryData.getItemsStartingWithChar(items, letterToDisplay[0]);
                }

                html.Append(Glossary.GetHtmlDisplay(CmsContext.currentPage, items, phData, charsWithData, letterToDisplay));
            } // else
            writer.Write(html.ToString());
        }
コード例 #3
0
        private GlossaryData[] ToGlossaryData(Rss.RssItemCollection items)
        {
            List <GlossaryData> ret = new List <GlossaryData>();

            foreach (Rss.RssItem item in items)
            {
                GlossaryData g = new GlossaryData();
                g.word        = item.Title;
                g.description = item.Description;

                ret.Add(g);
            } // foreach
            return(ret.ToArray());
        }
コード例 #4
0
        public string RunInlineGlossaryFilter(CmsPage pageBeingFiltered, string placeholderHtml)
        {
            try
            {
                bool enabled = CmsConfig.getConfigValue("GlossaryHighlightFilter:Enable", false); // disabled by default

                if (!enabled || CmsContext.currentEditMode == CmsEditMode.Edit)
                {
                    return(placeholderHtml);
                }

#if DEBUG
                CmsContext.currentPage.HeadSection.AddCSSStyleStatements("span.InlineGlossaryTerm { border-bottom: 1px dotted red; }");
#endif

                // -- get the glossaryID to get data for (language specific)
                int    glossaryId  = 2;
                string glossaryIds = CmsConfig.getConfigValue("GlossaryHighlightFilter:GlossaryId", "");
                try
                {
                    string[] glossaryIdsParts = glossaryIds.Split(new char[] { CmsConfig.PerLanguageConfigSplitter }, StringSplitOptions.RemoveEmptyEntries);
                    if (glossaryIdsParts.Length >= CmsConfig.Languages.Length)
                    {
                        int index = CmsLanguage.IndexOf(CmsContext.currentLanguage.shortCode, CmsConfig.Languages);
                        if (index >= 0)
                        {
                            glossaryId = Convert.ToInt32(glossaryIdsParts[index]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error: GlossaryHighlightFilter is incorrectly configured!");
                }

                // -- get the glossary data from the database. The data is cached so that we don't hit the database for this info every time.
                GlossaryData[] gData;
                string         cacheKey = "GlossaryHighlightFilter_Data_" + glossaryId;
                if (!CmsContext.currentUserIsLoggedIn && System.Web.Hosting.HostingEnvironment.Cache[cacheKey] != null)
                {
                    gData = (GlossaryData[])System.Web.Hosting.HostingEnvironment.Cache[cacheKey];
                }
                else
                {
                    GlossaryDb db = new GlossaryDb();
                    gData = db.getGlossaryData(glossaryId);
                    if (!CmsContext.currentUserIsLoggedIn)
                    {
                        System.Web.Hosting.HostingEnvironment.Cache.Insert(cacheKey, gData, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
                    }

                    // go through longer words first (longer words/phrases are usually more specific than shorter ones)
                    gData = GlossaryData.SortByWordLength(gData, SortDirection.Descending);
                }

                // -- short-circuit processing if there aren't any glossary terms in the system.
                if (gData.Length == 0)
                {
                    return(placeholderHtml);
                }

                // -- process the placeholderHTML
                string html = placeholderHtml;

                List <string> toSurround = new List <string>();
                List <string> prefixs    = new List <string>();
                List <string> suffixs    = new List <string>();

                foreach (GlossaryData d in gData)
                {
                    int index = html.IndexOf(d.word.Trim(), StringComparison.CurrentCultureIgnoreCase);
                    if (index >= 0 && d.word.Trim().Length > 0)
                    {
                        // string safeDesc = StringUtils.AddSlashes(d.description);
                        string safeDesc = HttpUtility.HtmlEncode(d.word + ": " + d.description);
                        safeDesc = safeDesc.Replace("\n", " ");
                        safeDesc = safeDesc.Replace("\r", " ");
                        safeDesc = safeDesc.Replace("\t", " ");
                        safeDesc = safeDesc.Replace("  ", " ");

                        string prefix = "<span title=\"" + safeDesc + "\" class=\"InlineGlossaryTerm\">";
                        string suffix = "</span>";

                        toSurround.Add(d.word.Trim());
                        prefixs.Add(prefix);
                        suffixs.Add(suffix);
                    }
                } // foreach word

                html = SurroundInHtml(toSurround.ToArray(), prefixs.ToArray(), suffixs.ToArray(), html);

                return(html.ToString());
            }
            catch (Exception ex)
            {
                placeholderHtml += ("<!-- GlossaryHighlightingFilter Error: " + ex.Message + " -->");
            }

            return(placeholderHtml);
        }