WikiLink.m

From Knot Atlas
Jump to navigationJump to search

(*

*)
(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)










BeginPackage["WikiLink`",{"JLink`"}];

SetJarPath::usage="You'll need to call this, specifying the path to the jar files needed by this package. Hopefully, they're in the same place as the package itself.";

CreateWikiConnection::usage="CreateWikiConnection[URL, username, password] initialises a connection to a mediawiki server. The URL should typically end in \"index.php\". The username and password are optional.\n";\

WikiGetPageText::usage="WikiGetPageText[pagename] returns the raw text of the specified page.";\

WikiGetPageTexts::usage="WikiGetPageTexts[{pagename1, pagename2, ...}] returns a list of pairs of the form {{pagename1, text1}, {pagename2, text2}, ...}. If this failed for some pages, the expression $Failed will appear instead of the raw wiki text.";\

WikiSetPageText::usage=
    "WikiSetPageText[pagename, text] overwrites the contents of the specificied page with the given text.\n"<>\
"WikiSetPageText[pagename, text, summary] overwrites the contents of the specificied page with the given text and notes summary in the change log.\n";\

WikiSetPageTexts::usage="WikiSetPageText[{{pagename1, text1},{pagename2,text2},...}] efficiently sets multiple pages, by first checking which texts are already up to date.\n";\

WikiUploadFile::usage="WikiUploadFile[name, description] uploads the specified file to the wiki.";

WikiUserName::usage="WikiUserName[] returns either the name of the user you are logged in as, your IP address if you're not logged in, or $Failed if something more complicated has happened!";

WikiPageMatchQ;WikiPageFreeQ;WikiStringReplace;WikiStringCases;

Begin["`Private`"];

mediawikiConnection;

SetJarPath[path_String]:=
  Module[{},
    Print[
      "Be careful here to provide the path in a format your operating system understands, with a trailing slash!"]\
;
    InstallJava[];
    AddToClassPath[path<>"commons-httpclient-3.0-rc2.jar",path<>"commons-codec-1.3.jar",
      path<>"commons-logging.jar",path<>"commons-lang-2.1.jar",
      path<>"jdom.jar", path<>"wikilink.jar"];
    ]

CreateWikiConnection[baseURL_String]:=
  (InstallJava[];
    mediawikiConnection=JavaNew["wikilink.MediawikiConnection",baseURL];)

CreateWikiConnection[baseURL_String,username_String, password_String]:=
  (InstallJava[];
    mediawikiConnection=
      JavaNew["wikilink.MediawikiConnection",baseURL, username, password];)

WikiConnectionValidQ[]:=JavaObjectQ[mediawikiConnection]

WikiGetPageText::invalid=WikiSetPageText::invalid="You must call CreateWikiConnection before using WikiGetPageText or WikiSetPageText";

WikifyName[name_String]:=StringReplace[name," "\[Rule]"_"];

WikiGetPageText[name_String]:=
  If[WikiConnectionValidQ[],mediawikiConnection@getPageText[WikifyName[name]],
    Message[WikiGetPageText::invalid]]

WikiGetPageTexts[names:{__String}]:=
  If[WikiConnectionValidQ[],
    Module[{results,wikinames=WikifyName/@names},
      results=mediawikiConnection@getPageTexts[wikinames];
      rules={#\[LeftDoubleBracket]1\[RightDoubleBracket],$Failed}\
\[RuleDelayed]{#\[LeftDoubleBracket]1\[RightDoubleBracket],#\
\[LeftDoubleBracket]2\[RightDoubleBracket]}&/@results;
      Transpose[{wikinames,Table[$Failed,{Length[wikinames]}]}]/.rules
      ]
    ]





WikiSetPageText[name_String,contents_String]:=
  WikiSetPageText[WikifyName[name],contents,""]

WikiSetPageText[name_String,contents_String, summary_String]:=
  If[WikiConnectionValidQ[],
    mediawikiConnection@setPageText[WikifyName[name], contents, summary],
    Message[WikiSetPageText::invalid]
    ]

WikiSetPageTexts[uploadPairs_List]:=
  If[WikiConnectionValidQ[],
    mediawikiConnection@
      setPageTexts[{WikifyName[#\[LeftDoubleBracket]1\[RightDoubleBracket]],#\
\[LeftDoubleBracket]2\[RightDoubleBracket]}&/@uploadPairs],
    Message[WikiSetPageText::invalid]
    ]

WikiUploadFile[name_String,description_String]:=
  If[WikiConnectionValidQ[],mediawikiConnection@uploadFile[name,description],
    Message[WikiUploadFile::invalid]]

WikiUserName[]:=Module[{text,cases},
    If[!WikiSetPageText["Who_Am_I","--~~~"],Return[$Failed]];
    text=WikiGetPageText["Who_Am_I"];
    cases=StringCases[text,"--[[User:"~~x__~~"|"~~x__~~"]]"\[RuleDelayed]x];
    If[Length[cases]\[Equal]1,
      Return[cases\[LeftDoubleBracket]1\[RightDoubleBracket]]];
    Return[$Failed];
    ]

WikiPageMatchQ[name_String,patt_]:=StringMatchQ[WikiGetPageText[name],patt]

WikiPageMatchQ[names:{__String},patt_]:=
  StringMatchQ[#\[LeftDoubleBracket]2\[RightDoubleBracket],patt]&/@
    WikiGetPageTexts[names]

WikiPageFreeQ[name_String,patt_]:=StringFreeQ[WikiGetPageText[name],patt]

WikiPageFreeQ[names:{__String},patt_]:=
  StringFreeQ[#\[LeftDoubleBracket]2\[RightDoubleBracket],patt]&/@
    WikiGetPageTexts[names]

WikiStringReplace[name_String,rules_]:=
  WikiSetPageText[name,StringReplace[WikiGetPageText[name],rules]]

WikiStringReplace[name_String,rules_,n_Integer]:=
  WikiSetPageText[name,StringReplace[WikiGetPageText[name],rules,n]]

WikiStringReplace[name_String,rules_,description_String]:=
  WikiSetPageText[name,StringReplace[WikiGetPageText[name],rules],description]

WikiStringReplace[name_String,rules_,n_Integer,description_String]:=
  WikiSetPageText[name,StringReplace[WikiGetPageText[name],rules,n],
    description]

WikiStringReplace[names:{__String},rules_]:=
  WikiSetPageTexts[{#\[LeftDoubleBracket]1\[RightDoubleBracket],
          StringReplace[#\[LeftDoubleBracket]2\[RightDoubleBracket],rules]}&/@
      WikiGetPageTexts[names]]

WikiStringReplace[names:{__String},rules_,n_Integer]:=
  WikiSetPageTexts[{#\[LeftDoubleBracket]1\[RightDoubleBracket],
          StringReplace[#\[LeftDoubleBracket]2\[RightDoubleBracket],rules,
            n]}&/@WikiGetPageTexts[names]]

WikiStringCases[name_String,rules_]:=StringCases[WikiGetPageText[name],rules]

WikiStringCases[names:{__String},
    rules_]:={#\[LeftDoubleBracket]1\[RightDoubleBracket],
        StringCases[#\[LeftDoubleBracket]2\[RightDoubleBracket],rules]}&/@
    WikiGetPageTexts[name]

End[];

EndPackage[];
(*
  • )