Пример #1
0
        public string AddToPage(string sg, Article article)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            string foldername;
            if (article.Date.Month < 10)
                foldername = "0" + article.Date.Month;
            else
                foldername = article.Date.Month.ToString(CultureInfo.InvariantCulture);
            foldername += " " + article.Date.ToString("MMMM");
            PageData page = (PageData)_client.Read(GetPage(sg, foldername), _readOptions);
            //if (!page.IsEditable.GetValueOrDefault())
            //{
            //    page = (PageData)_client.CheckOut(page.Id, true, _readOptions);
            //}

            List<ComponentPresentationData> componentPresentations = page.ComponentPresentations.ToList();
            string articleId = GetUriInBlueprintContext(article.Id, ResolveUrl(Constants.WebSitePublication));
            string ctId = GetUriInBlueprintContext(ResolveUrl(Constants.ArticleComponentTemplateUrl),
                                                   ResolveUrl(Constants.WebSitePublication));
            ComponentPresentationData cp = new ComponentPresentationData();
            if (articleId != null && articleId != TcmUri.UriNull)
            {
                cp.Component = new LinkToComponentData { IdRef = articleId };
                cp.ComponentTemplate = new LinkToComponentTemplateData { IdRef = ctId };
                componentPresentations.Add(cp);
                page.ComponentPresentations = componentPresentations.ToArray();
            }
            page = (PageData)_client.Update(page, _readOptions);
            // Looks like it's still checked out at the end of this...
            if (page.IsEditable.HasValue && page.IsEditable == true)
                _client.CheckIn(GetVersionlessUri(page.Id), null);

            watch.Stop();
            Console.WriteLine("Added component presentation in " + watch.ElapsedMilliseconds + " milliseconds");
            return page.Id;
        }
Пример #2
0
 public string GetPageIdForArticle(Article article)
 {
     UsingItemsFilterData filter = new UsingItemsFilterData { ItemTypes = new[] { ItemType.Page } };
     foreach (var xNode in _client.GetListXml(article.Id, filter).Nodes())
     {
         var node = (XElement) xNode;
         return node.Attribute("ID").Value;
     }
     return null;
 }
Пример #3
0
 public bool IsArticleInPage(Article article)
 {
     UsingItemsFilterData filter = new UsingItemsFilterData { ItemTypes = new[] { ItemType.Page } };
     return _client.GetListXml(article.Id, filter).Nodes().Any();
 }
Пример #4
0
        static void Main()
        {
            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");

            ContentManager cm = new ContentManager(client);

            List<Source> sources = cm.GetSources();
            int countSources = sources.Count;
            Console.WriteLine("Loaded " + countSources + " sources. Starting to process.");
            XmlNamespaceManager nm = new XmlNamespaceManager(new NameTable());
            nm.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

            Dictionary<Source, List<Article>> addedContent = new Dictionary<Source, List<Article>>();

            foreach (var source in sources)
            {
                Console.WriteLine("Loading content for source " + source.Title);
                XmlDocument feedXml = null;

                WebRequest wrq = WebRequest.Create(source.RssFeedUrl);
                wrq.Proxy = WebRequest.DefaultWebProxy;
                wrq.Proxy.Credentials = CredentialCache.DefaultCredentials;

                XmlTextReader reader = null;
                SyndicationFeed feed = null;
                try
                {
                    reader = new XmlTextReader(wrq.GetResponse().GetResponseStream());
                    feed = SyndicationFeed.Load(reader);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not read response from source" + ex.Message);
                }
                if (reader == null || feed == null) continue;

                int countItems = feed.Items.Count();
                Console.WriteLine("Loaded " + countItems + " items from source. Processing");
                int count = 0;
                List<Article> newArticles = new List<Article>();
                foreach (var item in feed.Items)
                {
                    count++;
                    Person author = null;
                    if (item.Authors.Count == 0)
                    {
                        //Console.WriteLine("Could not find an author in feed source, checking for default");
                        if (source.DefaultAuthor != null)
                        {
                            author = source.DefaultAuthor;
                            //Console.WriteLine("Using default author " + author.Name);
                        }
                        else
                        {
                            //Console.WriteLine("Could not find default author, being creative");
                            if (feedXml == null)
                            {
                                try
                                {
                                    feedXml = new XmlDocument();
                                    feedXml.Load(source.RssFeedUrl);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Something went wrong loading " + source.RssFeedUrl);
                                    Console.WriteLine(ex.ToString());
                                }

                            }
                            if (feedXml != null)
                            {
                                string xpath = "/rss/channel/item[" + count + "]/dc:creator";
                                if (feedXml.SelectSingleNode(xpath, nm) != null)
                                {
                                    author =
                                        cm.FindPersonByNameOrAlternate(feedXml.SelectSingleNode(xpath, nm).InnerText);
                                    if (author == null)
                                    {
                                        author = new Person(client) { Name = feedXml.SelectSingleNode(xpath, nm).InnerText };
                                        author.Save();
                                        author =
                                            cm.FindPersonByNameOrAlternate(
                                                feedXml.SelectSingleNode(xpath, nm).InnerText, true);
                                    }
                                }
                            }
                        }

                    }
                    else
                    {
                        string nameOrAlternate;
                        SyndicationPerson syndicationPerson = item.Authors.First();
                        if (string.IsNullOrEmpty(syndicationPerson.Name))
                            nameOrAlternate = syndicationPerson.Email;
                        else
                            nameOrAlternate = syndicationPerson.Name;

                        author = cm.FindPersonByNameOrAlternate(nameOrAlternate);
                    }
                    if (author == null)
                    {
                        string name = string.Empty;
                        if (item.Authors.Count > 0)
                        {
                            if (item.Authors[0].Name != null)
                                name = item.Authors[0].Name;
                            else
                                name = item.Authors[0].Email;
                        }
                        author = new Person(client) { Name = name };
                        if (source.IsStackOverflow)
                        {
                            author.StackOverflowId = item.Authors[0].Uri;
                        }
                        author.Save();
                        author = cm.FindPersonByNameOrAlternate(name, true);
                    }

                    List<Person> authors = new List<Person> { author };
                    //Console.WriteLine("Using author: " + author.Name);
                    string stackOverflowId = null;
                    if (source.IsStackOverflow)
                    {
                        if (string.IsNullOrEmpty(author.StackOverflowId))
                        {
                            author.StackOverflowId = item.Authors[0].Uri;
                            author.Save(true);
                        }
                    }

                    if (source.IsTridionStackExchange)
                    {
                        stackOverflowId = item.Id;
                    }

                    if (item.PublishDate.DateTime > DateTime.MinValue)
                    {
                        // Organize content by Date
                        // Year
                        // Month
                        // Day
                        string store = cm.GetFolderForDate(item.PublishDate.DateTime);
                        string articleTitle;
                        if (string.IsNullOrEmpty(item.Title.Text))
                        {
                            articleTitle = "No title specified";
                        }
                        else
                        {
                            articleTitle = item.Title.Text;
                        }
                        articleTitle = articleTitle.Trim();

                        OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData();
                        bool alreadyExists = false;
                        XElement items = client.GetListXml(store, filter);
                        foreach (XElement node in items.Nodes())
                        {
                            if (source.IsTridionStackExchange)
                            {
                                Article a = new Article(new TcmUri(node.Attribute("ID").Value), client);
                                if (a.StackOverFlowQuestionId == stackOverflowId)
                                {
                                    alreadyExists = true;
                                    if (a.Title != node.Attribute("Title").Value)
                                    {
                                        a.Title = node.Attribute("Title").Value;
                                        a.Save();
                                        Console.WriteLine("Modified title of article with TrEx ID: " + stackOverflowId);
                                    }
                                    continue;
                                }

                            }

                            if (!node.Attribute("Title").Value.Equals(articleTitle)) continue;
                            alreadyExists = true;
                            break;
                        }

                        // Loop differently if this is Tridion on StackExchange (titles tend to change as people fix the content)

                        if (!alreadyExists)
                        {

                            //Console.WriteLine(articleTitle + " is a new article. Saving");
                            Console.Write(".");
                            Article article = new Article(client, new TcmUri(store));

                            string content = "";
                            string summary = "";
                            try
                            {
                                if (item.Content != null)
                                {
                                    content = ((TextSyndicationContent)item.Content).Text;
                                }
                                if (item.Summary != null)
                                {
                                    summary = item.Summary.Text;
                                }
                                if (!string.IsNullOrEmpty(content))
                                {
                                    try
                                    {
                                        content = Utilities.ConvertHtmlToXhtml(content);
                                    }
                                    catch (Exception)
                                    {
                                        content = null;
                                    }
                                }
                                if (!string.IsNullOrEmpty(summary))
                                {
                                    try
                                    {
                                        summary = Utilities.ConvertHtmlToXhtml(summary);
                                    }
                                    catch (Exception)
                                    {
                                        summary = null;
                                    }

                                }

                                if (string.IsNullOrEmpty(summary))
                                {
                                    summary = !string.IsNullOrEmpty(content) ? content : "Could not find summary";
                                }
                                if (string.IsNullOrEmpty(content))
                                {
                                    content = !string.IsNullOrEmpty(summary) ? summary : "Could not find content";
                                }

                            }
                            catch (Exception ex)
                            {
                                content = "Could not convert source description to XHtml. " + ex.Message;
                                content += ((TextSyndicationContent)item.Content).Text;
                            }
                            article.Authors = authors;
                            article.Body = content;
                            article.Date = item.PublishDate.DateTime;
                            article.DisplayTitle = item.Title.Text;
                            article.Title = articleTitle;
                            article.Summary = summary;
                            article.Url = item.Links.First().Uri.AbsoluteUri;
                            //if (stackOverflowId != null) article.StackOverFlowQuestionId = stackOverflowId;
                            List<string> categories = new List<string>();
                            foreach (var category in item.Categories)
                            {
                                categories.Add(category.Name);
                            }
                            article.Categories = categories;
                            article.Source = source;
                            article.Save();

                            Console.Write("#");
                            newArticles.Add(article);
                        }
                        else
                        {
                            Console.Write(".");
                        }
                    }

                }
                if (newArticles.Count > 0)
                {
                    addedContent.Add(source, newArticles);
                }
            }
            List<string> idsToPublish = new List<string>();
            if (addedContent.Count > 0)
            {
                Console.WriteLine("============================================================");
                Console.WriteLine("Added content");
                foreach (Source source in addedContent.Keys)
                {
                    string sg = cm.GetStructureGroup(source.Title, cm.ResolveUrl(Constants.RootStructureGroup));
                    Console.WriteLine("Source: " + source.Content.Title + "(" + addedContent[source].Count + ")");
                    foreach (Article article in addedContent[source])
                    {
                        string yearSg = cm.GetStructureGroup(article.Date.Year.ToString(CultureInfo.InvariantCulture), sg);
                        string pageId = cm.AddToPage(yearSg, article);
                        if (!idsToPublish.Contains(pageId)) idsToPublish.Add(pageId);
                        Console.WriteLine(article.Title + ", " + article.Authors[0].Name);
                    }
                    Console.WriteLine("-------");
                }
                Console.WriteLine("============================================================");
            }

            //Publishing
            cm.Publish(idsToPublish.ToArray(), "tcm:0-2-65537");

            Console.WriteLine("Finished, press any key to exit");
            Console.Read();
        }