wiki-monkey icon indicating copy to clipboard operation
wiki-monkey copied to clipboard

Use global-context MediaWiki variables?

Open lahwaacz opened this issue 10 years ago • 2 comments

I just remembered that wikEdDiff relies on some variables being set by MediaWiki, which actually works even with Greasemonkey (i.e. it is not specific to MediaWiki's userscripts). See the code: http://en.wikipedia.org/wiki/User:Cacycle/wikEdDiff.js (look for wgServer)

I have no idea how exactly this works, but there seem to be some interesting things, some of which I think you're currently getting via API. The following snippet is loaded on all ArchWiki pages:

mw.config.set({
    "wgLoadScript":"/load.php",
    "debug":false,
    "skin":"archlinux",
    "stylepath":"/skins",
    "wgUrlProtocols":"http\\:\\/\\/|https\\:\\/\\/|ftp\\:\\/\\/|ftps\\:\\/\\/|ssh\\:\\/\\/|sftp\\:\\/\\/|irc\\:\\/\\/|ircs\\:\\/\\/|xmpp\\:|sip\\:|sips\\:|gopher\\:\\/\\/|telnet\\:\\/\\/|nntp\\:\\/\\/|worldwind\\:\\/\\/|mailto\\:|tel\\:|sms\\:|news\\:|svn\\:\\/\\/|git\\:\\/\\/|mms\\:\\/\\/|bitcoin\\:|magnet\\:|urn\\:|geo\\:|\\/\\/",
    "wgArticlePath":"/index.php/$1",
    "wgScriptPath":"",
    "wgScriptExtension":".php",
    "wgScript":"/index.php",
    "wgVariantArticlePath":false,
    "wgActionPaths":{},
    "wgServer":"https://wiki.archlinux.org",
    "wgUserLanguage":"en",
    "wgContentLanguage":"en",
    "wgVersion":"1.22.14",
    "wgEnableAPI":true,
    "wgEnableWriteAPI":true,
    "wgMainPageTitle":"Main page",
    "wgFormattedNamespaces":{"-2":"Media","-1":"Special","0":"","1":"Talk","2":"User","3":"User talk","4":"ArchWiki","5":"ArchWiki talk","6":"File","7":"File talk","8":"MediaWiki","9":"MediaWiki talk","10":"Template","11":"Template talk","12":"Help","13":"Help talk","14":"Category","15":"Category talk"},
    "wgNamespaceIds":{"media":-2,"special":-1,"":0,"talk":1,"user":2,"user_talk":3,"archwiki":4,"archwiki_talk":5,"file":6,"file_talk":7,"mediawiki":8,"mediawiki_talk":9,"template":10,"template_talk":11,"help":12,"help_talk":13,"category":14,"category_talk":15,"image":6,"image_talk":7,"project":4,"project_talk":5},
    "wgSiteName":"ArchWiki",
    "wgFileExtensions":["png","gif","jpg","jpeg"],
    "wgDBname":"archwiki",
    "wgFileCanRotate":true,
    "wgAvailableSkins":{"cologneblue":"CologneBlue","archlinux":"ArchLinux","modern":"Modern","vector":"Vector","monobook":"MonoBook"},
    "wgExtensionAssetsPath":"/extensions",
    "wgCookiePrefix":"archwiki",
    "wgResourceLoaderMaxQueryLength":-1,
    "wgCaseSensitiveNamespaces":[],
    "wgLegalTitleChars":" %!\"$\u0026'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF"
});

There might be some even more interesting things mentioned in the documentation.

lahwaacz avatar Dec 02 '14 17:12 lahwaacz

Ok, first thank you for turning my attention to that script in the head, I don't know how I've been able to miss it all this time :) However after hours of research I have a loooot to say about this:

  • It's taken me almost 3 hours to isolate the c-r-a-z-i-l-y convoluted code that WikEdDiff uses to retrieve those parameters: "basically" it's composing a script in a string, which in turn is put in a script element, which in turn is appended to the head; the script sends an event with postMessage and handles it in yet another function finally storing the returned values from the script; what's funny is that those "wg_" values are only an *attempt_ to emulate the real MW variables, done simply by extrapolating parts of the URL string or similar ways, which is what also Wiki Monkey is doing when possible to avoid API queries. In conclusion, WikEdDiff completely ignores that script in the head :(
  • Unfortunately we can't use the variables in that snippet as they are, probably because user scripts are disabled on our wiki; I've tested it also on Wikipedia, and I think they can't be used there either because user scripts are disabled for not-logged-in users. A lot of JS code is missing from the head to make the window.mw object work, and that's why all those scripts in the head start with if (window.mw) {. The missing scripts are found in e.g. https://projects.archlinux.org/vhosts/wiki.archlinux.org.git/tree/resources . The documentation is in http://www.mediawiki.org/wiki/ResourceLoader/Default_modules#mediaWiki.config and related.
  • But, there is a hacky way to read that snippet anyway, and I've demonstrated it with a bunch of lines of code (6a158b59eac4235578d47d637e9280ed59a85dcc) in the develop#199 branch. I don't think it's more hacky than extracting those values from the URL or the DOM like I'm doing now, so I may really end up switching to this new cleaner method, after some thorough testing!

kynikos avatar Dec 03 '14 10:12 kynikos

Update: user scripts are now enabled on ArchWiki

kynikos avatar Jul 27 '16 10:07 kynikos