Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use server time rather than client time wherever possible #741

Merged
merged 3 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/twinkleblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ Twinkle.block.callback.main = function twinkleblockcallbackMain(pageobj) {
var text = pageobj.getPageText(),
params = pageobj.getCallbackParameters(),
messageData = params.messageData,
date = new Date();
date = new Date(pageobj.getLoadTime());

var dateHeaderRegex = new RegExp('^==+\\s*(?:' + date.getUTCMonthName() + '|' + date.getUTCMonthNameAbbrev() +
')\\s+' + date.getUTCFullYear() + '\\s*==+', 'mg');
Expand Down
5 changes: 2 additions & 3 deletions modules/twinkleprod.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ Twinkle.prod.callbacks = {

// Alert if article is at least three days old, not in Category:Living people, and BLPPROD is selected
if (params.blp) {
var now = new Date().toISOString();
var timeDiff = (new Date(now) - new Date(params.creation)) / 1000 / 60 / 60 / 24; // days from milliseconds
var timeDiff = (new Date(pageobj.getLoadTime()).getTime() - new Date(params.creation).getTime()) / 1000 / 60 / 60 / 24; // days from milliseconds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Yes, much better!

var blpcheck_re = /\[\[Category:Living people\]\]/i;
if (!blpcheck_re.test(text) && timeDiff > 3) {
if (!confirm('Please note that the article is not in Category:Living people and hence may be ineligible for BLPPROD. Are you sure you want to continue? \n\nYou may wish to add the category if you proceed, unless the article is about a recently deceased person.')) {
Expand Down Expand Up @@ -371,7 +370,7 @@ Twinkle.prod.callbacks = {
}

// create monthly header if it doesn't exist already
var date = new Date();
var date = new Date(pageobj.getLoadTime());
var headerRe = new RegExp('^==+\\s*' + date.getUTCMonthName() + '\\s+' + date.getUTCFullYear() + '\\s*==+', 'm');
if (!headerRe.exec(text)) {
text += '\n\n=== ' + date.getUTCMonthName() + ' ' + date.getUTCFullYear() + ' ===';
Expand Down
2 changes: 1 addition & 1 deletion modules/twinklespeedy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ Twinkle.speedy.callbacks = {
}

// create monthly header
var date = new Date();
var date = new Date(pageobj.getLoadTime());
var headerRe = new RegExp('^==+\\s*' + date.getUTCMonthName() + '\\s+' + date.getUTCFullYear() + '\\s*==+', 'm');
if (!headerRe.exec(text)) {
appendText += '\n\n=== ' + date.getUTCMonthName() + ' ' + date.getUTCFullYear() + ' ===';
Expand Down
2 changes: 1 addition & 1 deletion modules/twinklewarn.js
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ Twinkle.warn.callbacks = {
}
}

var date = new Date();
var date = new Date(pageobj.getLoadTime());

if (params.sub_group in history) {
var temp_time = new Date(history[params.sub_group]);
Expand Down
19 changes: 11 additions & 8 deletions modules/twinklexfd.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ Twinkle.xfd.callbacks = {
wikipedia_page.load(Twinkle.xfd.callbacks.afd.discussionPage);

// Today's list
var date = new Date();
var date = new Date(pageobj.getLoadTime());
wikipedia_page = new Morebits.wiki.page('Wikipedia:Articles for deletion/Log/' + date.getUTCFullYear() + ' ' +
date.getUTCMonthName() + ' ' + date.getUTCDate(), "Adding discussion to today's list");
wikipedia_page.setFollowRedirect(true);
Expand Down Expand Up @@ -982,7 +982,7 @@ Twinkle.xfd.callbacks = {
var params = pageobj.getCallbackParameters();
var statelem = pageobj.getStatusElement();

var date = new Date();
var date = new Date(pageobj.getLoadTime());
var date_header = '===' + date.getUTCMonthName() + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear() + '===\n';
var date_header_regex = new RegExp('(===\\s*' + date.getUTCMonthName() + '\\s+' + date.getUTCDate() + ',\\s+' + date.getUTCFullYear() + '\\s*===)');
var new_data = '{{subst:mfd3|pg=' + Morebits.pageNameNorm + params.numbering + '}}';
Expand Down Expand Up @@ -1232,7 +1232,8 @@ Twinkle.xfd.callbacks = {
var query = {
'action': 'query',
'titles': mw.config.get('wgPageName'),
'redirects': true
'redirects': true,
'curtimestamp': true
};
var wikipedia_api = new Morebits.wiki.api('Finding target of redirect', query, Twinkle.xfd.callbacks.rfd.findTargetCallback(callback));
wikipedia_api.params = params;
Expand All @@ -1242,20 +1243,22 @@ Twinkle.xfd.callbacks = {
// This is a closure for the callback from the above API request, which gets the target of the redirect
findTargetCallback: function(callback) {
return function(apiobj) {
var xmlDoc = apiobj.responseXML;
var target = $(xmlDoc).find('redirects r').first().attr('to');
var $xmlDoc = $(apiobj.responseXML);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that elsewhere in xfd we don't use the jquery naming convention; this is the only one that actually uses it for more than convenience, so it's reasonable to do here.

var curtimestamp = $xmlDoc.find('api').attr('curtimestamp');
var target = $xmlDoc.find('redirects r').first().attr('to');
if (!target) {
apiobj.statelem.error('This page is currently not a redirect, aborting');
return;
}
var section = $xmlDoc.find('redirects r').first().attr('tofragment');
apiobj.params.curtimestamp = curtimestamp;
apiobj.params.target = target;
var section = $(xmlDoc).find('redirects r').first().attr('tofragment');
apiobj.params.section = section;
callback(apiobj.params);
};
},
main: function(params) {
var date = new Date();
var date = new Date(params.curtimestamp);
params.logpage = 'Wikipedia:Redirects for discussion/Log/' + date.getUTCFullYear() + ' ' + date.getUTCMonthName() + ' ' + date.getUTCDate();
params.discussionpage = params.logpage + '#' + Morebits.pageNameNorm;

Expand Down Expand Up @@ -1398,7 +1401,7 @@ Twinkle.xfd.callback.evaluate = function(e) {
}

var query, wikipedia_page, wikipedia_api, logpage, params;
var date = new Date();
var date = new Date(); // XXX: avoid use of client clock, still used by TfD, FfD and CfD
switch (type) {

case 'afd': // AFD
Expand Down
10 changes: 10 additions & 0 deletions morebits.js
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,13 @@ Morebits.wiki.page = function(pageName, currentAction) {
return ctx.pageExists;
};

/**
* @returns {string} ISO 8601 timestamp at which the page was last loaded
*/
siddharthvp marked this conversation as resolved.
Show resolved Hide resolved
this.getLoadTime = function() {
return ctx.loadTime;
};

/**
* @returns {string} the user who created the page following lookupCreation()
*/
Expand Down Expand Up @@ -2517,6 +2524,9 @@ Morebits.wiki.page = function(pageName, currentAction) {
return;
}
ctx.loadTime = $(xml).find('page').attr('starttimestamp');
// XXX: starttimestamp is present because of intoken=edit parameter in the API call.
// When replacing that with meta=tokens (#615), add the curtimestamp parameter to the API call
// and change 'starttimestamp' here to 'curtimestamp'
if (!ctx.loadTime) {
ctx.statusElement.error('Failed to retrieve start timestamp.');
ctx.onLoadFailure(this);
Expand Down