Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DinisCruz/8915366 to your computer and use it in GitHub Desktop.
Save DinisCruz/8915366 to your computer and use it in GitHub Desktop.
Scripts to fix broken links in TeamMentor.Net 2.0 and 3.5 libraries (after a number of articles were removed)
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var otherHrefsFound = (Dictionary<string, List<Article_WS>> )_otherHrefsFound;
var okList = (Dictionary<string, List<Article_WS>> )_okList;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
var articlesToProcess = (List<Article_WS> )_articlesToProcess;
var articleToFix = notokList.first();
return articleToFix;
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var otherHrefsFound = (Dictionary<string, List<Article_WS>> )_otherHrefsFound;
var okList = (Dictionary<string, List<Article_WS>> )_okList;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
var articlesToProcess = (List<Article_WS> )_articlesToProcess;
var articleToFix = notokList.first();
var link = articleToFix.Key;
var articles = articleToFix.Value;
return articles.first().content();
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
var articleToFix = notokList.first();
var link = articleToFix.Key;
var articles = articleToFix.Value;
var htmlDocument = articles.first()
.content()
.htmlDocument();
return htmlDocument;
//using FluentSharp.For_HtmlAgilityPack
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
//O2Ref:tmp6759.tmp
//using SecurityInnovation.TeamMentor;
//O2Tag_SetInvocationParametersToDynamic
//O2Ref:Microsoft.CSharp.dll
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
Func<string, string,string,string> getHtmlTextToRemove =
(htmlToSearch, url, text)=>
{
var htmlDocument = htmlToSearch.htmlDocument();
var linkToRemove = (from link in htmlDocument.links()
where link.outerHtml().contains(url) &&
link.innerHtml().contains(text)
select link).first();
return linkToRemove.notNull()
? linkToRemove.outerHtml()
: "";
};
var articleToFix = notokList.first();
var articles = articleToFix.Value;
var brokenLinkUrl = articleToFix.Key.split(":::").first().trim();
var brokenLinkText = articleToFix.Key.split(":::").second().trim();
return getHtmlTextToRemove(articles.first().content(),
brokenLinkUrl,
brokenLinkText);
//using FluentSharp.For_HtmlAgilityPack
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
//O2Ref:tmp6759.tmp
//using SecurityInnovation.TeamMentor;
//O2Tag_SetInvocationParametersToDynamic
//O2Ref:Microsoft.CSharp.dll
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
Func<string, string,string,string> getHtmlTextToRemove =
(htmlToSearch, url, text)=>
{
var htmlDocument = htmlToSearch.htmlDocument();
var linkToRemove = (from link in htmlDocument.links()
where link.outerHtml().contains(url) &&
link.innerHtml().contains(text)
select link).first();
return linkToRemove.notNull()
? linkToRemove.outerHtml()
: "";
};
var articleToFix = notokList.first();
var articles = articleToFix.Value;
var brokenLinkUrl = articleToFix.Key.split(":::").first().trim();
var brokenLinkText = articleToFix.Key.split(":::").second().trim();
var targetHtml = articles.first().content();
var htmlToRemove = getHtmlTextToRemove(targetHtml,
brokenLinkUrl,
brokenLinkText);
"test browser".popupWindow().add_WebBrowser()
.set_Text(targetHtml)
.insert_Below().add_SourceCodeEditor()
.set_Text(targetHtml);
//using FluentSharp.For_HtmlAgilityPack
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
//O2Ref:tmp6759.tmp
//using SecurityInnovation.TeamMentor;
//O2Tag_SetInvocationParametersToDynamic
//O2Ref:Microsoft.CSharp.dll
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
Func<string, string,string,string> getHtmlTextToRemove =
(htmlToSearch, url, text)=>
{
var htmlDocument = htmlToSearch.htmlDocument();
var linkToRemove = (from link in htmlDocument.links()
where link.outerHtml().contains(url) &&
link.innerHtml().contains(text)
select link).first();
return linkToRemove.notNull()
? linkToRemove.outerHtml() + ":::" + linkToRemove.ParentNode.outerHtml()
: "";
};
/*var popupWindow = "DataGrivView with links to remove".popupWindow();
var dataGridView = popupWindow.add_DataGridView()
.add_Columns("Url To remove",
"Article Title to remove",
"Html to remove",
"Html to remove (Parent Node)");
*/
foreach(var articleToFix in notokList)
{
var articles = articleToFix.Value;
var brokenLinkUrl = articleToFix.Key.split(":::").first().trim();
var brokenLinkText = articleToFix.Key.split(":::").second().trim();
foreach(var article in articles)
{
var targetHtml = article.content();
var htmlToRemove = getHtmlTextToRemove(targetHtml,
brokenLinkUrl,
brokenLinkText);
var foundLink = htmlToRemove.split(":::").first();
var parentNode = htmlToRemove.split(":::").second();
if (parentNode.split("<li>").size() >0 )
return foundLink.line().line() +
parentNode.line().line() +
parentNode.tidyHtml();
//dataGridView.add_Row(brokenLinkUrl, brokenLinkText, foundLink, parentNode);
}
}
/*"test browser".popupWindow().add_WebBrowser()
.set_Text(targetHtml)
.insert_Below().add_SourceCodeEditor()
.set_Text(targetHtml);*/
//using O2.XRules.Database.Utils
//using FluentSharp.For_HtmlAgilityPack
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
//O2Ref:tmp6759.tmp
//using SecurityInnovation.TeamMentor;
//O2Tag_SetInvocationParametersToDynamic
//O2Ref:Microsoft.CSharp.dll
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
Func<string, string,string,string> getHtmlTextToRemove =
(htmlToSearch, url, text)=>
{
var htmlDocument = htmlToSearch.htmlDocument();
var linkToRemove = (from link in htmlDocument.links()
where link.outerHtml().contains(url) &&
link.innerHtml().contains(text)
select link).first();
return linkToRemove.notNull()
? linkToRemove.outerHtml() + ":::" + linkToRemove.ParentNode.outerHtml()
: "";
};
var popupWindow = "DataGrivView with links to remove".popupWindow();
var dataGridView = popupWindow.add_DataGridView()
.add_Columns("Url To remove",
"Article Title to remove",
"Html to remove (ParentNode)",
"Html to remove (Fixed)",
"Html to remove (size)");
foreach(var articleToFix in notokList)
{
var articles = articleToFix.Value;
var brokenLinkUrl = articleToFix.Key.split(":::").first().trim();
var brokenLinkText = articleToFix.Key.split(":::").second().trim();
foreach(var article in articles)
{
var targetHtml = article.content();
var htmlToRemove = getHtmlTextToRemove(targetHtml,
brokenLinkUrl,
brokenLinkText);
var foundLink = htmlToRemove.split(":::").first();
var parentNode = htmlToRemove.split(":::").second();
var liSplit = parentNode.split("<li>");
if (liSplit.size() < 2 )
continue; // this shouldn't really happen
if (liSplit.size() == 2 ) // scenario 1 : easy to handle , just remove parentNode
{
htmlToRemove = parentNode;
//remove parentNode
}
else // scenario 2 : first last LI tag then remove foundLink from parentNode
{
var withoutLastLITag = parentNode.subString(0, parentNode.indexLast("</li>"));
var withoutFoundLink = withoutLastLITag.subString_After(foundLink);
htmlToRemove = parentNode.remove(withoutFoundLink);
//return withoutFoundLink.line().line() + parentNode;
}
dataGridView.add_Row(brokenLinkUrl, brokenLinkText, parentNode, htmlToRemove, htmlToRemove.size());
}
}
/*"test browser".popupWindow().add_WebBrowser()
.set_Text(targetHtml)
.insert_Below().add_SourceCodeEditor()
.set_Text(targetHtml);*/
//using O2.XRules.Database.Utils
//using FluentSharp.For_HtmlAgilityPack
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
//O2Ref:tmp6759.tmp
//using SecurityInnovation.TeamMentor;
//O2Tag_SetInvocationParametersToDynamic
//O2Ref:Microsoft.CSharp.dll
var teamMentor = (API_TeamMentor_WebServices)_teamMentor;
var target = (string)_target;
var notokList = (Dictionary<string, List<Article_WS>> )_notokList;
Func<string, string,string,string> getHtmlTextToRemove =
(htmlToSearch, url, text)=>
{
var htmlDocument = htmlToSearch.htmlDocument();
var linkToRemove = (from link in htmlDocument.links()
where link.outerHtml().contains(url) &&
link.innerHtml().contains(text)
select link).first();
return linkToRemove.notNull()
? linkToRemove.outerHtml() + ":::" + linkToRemove.ParentNode.outerHtml()
: "";
};
//var popupWindow = "DataGrivView with links to remove".popupWindow();
/*var dataGridView = popupWindow.add_DataGridView()
.add_Columns("Url To remove",
"Article Title to remove",
"Html to remove (ParentNode)",
"Html to remove (Fixed)",
"Html to remove (size)");
*/
foreach(var articleToFix in notokList)
{
var articles = articleToFix.Value;
var brokenLinkUrl = articleToFix.Key.split(":::").first().trim();
var brokenLinkText = articleToFix.Key.split(":::").second().trim();
foreach(var article in articles)
{
var targetHtml = article.content();
var htmlData = getHtmlTextToRemove(targetHtml,
brokenLinkUrl,
brokenLinkText);
var htmlToReplace = "";
var htmlToReplaceWith = "";
var foundLink = htmlData.split(":::").first();
var parentNode = htmlData.split(":::").second();
var liSplit = parentNode.split("<li>");
if (liSplit.size() < 2 )
continue; // this shouldn't really happen
if (liSplit.size() == 2 ) // scenario 1 : easy to handle , just remove parentNode
{
htmlToReplace = parentNode;
htmlToReplaceWith = "";
}
else // scenario 2 : first last LI tag then remove foundLink from parentNode
{
var withoutLastLITag = parentNode.subString(0, parentNode.indexLast("</li>"));
var withoutFoundLink = withoutLastLITag.subString_After(foundLink);
htmlToReplace = parentNode;
htmlToReplaceWith = parentNode.remove(withoutFoundLink);;
//return withoutFoundLink.line().line() + parentNode;
}
"Replacing from article {0} with Title '{1}' the content '{2}' with '{3}".info(
article.Id,
article.Title,
htmlToReplace,
htmlToReplaceWith);
//check that htmlToReplace is there and htmlToReplaceWith is not there
if(targetHtml.contains(htmlToReplace).isFalse() ||targetHtml.contains(htmlToReplaceWith).isTrue() && htmlToReplaceWith.valid())
return "Error on {0} with {1}".format(targetHtml , htmlToReplace);
// finally do the replacement
}
}
return "All Good";
/*"test browser".popupWindow().add_WebBrowser()
.set_Text(targetHtml)
.insert_Below().add_SourceCodeEditor()
.set_Text(targetHtml);*/
//using O2.XRules.Database.Utils
//using FluentSharp.For_HtmlAgilityPack
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
//O2Ref:tmp6759.tmp
//using SecurityInnovation.TeamMentor;
//O2Tag_SetInvocationParametersToDynamic
//O2Ref:Microsoft.CSharp.dll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment