MediaWiki:RecentchangesFilter.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * Recentchanges-Filter
 * 
 * Used for e.g. [[Commons:User scripts/BSIcon]]
 *
 * @rev 2 (2022-12-21)
 * @author Rainer Rillke, 2013
 */
// List the global variables for jsHint-Validation. Please make sure that it passes http://jshint.com/
// Scheme: globalVariable:allowOverwriting[, globalVariable:allowOverwriting][, globalVariable:allowOverwriting]
/*global jQuery:false, mediaWiki:false*/

// Set jsHint-options. You should not set forin or undef to false if your script does not validate.
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, curly:false, browser:true, onecase:true*/

(function($, mw) {
	'use strict';

	var RCF, msg, css = {};

	msg = {

	};

	mw.messages.set(msg);

	RCF = function() {};

	RCF.fn = RCF.prototype = {
		queryinfo: {
			rcstart: {
				type: 'timestamp'
			},
			rcend: {
				type: 'timestamp'
			},
			rcdir: {
				type: 'xlist',
				vals: ['newer', 'older'],
				def: 'older'
			},
			rcnamespace: {
				type: 'namespaces'
			},
			rcuser: {
				type: 'user'
			},
			rcexcludeuser: {
				type: 'user'
			},
			rctag: {
				type: 'text'
			},
			rcshow: {
				type: 'list',
				vals: ['minor', 'bot', 'anon', 'redirect', 'patrolled']
			},
			rclimit: {
				type: 'number',
				def: 10,
				max: 500,
				min: 1
			},
			rctype: {
				type: 'list',
				vals: ['edit', 'external', 'new', 'log']
			},
			rctoponly: {
				type: 'boolean'
			},
			rccontinue: {
				type: 'continue'
			},
			rcprop: {
				type: 'prop',
				vals: ['user', 'userid', 'comment', 'parsedcomment', 'flags', 'timestamp', 'title', 'ids', 'sizes', 'redirect', 'patrolled', 'loginfo', 'tags'],
				def: 'title|timestamp|ids'
			}
		},
		profiles: {
			'BSIcon': {
				autocontinue: 1,
				stop: 1,
				query: {
					rcnamespace: 6,
					rcshow: '!anon|!bot',
					rclimit: 500,
					rctype: 'log',
					rcprop: 'title|ids|user|timestamp|loginfo'
				},
				filter: {
					title: /^File:bsicon.*?\.svg$/i,
					revid: 0
				},
				show: {
					$ui: $('<div>'),
					$ul: $('<ul class="rcf-loglist">'),
					$preG: $('<pre>'),
					$preL: $('<pre>'),
					$getUI: function() {
						if (!css.BSIcon) {
							css.BSIcon = 1;
							mw.util.addCSS(
								'ul.rcf-loglist>li.rcf-upload { list-style-image:url(//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Go-up.svg/20px-Go-up.svg.png) } ' +
								'ul.rcf-loglist>li.rcf-delete { list-style-image: url(//upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Crystal_Project_cancel.png/16px-Crystal_Project_cancel.png)  }' +
								'ul.rcf-loglist>li.rcf-move { list-style-image: url(//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/File_move_icon.svg/23px-File_move_icon.svg.png)  }');
						}
						return this.$ui.append(this.$ul, this.$preG, this.$preL);
					},
					newEntry: function(c) {
						var $li = $('<li>').attr('class', 'rcf-' + c.logtype),
							$time = $('<span>').text(c.timestamp && c.timestamp.replace(/T|Z/g, ' ')).appendTo($li),
							$user = $('<a>').attr('href', mw.util.getUrl('User:' + c.user)).text(c.user).appendTo($li),
							$space = $('<span>').text(' - ').appendTo($li),
							$title = $('<a>').attr('href', mw.util.getUrl(c.title)).text(c.title).appendTo($li);
						this.$ul.append($li);
						this.$preG.append(c.title + '\n');
						this.$preL.append('* [[:' + c.title + ']]\n');
					}
				}
			}
		},
		profile2UI: function() {

		},
		ui2Profile: function() {

		},
		profile2query: function() {

		},
		$runProfile: function(p) {
			var $ui = $('<div>'),
				$btn,
				$def;

			if (p.stop) {
				$btn = $('<button>').text("STOP").button().addClass('ui-button-red ui-button-large').appendTo($ui).click(function() {
					$def.kill = true;
					_disable();
				});
			}
			var _disable = function() {
				if ($btn) $btn.button({
						disabled: true
					});
			};
			var _query = function() {
				var q = $.extend({
					'action': 'query',
					'list': 'recentchanges'
				}, p.query);
				mw.loader.using('ext.gadget.libAPI', function() {
					$def = mw.libs.commons.api.$autoQuery(q, {
						cache: false,
						method: 'POST'
					}, 300).progress(_resp).done(_disable).fail(_disable);
				});
			};
			var _filter = function(c) {
				var ret = true;
				$.each(p.filter, function(param, f) {
					var v = c[param];
					if (!v) return;
					switch (typeof f) {
						case 'object':
							if (!(f instanceof RegExp)) return;
							ret = ret && f.test(v);
							break;
						default:
							ret = ret && (f === v);
							break;
					}
				});
				return ret;
			};
			var _resp = function(r) {
				$.each(r.query.recentchanges, function(i, rc) {
					if (!_filter(rc)) return;
					p.show.newEntry(rc);
				});
			};

			$ui.append(p.show.$getUI());
			_query();

			return $ui;
		},
		$getUI: function() {

		},
		startUp: function() {
			var _t = this;
			mw.loader.using(['mediawiki.util', 'jquery.ui'], function() {
				$(function() {
					var pgParts = mw.config.get('wgPageName').match(/^Commons:User_scripts\/(.+)$/);
					if (pgParts && pgParts[1]) {
						if (pgParts[1] in _t.profiles) {
							$('#mw-content-text').empty().append(_t.$runProfile(_t.profiles[pgParts[1]]));
						}
					}
				});
			});
			return _t;
		}
	};

	mw.libs.RecentchangesFilter = RCF;
	window.RecentchangesFilter = new RCF().startUp();

}(jQuery, mediaWiki));