private string BuildUrl(mojoPortal.SearchIndex.IndexItem indexItem)
        {
            if (indexItem.UseQueryStringParams)
            {
                return SiteRoot + "/" + indexItem.ViewPage
                    + "?pageid="
                    + indexItem.PageId.ToString(CultureInfo.InvariantCulture)
                    + "&mid="
                    + indexItem.ModuleId.ToString(CultureInfo.InvariantCulture)
                    + "&ItemID="
                    + indexItem.ItemId.ToString(CultureInfo.InvariantCulture)
                    + indexItem.QueryStringAddendum;

            }
            else
            {
                return SiteRoot + "/" + indexItem.ViewPage;
            }
        }
        private void RenderSearchResults(HttpContext context, mojoPortal.SearchIndex.IndexItemCollection searchResults)
        {
            context.Response.ContentType = "application/xml";
            Encoding encoding = new UTF8Encoding();
            context.Response.ContentEncoding = encoding;

            string sanitizedQuery = SecurityHelper.RemoveMarkup(query);

            using (XmlTextWriter xmlTextWriter = new XmlTextWriter(context.Response.OutputStream, encoding))
            {
                xmlTextWriter.Formatting = Formatting.Indented;

                xmlTextWriter.WriteStartDocument();

                xmlTextWriter.WriteStartElement("feed");
                xmlTextWriter.WriteAttributeString("xmlns", "http://www.w3.org/2005/Atom");
                xmlTextWriter.WriteAttributeString("xmlns:opensearch", "http://a9.com/-/spec/opensearch/1.1/");

                xmlTextWriter.WriteStartElement("title");
                xmlTextWriter.WriteValue(string.Format(CultureInfo.InvariantCulture, Resource.SearchTitleFormat, siteSettings.SiteName, sanitizedQuery));
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("link");
                xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchResults.ashx?q=" + context.Server.UrlEncode(sanitizedQuery));
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("updated");
                xmlTextWriter.WriteValue(DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture).Replace(" ", "T"));
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("author");

                xmlTextWriter.WriteStartElement("name");
                xmlTextWriter.WriteValue(siteSettings.CompanyName);
                xmlTextWriter.WriteEndElement();
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("id");
                xmlTextWriter.WriteValue("urn:uuid:" + siteSettings.SiteGuid.ToString());
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("opensearch:totalResults");
                xmlTextWriter.WriteValue(totalHits.ToString(CultureInfo.InvariantCulture));
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("opensearch:startIndex");
                xmlTextWriter.WriteValue(start.ToString(CultureInfo.InvariantCulture));
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("opensearch:itemsPerPage");
                xmlTextWriter.WriteValue(pageSize.ToString(CultureInfo.InvariantCulture));
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("opensearch:Query");
                xmlTextWriter.WriteAttributeString("role", "request");
                xmlTextWriter.WriteAttributeString("searchTerms", sanitizedQuery);
                xmlTextWriter.WriteAttributeString("startPage", "1");
                xmlTextWriter.WriteEndElement();

                //this is a link to the xhtml search page
                xmlTextWriter.WriteStartElement("link");
                xmlTextWriter.WriteAttributeString("rel", "alternate");
                xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchResults.aspx?q="
                    + context.Server.UrlEncode(sanitizedQuery)
                    + "&p=" + pageNumber.ToString(CultureInfo.InvariantCulture));
                xmlTextWriter.WriteAttributeString("type", "text/html");
                xmlTextWriter.WriteEndElement();

                xmlTextWriter.WriteStartElement("link");
                xmlTextWriter.WriteAttributeString("rel", "self");
                xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchResults.ashx?q="
                    + context.Server.UrlEncode(sanitizedQuery)
                    + "&p=" + pageNumber.ToString(CultureInfo.InvariantCulture));
                xmlTextWriter.WriteAttributeString("type", "application/atom+xml");
                xmlTextWriter.WriteEndElement();

                if (totalPages > 1)
                {
                    xmlTextWriter.WriteStartElement("link");
                    xmlTextWriter.WriteAttributeString("rel", "first");
                    xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchResults.ashx?q="
                        + context.Server.UrlEncode(sanitizedQuery)
                        + "&p=1");
                    xmlTextWriter.WriteAttributeString("type", "application/atom+xml");
                    xmlTextWriter.WriteEndElement();

                    if (pageNumber > 1)
                    {
                        xmlTextWriter.WriteStartElement("link");
                        xmlTextWriter.WriteAttributeString("rel", "previous");
                        xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchResults.ashx?q="
                            + context.Server.UrlEncode(sanitizedQuery)
                            + "&p=" + (pageNumber -1).ToString(CultureInfo.InvariantCulture));
                        xmlTextWriter.WriteAttributeString("type", "application/atom+xml");
                        xmlTextWriter.WriteEndElement();

                    }

                    if (pageNumber < totalPages)
                    {
                        xmlTextWriter.WriteStartElement("link");
                        xmlTextWriter.WriteAttributeString("rel", "next");
                        xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchResults.ashx?q="
                            + context.Server.UrlEncode(sanitizedQuery)
                            + "&p=" + (pageNumber + 1).ToString(CultureInfo.InvariantCulture));
                        xmlTextWriter.WriteAttributeString("type", "application/atom+xml");
                        xmlTextWriter.WriteEndElement();

                    }

                    xmlTextWriter.WriteStartElement("link");
                    xmlTextWriter.WriteAttributeString("rel", "last");
                    xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchResults.ashx?q="
                        + context.Server.UrlEncode(sanitizedQuery)
                        + "&p=" + totalPages.ToString(CultureInfo.InvariantCulture));
                    xmlTextWriter.WriteAttributeString("type", "application/atom+xml");
                    xmlTextWriter.WriteEndElement();

                }

                ////this is a  link to the description
                xmlTextWriter.WriteStartElement("link");
                xmlTextWriter.WriteAttributeString("rel", "search");
                xmlTextWriter.WriteAttributeString("type", "application/opensearchdescription+xml");
                xmlTextWriter.WriteAttributeString("href", SiteRoot + "/SearchEngineInfo.ashx");
                xmlTextWriter.WriteEndElement();

                foreach (mojoPortal.SearchIndex.IndexItem item in searchResults)
                {
                    xmlTextWriter.WriteStartElement("entry");

                    xmlTextWriter.WriteStartElement("link");
                    xmlTextWriter.WriteValue(BuildUrl(item));
                    xmlTextWriter.WriteEndElement();

                    xmlTextWriter.WriteStartElement("id");
                    xmlTextWriter.WriteValue(BuildUrl(item));
                    xmlTextWriter.WriteEndElement();

                    //xmlTextWriter.WriteRaw("<updated>2003-12-13T18:30:02Z</updated>");

                    xmlTextWriter.WriteStartElement("content");
                    xmlTextWriter.WriteAttributeString("type", "text");
                    xmlTextWriter.WriteCData(item.Intro);
                    xmlTextWriter.WriteEndElement();

                    xmlTextWriter.WriteEndElement();
                }

                xmlTextWriter.WriteEndElement();
            }
        }
Esempio n. 3
0
        public static void RefreshFeed(
            mojoPortal.Business.RssFeed feedInfo,
            int moduleId,
            Guid moduleGuid,
            int maxDaysOld,
            int maxEntriesPerFeed,
            bool enableSelectivePublishing)
        {
            if (feedInfo == null) { return; }

            try
            {
                if (FeedManagerConfiguration.UseReadWriteLockForCacheMenagement)
                {
                    cacheLock.AcquireWriterLock(cacheLockTimeoutInMilliseconds);
                }

                DateTime cutoffDate = DateTime.Now.AddDays(-maxDaysOld);

                mojoPortal.Business.RssFeed.DeleteExpiredEntriesByModule(moduleGuid, cutoffDate);
                mojoPortal.Business.RssFeed.DeleteUnPublishedEntriesByFeed(feedInfo.ItemId);

                int entriesAdded = 0;
                string siteRoot = SiteUtils.GetNavigationSiteRoot();
                string secureSiteRoot = SiteUtils.GetSecureNavigationSiteRoot();

                bool publish = true;
                if (enableSelectivePublishing)
                {
                    if (!feedInfo.PublishByDefault)
                    {
                        publish = false;
                    }
                }

                string feedUrl = feedInfo.RssUrl;
                if (feedUrl.StartsWith("~/"))
                {
                    feedUrl = WebUtils.ResolveServerUrl(feedUrl).Replace("https:", "http:");
                }

                try
                {
                    GenericSyndicationFeed gsFeed = GenericSyndicationFeed.Create(new Uri(FormatFeedUrl(feedUrl, siteRoot, secureSiteRoot)));

                    #region RSSFeed_management
                    if (gsFeed.Format == SyndicationContentFormat.Rss)
                    {
                        Argotic.Syndication.RssFeed rssFeed = gsFeed.Resource as Argotic.Syndication.RssFeed;
                        if (rssFeed != null)
                        {

                            foreach (Argotic.Syndication.RssItem rssItem in rssFeed.Channel.Items)
                            {
                                if ((rssItem.PublicationDate >= cutoffDate) || (maxDaysOld == 0))
                                {
                                    if ((entriesAdded < maxEntriesPerFeed) || (maxEntriesPerFeed == 0))
                                    {

                                        string entryBlob = rssItem.Title + rssItem.Link.ToString();
                                        int entryHash = GetEntryHash(entryBlob);

                                        if (UpdateEntry(
                                            moduleGuid,
                                            EnsureDate(rssItem.PublicationDate),
                                            rssItem.Title,
                                            rssItem.Author,
                                            feedInfo.RssUrl,
                                            rssItem.Description,
                                            rssItem.Link.ToString(),
                                            entryHash,
                                            feedInfo.ItemGuid,
                                            feedInfo.ItemId,
                                            publish) > 0)
                                        {
                                            entriesAdded += 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #endregion

                    #region ATOMFeed_management
                    if (gsFeed.Format == SyndicationContentFormat.Atom)
                    {
                        Argotic.Syndication.AtomFeed atomFeed = gsFeed.Resource as Argotic.Syndication.AtomFeed;

                        foreach (AtomEntry atItem in atomFeed.Entries)
                        {

                            if ((atItem.PublishedOn >= cutoffDate) || (maxDaysOld == 0))
                            {
                                if ((entriesAdded < maxEntriesPerFeed) || (maxEntriesPerFeed == 0))
                                {

                                    string entryLink = string.Empty;
                                    StringBuilder entryAuthor = new StringBuilder();
                                    string comma = string.Empty;

                                    foreach (AtomPersonConstruct atPerson in atItem.Authors)
                                    {
                                        entryAuthor.Append(comma + atPerson.Name);
                                        comma = ",";
                                    }

                                    if (entryAuthor.Length == 0)
                                    {
                                        foreach (AtomPersonConstruct atPerson in atomFeed.Authors)
                                        {
                                            entryAuthor.Append(comma + atPerson.Name);
                                            comma = ",";
                                        }

                                    }

                                    foreach (AtomLink atLink in atItem.Links)
                                    {
                                        if (atLink.Relation == "alternate")
                                        {
                                            entryLink = atLink.Uri.ToString();
                                        }
                                    }

                                    if ((entryLink.Length == 0) && (atItem.Links.Count > 0))
                                    {
                                        entryLink = atItem.Links[0].Uri.ToString();

                                    }

                                    string content = string.Empty;
                                    if (atItem.Content == null)
                                    {
                                        if (atItem.Summary != null)
                                        {
                                            content = atItem.Summary.Content;
                                        }
                                    }
                                    else
                                    {
                                        content = atItem.Content.Content;
                                    }

                                    if (content.Length == 0) { continue; }

                                    string entryBlob = atItem.Title.ToString() + entryLink;
                                    int entryHash = GetEntryHash(entryBlob);

                                    if (UpdateEntry(
                                        moduleGuid,
                                        EnsureDate(atItem.PublishedOn),
                                        atItem.Title.ToString(),
                                        entryAuthor.ToString(),
                                        feedInfo.RssUrl,
                                        content,
                                        entryLink,
                                        entryHash,
                                        feedInfo.ItemGuid,
                                        feedInfo.ItemId,
                                        publish) > 0)
                                    {
                                        entriesAdded += 1;
                                    }

                                }

                            }

                        }

                    }

                    #endregion
                }
                catch (WebException ex)
                {
                    if (log.IsErrorEnabled)
                    {
                        string logMsg = String.Format("There was a problem trying to read the feed for url {0}.  Ignoring.", feedInfo.RssUrl);
                        log.Error(logMsg, ex);
                    }
                }
                catch (UriFormatException ex)
                {
                    if (log.IsErrorEnabled)
                    {
                        string logMsg = String.Format("There was a problem trying to read the feed for url {0}.  Ignoring.", feedInfo.RssUrl);
                        log.Error(logMsg, ex);
                    }
                }
                catch (System.Net.Sockets.SocketException ex)
                {
                    if (log.IsErrorEnabled)
                    {
                        string logMsg = String.Format("There was a problem trying to read the feed for url {0}.  Ignoring.", feedInfo.RssUrl);
                        log.Error(logMsg, ex);
                    }
                }
                catch (System.Xml.XmlException ex)
                {
                    if (log.IsErrorEnabled)
                    {
                        string logMsg = String.Format("There was a problem trying to read the feed for url {0}.  Ignoring.", feedInfo.RssUrl);
                        log.Error(logMsg, ex);
                    }
                }
                catch (System.Security.SecurityException ex)
                {
                    log.Error("Could not load feed due to security exception. Must be running in restricted trust level. Creating server side web requests is not allowed in current configuration.", ex);

                }
                catch (System.Data.Common.DbException ex)
                {
                    log.Error("Error updating feed database cache", ex);

                }
            }
            finally
            {
                if (FeedManagerConfiguration.UseReadWriteLockForCacheMenagement)
                {
                    try
                    {
                        cacheLock.ReleaseWriterLock();
                    }
                    catch (ApplicationException ex)
                    {
                        log.Error("swallowed error", ex);
                    }
                }
            }
        }