Skip to content

Commit

Permalink
add more mw base modules, also update existing
Browse files Browse the repository at this point in the history
Such as mw.hook, mw.html, mw.Message.

To ease maintenance, instead of modifying mediawiki.js directly to stub mw.loader, the existing implementation is left as is and the stubbing is done afterwards.
  • Loading branch information
siddharthvp committed Apr 19, 2021
1 parent 7bd8406 commit 033c790
Show file tree
Hide file tree
Showing 13 changed files with 3,149 additions and 128 deletions.
12 changes: 9 additions & 3 deletions PATCHES.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@

Last updated from commit [abc8f2aa067488673b70fe6e838af67f685f6fc9](https://github.com/wikimedia/mediawiki/blob/abc8f2aa067488673b70fe6e838af67f685f6fc9) on [wikimedia/mediawiki](https://github.com/wikimedia/mediawiki).
Last updated from commit [bdd211fb801ee056a3c89e2291cb26ca2961f701](https://github.com/wikimedia/mediawiki/blob/bdd211fb801ee056a3c89e2291cb26ca2961f701) on [wikimedia/mediawiki](https://github.com/wikimedia/mediawiki).

| File | Original file | Patches |
|--------------|---------------|----------|
| mediawiki.js | [resources/src/startup/mediawiki.js] | Stubbed the 2000-line long mw.loader as it is useless in node.js |
| mediawiki.js | [resources/src/startup/mediawiki.js] | None |
| mediawiki.base.js | [resources/src/mediawiki.base/mediawiki.base.js] | Removed the last few lines: `while ( queue[ 0 ] ) { window.RLQ.push( queue.shift() ); }`
| legacy.wikibits.js | [resources/src/mediawiki.base/legacy.wikibits.js] | None
| mediawiki.errorLogger.js | [resources/src/mediawiki.base/mediawiki.errorLogger.js] | None
| util/util.js | [resources/src/mediawiki.util/util.js] | None |
| util/jquery.accessKeyLabel.js | [resources/src/mediawiki.util/jquery.accessKeyLabel.js] | None |
| Title/Title.js| [resources/src/mediawiki.Title/Title.js] | Changed import `require( 'mediawiki.String' )` to `require( '../String' )` |
| Title/phpCharToUpper.json | [resources/src/mediawiki.Title/phpCharToUpper.json] | None |
| String.js | [resources/src/mediawiki.String.js] | None |

Extra files added: util/config.json.
Extra files added: `config.json`, `util/config.json` (In real MediaWiki, these are generated on-the-fly based on wiki configurations by the backend PHP, so there's nowhere we can copy the files from.)

[resources/src/startup/mediawiki.js]: https://github.com/wikimedia/mediawiki/blob/master/resources/src/startup/mediawiki.js
[resources/src/mediawiki.base/mediawiki.base.js]: https://github.com/wikimedia/mediawiki/blob/master/resources/src/mediawiki.base/mediawiki.base.js
[resources/src/mediawiki.base/legacy.wikibits.js]: https://github.com/wikimedia/mediawiki/blob/master/resources/src/mediawiki.base/legacy.wikibits.js
[resources/src/mediawiki.base/mediawiki.errorLogger.js]: https://github.com/wikimedia/mediawiki/blob/master/resources/src/mediawiki.base/mediawiki.errorLogger.js
[resources/src/mediawiki.util/util.js]: https://github.com/wikimedia/mediawiki/blob/master/resources/src/mediawiki.util/util.js
[resources/src/mediawiki.util/jquery.accessKeyLabel.js]: https://github.com/wikimedia/mediawiki/blob/master/resources/src/mediawiki.util/jquery.accessKeyLabel.js
[resources/src/mediawiki.Title/Title.js]: https://github.com/wikimedia/mediawiki/blob/master/resources/src/mediawiki.Title/Title.js
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Honest MediaWiki JS interface mocking in Node.js.

No unnecessary changes to the original source files. Directly copied from MediaWiki core with [bare minimum modifications](https://github.com/wikimedia-gadgets/mock-mediawiki/blob/main/PATCHES.md). Currently includes mw.config, mw.util, and mw.Title. jQuery is also included from the npm package.
No unnecessary changes to the original source files. Directly copied from MediaWiki core with [bare minimum modifications](https://github.com/wikimedia-gadgets/mock-mediawiki/blob/main/PATCHES.md). Currently includes mw.util, mw.Title, and base modules such as mw.config, mw.hook, mw.html, and mw.Message. jQuery is also included from [its npm package](npmjs.com/package/jquery).

To stay true to the original source, `mw` and `$` are made available as globals, rather than exported from the module.

Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
global.$ = global.jQuery = require("jquery");

// needed in ./lib/mediawiki
global.$VARS = {
// needed in ./mediawiki
wgLegacyJavaScriptGlobals: false,
};
require("./lib/mediawiki");
global.mw = global.mediaWiki = window.mw;

require("./mw-config-data");
require("./lib/mediawiki.base");
require("./lib/util/util");
require("./lib/Title/Title");

require("./stubs/mw.loader");

module.exports = {
setPage: function (pageName, pageId, pageIsRedirect, pageRestrictions) {
mw.config.set("wgPageName", pageName);
Expand Down
121 changes: 121 additions & 0 deletions lib/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"wgArticleId": 2,
"wgIsRedirect": false,
"wgPageName": "TestPage",
"wgNamespaceNumber": 0,
"wgCanonicalNamespace": "",
"wgRestrictionEdit": [
"autoconfirmed"
],
"wgPageContentModel": "wikitext",
"wgPageContentLanguage": "en",
"wgCategories": [],
"wgRevisionId": "234234",
"wgRelevantPageName": "TestPage",
"wgRelevantPageIsProbablyEditable": true,
"wgRelevantArticleId": 2,

"wgUserId": 234,
"wgUserGroups": [
"sysop",
"*",
"user",
"autoconfirmed"
],
"wgUserName": "Wikiuser",
"wgUserLanguage": "en",
"wgUserEditCount": 4200,
"wgUserRegistration": 1401893192000,

"wgServer": "http://localhost:8080",
"wgScript": "/index.php",
"wgArticlePath": "/index.php/$1",
"wgScriptPath": "",
"wgContentLanguage": "en",
"wgLegalTitleChars": " %!\"$&'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF",
"wgIllegalFileChars": ":/\\\\",
"wgFormattedNamespaces": {
"-2": "Media",
"-1": "Special",
"0": "",
"1": "Talk",
"2": "User",
"3": "User talk",
"4": "Wikipedia",
"5": "Wikipedia 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",
"100": "Portal",
"101": "Portal talk",
"108": "Book",
"109": "Book talk",
"118": "Draft",
"119": "Draft talk",
"446": "Education Program",
"447": "Education Program talk",
"710": "TimedText",
"711": "TimedText talk",
"828": "Module",
"829": "Module talk",
"2300": "Gadget",
"2301": "Gadget talk",
"2302": "Gadget definition",
"2303": "Gadget definition talk"
},
"wgNamespaceIds": {
"media": -2,
"special": -1,
"": 0,
"talk": 1,
"user": 2,
"user_talk": 3,
"wikipedia": 4,
"wikipedia_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,
"portal": 100,
"portal_talk": 101,
"book": 108,
"book_talk": 109,
"draft": 118,
"draft_talk": 119,
"education_program": 446,
"education_program_talk": 447,
"timedtext": 710,
"timedtext_talk": 711,
"module": 828,
"module_talk": 829,
"gadget": 2300,
"gadget_talk": 2301,
"gadget_definition": 2302,
"gadget_definition_talk": 2303,
"wp": 4,
"wt": 5,
"image": 6,
"image_talk": 7,
"project": 4,
"project_talk": 5
},
"wgContentNamespaces": [0],
"wgCaseSensitiveNamespaces": [],
"pageTriageNamespaces": [
0,
2
]
}
104 changes: 104 additions & 0 deletions lib/legacy.wikibits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* MediaWiki legacy wikibits
*/
var msg,
loadedScripts = {};

function wikiUrlencode( page ) {
return encodeURIComponent( String( page ) )
.replace( /'/g, '%27' )
.replace( /%20/g, '_' )
// wfUrlencode replacements
.replace( /%3B/g, ';' )
.replace( /%40/g, '@' )
.replace( /%24/g, '$' )
.replace( /%21/g, '!' )
.replace( /%2A/g, '*' )
.replace( /%28/g, '(' )
.replace( /%29/g, ')' )
.replace( /%2C/g, ',' )
.replace( /%2F/g, '/' )
.replace( /%7E/g, '~' )
.replace( /%3A/g, ':' );
}

/**
* @deprecated since 1.17 Use jQuery instead
*/
mw.log.deprecate( window, 'addOnloadHook', function ( fn ) {
$( function () {
fn();
} );
}, 'Use jQuery instead.' );

/**
* Wikipage import methods
*
* See https://www.mediawiki.org/wiki/ResourceLoader/Legacy_JavaScript#wikibits.js
*/

/**
* @deprecated since 1.17 Use mw.loader instead. Warnings added in 1.25.
* @param {string} url
* @return {HTMLElement} Script tag
*/
function importScriptURI( url ) {
var s;
if ( loadedScripts[ url ] ) {
return null;
}
loadedScripts[ url ] = true;
s = document.createElement( 'script' );
s.setAttribute( 'src', url );
document.head.appendChild( s );
return s;
}

function importScript( page ) {
var uri = mw.config.get( 'wgScript' ) + '?title=' + wikiUrlencode( page ) +
'&action=raw&ctype=text/javascript';
return importScriptURI( uri );
}

/**
* @deprecated since 1.17 Use mw.loader instead. Warnings added in 1.25.
* @param {string} url
* @param {string} media
* @return {HTMLElement} Link tag
*/
function importStylesheetURI( url, media ) {
var l = document.createElement( 'link' );
l.rel = 'stylesheet';
l.href = url;
if ( media ) {
l.media = media;
}
document.head.appendChild( l );
return l;
}

function importStylesheet( page ) {
var uri = mw.config.get( 'wgScript' ) + '?title=' + wikiUrlencode( page ) +
'&action=raw&ctype=text/css';
return importStylesheetURI( uri );
}

msg = 'Use mw.loader instead.';
mw.log.deprecate( window, 'loadedScripts', loadedScripts, msg );
mw.log.deprecate( window, 'importScriptURI', importScriptURI, msg );
mw.log.deprecate( window, 'importStylesheetURI', importStylesheetURI, msg );
// Not quite deprecated yet.
window.importScript = importScript;
window.importStylesheet = importStylesheet;

/**
* Replace document.write/writeln with basic html parsing that appends
* to the <body> to avoid blanking pages. Added JavaScript will not run.
*
* @deprecated since 1.26
*/
[ 'write', 'writeln' ].forEach( function ( method ) {
mw.log.deprecate( document, method, function () {
$( document.body ).append( $.parseHTML( Array.prototype.join.call( arguments, '' ) ) );
}, 'Use jQuery or mw.loader.load instead.', 'document.' + method );
} );
Loading

0 comments on commit 033c790

Please sign in to comment.