FANDOM


(This is still WIP so I won't be making a documentation soon I guess)
 
m (Update)
Line 3: Line 3:
 
* Author: KockaAdmiralac <1405223@gmail.com>
 
* Author: KockaAdmiralac <1405223@gmail.com>
 
* Description: Adds patrol links to RecentChanges
 
* Description: Adds patrol links to RecentChanges
  +
* Version: v1.1
  +
* TODO: Merge with PatrolAll
 
*/
 
*/
 
(function() {
 
(function() {
Line 17: Line 19:
 
window.PatrolRCLoaded = true;
 
window.PatrolRCLoaded = true;
   
var api, token, text;
+
var PatrolRC = {
+
preload: function() {
function init() {
+
this.api = new mw.Api();
api.get({
+
this.api.get({
action: 'query',
+
action: 'query',
list: 'recentchanges',
+
meta: 'allmessages',
rctoken: 'patrol'
+
ammessages: [
}).done(function(d) {
+
'error', // "error"
token = d.query.recentchanges[0].patroltoken;
+
'markaspatrolleddiff', // "mark as patrolled"
if (!token) {
+
'visualeditor-dialog-action-done' // "done"
  +
].join('|')
  +
}).done($.proxy(this.cbPreload, this));
  +
},
  +
cbPreload: function(d) {
  +
var msg = d.query.allmessages;
  +
this.i18n = {
  +
error: msg[0]['*'],
  +
patrol: msg[1]['*'],
  +
done: msg[2]['*']
  +
};
  +
window.ajaxCallAgain = window.ajaxCallAgain || [];
  +
window.ajaxCallAgain.push($.proxy(this.init, this));
  +
this.init();
  +
},
  +
init: function() {
  +
this.api.get({
  +
action: 'query',
  +
list: 'recentchanges',
  +
rctype: 'new|edit',
  +
rclimit: 'max',
  +
rctoken: 'patrol',
  +
rcprop: 'patrolled|ids',
  +
rcshow: '!patrolled'
  +
}).done($.proxy(this.cbList, this));
  +
},
  +
cbList: function(d) {
  +
this.changes = d.query.recentchanges;
  +
this.token = this.changes[0].patroltoken;
  +
if (!this.token) {
 
console.warn('[PatrolRC] Unable to obtain patrol token');
 
console.warn('[PatrolRC] Unable to obtain patrol token');
 
return;
 
return;
 
}
 
}
$('.rc-conntent abbr.unpatrolled').each(function() {
+
$('.rc-conntent abbr.unpatrolled').each($.proxy(this.each, this));
var $ch = $(this).parent().parent().children();
+
},
if (
+
each: function(_, el) {
$ch.length === 2 ||
+
var $ch = $(el).parent().parent().children();
$ch.first().html().trim().length === 0
+
if (
) {
+
$ch.length === 2 ||
$ch.last().append(
+
$ch.first().html().trim().length === 0
'&nbsp;(',
+
) {
$('<a>', {
+
$ch.last().append(
'class': 'PatrolRC',
+
'&nbsp;(',
text: text
+
$('<a>', {
}).click(click),
+
'class': 'PatrolRC',
')'
+
text: this.i18n.patrol
);
+
}).click($.proxy(this.click, this)),
}
+
')'
  +
);
  +
}
  +
},
  +
getLink: function($par, name) {
  +
return $par.find(
  +
'> a[href*="&' + name + '="], > a[href*="?' + name + '="]'
  +
).first().attr('href');
  +
},
  +
getRcidByCurid: function(curid) {
  +
var changes = this.changes.filter(function(el) {
  +
return el.pageid === Number(curid) && el.type === 'new';
 
});
 
});
});
+
if (changes.length === 1) {
}
+
return changes[0].rcid;
+
}
function click() {
+
},
var $link = $(this).parent()
+
click: function(e) {
.find('> a[href*="&rcid="], > a[href*="?rcid="]');
+
var $this = $(e.target),
if ($link.length === 1) {
+
$par = $this.parent(),
var url = new mw.Uri($link.attr('href')),
+
$rcid = this.getLink($par, 'rcid'),
$this = $(this);
+
$curid = this.getLink($par, 'curid'),
// console.log(url.query.rcid, token);
+
rcid;
 
$this.html($('<img>', {
 
$this.html($('<img>', {
 
src: config.stylepath + '/common/images/ajax.gif'
 
src: config.stylepath + '/common/images/ajax.gif'
 
}));
 
}));
api.post({
+
if ($rcid) {
action: 'patrol',
+
rcid = new mw.Uri($rcid).query.rcid;
rcid: url.query.rcid,
+
} else if ($curid) {
token: token
+
rcid = this.getRcidByCurid(new mw.Uri($curid).query.curid);
}).done(function(d) {
+
} else {
  +
var t = new mw.Title(new mw.Uri(
  +
$par.find('a').first().attr('href')
  +
).path.substring(6));
  +
if (t.getNamespaceId() === 1201) {
  +
rcid = this.getRcidByCurid(t.getNameText());
  +
}
  +
}
  +
if (rcid) {
  +
this.api.post({
  +
action: 'patrol',
  +
rcid: rcid,
  +
token: this.token
  +
}).done(this.cbGeneratePatrol($this));
  +
} else {
  +
$this.text(this.i18n.error);
  +
}
  +
},
  +
// Why do I think this is a good idea
  +
cbGeneratePatrol: function($el) {
  +
return $.proxy(function(d) {
 
if (d.error) {
 
if (d.error) {
$this.html('error');
+
$el.text(this.i18n.error + ': ' + d.error.code);
 
} else {
 
} else {
// TODO: Find i18n message for this
+
$el.text(this.i18n.done).css({
$this.html('done').css({
 
 
color: 'gray',
 
color: 'gray',
 
'text-decoration': 'line-through'
 
'text-decoration': 'line-through'
 
});
 
});
 
}
 
}
});
+
}, this);
} else {
 
$(this).text('error');
 
 
}
 
}
}
+
};
   
mw.loader.using('mediawiki.api').then(function() {
+
mw.loader.using('mediawiki.api').then($.proxy(PatrolRC.preload, PatrolRC));
api = new mw.Api();
 
api.get({
 
action: 'query',
 
meta: 'allmessages',
 
ammessages: 'markaspatrolleddiff'
 
}).done(function(d) {
 
text = d.query.allmessages[0]['*'];
 
init();
 
window.ajaxCallAgain = window.ajaxCallAgain || [];
 
window.ajaxCallAgain.push(init);
 
});
 
});
 
 
})();
 
})();

Revision as of 18:04, October 1, 2017

/**
 * Name:        PatrolRC
 * Author:      KockaAdmiralac <1405223@gmail.com>
 * Description: Adds patrol links to RecentChanges
 * Version:     v1.1
 * TODO:        Merge with PatrolAll
 */
(function() {
    var config = mw.config.get([
        'stylepath',
        'wgCanonicalSpecialPageName'
    ]);
    if (
        config.wgCanonicalSpecialPageName !== 'Recentchanges' ||
        window.PatrolRCLoaded
    ) {
        return;
    }
    window.PatrolRCLoaded = true;
 
    var PatrolRC = {
        preload: function() {
            this.api = new mw.Api();
            this.api.get({
                action: 'query',
                meta: 'allmessages',
                ammessages: [
                    'error',                          // "error"
                    'markaspatrolleddiff',            // "mark as patrolled"
                    'visualeditor-dialog-action-done' // "done"
                ].join('|')
            }).done($.proxy(this.cbPreload, this));
        },
        cbPreload: function(d) {
            var msg = d.query.allmessages;
            this.i18n = {
                error: msg[0]['*'],
                patrol: msg[1]['*'],
                done: msg[2]['*']
            };
            window.ajaxCallAgain = window.ajaxCallAgain || [];
            window.ajaxCallAgain.push($.proxy(this.init, this));
            this.init();
        },
        init: function() {
            this.api.get({
                action: 'query',
                list: 'recentchanges',
                rctype: 'new|edit',
                rclimit: 'max',
                rctoken: 'patrol',
                rcprop: 'patrolled|ids',
                rcshow: '!patrolled'
            }).done($.proxy(this.cbList, this));
        },
        cbList: function(d) {
            this.changes = d.query.recentchanges;
            this.token = this.changes[0].patroltoken;
            if (!this.token) {
                console.warn('[PatrolRC] Unable to obtain patrol token');
                return;
            }
            $('.rc-conntent abbr.unpatrolled').each($.proxy(this.each, this));
        },
        each: function(_, el) {
            var $ch = $(el).parent().parent().children();
            if (
                $ch.length === 2 ||
                $ch.first().html().trim().length === 0
            ) {
                $ch.last().append(
                    '&nbsp;(',
                    $('<a>', {
                        'class': 'PatrolRC',
                        text: this.i18n.patrol
                    }).click($.proxy(this.click, this)),
                    ')'
                );
            }
        },
        getLink: function($par, name) {
            return $par.find(
                '> a[href*="&' + name + '="], > a[href*="?' + name + '="]'
            ).first().attr('href');
        },
        getRcidByCurid: function(curid) {
            var changes = this.changes.filter(function(el) {
                return el.pageid === Number(curid) && el.type === 'new';
            });
            if (changes.length === 1) {
                return changes[0].rcid;
            }
        },
        click: function(e) {
            var $this = $(e.target),
                $par = $this.parent(),
                $rcid = this.getLink($par, 'rcid'),
                $curid = this.getLink($par, 'curid'),
                rcid;
            $this.html($('<img>', {
                src: config.stylepath + '/common/images/ajax.gif'
            }));
            if ($rcid) {
                rcid = new mw.Uri($rcid).query.rcid;
            } else if ($curid) {
                rcid = this.getRcidByCurid(new mw.Uri($curid).query.curid);
            } else {
                var t = new mw.Title(new mw.Uri(
                    $par.find('a').first().attr('href')
                ).path.substring(6));
                if (t.getNamespaceId() === 1201) {
                    rcid = this.getRcidByCurid(t.getNameText());
                }
            }
            if (rcid) {
                this.api.post({
                    action: 'patrol',
                    rcid: rcid,
                    token: this.token
                }).done(this.cbGeneratePatrol($this));
            } else {
                $this.text(this.i18n.error);
            }
        },
        // Why do I think this is a good idea
        cbGeneratePatrol: function($el) {
            return $.proxy(function(d) {
                if (d.error) {
                    $el.text(this.i18n.error + ': ' + d.error.code);
                } else {
                    $el.text(this.i18n.done).css({
                        color: 'gray',
                        'text-decoration': 'line-through'
                    });
                }
            }, this);
        }
    };
 
    mw.loader.using('mediawiki.api').then($.proxy(PatrolRC.preload, PatrolRC));
})();
Community content is available under CC-BY-SA unless otherwise noted.