|
|
| Line 1: |
Line 1: |
| − | /**
| + | console.log("your JS loaded it didn't do anything"); |
| − | * Rollback
| |
| − | * @description Perform rollbacks without needing to be in the usergroup
| |
| − | * @author Ozank
| |
| − | */
| |
| − | (function() {
| |
| − | 'use strict';
| |
| − | // Exit if normal rollback links are present or the wiki disabled the script
| |
| − | if ($('.mw-rollback-link').length || window.RollbackWikiDisable) {
| |
| − | return;
| |
| − | }
| |
| − | var Rollback = $.extend(window.Rollback, {
| |
| − | config: mw.config.get([
| |
| − | 'wgAction',
| |
| − | 'wgCanonicalSpecialPageName',
| |
| − | 'wgPageName'
| |
| − | ]),
| |
| − | _preload: 1,
| |
| − | getPageType: function() {
| |
| − | if (
| |
| − | this.config.wgAction === 'history' &&
| |
| − | $('#pagehistory li').length > 1
| |
| − | ) {
| |
| − | return 'history';
| |
| − | } else if (
| |
| − | this.config.wgCanonicalSpecialPageName === 'Contributions'
| |
| − | ) {
| |
| − | return 'contributions';
| |
| − | } else if (
| |
| − | mw.util.getParamValue('diff') &&
| |
| − | $('#differences-nextlink').length === 0
| |
| − | ) {
| |
| − | return 'diff';
| |
| − | }
| |
| − | },
| |
| − | preload: function() {
| |
| − | if (--this._preload === 0) {
| |
| − | this.init();
| |
| − | }
| |
| − | },
| |
| − | init: function() {
| |
| − | this.api = new mw.Api();
| |
| − | this.performRollbackCallback = this.performRollbackCallback
| |
| − | .bind(this);
| |
| − | var type = this.getPageType();
| |
| − | if (type) {
| |
| − | this[type]();
| |
| − | }
| |
| − | mw.hook('quickdiff.ready').add(this.quickDiff.bind(this));
| |
| − | },
| |
| − | getLink: function(page, user) {
| |
| − | return $('<a>', {
| |
| − | 'click': this.click.bind(this),
| |
| − | 'data-id': page,
| |
| − | 'data-user': user,
| |
| − | 'href': '#',
| |
| − | 'text': 'rollback'
| |
| − | });
| |
| − | },
| |
| − | history: function() {
| |
| − | $('#pagehistory li:first .mw-history-undo a').before(
| |
| − | $('<span>', {
| |
| − | 'class': 'mw-custom-rollback-link'
| |
| − | }).append(this.getLink(
| |
| − | this.config.wgPageName,
| |
| − | $('.mw-userlink:first').text()
| |
| − | )),
| |
| − | ' | '
| |
| − | );
| |
| − | },
| |
| − | contributions: function() {
| |
| − | $('#mw-content-text ul li').each(function(_, el) {
| |
| − | // Fix context
| |
| − | var $this = $(el);
| |
| − | if ($this.find('.mw-uctop').length) {
| |
| − | $this.append(
| |
| − | ' ',
| |
| − | $('<span>', {
| |
| − | 'class': 'mw-custom-rollback-link'
| |
| − | }).append(
| |
| − | '[',
| |
| − | this.getLink(
| |
| − | $this.find('a:first').attr('title'),
| |
| − | this.config.wgPageName.split('/')[1]
| |
| − | ),
| |
| − | ']'
| |
| − | )
| |
| − | );
| |
| − | }
| |
| − | }.bind(this));
| |
| − | },
| |
| − | diff: function() {
| |
| − | $('.mw-usertoollinks:last').after(
| |
| − | ' ',
| |
| − | $('<span>', {
| |
| − | 'class': 'mw-custom-rollback-link'
| |
| − | }).append(
| |
| − | '[',
| |
| − | this.getLink(
| |
| − | this.config.wgPageName,
| |
| − | $('#mw-diff-ntitle2 .mw-userlink').text()
| |
| − | ),
| |
| − | ']'
| |
| − | )
| |
| − | );
| |
| − | },
| |
| − | quickDiff: function(modal) {
| |
| − | // See getDiffTitle from QuickDiff.
| |
| − | var prevTitle = modal.data.content
| |
| − | .find('#mw-diff-otitle1 a')
| |
| − | .attr('title'),
| |
| − | currTitle = modal.data.content
| |
| − | .find('#mw-diff-ntitle1 a')
| |
| − | .attr('title');
| |
| − | if (prevTitle !== currTitle) {
| |
| − | // This is a Special:ComparePages diff.
| |
| − | return;
| |
| − | }
| |
| − | modal.$content.find('.mw-usertoollinks:last').after(
| |
| − | ' ',
| |
| − | $('<span>', {
| |
| − | 'class': 'mw-custom-rollback-link'
| |
| − | }).append(
| |
| − | '[',
| |
| − | this.getLink(
| |
| − | currTitle,
| |
| − | modal.$content
| |
| − | .find('#mw-diff-ntitle2 .mw-userlink')
| |
| − | .text()
| |
| − | ),
| |
| − | ']'
| |
| − | )
| |
| − | );
| |
| − | },
| |
| − | click: function(event) {
| |
| − | event.preventDefault();
| |
| − | if (this.confirm && !confirm('confirm pls')) {
| |
| − | return;
| |
| − | }
| |
| − | var $this = $(event.target);
| |
| − | this.getRevisionIdAndContent(
| |
| − | $this.data('id'),
| |
| − | $this.data('user').replace(/_/g, ' ')
| |
| − | );
| |
| − | $this.parent().remove();
| |
| − | },
| |
| − | getRevisionIdAndContent: function(title, target) {
| |
| − | this.api.get({
| |
| − | action: 'query',
| |
| − | cb: Date.now(),
| |
| − | indexpageids: 1,
| |
| − | prop: 'revisions',
| |
| − | rvlimit: 'max',
| |
| − | rvprop: 'user|ids',
| |
| − | titles: title
| |
| − | }).done(this.getRevisionIdCallback.bind(this, target)).fail(
| |
| − | this.outputError.bind(this, 'revisionFail')
| |
| − | );
| |
| − | },
| |
| − | getRevisionIdCallback: function(target, data) {
| |
| − | if (data.error) {
| |
| − | this.outputError('revisionFail', data.error.code);
| |
| − | return;
| |
| − | }
| |
| − | var revisions = data.query.pages[data.query.pageids[0]].revisions;
| |
| − | // Don't rollback if the page has been edited by somebody else
| |
| − | if (target !== revisions[0].user) {
| |
| − | this.outputError('editConflict');
| |
| − | return;
| |
| − | }
| |
| − | var lastUser, revId;
| |
| − | for (var i in revisions) {
| |
| − | if (revisions[i].user !== target) {
| |
| − | // Remember last author
| |
| − | lastUser = revisions[i].user;
| |
| − | // Get revision to revert to
| |
| − | revId = revisions[i].revid;
| |
| − | break;
| |
| − | }
| |
| − | }
| |
| − | if (!lastUser) {
| |
| − | this.outputError('singleEditor');
| |
| − | return;
| |
| − | }
| |
| − | this.api.get({
| |
| − | action: 'query',
| |
| − | cb: Date.now(),
| |
| − | indexpageids: 1,
| |
| − | prop: 'revisions',
| |
| − | revids: revId,
| |
| − | rvprop: 'content'
| |
| − | }).done(
| |
| − | this.getRevisionContentCallback.bind(this, target, lastUser)
| |
| − | ).fail(
| |
| − | this.outputError.bind(this, 'contentFail', undefined)
| |
| − | );
| |
| − | },
| |
| − | getRevisionContentCallback: function(target, lastUser, data) {
| |
| − | if (data.error) {
| |
| − | this.outputError('contentFail', data.error.code);
| |
| − | return;
| |
| − | }
| |
| − | // Can be no content on page
| |
| − | var page = data.query.pages[data.query.pageids[0]],
| |
| − | content = page.revisions ? page.revisions[0]['*'] : '';
| |
| − | this.performRollback(page.title, content, target, lastUser);
| |
| − | },
| |
| − | performRollback: function(page, text, user, user2) {
| |
| − | this.api.post({
| |
| − | action: 'edit',
| |
| − | bot: true,
| |
| − | minor: true,
| |
| − | summary: 'rv',
| |
| − | text: text,
| |
| − | title: page,
| |
| − | token: mw.user.tokens.get('editToken')
| |
| − | }).done(this.performRollbackCallback).fail(
| |
| − | this.outputError.bind(this, 'editFail')
| |
| − | );
| |
| − | },
| |
| − | performRollbackCallback: function(data) {
| |
| − | if (data.error) {
| |
| − | this.outputError('editFail', data.error.code);
| |
| − | } else {
| |
| − | mw.notify('Reverted successfully');
| |
| − | }
| |
| − | },
| |
| − | outputError: function(message, code) {
| |
| − | mw.notify('error: ' + code, {
| |
| − | type: 'error'
| |
| − | });
| |
| − | }
| |
| − | });
| |
| − | mw.loader.using([
| |
| − | 'mediawiki.api',
| |
| − | 'mediawiki.user',
| |
| − | 'mediawiki.util',
| |
| − | 'mediawiki.notification'
| |
| − | ], Rollback.preload.bind(Rollback));
| |
| − | })();
| |