コード例 #1
0
        private void SubmitEngine(string engine, string sitemapUrl)
        {
            //Check if it is not localhost because search engines returns an error
            if (!sitemapUrl.Contains("http://localhost"))
            {
                string request = string.Concat(engine, SitemapItem.HtmlEncode(sitemapUrl));

                System.Net.HttpWebRequest httpRequest =
                    (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(request);
                try
                {
                    System.Net.WebResponse webResponse = httpRequest.GetResponse();

                    System.Net.HttpWebResponse httpResponse = (System.Net.HttpWebResponse)webResponse;
                    if (httpResponse.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        Log.Error(string.Format("Cannot submit sitemap to \"{0}\"", engine), this);
                    }
                }
                catch
                {
                    Log.Warn(string.Format("The serachengine \"{0}\" returns an 404 error", request), this);
                }
            }
        }
コード例 #2
0
        private XmlDocument BuildSitemapItem(XmlDocument doc, SitemapItem item, Site site)
        {
            XmlNode urlsetNode = doc.LastChild;

            XmlNode urlNode = doc.CreateElement("url");

            urlsetNode.AppendChild(urlNode);

            XmlNode locNode = doc.CreateElement("loc");

            urlNode.AppendChild(locNode);
            locNode.AppendChild(doc.CreateTextNode(item.Location));

            XmlNode lastmodNode = doc.CreateElement("lastmod");

            urlNode.AppendChild(lastmodNode);
            lastmodNode.AppendChild(doc.CreateTextNode(item.LastModified));

            if (!string.IsNullOrWhiteSpace(item.ChangeFrequency))
            {
                XmlNode changeFrequencyNode = doc.CreateElement("changefreq");
                urlNode.AppendChild(changeFrequencyNode);
                changeFrequencyNode.AppendChild(doc.CreateTextNode(item.ChangeFrequency));
            }

            if (!string.IsNullOrWhiteSpace(item.Priority))
            {
                var priorityNode = doc.CreateElement("priority");
                urlNode.AppendChild(priorityNode);
                priorityNode.AppendChild(doc.CreateTextNode(item.Priority));
            }

            return(doc);
        }
コード例 #3
0
        public SitemapItem(Item item, SiteContext site, Item parentItem)
        {
            Priority        = item[Constants.SeoSettings.Priority];
            ChangeFrequency = item[Constants.SeoSettings.ChangeFrequency].ToLower();
            LastModified    = HtmlEncode(item.Statistics.Updated.ToLocalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"));
            Id    = item.ID.Guid;
            Title = item[Constants.SeoSettings.Title];
            var itemUrl = HtmlEncode(GetItemUrl(item, site));

            if (parentItem == null)
            {
                Location = itemUrl;
            }
            else
            {
                Location = GetSharedItemUrl(item, site, parentItem);
            }
            Language[] languages = item.Languages;
            HrefLangs = new List <SitemapItemHrefLang>();
            Language[] languageArray = languages;
            for (int i = 0; i < (int)languageArray.Length; i++)
            {
                Language language      = languageArray[i];
                string   sharedItemUrl = SitemapItem.HtmlEncode(SitemapItem.GetItemUrl(item, site, language));
                if (parentItem != null)
                {
                    sharedItemUrl = SitemapItem.GetSharedItemUrl(item, site, parentItem);
                }
                this.HrefLangs.Add(new SitemapItemHrefLang()
                {
                    Href     = sharedItemUrl,
                    HrefLang = language.CultureInfo.TwoLetterISOLanguageName
                });
            }
        }
コード例 #4
0
        private XmlDocument BuildSitemapItem(XmlDocument doc, SitemapItem item)
        {
            var urlsetNode = doc.LastChild;

            XmlNode urlNode = doc.CreateElement("url");

            urlsetNode.AppendChild(urlNode);

            XmlNode locNode = doc.CreateElement("loc");

            urlNode.AppendChild(locNode);
            locNode.AppendChild(doc.CreateTextNode(item.Location));

            if (SitemapManagerConfiguration.IncludeLastModInXml)
            {
                XmlNode lastmodNode = doc.CreateElement("lastmod");
                urlNode.AppendChild(lastmodNode);
                lastmodNode.AppendChild(doc.CreateTextNode(item.LastModified));
            }

            if (item.HrefLangs != null && item.HrefLangs.Count > 0)
            {
                foreach (var hrefLang in item.HrefLangs)
                {
                    var xmlElement   = doc.CreateElement("xhtml", "link", SitemapManagerConfiguration.XmlnsXhtmlTpl);
                    var xmlAttribute = doc.CreateAttribute("rel");
                    xmlAttribute.Value = "alternate";
                    xmlElement.Attributes.Append(xmlAttribute);
                    xmlAttribute       = doc.CreateAttribute("hreflang");
                    xmlAttribute.Value = hrefLang.HrefLang;
                    xmlElement.Attributes.Append(xmlAttribute);
                    xmlAttribute       = doc.CreateAttribute("href");
                    xmlAttribute.Value = hrefLang.Href;
                    xmlElement.Attributes.Append(xmlAttribute);
                    urlNode.AppendChild(xmlElement);
                }
            }

            if (!string.IsNullOrWhiteSpace(item.ChangeFrequency))
            {
                XmlNode changeFrequencyNode = doc.CreateElement("changefreq");
                urlNode.AppendChild(changeFrequencyNode);
                changeFrequencyNode.AppendChild(doc.CreateTextNode(item.ChangeFrequency));
            }

            if (!string.IsNullOrWhiteSpace(item.Priority))
            {
                var priorityNode = doc.CreateElement("priority");
                urlNode.AppendChild(priorityNode);
                priorityNode.AppendChild(doc.CreateTextNode(item.Priority));
            }

            return(doc);
        }
コード例 #5
0
        private XmlDocument BuildSitemapItem(XmlDocument doc, SitemapItem item, Site site)
        {
            XmlNode urlsetNode = doc.LastChild;

            XmlNode urlNode = doc.CreateElement("url");
            urlsetNode.AppendChild(urlNode);

            XmlNode locNode = doc.CreateElement("loc");
            urlNode.AppendChild(locNode);
            locNode.AppendChild(doc.CreateTextNode(item.Location));

            XmlNode lastmodNode = doc.CreateElement("lastmod");
            urlNode.AppendChild(lastmodNode);
            lastmodNode.AppendChild(doc.CreateTextNode(item.LastModified));

            if (!string.IsNullOrWhiteSpace(item.ChangeFrequency))
            {
                XmlNode changeFrequencyNode = doc.CreateElement("changefreq");
                urlNode.AppendChild(changeFrequencyNode);
                changeFrequencyNode.AppendChild(doc.CreateTextNode(item.ChangeFrequency));
            }

            if (!string.IsNullOrWhiteSpace(item.Priority))
            {
                var priorityNode = doc.CreateElement("priority");
                urlNode.AppendChild(priorityNode);
                priorityNode.AppendChild(doc.CreateTextNode(item.Priority));
            }

            return doc;
        }
コード例 #6
0
        private List <SitemapItem> GetSitemapItems(string rootPath)
        {
            var disTpls           = _config.EnabledTemplates;
            var excludeItemsField = _config.ExcludedItems;

            var database = Factory.GetDatabase(SitemapManagerConfiguration.WorkingDatabase);

            //Get the content root item
            var contentRoot = database.Items[rootPath];

            //Get the descendents of the content root
            IEnumerable <Item> descendants;
            var user = Sitecore.Security.Accounts.User.FromName(Constants.SitemapParserUser, true);

            using (new Sitecore.Security.Accounts.UserSwitcher(user))
            {
                descendants = contentRoot.Axes.GetDescendants()
                              .Where(i => i[Constants.XmlSettings.ExcludeItemFromSitemap] != "1");
            }

            // getting shared content
            //TODO: Unverified and un-tested. Might need some modifications before it can be used.
            var sharedModels      = new List <List <SitemapItem> >();
            var sharedDefinitions = Db.SelectItems(string.Format("fast:{0}/*", _config.SitemapConfigurationItemPath).Replace("-", "#-#"));
            var site             = Factory.GetSite(_config.SiteName);
            var enabledTemplates = BuildListFromString(disTpls, '|');
            var excludeItems     = BuildListFromString(excludeItemsField, '|');

            foreach (var sharedDefinition in sharedDefinitions)
            {
                if (string.IsNullOrWhiteSpace(sharedDefinition[Constants.SharedContent.ContentLocationFieldName]) ||
                    string.IsNullOrWhiteSpace(sharedDefinition[Constants.SharedContent.ParentItemFieldName]))
                {
                    continue;
                }
                var contentLocation = ((DatasourceField)sharedDefinition.Fields[Constants.SharedContent.ContentLocationFieldName]).TargetItem;
                var parentItem      = ((DatasourceField)sharedDefinition.Fields[Constants.SharedContent.ParentItemFieldName]).TargetItem;
                var sharedItems     = new List <Item>();
                if (BucketManager.IsBucket(contentLocation))
                {
                    var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(contentLocation));
                    using (var searchContext = index.CreateSearchContext())
                    {
                        var searchResultItem =
                            searchContext.GetQueryable <SearchResultItem>()
                            .Where(item => item.Paths.Contains(contentLocation.ID) && item.ItemId != contentLocation.ID)
                            .ToList();
                        sharedItems.AddRange(searchResultItem.Select(i => i.GetItem()));
                    }
                }
                else
                {
                    sharedItems.AddRange(contentLocation.Axes.GetDescendants());
                }

                var cleanedSharedItems = from itm in sharedItems
                                         where itm.Template != null && enabledTemplates.Select(t => t.ToLower()).Contains(itm.Template.ID.ToString().ToLower()) &&
                                         !excludeItems.Contains(itm.ID.ToString())
                                         select itm;
                var sharedSitemapItems = cleanedSharedItems.Select(i => SitemapItem.GetLanguageSitemapItems(i, site, parentItem, _config));
                sharedModels.AddRange(sharedSitemapItems);
            }

            //All content items
            var contentItems = descendants.ToList();

            contentItems.Insert(0, contentRoot);

            //Filter out the content items that belongs to Sitemap Configuration Manager
            //Should belong to enabled templates
            //Should not be an excluded item
            var selected = from itm in contentItems
                           where itm.Template != null && enabledTemplates.Contains(itm.Template.ID.ToString()) &&
                           !excludeItems.Contains(itm.ID.ToString())
                           select itm;

            //Get sitemap items in Language blocks per item
            var languageSitemapItems = selected.Select(i => SitemapItem.GetLanguageSitemapItems(i, site, null, _config)).ToList();

            //Final list of sitamap items
            var sitemapItems = new List <SitemapItem>();

            //Adding shared items to the sitemap items list
            foreach (var sharedModel in sharedModels)
            {
                sitemapItems.AddRange(sharedModel);
            }

            //Adding language specific sitemap items to the sitemap items list
            foreach (var laguageSitemapItem in languageSitemapItems)
            {
                sitemapItems.AddRange(laguageSitemapItem);
            }

            //Order the items based on priority in descending order with a cap of max items
            sitemapItems = sitemapItems.OrderByDescending(u => u.Priority).Take(int.Parse(Constants.XmlSettings.UrlLimit)).ToList();

            return(sitemapItems);
        }
コード例 #7
0
        private XmlDocument BuildSitemapItem(XmlDocument doc, SitemapItem item, Site site)
        {
            string sitesMultilingual = SitemapManagerConfiguration.SitesMultilingual;

            var sitesMultilingualList = sitesMultilingual.Split('|').ToList();

            XmlNode urlsetNode = doc.LastChild;

            XmlNode urlNode = doc.CreateElement("url");

            urlsetNode.AppendChild(urlNode);

            XmlNode locNode = doc.CreateElement("loc");

            urlNode.AppendChild(locNode);
            locNode.AppendChild(doc.CreateTextNode(item.Location));

            XmlNode lastmodNode = doc.CreateElement("lastmod");

            urlNode.AppendChild(lastmodNode);
            lastmodNode.AppendChild(doc.CreateTextNode(item.LastModified));

            var siteIsMultilingual = sitesMultilingualList.FirstOrDefault(x => x.Equals(site.Name));

            if (siteIsMultilingual != null)
            {
                if ((item.HrefLangs == null ? false : item.HrefLangs.Count > 0))
                {
                    foreach (SitemapItemHrefLang hrefLang in item.HrefLangs)
                    {
                        XmlElement   xmlElement   = doc.CreateElement("xhtml", "link", SitemapManagerConfiguration.XmlnsXhtmlTpl);
                        XmlAttribute xmlAttribute = doc.CreateAttribute("rel");
                        xmlAttribute.Value = "alternate";
                        xmlElement.Attributes.Append(xmlAttribute);
                        xmlAttribute       = doc.CreateAttribute("hreflang");
                        xmlAttribute.Value = hrefLang.HrefLang;
                        xmlElement.Attributes.Append(xmlAttribute);
                        xmlAttribute       = doc.CreateAttribute("href");
                        xmlAttribute.Value = hrefLang.Href;
                        xmlElement.Attributes.Append(xmlAttribute);
                        urlNode.AppendChild(xmlElement);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(item.ChangeFrequency))
            {
                XmlNode changeFrequencyNode = doc.CreateElement("changefreq");
                urlNode.AppendChild(changeFrequencyNode);
                changeFrequencyNode.AppendChild(doc.CreateTextNode(item.ChangeFrequency));
            }

            if (!string.IsNullOrWhiteSpace(item.Priority))
            {
                var priorityNode = doc.CreateElement("priority");
                urlNode.AppendChild(priorityNode);
                priorityNode.AppendChild(doc.CreateTextNode(item.Priority));
            }

            return(doc);
        }