Permalink
Browse files

couple fixes to MediaWiki APIs

  • Loading branch information...
DinisCruz committed Sep 24, 2012
1 parent 3d99c5b commit 7c46d550b6f5717e7339c7f852185cf22adc576e
@@ -1,11 +1,30 @@
<?xml version="1.0"?>
<H2>
- <SourceCode>var topPanel = O2Gui.open&lt;Panel&gt;("Tool - OWASP WIKI Diff of Latests Changes", 800,500);
-var wikiApi = new OwaspWikiAPI();
-topPanel.show_Diff_LatestChanges(wikiApi,100);
-return wikiApi;
-//O2File:OwaspWikiAPI.cs
-//O2File:O2MediaWikiAPI.cs
-//O2Ref:O2_External_IE.dll</SourceCode>
+ <SourceCode>var topPanel = O2Gui.open&lt;Panel&gt;("Tool - OWASP WIKI Diff of Latests Changes", 800,500).insert_LogViewer();
+//var topPanel = panel.add_Panel(true);
+
+//Web.Https.ignoreServerSslErrors();
+var diffOnly = false;
+var numberOfArticles = 100;
+
+Action loadData =
+ ()=&gt;{
+ var wikiApi = new OwaspWikiAPI();
+ topPanel.show_Diff_LatestPagesChanges(wikiApi,numberOfArticles,diffOnly);
+ };
+
+topPanel.insert_Above(40,"Options")
+ .add_Link("Reload Data", loadData).top(3)
+ .append_Label("Number of Diffs to Load (only the 'pages diff' will be shown)")
+ .append_TextBox(numberOfArticles.str()).onTextChange((value)=&gt;numberOfArticles = value.toInt())
+ .append_CheckBox("Only Load Diffs (vs loading the current page data)", (value)=&gt; diffOnly = value).tick();
+
+loadData();
+return "done";
+//O2File:OwaspWikiAPI.cs
+//O2File:O2MediaWikiAPI.cs
+//O2Ref:O2_External_IE.dll
+//O2Package:owasp.ico
+</SourceCode>
<ReferencedAssemblies />
</H2>
@@ -12,19 +12,19 @@
using O2.DotNetWrappers.Windows;
using O2.DotNetWrappers.Network;
using O2.DotNetWrappers.ExtensionMethods;
-using O2.External.SharpDevelop.ExtensionMethods;
+//using O2.External.SharpDevelop.ExtensionMethods;
using O2.Views.ASCX;
using O2.Views.ASCX.DataViewers;
using O2.Views.ASCX.ExtensionMethods;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
-using O2.External.IE.Wrapper;
-using O2.External.IE.ExtensionMethods;
+//using O2.External.IE.Wrapper;
+//using O2.External.IE.ExtensionMethods;
using O2.XRules.Database.Utils;
-//O2Ref:O2_External_IE.dll
+//_O2Ref:O2_External_IE.dll
//O2Ref:System.Xml.Linq.dll
//O2Ref:System.Xml.dll
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
@@ -378,7 +378,8 @@ public static string editPage(this O2MediaWikiAPI wikiApi,string page, string pa
{
var response = wikiApi.editPage(page, pageContent, "", "");
var result = response.xRoot().element("edit").attribute("result").value();
- if (result == "Failure")
+
+/* if (result == "Failure")
{
"trying to save using captcha".info();
var captchaId = response.xRoot().element("edit").element("captcha").attribute("id").value();
@@ -390,7 +391,7 @@ public static string editPage(this O2MediaWikiAPI wikiApi,string page, string pa
"MediaWiki Captcha Answer: {0}".info(captchaAnswer);
response = wikiApi.editPage(page, pageContent, captchaId, captchaAnswer.str());
return response;
- }
+ }*/
}
catch (Exception ex)
{
@@ -642,34 +643,42 @@ public static string action_query(this O2MediaWikiAPI wikiApi, string queryType,
public static List<XElement> getQueryContinueResults(this O2MediaWikiAPI wikiApi, string pages, string limitVar, int limitValue,
string properyType,string propertyName , string continueVarName , string continueValue,
string dataElement, int maxItemsToFetch, bool resolveRedirects)
- {
+ {
var results = new List<XElement>();
- var cmd = "{0}&{1}={2}".format(propertyName, limitVar, limitValue);
- if (resolveRedirects)
- cmd += "&redirects"; // to automatically resolve redirects
- if (continueValue != "")
- cmd += "&{0}={1}".format(continueVarName, continueValue);
- var data = wikiApi.action_query(properyType,cmd, pages).xRoot();
- if (data == null || data.elements("query-continue").size() == 0)
- continueValue = "";
- else
- continueValue = data.elements("query-continue").element(propertyName).attribute(continueVarName).Value;
- if (data!= null)
- results.AddRange(data.elementsAll(dataElement));
- if (maxItemsToFetch > -1 && maxItemsToFetch < results.size())
- {
- "in O2MediaWikiAPI.getQueryContinueResults, maxItemsToFetch reached ({0}), so stoping recursive fetch".debug(maxItemsToFetch);
- return results;
- }
-
- //continueValue.error();
- if (continueValue != "")
- results.AddRange(wikiApi.getQueryContinueResults(pages, limitVar, limitValue,
- properyType, propertyName,
- continueVarName, continueValue, dataElement,
- maxItemsToFetch, resolveRedirects));
- //wikiApi.templates(pages, rvlimit, continueValue)
- //);
+ try
+ {
+ var cmd = "{0}&{1}={2}".format(propertyName, limitVar, limitValue);
+ if (resolveRedirects)
+ cmd += "&redirects"; // to automatically resolve redirects
+ if (continueValue != "")
+ cmd += "&{0}={1}".format(continueVarName, continueValue);
+ var data = wikiApi.action_query(properyType,cmd, pages).xRoot();
+ if (data == null || data.elements("query-continue").size() == 0)
+ continueValue = "";
+ else
+ continueValue = data.elements("query-continue").element(propertyName).attribute(continueVarName).Value;
+ if (data!= null)
+ results.AddRange(data.elementsAll(dataElement));
+ if (maxItemsToFetch > -1 && maxItemsToFetch < results.size())
+ {
+ "in O2MediaWikiAPI.getQueryContinueResults, maxItemsToFetch reached ({0}), so stoping recursive fetch".debug(maxItemsToFetch);
+ return results;
+ }
+
+ //continueValue.error();
+ if (continueValue != "")
+ results.AddRange(wikiApi.getQueryContinueResults(pages, limitVar, limitValue,
+ properyType, propertyName,
+ continueVarName, continueValue, dataElement,
+ maxItemsToFetch, resolveRedirects));
+ //wikiApi.templates(pages, rvlimit, continueValue)
+ //);
+ }
+ catch(Exception ex)
+ {
+ ex.log("in getQueryContinueResults");
+ }
+
return results;
}
@@ -748,9 +757,9 @@ public static List<XElement> revisionsRaw(this O2MediaWikiAPI wikiApi, string pa
return wikiApi.getQueryContinueResults(page, rvlimit, propertyName, continueVarName, "", dataElement);
}
- public static string diff_Current(this O2MediaWikiAPI wikiApi, string page)
+ public static string diff_Current(this O2MediaWikiAPI wikiApi, string page, bool diffOnly)
{
- var requestData = "title={0}&diff=cur&action=render".format(page);
+ var requestData = "title={0}&diff=cur&action=render&diffonly={1}".format(page, (diffOnly) ? 1 : 0);
var diffContent = wikiApi.getIndexPhp(requestData);
return wikiApi.wrapOnHtmlPage(diffContent);
}
@@ -1159,6 +1168,38 @@ public static string getMediaHref(this O2MediaWikiAPI wikiApi, string mediaName)
}
return "";
}
+
+ public static List<Items> getDataFromPagesTemplateData(this O2MediaWikiAPI wikiApi, List<string> pages, bool addWikiTest = false)
+ {
+ var allData = new List<Items>();
+ foreach(var page in pages)
+ allData.add(wikiApi.getDataFromPageTemplateData(page, addWikiTest));
+ return allData;
+ }
+ public static Items getDataFromPageTemplateData(this O2MediaWikiAPI wikiApi, string page, bool addWikiTest = false)
+ {
+ var wikiText = wikiApi.wikiText(page);
+ var items = new Items();
+ items.add("_TARGET_PAGE", page);
+ items.add("_DATETIME_NOW", DateTime.Now.str());
+ if (addWikiTest)
+ items.add("_WIKITEXT", wikiText ?? "");
+ if (wikiText.valid())
+ {
+ var linesWithData = wikiText.lines().containing("|");
+ foreach(var line in linesWithData)
+ {
+ var indexOf = line.IndexOf("=");
+ if (indexOf > 0)
+ {
+ var name = line.subString(1,indexOf-1).trim();
+ var value = line.subString(indexOf + 1).trim();
+ items.add(name,value);
+ }
+ }
+ }
+ return items;
+ }
public static string pageUrl(this O2MediaWikiAPI wikiApi, string page)
{
@@ -1176,35 +1217,37 @@ public static WebBrowser add_WikiHelpPage(this Control control, O2MediaWikiAPI w
});
return browser;
}
+
-
- public static T show_Diff_LatestChanges<T>(this T control, O2MediaWikiAPI wikiApi)
+ public static T show_Diff_LatestPagesChanges<T>(this T control, O2MediaWikiAPI wikiApi, bool diffOnly = true)
where T : Control
{
- return control.show_Diff_LatestChanges(wikiApi,100);
+ return control.show_Diff_LatestPagesChanges(wikiApi,100, diffOnly);
}
- public static T show_Diff_LatestChanges<T>(this T control, O2MediaWikiAPI wikiApi, int itemsToShow)
+
+ public static T show_Diff_LatestPagesChanges<T>(this T control, O2MediaWikiAPI wikiApi, int itemsToShow, bool diffOnly = true)
where T : Control
{
var cancelLoad = false;
- var browser = control.add_WebBrowser();
+ var browser = control.clear().add_WebBrowser().silent(true);
var treeView = browser.insert_Left<TreeView>(200)
.showSelection()
- .afterSelect<string>((html)=>browser.set_Text(html));
+ .afterSelect<string>((html)=>O2Thread.mtaThread(()=> browser.set_Text(html)));
treeView.backColor(Color.LightPink);
- var recentPages = wikiApi.recentPages(itemsToShow);
+ var recentPages = wikiApi.recentPages(itemsToShow);
treeView.backColor(Color.Azure);
treeView.add_ContextMenu().add_MenuItem("Cancel Load", ()=>cancelLoad = true);
+
foreach(var recentPage in recentPages)
{
- var diffHtml = wikiApi.diff_Current(recentPage);
+ var diffHtml = wikiApi.diff_Current(recentPage, diffOnly);
treeView.add_Node(recentPage, diffHtml);
if (treeView.nodes().size()==1)
treeView.selectFirst();
if (cancelLoad)
break;
}
- treeView.backColor(Color.White);
+ treeView.backColor(Color.White);
return control;
}
@@ -9,11 +9,7 @@
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
-
//O2File:O2MediaWikiApi.cs
-//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll
-//O2Ref:System.Xml.Linq.dll
-//O2Ref:System.Xml.dll
namespace O2.XRules.Database.APIs
{
@@ -31,6 +27,7 @@ public OwaspWikiAPI(bool loadStylesFromWebsite)
{
this.ApiName = "OwaspWiki";
init("https://www.owasp.org/api.php");
+
this.Styles = (loadStylesFromWebsite) ? owaspStyles() : "";
}
@@ -39,20 +36,32 @@ public override string ToString()
return ApiName;
}
- //TODO: add detection to see if we are a)online and b) able to access the www.owasp.org website
-
+ //TODO: add detection to see if we are a)online and b) able to access the www.owasp.org website
// dynamically (one per session) grab the current header scripts used in OWASP
public string owaspStyles()
- {
+ {
try
{
+ "Preloading OWASP Wiki Styles".debug();
//var page = @"http://www.owasp.org/index.php?title=Special:UserLogin";
- var page = "http://www.owasp.org/index.php?title=Test&diff=cur";
+
+ var cssFiles = new [] {"https://www.owasp.org/load.php?debug=false&lang=en&modules=mediawiki.action.history.diff&only=styles&skin=vector&*" ,
+ "https://www.owasp.org/load.php?debug=false&lang=en&modules=mediawiki.legacy.commonPrint%2Cshared%7Cskins.vector&only=styles&skin=vector&*"};
+
+ var headerText = "<style>".line() +
+ cssFiles[0].url().get_Html().line() +
+ cssFiles[1].url().get_Html().line() +
+ "</style>".line();
+
+
+ return headerText;
+
+/* var page = "http://www.owasp.org/index.php?title=Test&diff=cur";
var codeToParse = page.uri().getHtml();
var htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml(codeToParse);
-
+
var headerText = "".line().line();
foreach(var node in htmlDocument.DocumentNode.SelectNodes("//link[@type='text/css']"))
{
@@ -80,7 +89,8 @@ public string owaspStyles()
AllOk = true;
return headerText.line().line();
}
- "problem retrieving owasp.org headers".error();
+
+ "problem retrieving owasp.org headers".error(); */
}
catch(Exception ex)
{
@@ -242,9 +242,49 @@ public object dynamicMethod()
return null;
}
}
-
- public static class Extra_Form
+ public static class Extra_Collections
+ {
+ public static List<T> take<T>(this IEnumerable<T> data, int count)
+ {
+ if (count == -1)
+ return data.toList();
+ return data.Take(count).toList();
+ }
+ }
+ public static class Extra_Items
{
+ public static List<string> uniqueKeys(this List<Items> itemsList)
+ {
+ return (from items in itemsList
+ from key in items.keys()
+ select key).distinct();
+ }
+
+ public static List<string> uniqueKeys_WithValidValue(this List<Items> itemsList)
+ {
+ return (from items in itemsList
+ from item in items
+ where item.Value.valid()
+ select item.Key).distinct();
+ }
+
+ public static List<string> values(this Items items, List<string> columns)
+ {
+ return (from column in columns
+ select items[column]).toList();
+ }
+
+
+ public static Dictionary<string,List<string>> indexBy_Key(this List<Items> itemsList)
+ {
+ var mappedData = new Dictionary<string, List<string>>();
+ foreach(var items in itemsList)
+ foreach(var item in items)
+ mappedData.add(item.Key, item.Value);
+ return mappedData;
+ }
+
+
}
Binary file not shown.

0 comments on commit 7c46d55

Please sign in to comment.