MediaWiki:Gadget-morebits.js: Difference between revisions

Content deleted Content added
Repo at ed1b903: Add basic watchlist expiry support, do away with setWatchlistFromPreferences
Repo at 3813f11: Minor jsdoc improvements; Enforce newer API error message format, defaulting to html; Teach codify to $.parseHTML in status messages; date.format: add SSS for milliseconds fragment; Getter for page's content model
Line 1,686:
* | s | Seconds |
* | ss | Seconds (padded) |
* | SSS | Milliseconds fragment, padded |
* | d | Day number of the week (Sun=0) |
* | ddd | Abbreviated day name |
Line 1,724 ⟶ 1,725:
}
 
var pad = function(num, len) {
returnlen num= <len 10|| ?2; '0'// +Up numto :length num;of 00 + 1
return ('00' + num).toString().slice(0 - len);
};
var h24 = udate.getHours(), m = udate.getMinutes(), s = udate.getSeconds(), ms = udate.getMilliseconds();
var D = udate.getDate(), M = udate.getMonth() + 1, Y = udate.getFullYear();
var h12 = h24 % 12 || 12, amOrPm = h24 >= 12 ? 'PM' : 'AM';
Line 1,734 ⟶ 1,736:
'mm': pad(m), 'm': m,
'ss': pad(s), 's': s,
'SSS': pad(ms, 3),
'dddd': udate.getDayName(), 'ddd': udate.getDayNameAbbrev(), 'd': udate.getDay(),
'DD': pad(D), 'D': D,
Line 1,747 ⟶ 1,750:
* Y{1,2}(Y{2})? matches exactly 1, 2 or 4 occurrences of 'Y'
*/
/H{1,2}|h{1,2}|m{1,2}|s{1,2}|SSS|d(d{2,3})?|D{1,2}|M{1,4}|Y{1,2}(Y{2})?|A/g,
function(match) {
return replacementMap[match];
Line 1,958 ⟶ 1,961:
this.query = query;
this.query.assert = 'user';
// Enforce newer error formats, preferring html
if (!query.errorformat || ['wikitext', 'plaintext'].indexOf(query.errorformat) === -1) {
this.query.errorformat = 'html';
}
// Explicitly use the wiki's content language to minimize confusion,
// see #1179 for discussion
this.query.uselang = 'content';
this.query.errorlang = 'uselang';
this.query.errorsuselocal = 1;
 
this.onSuccess = onSuccess;
this.onError = onError;
Line 2,046 ⟶ 2,059:
this.statusText = statusText;
this.response = this.responseXML = response;
// Limit to first error
if (this.query.format === 'json') {
this.errorCode = response.errorerrors && response.errorerrors[0].code;
if (this.query.errorformat === 'html') {
this.errorText = response.errorerrors && response.errorerrors[0].infohtml;
} else if (this.query.errorformat === 'wikitext' || this.query.errorformat === 'plaintext') {
this.errorText = $(response).find('error')errors && response.errors[0].attr('info')text;
}
} else {
this.errorCode = $(response).find('errors error').eq(0).attr('code');
// Sufficient for html, wikitext, or plaintext errorformats
this.errorText = $(response).find('error').attr('info');
this.errorText = $(response).find('errors error').eq(0).text();
}
 
Line 2,303 ⟶ 2,322:
lastEditTime: null,
pageID: null,
contentModel: null,
revertCurID: null,
revertUser: null,
Line 2,938 ⟶ 2,958:
this.getPageID = function() {
return ctx.pageID;
};
 
/**
* @returns {string} - Content model of the page. Possible values
* include (but may not be limited to): `wikitext`, `javascript`,
* `css`, `json`, `Scribunto`, `sanitized-css`, `MassMessageListContent`.
* Also gettable via `mw.config.get('wgPageContentModel')`.
*/
this.getContentModel = function() {
return ctx.contentModel;
};
 
Line 3,371 ⟶ 3,401:
return;
}
 
ctx.contentModel = $(xml).find('page').attr('contentmodel');
 
// extract protection info, to alert admins when they are about to edit a protected page
Line 4,203 ⟶ 4,235:
* @param {string} text - Wikitext containing a template.
* @param {number} [start=0] - Index noting where in the text the template begins.
* @returns {object} `{name: templateName, parameters: {key: value}}`.FIXME TODO
*/
Morebits.wikitext.parseTemplate = function(text, start) {
Line 4,630 ⟶ 4,662:
Morebits.status.prototype = {
stat: null,
statRaw: null,
text: null,
textRaw: null,
Line 4,654 ⟶ 4,687:
 
/**
* Create a document fragment with the status text, parsing as HTML.
* Runs upon construction for text (part before colon) and upon
* render/update for status (part after colon).
*
* @param {(string|Element|Array)} obj
Line 4,666 ⟶ 4,701:
result = document.createDocumentFragment();
for (var i = 0; i < obj.length; ++i) {
if (typeof obj[i] ===instanceof 'string'Element) {
result.appendChild(document.createTextNode(obj[i]));
} else if (obj[i] instanceof Element) {
result.appendChild(obj[i]);
} else {
} // Else cosmic radiation made something shit
$.parseHTML(obj[i]).forEach(function(elem) {
result.appendChild(document.createTextNode(obj[i])elem);
});
}
}
return result;
Line 4,684 ⟶ 4,721:
*/
update: function(status, type) {
this.statRaw = status;
this.stat = this.codify(status);
if (type) {
Line 4,697 ⟶ 4,735:
 
// also log error messages in the browser console
console.error(this.textRaw + ': ' + statusthis.statRaw); // eslint-disable-line no-console
}
}
Line 4,867 ⟶ 4,905:
* for each.
*
* `setPageList(wikitextpageList)`: Sets the list of pages to work on. It should be an
* array of page names (strings).
*
* `setOption(optionName, optionValue)`: Sets a known option: