মিডিয়াৱিকি:Gadget-CommentsInLocalTime.js

অসমীয়া ৱিকিপিডিয়াৰ পৰা

টোকা: প্ৰকাশ কৰাৰ পাছত পৰিৱৰ্তনসমূহ চাবৰ বাবে আপোনাৰ ব্ৰাউজাৰত কেশ্ব অগ্ৰাহ্য কৰিবলগীয়া হ'ব পাৰে।

  • ফায়াৰফক্স / চাফাৰি: Shift ধৰি ৰাখি Reload ক্লিক কৰক, বা Ctrl-F5 আৰু Ctrl-R ৰ ভিতৰত যিকোনো এটা ক্লিক কৰক (মেকত ⌘-R)
  • গুগল ক্ৰ'ম: Ctrl-Shift-R টিপক (মেকত ⌘-Shift-R)
  • ইণ্টাৰনে'ট এক্সপ্ল'ৰাৰ / এড্‌জ: Ctrl ধৰি ৰাখি Refresh ক্লিক কৰক, বা Ctrl-F5 টিপক
  • অপেৰা: Ctrl-F5 টিপক।
//  _________________________________________________________________________________________
// |                                                                                         |
// |                    === WARNING: GLOBAL GADGET FILE ===                                  |
// |                  Changes to this page affect many users.                                |
// | Please discuss changes on the talk page or on [[Wikipedia_talk:Gadget]] before editing. |
// |_________________________________________________________________________________________|
//
// Imported from [[User:Gary King/localize comments.js]] (since moved to [[User:Gary King/comments in local time.js]])
// This script changes timestamps such as those in comments to be relative to the local time. See [[Wikipedia:Comments in Local Time]]

if (typeof(unsafeWindow) != 'undefined')
{
    var console = unsafeWindow.console;
    var mw = unsafeWindow.mw;
}

$(commentsInLocalTime);

var language;
function commentsInLocalTime()
{
	//  Not sure why this is occuring, (possibly due to the unsafe window check above)
	// But large amount of errors with "Uncaught TypeError: Cannot read property 'config' of undefined" so exit if that's the case.
	if(!mw ||!mw.config) return;
    /*
        Language
        
        LOCALIZING THIS SCRIPT
        To localize this script, change the terms below, to the RIGHT of the colons,
        to the correct term used in that language.
        
        For example, in the French language,
        
        'Today'        :     'Today',
        
        would be
        
        'Today'        :     'Aujourd’hui',
    */
    language =
    {
        // relative terms
        'Today'        :     'আজি',
        'Yesterday'    :     'কালি',
        'Tomorrow'    :     'কালিলৈ',//needs differentiation
        
        'last'        :     'যোৱা',
        'this'        :     'বৰ্তমান',
        
        // days of the week
        'Sunday'    :     'দেওবাৰ',
        'Monday'    :     'সোমবাৰ',
        'Tuesday'    :     'মংগলবাৰ',
        'Wednesday'    :     'বুধবাৰ',
        'Thursday'    :     'বৃহস্পতিবাৰ',
        'Friday'    :     'শুক্ৰবাৰ',
        'Saturday'    :     'শনিবাৰ',
        
        // months of the year
        'January'    :     'জানুৱাৰী',
        'February'    :     'ফেব্ৰুৱাৰী',
        'March'        :     'মাৰ্চ',
        'April'        :     'এপ্ৰিল',
        'May'        :     'মে',
        'June'        :     'জুন',
        'July'        :     'জুলাই',
        'August'    :     'আগষ্ট',
        'September'    :     'ছেপ্তেম্বৰ',
        'October'    :     'অক্টোবৰ',
        'November'    :     'নৱেম্বৰ',
        'December'    :     'ডিচেম্বৰ',
        
        // difference words
        'ago'        :     'পূৰ্ব',
        'from now'    :     'এতিয়াৰ পৰা',
        
        // date phrases
        'year'        :     'বছৰ',
        'years'        :     'বছৰ',
        'month'        :     'মাহ',
        'months'    :     'মাহ',
        'day'        :     'দিন',
        'days'        :     'দিন'
    };

    /*
        Settings
    */
    if (typeof(LocalComments) == 'undefined') LocalComments = {};
    if (typeof(LocalComments.dateDifference) == 'undefined') LocalComments.dateDifference = true;
    if (typeof(LocalComments.dropDays) == 'undefined') LocalComments.dropDays = 0;
    if (typeof(LocalComments.dropMonths) == 'undefined') LocalComments.dropMonths = 0;
    if (typeof(LocalComments.dateFormat) == 'undefined')
    {
        // Deprecated: LocalizeConfig
        if (typeof(LocalizeConfig) != 'undefined' && typeof(LocalizeConfig.dateFormat) != 'undefined' && LocalizeConfig.dateFormat != '') LocalComments.dateFormat = LocalizeConfig.dateFormat;
        else LocalComments.dateFormat = 'dmy';
    }
    if (typeof(LocalComments.dayOfWeek) == 'undefined') LocalComments.dayOfWeek = true;
    if (typeof(LocalComments.timeFirst) == 'undefined') LocalComments.timeFirst = true;
    if (typeof(LocalComments.twentyFourHours) == 'undefined') LocalComments.twentyFourHours = false;
    /*
        End Settings
    */

    if (mw.config.get('wgCanonicalNamespace') == '' || mw.config.get('wgCanonicalNamespace') == 'MediaWiki' || mw.config.get('wgCanonicalNamespace') == 'Special' || mw.config.get('wgAction') == 'history') return false;
    replaceText($('#' + ((mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit') ? 'wikiPreview' : 'bodyContent')), /(\d\d):(\d\d), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/);
}

function addLeadingZero(number)
{
    if (number < 10) number = '0' + number;
    return number;
}

function adjustTime(originalTimestamp, search)
{
    var time = originalTimestamp.match(search);
    var oldHour = time[1], oldMinute = time[2], oldDay = time[3], oldMonth = time[4], oldYear = time[5];
    
    var today = new Date(), yesterday = new Date(), tomorrow = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    tomorrow.setDate(tomorrow.getDate() + 1);

    // set the date entered
    var time = new Date();
    time.setUTCFullYear(oldYear, convertMonthToNumber(oldMonth), oldDay);
    time.setUTCHours(oldHour);
    time.setUTCMinutes(oldMinute);

    // determine the time offset
    var utcOffset = -1 * time.getTimezoneOffset() / 60;
    if (utcOffset >= 0) utcOffset = '+' + utcOffset;
    else utcOffset = '−' + Math.abs(utcOffset);

    // set the date bits to output
    var year = time.getFullYear();
    var month = addLeadingZero(time.getMonth() + 1);
    var day = time.getDate();
    var hour = parseInt(time.getHours());
    var minute = addLeadingZero(time.getMinutes());

    // output am or pm depending on the date
    var ampm = '';
    if (LocalComments.twentyFourHours) hour = addLeadingZero(hour);
    else
    {
        ampm = ' am';
        if (hour > 11) ampm = ' pm';
        if (hour > 12) hour -= 12;
        if (hour == '00') hour = 12;
    }

    // return 'today' or 'yesterday' if that is the case
    if (year == today.getFullYear() && month == addLeadingZero(today.getMonth() + 1) && day == today.getDate()) var date = language['Today'];
    else if (year == yesterday.getFullYear() && month == addLeadingZero(yesterday.getMonth() + 1) && day == yesterday.getDate()) var date = language['Yesterday'];
    else if (year == tomorrow.getFullYear() && month == addLeadingZero(tomorrow.getMonth() + 1) && day == tomorrow.getDate()) var date = language['Tomorrow'];
    else
    {
        // calculate day of week
        var dayNames = new Array(language['Sunday'], language['Monday'], language['Tuesday'], language['Wednesday'], language['Thursday'], language['Friday'], language['Saturday']);
        var dayOfTheWeek = dayNames[time.getDay()];

        var descriptiveDifference = '';
        var last = '';

        if (LocalComments.dateDifference)
        {
            // calculate time difference from today
            var millisecondsAgo = today.getTime() - time.getTime();
            var daysAgo = Math.abs(Math.round(millisecondsAgo / 1000 / 60 / 60 / 24));

            var differenceWord = '', last = '';
            if (millisecondsAgo >= 0)
            {
                differenceWord = language['ago'];
                if (daysAgo <= 7) last = language['last'] + ' ';
            }
            else
            {
                differenceWord = language['from now'];
                if (daysAgo <= 7) last = language['this'] + ' ';
            }

            // This method of computing the years & months is not exact.
            // However, it's better than the previous method that used
            // 1 January + delta days. That was usually quite off because
            // it mapped the second delta month to February, which has only 28 days.
            // This method is usually not more than one day off.

            var monthsAgo = Math.floor(daysAgo / 365 * 12);        // close enough
            var totalMonthsAgo = monthsAgo;
            var yearsAgo = Math.floor(monthsAgo / 12);

            if (monthsAgo < LocalComments.dropMonths) yearsAgo = 0;
            else if (LocalComments.dropMonths > 0) monthsAgo = 0;
            else monthsAgo = monthsAgo - yearsAgo * 12;

            if (daysAgo < LocalComments.dropDays)
            {
                monthsAgo = 0;
                yearsAgo = 0;
            }
            else if (LocalComments.dropDays > 0) daysAgo = 0;
            else daysAgo = daysAgo - Math.floor(totalMonthsAgo * 365 / 12);

            var descriptiveParts = [];
            if (yearsAgo > 0)
            {
                var fmtYears = yearsAgo + ' ' + pluralize(language['year'], yearsAgo, language['years']);
                descriptiveParts.push(fmtYears);
            }
            if (monthsAgo > 0)
            {
                var fmtMonths = monthsAgo + ' ' + pluralize(language['month'], monthsAgo, language['months']);
                descriptiveParts.push(fmtMonths);
            }
            if (daysAgo > 0)
            {
                var fmtDays = daysAgo + ' ' + pluralize(language['day'], daysAgo, language['days']);
                descriptiveParts.push(fmtDays);
            }

            descriptiveDifference = ' (' + descriptiveParts.join(', ') + ' ' + differenceWord + ')';
        }

        // format the date according to user preferences
        var formattedDate = '', monthName = convertNumberToMonth(time.getMonth());

        switch (LocalComments.dateFormat.toLowerCase())
        {
            case 'dmy':
                formattedDate = day + ' ' + monthName + ' ' + year;
                break;
            case 'mdy':
                formattedDate = monthName + ' ' + day + ', ' + year;
                break;
            default:
                formattedDate = year + '-' + month + '-' + addLeadingZero(day);
        }

        var formattedDayOfTheWeek = '';
        if (LocalComments.dayOfWeek) formattedDayOfTheWeek = ', ' + last + dayOfTheWeek;
        var date = formattedDate + formattedDayOfTheWeek + descriptiveDifference;
    }

    var finalTime = hour + ':' + minute + ampm;
    if (LocalComments.timeFirst) var returnDate = finalTime + ', ' + date + ' (UTC' + utcOffset + ')';
    else var returnDate = date + ', ' + finalTime + ' (UTC' + utcOffset + ')';
    
    return [returnDate, time];
}

function convertMonthToNumber(month)
{
   var output = new Date(month + ' 1, 2001');
   return output.getMonth();
}

function convertNumberToMonth(number)
{
    var month = new Array(language['January'], language['February'], language['March'], language['April'], language['May'], language['June'], language['July'], language['August'], language['September'], language['October'], language['November'], language['December']);
    return month[number];
}

function pluralize(term, count, plural)
{
    if (plural == null) plural = term + 's';
    return (count == 1 ? term : plural);
}

function replaceText(node, search)
{
    if (!node.length) return false;
    if (node[0].nodeType == 3)
    {
        var value = node[0].nodeValue;
        var matches = value.match(search);

        if (matches != null)
        {
            for (match = 0; match < matches.length; match++)
            {
                if (afterMatch != null && length != null) var position = afterMatch.search(search) + beforeMatch.length + length;
                else var position = value.search(search);
                
                var length = matches[match].toString().length;
                var beforeMatch = value.substring(0, position);
                var afterMatch = value.substring(position + length);
                var timeArray = adjustTime(matches[match].toString(), search);
                
                var span = $('<span class="localcomments" style="font-size: 95%; white-space: nowrap;" timestamp="' + timeArray[1].getTime() + '" title="' + matches[match] + '">' + timeArray[0] + '</span>');
                
                node.replaceWith(span);
                span.before($('<span class="before-localcomments"></span>').text(beforeMatch));
                span.after($('<span class="after-localcomments"></span>').text(afterMatch));

                break;
            }
        }
    }
    else
    {
        var children = [];
        var child = node.contents().eq(0);
        
        while (child.length)
        {
            children.push(child);
            child = $(child[0].nextSibling);
        }
        
        for (var child = 0; child < children.length; child++)
        {
            replaceText(children[child], search);
        }
    }
}