User:Chlod/Scripts/Deputy.js

/*!

*

* DEPUTY

*

* A copyright management and investigation assistance tool.

*

* ------------------------------------------------------------------------

*

* Copyright 2022 Chlod Aidan Alejandro

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*

* ------------------------------------------------------------------------

*

* NOTE TO USERS AND DEBUGGERS: This userscript is originally written in

* TypeScript. The original TypeScript code is converted to raw JavaScript

* during the build process. To view the original source code, visit

*

* https://github.com/ChlodAlejandro/deputy

*

* ------------------------------------------------------------------------

*

* This script compiles with the following dependencies:

* * [https://github.com/Microsoft/tslib tslib] - 0BSD, Microsoft

* * [https://github.com/jakearchibald/idb idb] - ISC, Jake Archibald

* * [https://github.com/JSmith01/broadcastchannel-polyfill broadcastchannel-polyfill] - Unlicense, Joshua Bell

* * [https://github.com/Lusito/tsx-dom tsx-dom] - MIT, Santo Pfingsten

*

*/

//

(function () {

'use strict';

/******************************************************************************

Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any

purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH

REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY

AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,

INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM

LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR

OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR

PERFORMANCE OF THIS SOFTWARE.

***************************************************************************** */

/* global Reflect, Promise, SuppressedError, Symbol, Iterator */

function __awaiter(thisArg, _arguments, P, generator) {

function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }

return new (P || (P = Promise))(function (resolve, reject) {

function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }

function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }

function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }

step((generator = generator.apply(thisArg, _arguments || [])).next());

});

}

typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {

var e = new Error(message);

return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;

};

const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);

let idbProxyableTypes;

let cursorAdvanceMethods;

// This is a function to prevent it throwing up in node environments.

function getIdbProxyableTypes() {

return (idbProxyableTypes ||

(idbProxyableTypes = [

IDBDatabase,

IDBObjectStore,

IDBIndex,

IDBCursor,

IDBTransaction,

]));

}

// This is a function to prevent it throwing up in node environments.

function getCursorAdvanceMethods() {

return (cursorAdvanceMethods ||

(cursorAdvanceMethods = [

IDBCursor.prototype.advance,

IDBCursor.prototype.continue,

IDBCursor.prototype.continuePrimaryKey,

]));

}

const transactionDoneMap = new WeakMap();

const transformCache = new WeakMap();

const reverseTransformCache = new WeakMap();

function promisifyRequest(request) {

const promise = new Promise((resolve, reject) => {

const unlisten = () => {

request.removeEventListener('success', success);

request.removeEventListener('error', error);

};

const success = () => {

resolve(wrap(request.result));

unlisten();

};

const error = () => {

reject(request.error);

unlisten();

};

request.addEventListener('success', success);

request.addEventListener('error', error);

});

// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This

// is because we create many promises from a single IDBRequest.

reverseTransformCache.set(promise, request);

return promise;

}

function cacheDonePromiseForTransaction(tx) {

// Early bail if we've already created a done promise for this transaction.

if (transactionDoneMap.has(tx))

return;

const done = new Promise((resolve, reject) => {

const unlisten = () => {

tx.removeEventListener('complete', complete);

tx.removeEventListener('error', error);

tx.removeEventListener('abort', error);

};

const complete = () => {

resolve();

unlisten();

};

const error = () => {

reject(tx.error || new DOMException('AbortError', 'AbortError'));

unlisten();

};

tx.addEventListener('complete', complete);

tx.addEventListener('error', error);

tx.addEventListener('abort', error);

});

// Cache it for later retrieval.

transactionDoneMap.set(tx, done);

}

let idbProxyTraps = {

get(target, prop, receiver) {

if (target instanceof IDBTransaction) {

// Special handling for transaction.done.

if (prop === 'done')

return transactionDoneMap.get(target);

// Make tx.store return the only store in the transaction, or undefined if there are many.

if (prop === 'store') {

return receiver.objectStoreNames[1]

? undefined

: receiver.objectStore(receiver.objectStoreNames[0]);

}

}

// Else transform whatever we get back.

return wrap(target[prop]);

},

set(target, prop, value) {

target[prop] = value;

return true;

},

has(target, prop) {

if (target instanceof IDBTransaction &&

(prop === 'done' || prop === 'store')) {

return true;

}

return prop in target;

},

};

function replaceTraps(callback) {

idbProxyTraps = callback(idbProxyTraps);

}

function wrapFunction(func) {

// Due to expected object equality (which is enforced by the caching in `wrap`), we

// only create one new func per func.

// Cursor methods are special, as the behaviour is a little more different to standard IDB. In

// IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the

// cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense

// with real promises, so each advance methods returns a new promise for the cursor object, or

// undefined if the end of the cursor has been reached.

if (getCursorAdvanceMethods().includes(func)) {

return function (...args) {

// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use

// the original object.

func.apply(unwrap(this), args);

return wrap(this.request);

};

}

return function (...args) {

// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use

// the original object.

return wrap(func.apply(unwrap(this), args));

};

}

function transformCachableValue(value) {

if (typeof value === 'function')

return wrapFunction(value);

// This doesn't return, it just creates a 'done' promise for the transaction,

// which is later returned for transaction.done (see idbObjectHandler).

if (value instanceof IDBTransaction)

cacheDonePromiseForTransaction(value);

if (instanceOfAny(value, getIdbProxyableTypes()))

return new Proxy(value, idbProxyTraps);

// Return the same value back if we're not going to transform it.

return value;

}

function wrap(value) {

// We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because

// IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.

if (value instanceof IDBRequest)

return promisifyRequest(value);

// If we've already transformed this value before, reuse the transformed value.

// This is faster, but it also provides object equality.

if (transformCache.has(value))

return transformCache.get(value);

const newValue = transformCachableValue(value);

// Not all types are transformed.

// These may be primitive types, so they can't be WeakMap keys.

if (newValue !== value) {

transformCache.set(value, newValue);

reverseTransformCache.set(newValue, value);

}

return newValue;

}

const unwrap = (value) => reverseTransformCache.get(value);

/**

* Open a database.

*

* @param name Name of the database.

* @param version Schema version.

* @param callbacks Additional callbacks.

*/

function openDB(name, version, { blocked, upgrade, blocking, terminated } = {}) {

const request = indexedDB.open(name, version);

const openPromise = wrap(request);

if (upgrade) {

request.addEventListener('upgradeneeded', (event) => {

upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);

});

}

if (blocked) {

request.addEventListener('blocked', (event) => blocked(

// Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405

event.oldVersion, event.newVersion, event));

}

openPromise

.then((db) => {

if (terminated)

db.addEventListener('close', () => terminated());

if (blocking) {

db.addEventListener('versionchange', (event) => blocking(event.oldVersion, event.newVersion, event));

}

})

.catch(() => { });

return openPromise;

}

const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];

const writeMethods = ['put', 'add', 'delete', 'clear'];

const cachedMethods = new Map();

function getMethod(target, prop) {

if (!(target instanceof IDBDatabase &&

!(prop in target) &&

typeof prop === 'string')) {

return;

}

if (cachedMethods.get(prop))

return cachedMethods.get(prop);

const targetFuncName = prop.replace(/FromIndex$/, '');

const useIndex = prop !== targetFuncName;

const isWrite = writeMethods.includes(targetFuncName);

if (

// Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.

!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||

!(isWrite || readMethods.includes(targetFuncName))) {

return;

}

const method = async function (storeName, ...args) {

// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(

const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');

let target = tx.store;

if (useIndex)

target = target.index(args.shift());

// Must reject if op rejects.

// If it's a write operation, must reject if tx.done rejects.

// Must reject with op rejection first.

// Must resolve with op value.

// Must handle both promises (no unhandled rejections)

return (await Promise.all([

target[targetFuncName](...args),

isWrite && tx.done,

]))[0];

};

cachedMethods.set(prop, method);

return method;

}

replaceTraps((oldTraps) => ({

...oldTraps,

get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),

has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),

}));

const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];

const methodMap = {};

const advanceResults = new WeakMap();

const ittrProxiedCursorToOriginalProxy = new WeakMap();

const cursorIteratorTraps = {

get(target, prop) {

if (!advanceMethodProps.includes(prop))

return target[prop];

let cachedFunc = methodMap[prop];

if (!cachedFunc) {

cachedFunc = methodMap[prop] = function (...args) {

advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));

};

}

return cachedFunc;

},

};

async function* iterate(...args) {

// tslint:disable-next-line:no-this-assignment

let cursor = this;

if (!(cursor instanceof IDBCursor)) {

cursor = await cursor.openCursor(...args);

}

if (!cursor)

return;

cursor = cursor;

const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);

ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);

// Map this double-proxy back to the original, so other cursor methods work.

reverseTransformCache.set(proxiedCursor, unwrap(cursor));

while (cursor) {

yield proxiedCursor;

// If one of the advancing methods was not called, call continue().

cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());

advanceResults.delete(proxiedCursor);

}

}

function isIteratorProp(target, prop) {

return ((prop === Symbol.asyncIterator &&

instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||

(prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));

}

replaceTraps((oldTraps) => ({

...oldTraps,

get(target, prop, receiver) {

if (isIteratorProp(target, prop))

return iterate;

return oldTraps.get(target, prop, receiver);

},

has(target, prop) {

return isIteratorProp(target, prop) || oldTraps.has(target, prop);

},

}));

var version = "0.9.0";

var gitAbbrevHash = "317b503";

var gitBranch = "HEAD";

var gitDate = "Wed, 19 Feb 2025 00:13:53 +0800";

var gitVersion = "0.9.0+g317b503";

/**

*

*/

class MwApi {

/**

* @return A mw.Api for the current wiki.

*/

static get action() {

var _a;

return (_a = this._action) !== null && _a !== void 0 ? _a : (this._action = new mw.Api({

ajax: {

headers: {

'Api-User-Agent': `Deputy/${version} (https://w.wiki/7NWR; User:Chlod; wiki@chlod.net)`

}

},

parameters: {

format: 'json',

formatversion: 2,

utf8: true,

errorformat: 'html',

errorlang: mw.config.get('wgUserLanguage'),

errorsuselocal: true

}

}));

}

/**

* @return A mw.Rest for the current wiki.

*/

static get rest() {

var _a;

return (_a = this._rest) !== null && _a !== void 0 ? _a : (this._rest = new mw.Rest());

}

}

MwApi.USER_AGENT = `Deputy/${version} (https://w.wiki/7NWR; User:Chlod; wiki@chlod.net)`;

/**

* Log to the console.

*

* @param {...any} data

*/

function log(...data) {

console.log('[Deputy]', ...data);

}

/**

* Handles all browser-stored data for Deputy.

*/

class DeputyStorage {

/**

* Initialize the Deputy IndexedDB database.

*

* @return {void} A promise that resolves when a database connection is established.

*/

init() {

return __awaiter(this, void 0, void 0, function* () {

this.db = yield openDB('us-deputy', 1, {

upgrade(db, oldVersion, newVersion) {

let currentVersion = oldVersion;

// Adding new stores? Make sure to also add it in `reset()`!

const upgrader = {

0: () => {

db.createObjectStore('keyval', {

keyPath: 'key'

});

db.createObjectStore('casePageCache', {

keyPath: 'pageID'

});

db.createObjectStore('diffCache', {

keyPath: 'revid'

});

db.createObjectStore('diffStatus', {

keyPath: 'hash'

});

db.createObjectStore('pageStatus', {

keyPath: 'hash'

});

db.createObjectStore('tagCache', {

keyPath: 'key'

});

}

};

while (currentVersion < newVersion) {

upgrader[`${currentVersion}`]();

log(`Upgraded database from ${currentVersion} to ${currentVersion + 1}`);

currentVersion++;

}

}

});

yield this.getTags();

});

}

/**

* Get a value in the `keyval` store.

*

* @param key The key to get

*/

getKV(key) {

return __awaiter(this, void 0, void 0, function* () {

return window.deputy.storage.db.get('keyval', key)

.then((keyPair) => keyPair === null || keyPair === void 0 ? void 0 : keyPair.value);

});

}

/**

* Set a value in the `keyval` store.

*

* @param key The key to set

* @param value The value to set

*/

setKV(key, value) {

return __awaiter(this, void 0, void 0, function* () {

return window.deputy.storage.db.put('keyval', {

key: key,

value: value

}).then(() => true);

});

}

/**

* Get all MediaWiki tags and store them in the `tagCache` store.

*/

getTags() {

var _a;

return __awaiter(this, void 0, void 0, function* () {

this.tagCache = {};

const tagCache = yield window.deputy.storage.db.getAll('tagCache');

if (tagCache.length === 0 ||

// 7 days

Date.now() - ((_a = yield this.getKV('tagCacheAge')) !== null && _a !== void 0 ? _a : 0) > 6048e5) {

yield MwApi.action.getMessages(['*'], {

amenableparser: true,

amincludelocal: true,

amprefix: 'tag-'

}).then((messages) => {

for (const key in messages) {

this.tagCache[key] = messages[key];

mw.messages.set(key, messages[key]);

this.db.put('tagCache', { key, value: messages[key] });

}

this.setKV('tagCacheAge', Date.now());

});

}

else {

for (const { key, value } of tagCache) {

this.tagCache[key] = value;

mw.messages.set(key, value);

}

}

});

}

/**

* Reset the Deputy database. Very dangerous!

*/

reset() {

return __awaiter(this, void 0, void 0, function* () {

yield this.db.clear('keyval');

yield this.db.clear('casePageCache');

yield this.db.clear('diffCache');

yield this.db.clear('diffStatus');

yield this.db.clear('pageStatus');

yield this.db.clear('tagCache');

});

}

}

(function(global) {

var channels = [];

function BroadcastChannel(channel) {

var $this = this;

channel = String(channel);

var id = '$BroadcastChannel$' + channel + '$';

channels[id] = channels[id] || [];

channels[id].push(this);

this._name = channel;

this._id = id;

this._closed = false;

this._mc = new MessageChannel();

this._mc.port1.start();

this._mc.port2.start();

global.addEventListener('storage', function(e) {

if (e.storageArea !== global.localStorage) return;

if (e.newValue == null || e.newValue === '') return;

if (e.key.substring(0, id.length) !== id) return;

var data = JSON.parse(e.newValue);

$this._mc.port2.postMessage(data);

});

}

BroadcastChannel.prototype = {

// BroadcastChannel API

get name() {

return this._name;

},

postMessage: function(message) {

var $this = this;

if (this._closed) {

var e = new Error();

e.name = 'InvalidStateError';

throw e;

}

var value = JSON.stringify(message);

// Broadcast to other contexts via storage events...

var key = this._id + String(Date.now()) + '$' + String(Math.random());

global.localStorage.setItem(key, value);

setTimeout(function() {

global.localStorage.removeItem(key);

}, 500);

// Broadcast to current context via ports

channels[this._id].forEach(function(bc) {

if (bc === $this) return;

bc._mc.port2.postMessage(JSON.parse(value));

});

},

close: function() {

if (this._closed) return;

this._closed = true;

this._mc.port1.close();

this._mc.port2.close();

var index = channels[this._id].indexOf(this);

channels[this._id].splice(index, 1);

},

// EventTarget API

get onmessage() {

return this._mc.port1.onmessage;

},

set onmessage(value) {

this._mc.port1.onmessage = value;

},

addEventListener: function(/*type, listener , useCapture*/) {

return this._mc.port1.addEventListener.apply(this._mc.port1, arguments);

},

removeEventListener: function(/*type, listener , useCapture*/) {

return this._mc.port1.removeEventListener.apply(this._mc.port1, arguments);

},

dispatchEvent: function(/*event*/) {

return this._mc.port1.dispatchEvent.apply(this._mc.port1, arguments);

},

};

global.BroadcastChannel = global.BroadcastChannel || BroadcastChannel;

})(self);

/**

* Generates an ID using the current time and a random number. Quick and

* dirty way to generate random IDs.

*

* @return A string in the format `TIMESTAMP++RANDOM_NUMBER`

*/

function generateId () {

return `${Date.now()}++${Math.random().toString().slice(2)}`;

}

/**

* A constant map of specific one-way Deputy message types and their respective

* response messages.

*/

const OneWayDeputyMessageMap = {

sessionRequest: 'sessionResponse',

sessionResponse: 'sessionRequest',

sessionStop: 'acknowledge',

pageStatusRequest: 'pageStatusResponse',

pageStatusResponse: 'pageStatusRequest',

pageStatusUpdate: 'acknowledge',

revisionStatusUpdate: 'acknowledge',

pageNextRevisionRequest: 'pageNextRevisionResponse',

pageNextRevisionResponse: 'pageNextRevisionRequest',

userConfigUpdate: 'userConfigUpdate',

wikiConfigUpdate: 'wikiConfigUpdate'

};

// TODO: debug

const start = Date.now();

/**

* Handles inter-tab communication and automatically broadcasts events

* to listeners.

*/

class DeputyCommunications extends EventTarget {

/**

* Initialize communications. Begins listening for messages from other tabs.

*/

init() {

// Polyfills are loaded for BroadcastChannel support on older browsers.

// eslint-disable-next-line compat/compat

this.broadcastChannel = new BroadcastChannel('deputy-itc');

this.broadcastChannel.addEventListener('message', (event) => {

// TODO: debug

log(Date.now() - start, 'comms in: ', event.data);

if (event.data && typeof event.data === 'object' && event.data._deputy) {

this.dispatchEvent(Object.assign(new Event(event.data.type), {

data: event.data

}));

}

});

}

/**

* Sends data through this broadcast channel.

*

* @param data

* @return The sent message object

*/

send(data) {

const messageId = generateId();

const message = Object.assign(data, { _deputy: true, _deputyMessageId: messageId });

this.broadcastChannel.postMessage(message);

// TODO: debug

log(Date.now() - start, 'comms out:', data);

return message;

}

/**

*

* @param original

* @param reply

*/

reply(original, reply) {

this.send(Object.assign(reply, {

_deputyRespondsTo: original._deputyMessageId

}));

}

/**

* Sends a message and waits for the first response. Subsequent responses are

* ignored. Returns `null` once the timeout has passed with no responses.

*

* @param data

* @param timeout Time to wait for a response, 500ms by default

*/

sendAndWait(data, timeout = 500) {

return __awaiter(this, void 0, void 0, function* () {

return new Promise((res) => {

const message = this.send(data);

const handlers = {};

const clearHandlers = () => {

if (handlers.listener) {

this.broadcastChannel.removeEventListener('message', handlers.listener);

}

if (handlers.timeout) {

clearTimeout(handlers.timeout);

}

};

handlers.listener = ((event) => {

log(event);

if (event.data._deputyRespondsTo === message._deputyMessageId &&

event.data.type === OneWayDeputyMessageMap[data.type]) {

res(event.data);

clearHandlers();

}

});

handlers.timeout = setTimeout(() => {

res(null);

clearHandlers();

}, timeout);

this.broadcastChannel.addEventListener('message', handlers.listener);

});

});

}

/**

* @param type The type of message to send.

* @param callback The callback to call when the message is received.

* @param options Optional options for the event listener.

* @see {@link EventTarget#addEventListener}

*/

addEventListener(type, callback, options) {

super.addEventListener(type, callback, options);

}

}

/**

* Normalizes the title into an mw.Title object based on either a given title or

* the current page.

*

* @param title The title to normalize. Default is current page.

* @return {mw.Title} A mw.Title object. `null` if not a valid title.

* @private

*/

function normalizeTitle(title) {

if (title instanceof mw.Title) {

return title;

}

else if (typeof title === 'string') {

return new mw.Title(title);

}

else if (title == null) {

// Null check goes first to avoid accessing properties of `null`.

return new mw.Title(mw.config.get('wgPageName'));

}

else if (title.title != null && title.namespace != null) {

return new mw.Title(title.title, title.namespace);

}

else {

return null;

}

}

/**

* Get the content of a page on-wiki.

*

* @param page The page to get

* @param extraOptions Extra options to pass to the request

* @param api The API object to use

* @return A promise resolving to the page content. Resolves to `null` if missing page.

*/

function getPageContent (page, extraOptions = {}, api = MwApi.action) {

return api.get(Object.assign(Object.assign(Object.assign({ action: 'query', prop: 'revisions' }, (typeof page === 'number' ? {

pageids: page

} : {

titles: normalizeTitle(page).getPrefixedText()

})), { rvprop: 'ids|content', rvslots: 'main', rvlimit: '1' }), extraOptions)).then((data) => {

const fallbackText = extraOptions.fallbacktext;

if (data.query.pages[0].revisions == null) {

if (fallbackText) {

return Object.assign(fallbackText, {

page: data.query.pages[0]

});

}

else {

return null;

}

}

return Object.assign(data.query.pages[0].revisions[0].slots.main.content, {

contentFormat: data.query.pages[0].revisions[0].slots.main.contentformat,

revid: data.query.pages[0].revisions[0].revid,

page: data.query.pages[0]

});

});

}

/**

* Used by DeputyCasePage to access the page's raw wikitext, make changes,

* etc.

*/

class DeputyCasePageWikitext {

/**

*

* @param casePage

*/

constructor(casePage) {

this.casePage = casePage;

}

/**

* Gets the wikitext for this page.

*/

getWikitext() {

var _a;

return __awaiter(this, void 0, void 0, function* () {

return (_a = this.content) !== null && _a !== void 0 ? _a : (this.content = yield getPageContent(this.casePage.pageId));

});

}

/**

* Removes the cached wikitext for this page.

*/

resetCachedWikitext() {

this.content = undefined;

}

/**

* Gets the wikitext for a specific section. The section will be parsed using the

* wikitext cache if a section title was provided. Otherwise, it will attempt to

* grab the section using API:Query for an up-to-date version.

*

* @param section The section to edit

* @param n If the section heading appears multiple times in the page and n is

* provided, this function extracts the nth occurrence of that section heading.

*/

getSectionWikitext(section, n = 1) {

return __awaiter(this, void 0, void 0, function* () {

if (typeof section === 'number') {

return getPageContent(this.casePage.pageId, { rvsection: section }).then((v) => {

return Object.assign(v.toString(), {

revid: v.revid

});

});

}

else {

const wikitext = yield this.getWikitext();

const wikitextLines = wikitext.split('\n');

let capturing = false;

let captureLevel = 0;

let currentN = 1;

const sectionLines = [];

for (let i = 0; i < wikitextLines.length; i++) {

const line = wikitextLines[i];

const headerCheck = /^(=={1,5})\s*(.+?)\s*=={1,5}$/.exec(line);

if (!capturing &&

headerCheck != null &&

headerCheck[2] === section) {

if (currentN < n) {

currentN++;

}

else {

sectionLines.push(line);

capturing = true;

captureLevel = headerCheck[1].length;

}

}

else if (capturing) {

if (headerCheck != null && headerCheck[1].length <= captureLevel) {

capturing = false;

break;

}

else {

sectionLines.push(line);

}

}

}

return Object.assign(sectionLines.join('\n'), {

revid: wikitext.revid

});

}

});

}

}

/**

* Gets the page title of a given page ID.

*

* @param pageID

*/

function getPageTitle (pageID) {

var _a, _b, _c, _d;

return __awaiter(this, void 0, void 0, function* () {

const pageIdQuery = yield MwApi.action.get({

action: 'query',

pageids: pageID

});

const title = (_d = (_c = (_b = (_a = pageIdQuery === null || pageIdQuery === void 0 ? void 0 : pageIdQuery.query) === null || _a === void 0 ? void 0 : _a.pages) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.title) !== null && _d !== void 0 ? _d : null;

return title == null ? null : normalizeTitle(title);

});

}

/**

* Base class for Deputy cases. Extended into {@link DeputyCasePage} to refer to an

* active case page. Used to represent case pages in a more serializable way.

*/

class DeputyCase {

/**

* @return the title of the case page

*/

static get rootPage() {

return window.deputy.wikiConfig.cci.rootPage.get();

}

/**

* Checks if the current page (or a supplied page) is a case page (subpage of

* the root page).

*

* @param title The title of the page to check.

* @return `true` if the page is a case page.

*/

static isCasePage(title) {

return normalizeTitle(title).getPrefixedDb()

.startsWith(this.rootPage.getPrefixedDb() + '/');

}

/**

* Gets the case name by parsing the title.

*

* @param title The title of the case page

* @return The case name, or `null` if the title was not a valid case page

*/

static getCaseName(title) {

const _title = normalizeTitle(title);

if (!this.isCasePage(_title)) {

return null;

}

else {

return _title.getPrefixedText().replace(this.rootPage.getPrefixedText() + '/', '');

}

}

/**

* Creats a Deputy case object.

*

* @param pageId The page ID of the case page.

* @param title The title of the case page.

*/

static build(pageId, title) {

return __awaiter(this, void 0, void 0, function* () {

if (title == null) {

title = yield getPageTitle(pageId);

}

return new DeputyCase(pageId, title);

});

}

/**

* @param pageId The page ID of the case page.

* @param title The title of the case page.

*/

constructor(pageId, title) {

this.pageId = pageId;

this.title = title;

}

/**

* Gets the case name by parsing the title.

*

* @return The case name, or `null` if the title was not a valid case page

*/

getCaseName() {

return DeputyCase.getCaseName(this.title);

}

}

/**

* Returns the last item of an array.

*

* @param array The array to get the last element from

* @return The last element of the array

*/

function last(array) {

return array[array.length - 1];

}

/**

* Each WikiHeadingType implies specific fields in {@link WikiHeading}:

*

* - `PARSOID` implies that there is no headline element, and that the `h`

* element is the root heading element. This means `h.innerText` will be

* "Section title".

* - `OLD` implies that there is a headline element and possibly an editsection

* element, and that the `h` is the root heading element. This means that

* `h.innerText` will be "Section title[edit | edit source]" or similar.

* - `NEW` implies that there is a headline element and possibly an editsection

* element, and that a `div` is the root heading element. This means that

* `h.innerText` will be "Section title".

*/

var WikiHeadingType;

(function (WikiHeadingType) {

WikiHeadingType[WikiHeadingType["PARSOID"] = 0] = "PARSOID";

WikiHeadingType[WikiHeadingType["OLD"] = 1] = "OLD";

WikiHeadingType[WikiHeadingType["NEW"] = 2] = "NEW";

})(WikiHeadingType || (WikiHeadingType = {}));

/**

* Get relevant information from an H* element in a section heading.

*

* @param headingElement The heading element

* @return An object containing the relevant {@link WikiHeading} fields.

*/

function getHeadingElementInfo(headingElement) {

return {

h: headingElement,

id: headingElement.id,

title: headingElement.innerText,

level: +last(headingElement.tagName)

};

}

/**

* Annoyingly, there are many different ways that a heading can be parsed

* into depending on the version and the parser used for given wikitext.

*

* In order to properly perform such wiki heading checks, we need to identify

* if a given element is part of a wiki heading, and perform a normalization

* if so.

*

* Since this function needs to check many things before deciding if a given

* HTML element is part of a section heading or not, this also acts as an

* `isWikiHeading` check.

*

* The layout for a heading differs depending on the MediaWiki version:

*

* On 1.43+ (Parser)

* ```html

*

*

Parsed wikitext...

* Parsed wikitext...

* ```

*

* On pre-1.43

* ```html

*

* Parsed wikitext...

* ...

*

* ```

*

* Worst case execution time would be if this was run with an element which was

* outside a heading and deeply nested within the page.

*

* Backwards-compatibility support may be removed in the future. This function does not

* support Parsoid specification versions lower than 2.0.

*

* @param node The node to check for

* @param ceiling An element which `node` must be in to be a valid heading.

* This is set to the `.mw-parser-output` element by default.

* @return The root heading element (can be an <h2> or <div>),

* or `null` if it is not a valid heading.

*/

function normalizeWikiHeading(node, ceiling) {

var _a;

if (node == null) {

// Not valid input, obviously.

return null;

}

const rootNode = node.getRootNode();

// Break out of text nodes until we hit an element node.

while (node.nodeType !== node.ELEMENT_NODE) {

node = node.parentNode;

if (node === rootNode) {

// We've gone too far and hit the root. This is not a wiki heading.

return null;

}

}

// node is now surely an element.

let elementNode = node;

// If this node is the 1.43+ heading root, return it immediately.

if (elementNode.classList.contains('mw-heading')) {

return Object.assign({ type: WikiHeadingType.NEW, root: elementNode }, getHeadingElementInfo(Array.from(elementNode.children)

.find(v => /^H[123456]$/.test(v.tagName))));

}

// Otherwise, we're either inside or outside a mw-heading.

// To determine if we are inside or outside, we keep climbing up until

// we either hit an or a given stop point.

// The default stop point differs on Parsoid and standard parser:

// - On Parsoid, `` will be `.mw-body-content.mw-parser-output`.

// - On standard parser, we want `div.mw-body-content > div.mw-parser.output`.

// If such an element doesn't

// exist in this document, we just stop at the root element.

ceiling = (_a = ceiling !== null && ceiling !== void 0 ? ceiling : elementNode.ownerDocument.querySelector('.mw-body-content > .mw-parser-output, .mw-body-content.mw-parser-output')) !== null && _a !== void 0 ? _a : elementNode.ownerDocument.documentElement;

// While we haven't hit a heading, keep going up.

while (elementNode !== ceiling) {

if (/^H[123456]$/.test(elementNode.tagName)) {

// This element is a heading!

// Now determine if this is a MediaWiki heading.

if (elementNode.parentElement.classList.contains('mw-heading')) {

// This element's parent is a `div.mw-heading`!

return Object.assign({ type: WikiHeadingType.NEW, root: elementNode.parentElement }, getHeadingElementInfo(elementNode));

}

else {

const headline = elementNode.querySelector(':scope > .mw-headline');

if (headline != null) {

// This element has a `.mw-headline` child!

return {

type: WikiHeadingType.OLD,

root: elementNode,

h: elementNode,

id: headline.id,

title: headline.innerText,

level: +last(elementNode.tagName)

};

}

else if (elementNode.parentElement.tagName === 'SECTION' &&

elementNode.parentElement.firstElementChild === elementNode) {

// A

element is directly above this element, and it is the

// first element of that section!

// This is a specific format followed by the 2.8.0 MediaWiki Parsoid spec.

// https://www.mediawiki.org/wiki/Specs/HTML/2.8.0#Headings_and_Sections

return {

type: WikiHeadingType.PARSOID,

root: elementNode,

h: elementNode,

id: elementNode.id,

title: elementNode.innerText,

level: +last(elementNode.tagName)

};

}

else {

// This is a heading, but we can't figure out how it works.

// This usually means something inserted an

into the DOM, and we

// accidentally picked it up.

// In that case, discard it.

return null;

}

}

}

else if (elementNode.classList.contains('mw-heading')) {

// This element is the `div.mw-heading`!

// This usually happens when we selected an element from inside the

// `span.mw-editsection` span.

return Object.assign({ type: WikiHeadingType.NEW, root: elementNode }, getHeadingElementInfo(Array.from(elementNode.children)

.find(v => /^H[123456]$/.test(v.tagName))));

}

else {

// Haven't reached the top part of a heading yet, or we are not

// in a heading. Keep climbing up the tree until we hit the ceiling.

elementNode = elementNode.parentElement;

}

}

// We hit the ceiling. This is not a wiki heading.

return null;

}

/**

* Check if a given parameter is a wikitext heading parsed into HTML.

*

* Alias for `normalizeWikiHeading( el ) != null`.

*

* @param el The element to check

* @return `true` if the element is a heading, `false` otherwise

*/

function isWikiHeading(el) {

return normalizeWikiHeading(el) != null;

}

/**

* Finds section elements from a given section heading (and optionally a predicate)

*

* @param sectionHeading

* @param sectionHeadingPredicate A function which returns `true` if the section should stop here

* @return Section headings.

*/

function getSectionElements(sectionHeading, sectionHeadingPredicate = isWikiHeading) {

const sectionMembers = [];

let nextSibling = sectionHeading.nextElementSibling;

while (nextSibling != null && !sectionHeadingPredicate(nextSibling)) {

sectionMembers.push(nextSibling);

nextSibling = nextSibling.nextElementSibling;

}

return sectionMembers;

}

/**

* Handles Deputy case pages, controls UI features, among other things.

* This class should be able to operate both on the standard MediaWiki

* parser output and the Parsoid output.

*/

class DeputyCasePage extends DeputyCase {

/**

* @param pageId The page ID of the case page.

* @param title The title of the page being accessed

* @param document The document to be used as a reference.

* @param parsoid Whether this is a Parsoid document or not.

*/

static build(pageId, title, document, parsoid) {

return __awaiter(this, void 0, void 0, function* () {

const cachedInfo = yield window.deputy.storage.db.get('casePageCache', pageId !== null && pageId !== void 0 ? pageId : window.deputy.currentPageId);

if (cachedInfo != null) {

if (pageId != null) {

// Title might be out of date. Recheck for safety.

title = yield getPageTitle(pageId);

}

// Fix for old data (moved from section name to IDs as of c5251642)

const oldSections = cachedInfo.lastActiveSections.some((v) => v.indexOf(' ') !== -1);

if (oldSections) {

cachedInfo.lastActiveSections =

cachedInfo.lastActiveSections.map((v) => v.replace(/ /g, '_'));

}

const casePage = new DeputyCasePage(pageId, title, document, parsoid, cachedInfo.lastActive, cachedInfo.lastActiveSections);

if (oldSections) {

// Save to fix the data in storage

yield casePage.saveToCache();

}

return casePage;

}

else {

return new DeputyCasePage(pageId, title, document, parsoid);

}

});

}

/**

* @param pageId The page ID of the case page.

* @param title The title of the page being accessed

* @param document The document to be used as a reference.

* @param parsoid Whether this is a Parsoid document or not.

* @param lastActive

* @param lastActiveSessions

*/

constructor(pageId, title, document, parsoid, lastActive, lastActiveSessions) {

super(pageId !== null && pageId !== void 0 ? pageId : window.deputy.currentPageId, title !== null && title !== void 0 ? title : window.deputy.currentPage);

/**

* A timestamp of when this case page was last worked on.

*/

this.lastActive = Date.now();

/**

* The sections last worked on for this case page.

*/

this.lastActiveSections = [];

this.document = document !== null && document !== void 0 ? document : window.document;

this.parsoid = parsoid !== null && parsoid !== void 0 ? parsoid : /mw: http:\/\/mediawiki.org\/rdf\//.test(this.document.documentElement.getAttribute('prefix'));

this.wikitext = new DeputyCasePageWikitext(this);

this.lastActive = lastActive !== null && lastActive !== void 0 ? lastActive : Date.now();

this.lastActiveSections = lastActiveSessions !== null && lastActiveSessions !== void 0 ? lastActiveSessions : [];

}

/**

* Checks if a given element is a valid contribution survey heading.

*

* @param el The element to check for

* @return `true` if the given heading is a valid contribution survey heading.

*/

isContributionSurveyHeading(el) {

if (!(el instanceof HTMLElement)) {

return false;

}

const heading = normalizeWikiHeading(el);

return heading != null &&

// Require that this heading is already normalized.

// TODO: Remove at some point.

// This shouldn't be required if double-normalization wasn't a thing.

el === heading.h &&

// eslint-disable-next-line security/detect-non-literal-regexp

new RegExp(window.deputy.wikiConfig.cci.headingMatch.get()).test(heading.title);

}

/**

* Finds the first contribution survey heading. This is always an element

* with the content matching the pattern "Pages \d+ to \d+"

*

* @return The element of the heading.

*/

findFirstContributionSurveyHeadingElement() {

return this.findContributionSurveyHeadings()[0];

}

/**

* Find a contribution survey heading by section name.

*

* @param sectionIdentifier The section identifier to look for, usually the section

* name unless `useId` is set to true.

* @param useId Whether to use the section name instead of the ID

* @return The element of the heading.

*/

findContributionSurveyHeading(sectionIdentifier, useId = false) {

return this.findContributionSurveyHeadings()

.find((v) => normalizeWikiHeading(v)[useId ? 'id' : 'title'] === sectionIdentifier);

}

/**

* Finds all contribution survey headings. These are elements

* with the content matching the pattern "Pages \d+ to \d+"

*

* @return The element of the heading.

*/

findContributionSurveyHeadings() {

if (!DeputyCasePage.isCasePage()) {

throw new Error('Current page is not a case page. Expected subpage of ' +

DeputyCasePage.rootPage.getPrefixedText());

}

else {

return Array.from(this.document.querySelectorAll(

// All headings (`h1, h2, h3, h4, h5, h6`)

[1, 2, 3, 4, 5, 6]

.map((i) => `.mw-parser-output h${i}`)

.join(',')))

.filter((h) => this.isContributionSurveyHeading(h));

}

}

/**

* Gets all elements that are part of a contribution survey "section", that is

* a set of elements including the section heading and all elements succeeding

* the heading until (and exclusive of) the heading of the next section.

*

* In other words,

* YES: === Pages 1 to 2 ===

* YES: * Page 1

* YES: * Page 2

* YES:

* NO : === Pages 3 to 4 ===

*

* @param sectionHeading The section heading to work with

* @return An array of all HTMLElements covered by the section

*/

getContributionSurveySection(sectionHeading) {

const heading = normalizeWikiHeading(sectionHeading);

const ceiling = heading.root.parentElement;

return getSectionElements(heading.root, (el) => {

var _a, _b;

// TODO: Avoid double normalization

const norm = normalizeWikiHeading(el, ceiling);

return (heading.level >= ((_a = norm === null || norm === void 0 ? void 0 : norm.level) !== null && _a !== void 0 ? _a : Infinity)) ||

this.isContributionSurveyHeading((_b = norm === null || norm === void 0 ? void 0 : norm.h) !== null && _b !== void 0 ? _b : el);

});

}

/**

* Check if this page is cached.

*/

isCached() {

return __awaiter(this, void 0, void 0, function* () {

return (yield window.deputy.storage.db.get('casePageCache', this.pageId)) != null;

});

}

/**

* Saves the current page to the IDB page cache.

*/

saveToCache() {

return __awaiter(this, void 0, void 0, function* () {

yield window.deputy.storage.db.put('casePageCache', {

pageID: this.pageId,

lastActive: this.lastActive,

lastActiveSections: this.lastActiveSections

});

});

}

/**

* Deletes the current page from the cache. This is generally not advised, unless the

* user wishes to forget the case page entirely.

*/

deleteFromCache() {

return __awaiter(this, void 0, void 0, function* () {

yield window.deputy.storage.db.delete('casePageCache', this.pageId);

});

}

/**

* Bumps this page's last active timestamp.

*/

bumpActive() {

return __awaiter(this, void 0, void 0, function* () {

this.lastActive = Date.now();

yield this.saveToCache();

});

}

/**

* Add a section to the list of active sessions. This is used for automatic starting

* and for one-click continuation of past active sessions.

*

* @param sectionId The ID of the section to add.

*/

addActiveSection(sectionId) {

return __awaiter(this, void 0, void 0, function* () {

const lastActiveSection = this.lastActiveSections.indexOf(sectionId);

if (lastActiveSection === -1) {

this.lastActiveSections.push(sectionId);

yield this.saveToCache();

}

});

}

/**

* Remove a section from the list of active sections. This will disable autostart

* for this section.

*

* @param sectionId ID of the section to remove

*/

removeActiveSection(sectionId) {

return __awaiter(this, void 0, void 0, function* () {

const lastActiveSection = this.lastActiveSections.indexOf(sectionId);

if (lastActiveSection !== -1) {

this.lastActiveSections.splice(lastActiveSection, 1);

yield this.saveToCache();

}

});

}

}

var dist = {};

/* eslint-disable @typescript-eslint/no-unused-vars */

/**

* License: MIT

* @author Santo Pfingsten

* @see https://github.com/Lusito/tsx-dom

*/

Object.defineProperty(dist, "__esModule", { value: true });

var h_1 = dist.h = void 0;

function applyChild(element, child) {

if (child instanceof Element)

element.appendChild(child);

else if (typeof child === "string" || typeof child === "number")

element.appendChild(document.createTextNode(child.toString()));

else

console.warn("Unknown type to append: ", child);

}

function applyChildren(element, children) {

for (const child of children) {

if (!child && child !== 0)

continue;

if (Array.isArray(child))

applyChildren(element, child);

else

applyChild(element, child);

}

}

function transferKnownProperties(source, target) {

for (const key of Object.keys(source)) {

if (key in target)

target[key] = source[key];

}

}

function createElement(tag, attrs) {

const options = (attrs === null || attrs === void 0 ? void 0 : attrs.is) ? { is: attrs.is } : undefined;

if (attrs === null || attrs === void 0 ? void 0 : attrs.xmlns)

return document.createElementNS(attrs.xmlns, tag, options);

return document.createElement(tag, options);

}

function h(tag, attrs, ...children) {

if (typeof tag === "function")

return tag(Object.assign(Object.assign({}, attrs), { children }));

const element = createElement(tag, attrs);

if (attrs) {

for (const name of Object.keys(attrs)) {

// Ignore some debug props that might be added by bundlers

if (name === "__source" || name === "__self" || name === "is" || name === "xmlns")

continue;

const value = attrs[name];

if (name.startsWith("on")) {

const finalName = name.replace(/Capture$/, "");

const useCapture = name !== finalName;

const eventName = finalName.toLowerCase().substring(2);

element.addEventListener(eventName, value, useCapture);

}

else if (name === "style" && typeof value !== "string") {

// Special handler for style with a value of type CSSStyleDeclaration

transferKnownProperties(value, element.style);

}

else if (name === "dangerouslySetInnerHTML")

element.innerHTML = value;

else if (value === true)

element.setAttribute(name, name);

else if (value || value === 0)

element.setAttribute(name, value.toString());

}

}

applyChildren(element, children);

return element;

}

h_1 = dist.h = h;

/**

* The CCI session start link. Starts a CCI session when pressed.

*

* @param heading The heading to use as a basis

* @param casePage If a DeputyCasePage is provided, a "continue" button will be shown instead.

* @return The link element to be displayed

*/

function DeputyCCISessionStartLink (heading, casePage) {

return h_1("span", { class: "deputy dp-sessionStarter" },

h_1("span", { class: "dp-sessionStarter-bracket" }, "["),

h_1("a", { onClick: () => __awaiter(this, void 0, void 0, function* () {

if (casePage && casePage.lastActiveSections.length > 0) {

const headingId = heading.id;

if (window.deputy.config.cci.openOldOnContinue.get()) {

if (casePage.lastActiveSections.indexOf(headingId) === -1) {

yield casePage.addActiveSection(headingId);

}

yield window.deputy.session.DeputyRootSession.continueSession(casePage);

}

else {

yield window.deputy.session.DeputyRootSession.continueSession(casePage, [headingId]);

}

}

else {

yield window.deputy.session.DeputyRootSession.startSession(heading.h);

}

}) }, mw.message(casePage && casePage.lastActiveSections.length > 0 ?

'deputy.session.continue' :

'deputy.session.start').text()),

h_1("span", { class: "dp-sessionStarter-bracket" }, "]"));

}

/**

* Removes an element from its document.

*

* @param element

* @return The removed element

*/

function removeElement (element) {

var _a;

return (_a = element === null || element === void 0 ? void 0 : element.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(element);

}

/**

* Log errors to the console.

*

* @param {...any} data

*/

function error(...data) {

console.error('[Deputy]', ...data);

}

/**

* Unwraps an OOUI widget from its JQuery `$element` variable and returns it as an

* HTML element.

*

* @param el The widget to unwrap.

* @return The unwrapped widget.

*/

function unwrapWidget (el) {

if (el.$element == null) {

error(el);

throw new Error('Element is not an OOUI Element!');

}

return el.$element[0];

}

/**

* Creates the "Start working on section" overlay over existing contribution survey

* sections that are not upgraded.

*

* @param props

* @param props.casePage

* @param props.heading

* @param props.height

* @return HTML element

*/

function DeputyCCISessionAddSection (props) {

const { casePage, heading } = props;

const startButton = new OO.ui.ButtonWidget({

classes: ['dp-cs-section-addButton'],

icon: 'play',

label: mw.msg('deputy.session.add'),

flags: ['primary', 'progressive']

});

const element = h_1("div", { style: { height: props.height + 'px' }, class: "dp-cs-section-add" }, unwrapWidget(startButton));

startButton.on('click', () => {

// This element is automatically appended to the UL of the section, which is a no-no

// for ContributionSurveySection. This sneakily removes this element before any sort

// of activation is performed.

removeElement(element);

window.deputy.session.rootSession.activateSection(casePage, heading);

});

return element;

}

/**

* Clones a regular expression.

*

* @param regex The regular expression to clone.

* @param options

* @return A new regular expression object.

*/

function cloneRegex$1 (regex, options = {}) {

return new RegExp(options.transformer ? options.transformer(regex.source) :

`${options.pre || }${regex.source}${options.post || }`, regex.flags);

}

/**

* Contains information about a specific revision in a ContributionSurveyRow.

*/

class ContributionSurveyRevision {

/**

* Creates a new ContributionSurveyRowRevision

*

* @param row

* @param revisionData

*/

constructor(row, revisionData) {

Object.assign(this, revisionData);

this.row = row;

}

}

/**

* Data that constructs a raw contribution survey row.

*/

/**

* Parser for {@link ContributionSurveyRow}s.

*

* This is used directly in unit tests. Do not import unnecessary

* dependencies, as they may indirectly import the entire Deputy

* codebase outside a browser environment.

*/

class ContributionSurveyRowParser {

/**

*

* @param wikitext

*/

constructor(wikitext) {

this.wikitext = wikitext;

this.current = wikitext;

}

/**

* Parses a wikitext contribution survey row into a {@link RawContributionSurveyRow}.

* If invalid, an Error is thrown with relevant information.

*

* @return Components of a parsed contribution survey row.

*/

parse() {

var _a, _b;

this.current = this.wikitext;

const bullet = this.eatUntil(/^[^*\s]/g);

if (!bullet) {

throw new Error('dp-malformed-row');

}

const creation = this.eatExpression(/^\s*N\s*/g) != null;

const page = this.eatExpression(/\[\[([^\]|]+)(?:\|.*)?]]/g, 1);

if (!page) {

// Malformed or unparsable listing.

throw new Error('dp-undetectable-page-name');

}

let extras =

// 6789

(_a = this.eatUntil(/^(?:'''?)?\[\[Special:Diff\/\d+/, true)) !== null && _a !== void 0 ? _a :

// {{dif|12345|6789}}

this.eatUntil(/^(?:'''?)?{{dif\|\d+/, true);

let diffsBolded = false;

// At this point, `extras` is either a string or `null`. If it's a string,

// extras exist, and we should add them. If not, there's likely no more

// revisions to be processed here, and can assume that the rest is user comments.

const revids = [];

const revidText = {};

let diffs = null, comments, diffTemplate = '($2)';

if (extras) {

const starting = this.current;

let diff = true;

while (diff) {

const diffMatch =

// 6789

(_b = this.eatExpressionMatch(/\s*(?:?)?\[\[Special:Diff\/(\d+)(?:\|([^\]]*))?]](?:?)?/g)) !== null && _b !== void 0 ? _b :

// {{dif|12345|6789}}

this.eatExpressionMatch(/\s*(?:?)?{{dif\|(\d+)\|([^}]+)}}(?:?)?/g);

diff = diffMatch === null || diffMatch === void 0 ? void 0 : diffMatch[1];

if (diff != null) {

revids.push(+diff);

revidText[+diff] = diffMatch[2].replace(/^\(|\)$/g, '');

}

}

// All diff links removed. Get diff of starting and current to get entire diff part.

diffs = starting.slice(0, starting.length - this.current.length);

// Bolded diffs support.

if (diffs.slice(0, 3) === "'''" &&

diffs.slice(-3) === "'''" &&

!diffs.slice(3, -3).includes("'''")) {

diffsBolded = true;

}

// Pre-2014 style support.

if ((diffs !== null && diffs !== void 0 ? diffs : '').includes('{{dif')) {

diffsBolded = true;

diffTemplate = '{{dif|$1|($2)}}';

}

// Comments should be empty, but just in case we do come across comments.

comments = this.isEmpty() ? null : this.eatRemaining();

}

else {

// Try to grab extras. This is done by detecting any form of parentheses and

// matching them, including any possible included colon. If that doesn't work,

// try pulling out just the colon.

const maybeExtras = this.eatExpression(/\s*(?::\s*)?\(.+?\)(?:\s*:)?\s*/) || this.eatExpression(/\s*:\s*/g);

if (maybeExtras) {

extras = maybeExtras;

}

// Only comments probably remain. Eat out whitespaces and the rest is a comment.

extras = (extras || ) + (this.eatUntil(/^\S/g, true) || );

if (extras === '') {

extras = null;

}

comments = this.getCurrentLength() > 0 ? this.eatRemaining() : null;

}

// "{bullet}{creation}{page}{extras}{diffs}{comments}"

return {

type: (extras || comments || diffs) == null ? 'pageonly' : 'detailed',

bullet,

creation,

page,

extras,

diffs,

comments,

revids,

revidText,

diffTemplate,

diffsTemplate: diffsBolded ? "$1" : '$1'

};

}

/**

* Returns `true` if the working string is empty.

*

* @return `true` if the length of `current` is zero. `false` if otherwise.

*/

isEmpty() {

return this.current.length === 0;

}

/**

* @return the length of the working string.

*/

getCurrentLength() {

return this.current.length;

}

/**

* Views the next character to {@link ContributionSurveyRowParser#eat}.

*

* @return The first character of the working string.

*/

peek() {

return this.current[0];

}

/**

* Pops the first character off the working string and returns it.

*

* @return First character of the working string, pre-mutation.

*/

eat() {

const first = this.current[0];

this.current = this.current.slice(1);

return first;

}

/**

* Continue eating from the string until a string or regular expression

* is matched. Unlike {@link eatExpression}, passed regular expressions

* will not be re-wrapped with `^(?:)`. These must be added on your own if

* you wish to match the start of the string.

*

* @param pattern The string or regular expression to match.

* @param noFinish If set to `true`, `null` will be returned instead if the

* pattern is never matched. The working string will be reset to its original

* state if this occurs. This prevents the function from being too greedy.

* @return The consumed characters.

*/

eatUntil(pattern, noFinish) {

const starting = this.current;

let consumed = '';

while (this.current.length > 0) {

if (typeof pattern === 'string') {

if (this.current.startsWith(pattern)) {

return consumed;

}

}

else {

if (cloneRegex$1(pattern).test(this.current)) {

return consumed;

}

}

consumed += this.eat();

}

if (noFinish && this.current.length === 0) {

// We finished the string! Reset.

this.current = starting;

return null;

}

else {

return consumed;

}

}

/**

* Eats a given expression from the start of the working string. If the working

* string does not contain the given expression, `null` is returned (and not a

* blank string). Only eats once, so any expression must be greedy if different

* behavior is expected.

*

* The regular expression passed into this function is automatically re-wrapped

* with `^(?:)`. Avoid adding these expressions on your own.

*

* @param pattern The pattern to match.

* @param n The capture group to return (returns the entire string (`0`) by default)

* @return The consumed characters.

*/

eatExpression(pattern, n = 0) {

const expression = new RegExp(`^(?:${pattern.source})`,

// Ban global and multiline, useless since this only matches once and to

// ensure that the reading remains 'flat'.

pattern.flags.replace(/[gm]/g, ''));

const match = expression.exec(this.current);

if (match) {

this.current = this.current.slice(match[0].length);

return match[n];

}

else {

return null;

}

}

/**

* Eats a given expression from the start of the working string. If the working

* string does not contain the given expression, `null` is returned (and not a

* blank string). Only eats once, so any expression must be greedy if different

* behavior is expected.

*

* The regular expression passed into this function is automatically re-wrapped

* with `^(?:)`. Avoid adding these expressions on your own.

*

* @param pattern The pattern to match.

* @return A {@link RegExpExecArray}.

*/

eatExpressionMatch(pattern) {

const expression = new RegExp(`^(?:${pattern.source})`,

// Ban global and multiline, useless since this only matches once and to

// ensure that the reading remains 'flat'.

pattern.flags.replace(/[gm]/g, ''));

const match = expression.exec(this.current);

if (match) {

this.current = this.current.slice(match[0].length);

return match;

}

else {

return null;

}

}

/**

* Consumes the rest of the working string.

*

* @return The remaining characters in the working string.

*/

eatRemaining() {

const remaining = this.current;

this.current = '';

return remaining;

}

}

var ContributionSurveyRowSort;

(function (ContributionSurveyRowSort) {

// Chronological

ContributionSurveyRowSort[ContributionSurveyRowSort["Date"] = 0] = "Date";

// Reverse chronological

ContributionSurveyRowSort[ContributionSurveyRowSort["DateReverse"] = 1] = "DateReverse";

// New size - old size

ContributionSurveyRowSort[ContributionSurveyRowSort["Bytes"] = 2] = "Bytes";

})(ContributionSurveyRowSort || (ContributionSurveyRowSort = {}));

/**

* Sleep for an specified amount of time.

*

* @param ms Milliseconds to sleep for.

*/

function sleep(ms) {

return __awaiter(this, void 0, void 0, function* () {

return new Promise((res) => {

setTimeout(res, ms);

});

});

}

/**

* Handles requests that might get hit by a rate limit. Wraps around

* `fetch` and ensures that all users of the Requester only request

* a single time per 100 ms on top of the time it takes to load

* previous requests. Also runs on four "threads", allowing at

* least a certain level of asynchronicity.

*

* Particularly used when a multitude of requests have a chance to

* DoS a service.

*/

class Requester {

/**

* Processes things in the fetchQueue.

*/

static processFetch() {

return __awaiter(this, void 0, void 0, function* () {

if (Requester.fetchActive >= Requester.maxThreads) {

return;

}

Requester.fetchActive++;

const next = Requester.fetchQueue.shift();

if (next) {

const data =

// eslint-disable-next-line prefer-spread

yield fetch.apply(null, next[1])

.then((res) => {

// Return false for survivable cases. In this case, we'll re-queue

// the request.

if (res.status === 429 || res.status === 502) {

return res.status;

}

else {

return res;

}

}, next[0][1]);

if (data instanceof Response) {

next[0][0](data);

}

else if (typeof data === 'number') {

Requester.fetchQueue.push(next);

}

}

yield sleep(Requester.minTime);

Requester.fetchActive--;

setTimeout(Requester.processFetch, 0);

});

}

}

/**

* Maximum number of requests to be processed simultaneously.

*/

Requester.maxThreads = 4;

/**

* Minimum amount of milliseconds to wait between each request.

*/

Requester.minTime = 100;

/**

* Requests to be performed. Takes tuples containing a resolve-reject pair and arguments

* to be passed into the fetch function.

*/

Requester.fetchQueue = [];

/**

* Number of requests currently being processed. Must be lower than

* {@link maxThreads}.

*/

Requester.fetchActive = 0;

Requester.fetch = (...args) => {

let res, rej;

const fakePromise = new Promise((_res, _rej) => {

res = _res;

rej = _rej;

});

Requester.fetchQueue.push([[res, rej], args]);

setTimeout(Requester.processFetch, 0);

return fakePromise;

};

/**

* Transforms the `redirects` object returned by MediaWiki's `query` action into an

* object instead of an array.

*

* @param redirects

* @param normalized

* @return Redirects as an object

*/

function toRedirectsObject(redirects, normalized) {

var _a;

if (redirects == null) {

return {};

}

const out = {};

for (const redirect of redirects) {

out[redirect.from] = redirect.to;

}

// Single-level redirect-normalize loop check

for (const normal of normalized) {

out[normal.from] = (_a = out[normal.to]) !== null && _a !== void 0 ? _a : normal.to;

}

return out;

}

/**

* A configuration. Defines settings and setting groups.

*/

class ConfigurationBase {

// eslint-disable-next-line jsdoc/require-returns-check

/**

* @return the configuration from the current wiki.

*/

static load() {

throw new Error('Unimplemented method.');

}

/**

* Creates a new Configuration.

*/

constructor() { }

/**

* Deserializes a JSON configuration into this configuration. This WILL overwrite

* past settings.

*

* @param serializedData

*/

deserialize(serializedData) {

var _a;

for (const group in this.all) {

const groupObject = this.all[group];

for (const key in this.all[group]) {

const setting = groupObject[key];

if (((_a = serializedData === null || serializedData === void 0 ? void 0 : serializedData[group]) === null || _a === void 0 ? void 0 : _a[key]) !== undefined) {

setting.set(setting.deserialize ?

// Type-checked upon declaration, just trust it to skip errors.

setting.deserialize(serializedData[group][key]) :

serializedData[group][key]);

}

}

}

}

/**

* @return the serialized version of the configuration. All `undefined` values are stripped

* from output. If a category remains unchanged from defaults, it is skipped. If the entire

* configuration remains unchanged, `null` is returned.

*/

serialize() {

const config = {};

for (const group of Object.keys(this.all)) {

const groupConfig = {};

const groupObject = this.all[group];

for (const key in this.all[group]) {

const setting = groupObject[key];

if (setting.get() === setting.defaultValue && !setting.alwaysSave) {

continue;

}

const serialized = setting.serialize ?

// Type-checked upon declaration, just trust it to skip errors.

setting.serialize(setting.get()) : setting.get();

if (serialized !== undefined) {

groupConfig[key] = serialized;

}

}

if (Object.keys(groupConfig).length > 0) {

config[group] = groupConfig;

}

}

if (Object.keys(config).length > 0) {

return config;

}

else {

return null;

}

}

}

/**

* Works like `Object.values`.

*

* @param obj The object to get the values of.

* @return The values of the given object as an array

*/

function getObjectValues(obj) {

return Object.keys(obj).map((key) => obj[key]);

}

/**

* Log warnings to the console.

*

* @param {...any} data

*/

function warn(...data) {

console.warn('[Deputy]', ...data);

}

/**

* Refers to a specific setting on the configuration. Should be initialized with

* a raw (serialized) type and an actual (deserialized) type.

*

* This is used for both client and wiki-wide configuration.

*/

class Setting {

/**

* @param options

* @param options.serialize Serialization function. See {@link Setting#serialize}

* @param options.deserialize Deserialization function. See {@link Setting#deserialize}

* @param options.alwaysSave See {@link Setting#alwaysSave}.

* @param options.defaultValue Default value. If not supplied, `undefined` is used.

* @param options.displayOptions See {@link Setting#displayOptions}

* @param options.allowedValues See {@link Setting#allowedValues}

*/

constructor(options) {

var _a, _b;

this.serialize = options.serialize;

this.deserialize = options.deserialize;

this.displayOptions = options.displayOptions;

this.allowedValues = options.allowedValues;

this.value = this.defaultValue = options.defaultValue;

this.alwaysSave = options.alwaysSave;

this.isDisabled = ((_a = options.displayOptions) === null || _a === void 0 ? void 0 : _a.disabled) != null ?

(typeof options.displayOptions.disabled === 'function' ?

options.displayOptions.disabled.bind(this) :

() => options.displayOptions.disabled) : () => false;

this.isHidden = ((_b = options.displayOptions) === null || _b === void 0 ? void 0 : _b.hidden) != null ?

(typeof options.displayOptions.hidden === 'function' ?

options.displayOptions.hidden.bind(this) :

() => options.displayOptions.hidden) : () => false;

}

/**

* @return `true` if `this.value` is not null or undefined.

*/

ok() {

return this.value != null;

}

/**

* @return The current value of this setting.

*/

get() {

return this.value;

}

/**

* Sets the value and performs validation. If the input is an invalid value, and

* `throwOnInvalid` is false, the value will be reset to default.

*

* @param v

* @param throwOnInvalid

*/

set(v, throwOnInvalid = false) {

if (this.locked) {

warn('Attempted to modify locked setting.');

return;

}

if (this.allowedValues) {

const keys = Array.isArray(this.allowedValues) ?

this.allowedValues : getObjectValues(this.allowedValues);

if (Array.isArray(v)) {

if (v.some((v1) => keys.indexOf(v1) === -1)) {

if (throwOnInvalid) {

throw new Error('Invalid value');

}

v = this.value;

}

}

else {

if (this.allowedValues && keys.indexOf(v) === -1) {

if (throwOnInvalid) {

throw new Error('Invalid value');

}

v = this.value;

}

}

}

this.value = v;

}

/**

* Resets this setting to its original value.

*/

reset() {

this.set(this.defaultValue);

}

/**

* Parses a given raw value and mutates the setting.

*

* @param raw The raw value to parse.

* @return The new value.

*/

load(raw) {

return (this.value = this.deserialize(raw));

}

/**

* Prevents the value of the setting from being changed. Used for debugging.

*/

lock() {

this.locked = true;

}

/**

* Allows the value of the setting to be changed. Used for debugging.

*/

unlock() {

this.locked = false;

}

}

Setting.basicSerializers = {

serialize: (value) => value,

deserialize: (value) => value

};

/**

* Checks if two MediaWiki page titles are equal.

*

* @param title1

* @param title2

* @return `true` if `title1` and `title2` refer to the same page

*/

function equalTitle(title1, title2) {

return normalizeTitle(title1).getPrefixedDb() === normalizeTitle(title2).getPrefixedDb();

}

var deputySettingsStyles = ".deputy-setting {margin-bottom: 1em;}.deputy-setting > .oo-ui-fieldLayout-align-top .oo-ui-fieldLayout-header .oo-ui-labelElement-label {font-weight: bold;}.dp-mb {margin-bottom: 1em;}.deputy-about {display: flex;}.deputy-about > :first-child {flex: 0;}.deputy-about > :first-child > img {height: 5em;width: auto;}.ltr .deputy-about > :first-child {margin-right: 1em;}.rtl .deputy-about > :first-child {margin-left: 1em;}.deputy-about > :nth-child(2) {flex: 1;}.deputy-about > :nth-child(2) > :first-child > * {display: inline;}.deputy-about > :nth-child(2) > :first-child > :first-child {font-weight: bold;font-size: 2em;}.deputy-about > :nth-child(2) > :first-child > :nth-child(2) {color: gray;vertical-align: bottom;margin-left: 0.4em;}.deputy-about > :nth-child(2) > :not(:first-child) {margin-top: 0.5em;}.ltr .deputy-about + div > :not(:last-child) {margin-right: 0.5em;}.rtl .deputy-about + div > :not(:last-child) {margin-left: 0.5em;}.ltr .deputy-about + div {text-align: right;}.rtl .deputy-about + div {text-align: left;}";

/**

* Works like `Object.fromEntries`

*

* @param obj The object to get the values of.

* @return The values of the given object as an array

*/

function fromObjectEntries(obj) {

const i = {};

for (const [key, value] of obj) {

i[key] = value;

}

return i;

}

/**

* Generates serializer and deserializer for serialized string enums.

*

* Trying to use anything that isn't a string enum here (union enum, numeral enum)

* will likely cause serialization/deserialization failures.

*

* @param _enum

* @param defaultValue

* @return An object containing a `serializer` and `deserializer`.

*/

function generateEnumSerializers(_enum, defaultValue) {

return {

serialize: (value) => value === defaultValue ? undefined : value,

deserialize: (value) => value

};

}

/**

* Generates configuration properties for serialized string enums.

*

* Trying to use anything that isn't a string enum here (union enum, numeral enum)

* will likely cause serialization/deserialization failures.

*

* @param _enum

* @param defaultValue

* @return Setting properties.

*/

function generateEnumConfigurationProperties(_enum, defaultValue) {

return Object.assign(Object.assign({}, generateEnumSerializers(_enum, defaultValue)), { displayOptions: {

type: 'radio'

}, allowedValues: fromObjectEntries(Array.from(new Set(Object.keys(_enum)).values())

.map((v) => [_enum[v], _enum[v]])), defaultValue: defaultValue });

}

var PortletNameView;

(function (PortletNameView) {

PortletNameView["Full"] = "full";

PortletNameView["Short"] = "short";

PortletNameView["Acronym"] = "acronym";

})(PortletNameView || (PortletNameView = {}));

var CompletionAction;

(function (CompletionAction) {

CompletionAction["Nothing"] = "nothing";

CompletionAction["Reload"] = "reload";

})(CompletionAction || (CompletionAction = {}));

var TripleCompletionAction;

(function (TripleCompletionAction) {

TripleCompletionAction["Nothing"] = "nothing";

TripleCompletionAction["Reload"] = "reload";

TripleCompletionAction["Redirect"] = "redirect";

})(TripleCompletionAction || (TripleCompletionAction = {}));

var ContributionSurveyRowSigningBehavior;

(function (ContributionSurveyRowSigningBehavior) {

ContributionSurveyRowSigningBehavior["Always"] = "always";

ContributionSurveyRowSigningBehavior["AlwaysTrace"] = "alwaysTrace";

ContributionSurveyRowSigningBehavior["AlwaysTraceLastOnly"] = "alwaysTraceLastOnly";

ContributionSurveyRowSigningBehavior["LastOnly"] = "lastOnly";

ContributionSurveyRowSigningBehavior["Never"] = "never";

})(ContributionSurveyRowSigningBehavior || (ContributionSurveyRowSigningBehavior = {}));

var DeputyPageToolbarState;

(function (DeputyPageToolbarState) {

DeputyPageToolbarState[DeputyPageToolbarState["Open"] = 0] = "Open";

DeputyPageToolbarState[DeputyPageToolbarState["Collapsed"] = 1] = "Collapsed";

DeputyPageToolbarState[DeputyPageToolbarState["Hidden"] = 2] = "Hidden";

})(DeputyPageToolbarState || (DeputyPageToolbarState = {}));

/**

* A button that performs an action when clicked. Shown in the preferences screen,

* and acts exactly like a setting, but always holds a value of 'null'.

*/

class Action extends Setting {

/**

* @param onClick

* @param displayOptions

*/

constructor(onClick, displayOptions = {}) {

super({

serialize: () => undefined,

deserialize: () => undefined,

displayOptions: Object.assign({}, displayOptions, { type: 'button' })

});

this.onClick = onClick;

}

}

/**

* A configuration. Defines settings and setting groups.

*/

class UserConfiguration extends ConfigurationBase {

/**

* @return the configuration from the current wiki.

*/

static load() {

const config = new UserConfiguration();

try {

if (mw.user.options.get(UserConfiguration.optionKey)) {

const decodedOptions = JSON.parse(mw.user.options.get(UserConfiguration.optionKey));

config.deserialize(decodedOptions);

}

}

catch (e) {

error(e, mw.user.options.get(UserConfiguration.optionKey));

mw.hook('deputy.i18nDone').add(function notifyConfigFailure() {

mw.notify(mw.msg('deputy.loadError.userConfig'), {

type: 'error'

});

mw.hook('deputy.i18nDone').remove(notifyConfigFailure);

});

config.save();

}

return config;

}

/**

* Creates a new Configuration.

*

* @param serializedData

*/

constructor(serializedData = {}) {

var _a;

super();

this.core = {

/**

* Numerical code that identifies this config version. Increments for every breaking

* configuration file change.

*/

configVersion: new Setting({

defaultValue: UserConfiguration.configVersion,

displayOptions: { hidden: true },

alwaysSave: true

}),

language: new Setting({

defaultValue: mw.config.get('wgUserLanguage'),

displayOptions: { type: 'select' }

}),

modules: new Setting({

defaultValue: ['cci', 'ante', 'ia'],

displayOptions: { type: 'checkboxes' },

allowedValues: ['cci', 'ante', 'ia']

}),

portletNames: new Setting(generateEnumConfigurationProperties(PortletNameView, PortletNameView.Full)),

seenAnnouncements: new Setting({

defaultValue: [],

displayOptions: { hidden: true }

}),

dangerMode: new Setting({

defaultValue: false,

displayOptions: {

type: 'checkbox'

}

}),

resetDatabase: new Action(() => __awaiter(this, void 0, void 0, function* () {

yield window.deputy.storage.reset();

}), {

disabled: () => !window.deputy,

extraOptions: {

flags: ['destructive']

}

}),

resetPreferences: new Action(() => __awaiter(this, void 0, void 0, function* () {

yield MwApi.action.saveOption(UserConfiguration.optionKey, null);

}), {

extraOptions: {

flags: ['destructive']

}

})

};

this.cci = {

enablePageToolbar: new Setting({

defaultValue: true,

displayOptions: {

type: 'checkbox'

}

}),

showCvLink: new Setting({

defaultValue: true,

displayOptions: {

type: 'checkbox'

}

}),

showUsername: new Setting({

defaultValue: false,

displayOptions: {

type: 'checkbox'

}

}),

autoCollapseRows: new Setting({

defaultValue: false,

displayOptions: {

type: 'checkbox'

}

}),

autoShowDiff: new Setting({

defaultValue: false,

displayOptions: {

type: 'checkbox'

}

}),

maxRevisionsToAutoShowDiff: new Setting({

defaultValue: 2,

displayOptions: {

type: 'number',

// Force any due to self-reference

disabled: (config) => !config.cci.autoShowDiff.get(),

extraOptions: {

min: 1

}

}

}),

maxSizeToAutoShowDiff: new Setting({

defaultValue: 500,

displayOptions: {

type: 'number',

// Force any due to self-reference

disabled: (config) => !config.cci.autoShowDiff.get(),

extraOptions: {

min: -1

}

}

}),

forceUtc: new Setting({

defaultValue: false,

displayOptions: {

type: 'checkbox'

}

}),

signingBehavior: new Setting(generateEnumConfigurationProperties(ContributionSurveyRowSigningBehavior, ContributionSurveyRowSigningBehavior.Always)),

signSectionArchive: new Setting({

defaultValue: true,

displayOptions: {

type: 'checkbox'

}

}),

openOldOnContinue: new Setting({

defaultValue: false,

displayOptions: {

type: 'checkbox'

}

}),

toolbarInitialState: new Setting(Object.assign(Object.assign({}, generateEnumSerializers(DeputyPageToolbarState, DeputyPageToolbarState.Open)), { defaultValue: DeputyPageToolbarState.Open, displayOptions: { hidden: true } }))

};

this.ante = {

enableAutoMerge: new Setting({

defaultValue: false,

displayOptions: {

type: 'checkbox',

disabled: 'unimplemented'

}

}),

onSubmit: new Setting(generateEnumConfigurationProperties(CompletionAction, CompletionAction.Reload))

};

this.ia = {

responses: new Setting(Object.assign(Object.assign({}, Setting.basicSerializers), { defaultValue: null, displayOptions: {

disabled: 'unimplemented',

type: 'unimplemented'

} })),

enablePageToolbar: new Setting({

defaultValue: true,

displayOptions: {

type: 'checkbox',

disabled: 'unimplemented'

}

}),

defaultEntirePage: new Setting({

defaultValue: true,

displayOptions: {

type: 'checkbox'

}

}),

defaultFromUrls: new Setting({

defaultValue: true,

displayOptions: {

type: 'checkbox'

}

}),

onHide: new Setting(generateEnumConfigurationProperties(TripleCompletionAction, TripleCompletionAction.Reload)),

onSubmit: new Setting(generateEnumConfigurationProperties(TripleCompletionAction, TripleCompletionAction.Reload)),

onBatchSubmit: new Setting(generateEnumConfigurationProperties(CompletionAction, CompletionAction.Reload))

};

this.type = 'user';

this.all = { core: this.core, cci: this.cci, ante: this.ante, ia: this.ia };

if (serializedData) {

this.deserialize(serializedData);

}

if (mw.storage.get(`mw-${UserConfiguration.optionKey}-lastVersion`) !== version) ;

mw.storage.set(`mw-${UserConfiguration.optionKey}-lastVersion`, version);

if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {

window.deputy.comms.addEventListener('userConfigUpdate', (e) => {

// Update the configuration based on another tab's message.

this.deserialize(e.data.config);

});

}

}

/**

* Saves the configuration.

*/

save() {

return __awaiter(this, void 0, void 0, function* () {

yield MwApi.action.saveOption(UserConfiguration.optionKey, JSON.stringify(this.serialize()));

});

}

}

UserConfiguration.configVersion = 1;

UserConfiguration.optionKey = 'userjs-deputy';

let InternalConfigurationGroupTabPanel$1;

/**

* Initializes the process element.

*/

function initConfigurationGroupTabPanel$1() {

InternalConfigurationGroupTabPanel$1 = class ConfigurationGroupTabPanel extends OO.ui.TabPanelLayout {

/**

* @return The {@Link Setting}s for this group.

*/

get settings() {

return this.config.config.all[this.config.group];

}

/**

* @param config Configuration to be passed to the element.

*/

constructor(config) {

super(`configurationGroupPage_${config.group}`);

this.config = config;

this.mode = config.config instanceof UserConfiguration ? 'user' : 'wiki';

if (this.mode === 'wiki') {

this.$element.append(new OO.ui.MessageWidget({

classes: [

'deputy', 'dp-mb'

],

type: 'warning',

label: mw.msg('deputy.settings.dialog.wikiConfigWarning')

}).$element);

}

for (const settingKey of Object.keys(this.settings)) {

const setting = this.settings[settingKey];

if (setting.isHidden(this.config.config)) {

continue;

}

switch (setting.displayOptions.type) {

case 'checkbox':

this.$element.append(this.newCheckboxField(settingKey, setting));

break;

case 'checkboxes':

this.$element.append(this.newCheckboxesField(settingKey, setting));

break;

case 'radio':

this.$element.append(this.newRadioField(settingKey, setting));

break;

case 'text':

this.$element.append(this.newStringField(settingKey, setting, setting.displayOptions.extraOptions));

break;

case 'number':

this.$element.append(this.newNumberField(settingKey, setting, setting.displayOptions.extraOptions));

break;

case 'page':

this.$element.append(this.newPageField(settingKey, setting, setting.displayOptions.extraOptions));

break;

case 'code':

this.$element.append(this.newCodeField(settingKey, setting, setting.displayOptions.extraOptions));

break;

case 'button':

this.$element.append(this.newButtonField(settingKey, setting, setting.displayOptions.extraOptions));

break;

default:

this.$element.append(this.newUnimplementedField(settingKey));

break;

}

}

}

/**

* Sets up the tab item

*/

setupTabItem() {

this.tabItem.setLabel(this.getMsg(this.config.group));

return this;

}

/**

* @return the i18n message for this setting tab.

*

* @param messageKey

*/

getMsg(messageKey) {

return mw.msg(`deputy.setting.${this.mode}.${messageKey}`);

}

/**

* Gets the i18n message for a given setting.

*

* @param settingKey

* @param key

* @return A localized string

*/

getSettingMsg(settingKey, key) {

return this.getMsg(`${this.config.group}.${settingKey}.${key}`);

}

/**

* @param settingKey

* @param allowedValues

* @return a tuple array of allowed values that can be used in OOUI `items` parameters.

*/

getAllowedValuesArray(settingKey, allowedValues) {

const items = [];

if (Array.isArray(allowedValues)) {

for (const key of allowedValues) {

const message = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.${key}`);

items.push([key, message.exists() ? message.text() : key]);

}

}

else {

for (const key of Object.keys(allowedValues)) {

const message = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.${key}`);

items.push([key, message.exists() ? message.text() : key]);

}

}

return items;

}

/**

* Creates an unimplemented setting notice.

*

* @param settingKey

* @return An HTMLElement of the given setting's field.

*/

newUnimplementedField(settingKey) {

const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);

return h_1("div", { class: "deputy-setting" },

h_1("b", null, this.getSettingMsg(settingKey, 'name')),

desc.exists() ? h_1("p", { style: { fontSize: '0.925em', color: '#54595d' } }, desc.text()) : '',

h_1("p", null, mw.msg('deputy.settings.dialog.unimplemented')));

}

/**

* Creates a checkbox field.

*

* @param settingKey

* @param setting

* @return An HTMLElement of the given setting's field.

*/

newCheckboxField(settingKey, setting) {

const isDisabled = setting.isDisabled(this.config.config);

const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);

const field = new OO.ui.CheckboxInputWidget({

selected: setting.get(),

disabled: isDisabled !== undefined && isDisabled !== false

});

const layout = new OO.ui.FieldLayout(field, {

align: 'inline',

label: this.getSettingMsg(settingKey, 'name'),

help: typeof isDisabled === 'string' ?

this.getSettingMsg(settingKey, isDisabled) :

desc.exists() ? desc.text() : undefined,

helpInline: true

});

field.on('change', () => {

setting.set(field.isSelected());

this.emit('change');

});

// Attach disabled re-checker

this.on('change', () => {

field.setDisabled(!!setting.isDisabled(this.config.config));

});

return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));

}

/**

* Creates a new checkbox set field.

*

* @param settingKey

* @param setting

* @return An HTMLElement of the given setting's field.

*/

newCheckboxesField(settingKey, setting) {

const isDisabled = setting.isDisabled(this.config.config);

const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);

const field = new OO.ui.CheckboxMultiselectInputWidget({

value: setting.get(),

disabled: isDisabled !== undefined && isDisabled !== false,

options: this.getAllowedValuesArray(settingKey, setting.allowedValues)

.map(([key, label]) => ({ data: key, label }))

});

const layout = new OO.ui.FieldLayout(field, {

align: 'top',

label: this.getSettingMsg(settingKey, 'name'),

help: typeof isDisabled === 'string' ?

this.getSettingMsg(settingKey, isDisabled) :

desc.exists() ? desc.text() : undefined,

helpInline: true

});

// TODO: @types/oojs-ui limitation

field.on('change', (items) => {

const finalData = Array.isArray(setting.allowedValues) ?

items :

field.getValue().map((v) => setting.allowedValues[v]);

setting.set(finalData);

this.emit('change');

});

// Attach disabled re-checker

this.on('change', () => {

field.setDisabled(!!setting.isDisabled(this.config.config));

});

return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));

}

/**

* Creates a new radio set field.

*

* @param settingKey

* @param setting

* @return An HTMLElement of the given setting's field.

*/

newRadioField(settingKey, setting) {

var _a;

const isDisabled = setting.isDisabled(this.config.config);

const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);

const field = new OO.ui.RadioSelectWidget({

disabled: isDisabled !== undefined && isDisabled !== false &&

!((_a = setting.displayOptions.readOnly) !== null && _a !== void 0 ? _a : false),

items: this.getAllowedValuesArray(settingKey, setting.allowedValues)

.map(([key, label]) => new OO.ui.RadioOptionWidget({

data: key,

label: label,

selected: setting.get() === key

})),

multiselect: false

});

const layout = new OO.ui.FieldLayout(field, {

align: 'top',

label: this.getSettingMsg(settingKey, 'name'),

help: typeof isDisabled === 'string' ?

this.getSettingMsg(settingKey, isDisabled) :

desc.exists() ? desc.text() : undefined,

helpInline: true

});

// OOUIRadioInputWidget

field.on('select', (items) => {

const finalData = Array.isArray(setting.allowedValues) ?

items.data :

setting.allowedValues[items.data];

setting.set(finalData);

this.emit('change');

});

// Attach disabled re-checker

this.on('change', () => {

field.setDisabled(!!setting.isDisabled(this.config.config));

});

return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));

}

/**

* Creates a new field that acts like a string field.

*

* @param FieldClass

* @param settingKey

* @param setting

* @param extraFieldOptions

* @return A Deputy setting field

*/

newStringLikeField(FieldClass, settingKey, setting, extraFieldOptions = {}) {

var _a, _b, _c;

const isDisabled = setting.isDisabled(this.config.config);

const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);

const field = new FieldClass(Object.assign({ readOnly: (_a = setting.displayOptions.readOnly) !== null && _a !== void 0 ? _a : false, value: (_c = (_b = setting.serialize) === null || _b === void 0 ? void 0 : _b.call(setting, setting.get())) !== null && _c !== void 0 ? _c : setting.get(), disabled: isDisabled !== undefined && isDisabled !== false }, extraFieldOptions));

const layout = new OO.ui.FieldLayout(field, {

align: 'top',

label: this.getSettingMsg(settingKey, 'name'),

help: typeof isDisabled === 'string' ?

this.getSettingMsg(settingKey, isDisabled) :

desc.exists() ? desc.text() : undefined,

helpInline: true

});

if (FieldClass === OO.ui.NumberInputWidget) {

field.on('change', (value) => {

setting.set(+value);

this.emit('change');

});

}

else {

field.on('change', (value) => {

setting.set(value);

this.emit('change');

});

}

// Attach disabled re-checker

this.on('change', () => {

field.setDisabled(setting.isDisabled(this.config.config));

});

return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));

}

/**

* Creates a new string setting field.

*

* @param settingKey

* @param setting

* @param extraFieldOptions

* @return An HTMLElement of the given setting's field.

*/

newStringField(settingKey, setting, extraFieldOptions) {

return this.newStringLikeField(OO.ui.TextInputWidget, settingKey, setting, extraFieldOptions);

}

/**

* Creates a new number setting field.

*

* @param settingKey

* @param setting

* @param extraFieldOptions

* @return An HTMLElement of the given setting's field.

*/

newNumberField(settingKey, setting, extraFieldOptions) {

return this.newStringLikeField(OO.ui.NumberInputWidget, settingKey, setting, extraFieldOptions);

}

/**

* Creates a new page title setting field.

*

* @param settingKey

* @param setting

* @param extraFieldOptions

* @return An HTMLElement of the given setting's field.

*/

newPageField(settingKey, setting, extraFieldOptions) {

return this.newStringLikeField(mw.widgets.TitleInputWidget, settingKey, setting, extraFieldOptions);

}

/**

* Creates a new code setting field.

*

* @param settingKey

* @param setting

* @param extraFieldOptions

* @return An HTMLElement of the given setting's field.

*/

newCodeField(settingKey, setting, extraFieldOptions) {

return this.newStringLikeField(OO.ui.MultilineTextInputWidget, settingKey, setting, extraFieldOptions);

}

/**

* Creates a new button setting field.

*

* @param settingKey

* @param setting

* @param extraFieldOptions

* @return An HTMLElement of the given setting's field.

*/

newButtonField(settingKey, setting, extraFieldOptions) {

const isDisabled = setting.isDisabled(this.config.config);

const msgPrefix = `deputy.setting.${this.mode}.${this.config.group}.${settingKey}`;

const desc = mw.message(`${msgPrefix}.description`);

const field = new OO.ui.ButtonWidget(Object.assign({ label: this.getSettingMsg(settingKey, 'name'), disabled: isDisabled !== undefined && isDisabled !== false }, extraFieldOptions));

const layout = new OO.ui.FieldLayout(field, {

align: 'top',

label: this.getSettingMsg(settingKey, 'name'),

help: typeof isDisabled === 'string' ?

this.getSettingMsg(settingKey, isDisabled) :

desc.exists() ? desc.text() : undefined,

helpInline: true

});

field.on('click', () => __awaiter(this, void 0, void 0, function* () {

try {

if (yield OO.ui.confirm(mw.msg(`${msgPrefix}.confirm`))) {

yield setting.onClick();

OO.ui.alert(mw.msg(`${msgPrefix}.success`));

}

}

catch (e) {

OO.ui.alert(mw.msg(`${msgPrefix}.failed`));

}

}));

return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));

}

};

}

/**

* Creates a new ConfigurationGroupTabPanel.

*

* @param config Configuration to be passed to the element.

* @return A ConfigurationGroupTabPanel object

*/

function ConfigurationGroupTabPanel (config) {

if (!InternalConfigurationGroupTabPanel$1) {

initConfigurationGroupTabPanel$1();

}

return new InternalConfigurationGroupTabPanel$1(config);

}

let windowManager;

/**

* Opens a temporary window. Use this for dialogs that are immediately destroyed

* after running. Do NOT use this for re-openable dialogs, such as the main ANTE

* dialog.

*

* @param window

* @return A promise. Resolves when the window is closed.

*/

function openWindow(window) {

return __awaiter(this, void 0, void 0, function* () {

return new Promise((res) => {

var _a;

if (!windowManager) {

windowManager = new OO.ui.WindowManager();

const parent = (_a = document.getElementById('mw-teleport-target')) !== null && _a !== void 0 ? _a : document.getElementsByTagName('body')[0];

parent.appendChild(unwrapWidget(windowManager));

}

windowManager.addWindows([window]);

windowManager.openWindow(window);

windowManager.on('closing', (win, closed) => {

closed.then(() => {

if (windowManager) {

const _wm = windowManager;

windowManager = null;

removeElement(unwrapWidget(_wm));

_wm.destroy();

res();

}

});

});

});

});

}

var deputySettingsEnglish = {

"deputy.about.version": "v$1 ($2)",

"deputy.about": "About",

"deputy.about.homepage": "Homepage",

"deputy.about.openSource": "Source",

"deputy.about.contact": "Contact",

"deputy.about.credit": "Deputy was made with the help of the English Wikipedia Copyright Cleanup WikiProject and the Wikimedia Foundation.",

"deputy.about.license": "Deputy is licensed under the [$1 Apache License 2.0]. The source code for Deputy is available on [$2 GitHub], and is free for everyone to view and suggest changes.",

"deputy.about.thirdParty": "Deputy is bundled with third party libraries to make development easier. All libraries have been vetted for user security and license compatibility. For more information, see the [$1 Licensing] section on Deputy's README.",

"deputy.about.buildInfo": "Deputy v$1 ($2), committed $3.",

"deputy.about.footer": "Made with love, coffee, and the tears of copyright editors.",

"deputy.settings.portlet": "Deputy preferences",

"deputy.settings.portlet.tooltip": "Opens a dialog to modify Deputy preferences",

"deputy.settings.wikiEditIntro.title": "This is a Deputy configuration page",

"deputy.settings.wikiEditIntro.current": "Deputy's active configuration comes from this page. Changing this page will affect the settings of all Deputy users on this wiki. Edit responsibly, and avoid making significant changes without prior discussion.",

"deputy.settings.wikiEditIntro.other": "This is a valid Deputy configuration page, but the configuration is currently being loaded from $1. If this becomes the active configuration page, changing it will affect the settings of all Deputy users on this wiki. Edit responsibly, and avoid making significant changes without prior discussion.",

"deputy.settings.wikiEditIntro.edit.current": "Modify configuration",

"deputy.settings.wikiEditIntro.edit.other": "Modify this configuration",

"deputy.settings.wikiEditIntro.edit.otherCurrent": "Modify the active configuration",

"deputy.settings.wikiEditIntro.edit.protected": "This page's protection settings do not allow you to edit the page.",

"deputy.settings.wikiOutdated": "Outdated configuration",

"deputy.settings.wikiOutdated.help": "Deputy has detected a change in this wiki's configuration for all Deputy users. We've automatically downloaded the changes for you, but you have to reload to apply the changes.",

"deputy.settings.wikiOutdated.reload": "Reload",

"deputy.settings.dialog.title": "Deputy Preferences",

"deputy.settings.dialog.unimplemented": "A way to modify this setting has not yet been implemented. Check back later!",

"deputy.settings.saved": "Preferences saved. Please refresh the page to see changes.",

"deputy.settings.dialog.wikiConfigWarning": "You are currently editing a wiki-wide Deputy configuration page. Changes made to this page may affect the settings of all Deputy users on this wiki. Edit responsibly, and avoid making significant changes without prior discussion.",

"deputy.setting.user.core": "Deputy",

"deputy.setting.user.core.language.name": "Language",

"deputy.setting.user.core.language.description": "Deputy's interface language. English (US) is used by default, and is used as a fallback if no translations are available. If the content of the wiki you work on is in a different language from the interface language, Deputy will need to load additional data to ensure edit summaries, text, etc., saved on-wiki match the wiki's content language. For this reason, we suggest keeping the interface language the same as the wiki's content language.",

"deputy.setting.user.core.modules.name": "Modules",

"deputy.setting.user.core.modules.description": "Choose the enabled Deputy modules. By default, all modules are enabled.\nDisabling specific modules won't make Deputy load faster, but it can remove\nUI features added by Deputy which may act as clutter when unused.",

"deputy.setting.user.core.modules.cci": "Contributor Copyright Investigations",

"deputy.setting.user.core.modules.ante": "{{int:deputy.ante}}",

"deputy.setting.user.core.modules.ia": "{{int:deputy.ia}}",

"deputy.setting.user.core.portletNames.name": "Portlet names",

"deputy.setting.user.core.portletNames.description": "Choose which names appear in the Deputy portlet (toolbox) links.",

"deputy.setting.user.core.portletNames.full": "Full names (e.g. Attribution Notice Template Editor)",

"deputy.setting.user.core.portletNames.short": "Shortened names (e.g. Attrib. Template Editor)",

"deputy.setting.user.core.portletNames.acronym": "Acronyms (e.g. ANTE)",

"deputy.setting.user.core.dangerMode.name": "Danger mode",

"deputy.setting.user.core.dangerMode.description": "Live on the edge. This disables most confirmations and warnings given by Deputy, only leaving potentially catastrophic actions, such as page edits which break templates. It also adds extra buttons meant for rapid case processing. Intended for clerk use; use with extreme caution.",

"deputy.setting.user.core.resetDatabase.name": "Reset database",

"deputy.setting.user.core.resetDatabase.description": "Resets the Deputy internal database. This clears all cached data, including saved page and revision statuses that haven't been saved on-wiki. This does not clear your Deputy preferences.",

"deputy.setting.user.core.resetDatabase.confirm": "Are you sure you want to reset the Deputy database? This action cannot be undone.",

"deputy.setting.user.core.resetDatabase.success": "Database reset successfully. Please refresh the page to see changes.",

"deputy.setting.user.core.resetDatabase.failed": "Could not reset the database. If this error persists, please contact the Deputy maintainers.",

"deputy.setting.user.core.resetPreferences.name": "Reset preferences",

"deputy.setting.user.core.resetPreferences.description": "Resets your Deputy preferences. This clears all settings you have saved in Deputy across all browsers.",

"deputy.setting.user.core.resetPreferences.confirm": "Are you sure you want to reset your Deputy preferences? This action cannot be undone.",

"deputy.setting.user.core.resetPreferences.success": "Preferences reset successfully. Please refresh the page to see changes.",

"deputy.setting.user.core.resetPreferences.failed": "Could not reset preferences. If this error persists, please contact the Deputy maintainers.",

"deputy.setting.user.cci": "CCI",

"deputy.setting.user.cci.enablePageToolbar.name": "Enable page toolbar",

"deputy.setting.user.cci.enablePageToolbar.description": "Enables the page toolbar, which is used to quickly show tools, analysis options, and related case information on a page that is the subject of a CCI investigation.",

"deputy.setting.user.cci.showCvLink.name": "Show \"cv\" (\"copyvios\") link for revisions",

"deputy.setting.user.cci.showCvLink.description": "Show a \"cv\" link next to \"cur\" and \"prev\" on revision rows. This link will only appear if this wiki is configured to use Earwig's Copyvio Detector.",

"deputy.setting.user.cci.showUsername.name": "Show username",

"deputy.setting.user.cci.showUsername.description": "Show the username of the user who made the edit on revision rows. This may be redundant for cases which only have one editor.",

"deputy.setting.user.cci.autoCollapseRows.name": "Automatically collapse rows",

"deputy.setting.user.cci.autoCollapseRows.description": "Automatically collapse rows when the page is loaded. This is useful for cases where each row has many revisions, but may be annoying for cases where each row has few revisions.",

"deputy.setting.user.cci.autoShowDiff.name": "Automatically show diffs",

"deputy.setting.user.cci.autoShowDiff.description": "Enabling automatic loading of diffs. Configurable with two additional options to avoid loading too much content.",

"deputy.setting.user.cci.maxRevisionsToAutoShowDiff.name": "Maximum revisions to automatically show diff",

"deputy.setting.user.cci.maxRevisionsToAutoShowDiff.description": "The maximum number of revisions for a given page to automatically show the diff for each revision in the main interface.",

"deputy.setting.user.cci.maxSizeToAutoShowDiff.name": "Maximum size to automatically show diff",

"deputy.setting.user.cci.maxSizeToAutoShowDiff.description": "The maximum size of a diff to be automatically shown, if the diff will be automatically shown (see \"Maximum revisions to automatically show diff\"). Prevents extremely large diffs from opening. Set to -1 to show regardless of size.",

"deputy.setting.user.cci.forceUtc.name": "Force UTC time",

"deputy.setting.user.cci.forceUtc.description": "Forces Deputy to use UTC time whenever displaying dates and times, irregardless of your system's timezone or your MediaWiki time settings.",

"deputy.setting.user.cci.signingBehavior.name": "Row signing behavior",

"deputy.setting.user.cci.signingBehavior.description": "Choose how Deputy should behave when signing rows. By default, all rows are always signed with your signature (~~~~). You may configure Deputy to only sign the last row or never sign. You can also configure Deputy to leave a hidden trace behind (), which helps Deputy (for other users) determine who assessed a row.",

"deputy.setting.user.cci.signingBehavior.always": "Always sign rows",

"deputy.setting.user.cci.signingBehavior.alwaysTrace": "Always leave a trace",

"deputy.setting.user.cci.signingBehavior.alwaysTraceLastOnly": "Always leave a trace, but sign the last row modified",

"deputy.setting.user.cci.signingBehavior.lastOnly": "Only sign the last row modified (prevents assessor recognition)",

"deputy.setting.user.cci.signingBehavior.never": "Never sign rows (prevents assessor recognition)",

"deputy.setting.user.cci.signSectionArchive.name": "Sign by default when archiving CCI sections",

"deputy.setting.user.cci.signSectionArchive.description": "If enabled, Deputy will enable the \"include my signature\" checkbox by default when archiving a CCI section.",

"deputy.setting.user.cci.openOldOnContinue.name": "Open old versions on continue",

"deputy.setting.user.cci.openOldOnContinue.description": "If enabled, all previously-open sections of a given case page will also be opened alongside the section where the \"continue CCI session\" link was clicked.",

"deputy.setting.user.ante": "ANTE",

"deputy.setting.user.ante.enableAutoMerge.name": "Merge automatically on run",

"deputy.setting.user.ante.enableAutoMerge.description": "If enabled, templates that can be merged will automatically be merged when the dialog opens.",

"deputy.setting.user.ante.enableAutoMerge.unimplemented": "This feature has not yet been implemented.",

"deputy.setting.user.ante.onSubmit.name": "Action on submit",

"deputy.setting.user.ante.onSubmit.description": "Choose what to do after editing attribution notice templates.",

"deputy.setting.user.ante.onSubmit.nothing": "Do nothing",

"deputy.setting.user.ante.onSubmit.reload": "Reload the page",

"deputy.setting.user.ia": "IA",

"deputy.setting.user.ia.responses.name": "Custom responses",

"deputy.setting.user.ia.responses.description": "A custom set of responses, or overrides for existing responses. If an entry\nwith the same key on both the wiki-wide configuration and the user configuration\nexists, the user configuration will override the wiki-wide configuration. Wiki-wide configuration responses can also be disabled locally. If this setting is empty, no overrides are made.",

"deputy.setting.user.ia.enablePageToolbar.name": "Enable page toolbar",

"deputy.setting.user.ia.enablePageToolbar.description": "If enabled, the page toolbar will be shown when dealing with CP cases. The IA page toolbar works slightly differently from the CCI page toolbar. Namely, it shows a button for responding instead of a status dropdown.",

"deputy.setting.user.ia.enablePageToolbar.unimplemented": "This feature has not yet been implemented.",

"deputy.setting.user.ia.defaultEntirePage.name": "Hide entire page by default",

"deputy.setting.user.ia.defaultEntirePage.description": "If enabled, the Infringement Assistant reporting window will hide the entire page by default.",

"deputy.setting.user.ia.defaultFromUrls.name": "Use URLs by default",

"deputy.setting.user.ia.defaultFromUrls.description": "If enabled, the Infringement Assistant reporting window will use URL inputs by default.",

"deputy.setting.user.ia.onHide.name": "Action on hide",

"deputy.setting.user.ia.onHide.description": "Choose what to do after the \"Hide content only\" button is selected and the relevant content is hidden from the page.",

"deputy.setting.user.ia.onHide.nothing": "Do nothing",

"deputy.setting.user.ia.onHide.reload": "Reload the page",

"deputy.setting.user.ia.onHide.redirect": "Redirect to the noticeboard page",

"deputy.setting.user.ia.onSubmit.name": "Action on submit",

"deputy.setting.user.ia.onSubmit.description": "Choose what to do after the \"Submit\" button is selected, the relevant content is hidden from the page, and the page is reported.",

"deputy.setting.user.ia.onSubmit.nothing": "Do nothing",

"deputy.setting.user.ia.onSubmit.reload": "Reload the page",

"deputy.setting.user.ia.onSubmit.redirect": "Redirect to the noticeboard page",

"deputy.setting.user.ia.onBatchSubmit.name": "Action on batch listing submit",

"deputy.setting.user.ia.onBatchSubmit.description": "When reporting a batch of pages, choose what to do after the \"Report\" button is selected and the pages are reported.",

"deputy.setting.user.ia.onBatchSubmit.nothing": "Do nothing",

"deputy.setting.user.ia.onBatchSubmit.reload": "Reload the noticeboard page",

"deputy.setting.wiki.core": "Core",

"deputy.setting.wiki.core.lastEdited.name": "Configuration last edited",

"deputy.setting.wiki.core.lastEdited.description": "The last time that this configuration was edited, as a timestamp. This is a way to ensure that all users are on the correct wiki-wide configuration version before changes are made. Checks are performed on every page load with Deputy.",

"deputy.setting.wiki.core.dispatchRoot.name": "Deputy Dispatch root URL",

"deputy.setting.wiki.core.dispatchRoot.description": "The URL to a Deputy Dispatch instance that can handle this wiki. Deputy Dispatch is a webserver responsible for centralizing and optimizing data used by Deputy, and can be used to reduce load on wikis. More information can be found at https://github.com/ChlodAlejandro/deputy-dispatch.",

"deputy.setting.wiki.core.changeTag.name": "Change tag",

"deputy.setting.wiki.core.changeTag.description": "Tag to use for all Deputy edits.",

"deputy.setting.wiki.cci": "CCI",

"deputy.setting.wiki.cci.enabled.name": "Enable contributor copyright investigations assistant",

"deputy.setting.wiki.cci.enabled.description": "Enables the CCI workflow assistant. This allows Deputy to replace the contribution survey found on CCI case pages with a graphical interface which works with other tabs to make the CCI workflow easier.",

"deputy.setting.wiki.cci.rootPage.name": "Root page",

"deputy.setting.wiki.cci.rootPage.description": "The main page that holds all subpages containing valid contribution copyright investigation cases.",

"deputy.setting.wiki.cci.headingMatch.name": "Heading title regular expression",

"deputy.setting.wiki.cci.headingMatch.description": "A regular expression that will be used to detect valid contribution surveyor heading. Since its usage is rather technical, this value should be edited by someone with technical knowledge of regular expressions.",

"deputy.setting.wiki.cci.collapseTop.name": "Collapsible wikitext (top)",

"deputy.setting.wiki.cci.collapseTop.description": "Placed just below a section heading when closing a contributor survey section. Use \"$1\" to denote user comments and signature. On the English Wikipedia, this is {{Template:collapse top}}. Other wikis may have an equivalent template. This should go hand in hand with \"{{int:deputy.setting.wiki.cci.collapseBottom.name}}\", as they are used as a pair.",

"deputy.setting.wiki.cci.collapseBottom.name": "Collapsible wikitext (bottom)",

"deputy.setting.wiki.cci.collapseBottom.description": "Placed at the end of a section when closing a contributor survey section. On the English Wikipedia, this is {{Template:collapse bottom}}. Other wikis may have an equivalent template.",

"deputy.setting.wiki.cci.earwigRoot.name": "Earwig's Copyvio Detector root URL",

"deputy.setting.wiki.cci.earwigRoot.description": "The URL to an instance of Earwig's Copyvio Detector that can handle this wiki. The official copyvio detector (copyvios.toolforge.org) can only handle Wikimedia wikis — you may change this behavior by specifying a custom instance that can process this wiki here.",

"deputy.setting.wiki.cci.resortRows.name": "Resort rows",

"deputy.setting.wiki.cci.resortRows.description": "Resort rows when saving the page. This is useful for cases where rows are added out of order, or when rows are added in a different order than they should be displayed.",

"deputy.setting.wiki.ante": "ANTE",

"deputy.setting.wiki.ante.enabled.name": "Enable the Attribution Notice Template Editor",

"deputy.setting.wiki.ante.enabled.description": "Enables ANTE for all users. ANTE is currently the least-optimized module for localization, and may not work for all wikis.",

"deputy.setting.wiki.ia": "IA",

"deputy.setting.wiki.ia.enabled.name": "Enable the Infringement Assistant",

"deputy.setting.wiki.ia.enabled.description": "Enables IA for all users. IA allows users to easily and graphically report pages with suspected or complicated copyright infringements.",

"deputy.setting.wiki.ia.rootPage.name": "Root page",

"deputy.setting.wiki.ia.rootPage.description": "The root page for Infringement Assistant. This should be the copyright problems noticeboard for this specific wiki. IA will only show quick response links for the root page and its subpages.",

"deputy.setting.wiki.ia.subpageFormat.name": "Subpage format",

"deputy.setting.wiki.ia.subpageFormat.description": "The format to use for subpages of the root page. This is a moment.js format string.",

"deputy.setting.wiki.ia.preload.name": "Preload",

"deputy.setting.wiki.ia.preload.description": "Defines the page content to preload the page with if a given subpage does not exist yet. This should be an existing page on-wiki. Leave blank to avoid using a preload entirely.",

"deputy.setting.wiki.ia.allowPresumptive.name": "Allow presumptive deletions",

"deputy.setting.wiki.ia.allowPresumptive.description": "Allows users to file listings for presumptive deletions. Note that the CCI setting \"Root page\" must be set for this to work, even if the \"CCI\" module is disabled entirely.",

"deputy.setting.wiki.ia.listingWikitext.name": "Listing wikitext",

"deputy.setting.wiki.ia.listingWikitext.description": "Defines the wikitext that will be used when adding listings to a noticeboard page. You may use \"$1\" to denote the page being reported, and \"$2\" for user comments (which shouldn't contain the signature).",

"deputy.setting.wiki.ia.listingWikitextMatch.name": "Regular expression for listings",

"deputy.setting.wiki.ia.listingWikitextMatch.description": "A regular expression that will be used to parse and detect listings on a given noticeboard page. Since its usage is rather technical, this value should be edited by someone with technical knowledge of regular expressions. This regular expression must provide three captured groups: group \"$1\" will catch any bullet point, space, or prefix, \"$2\" will catch the page title ONLY if the given page matches \"{{int:deputy.setting.wiki.ia.listingWikitext.name}}\" or \"{{int:deputy.setting.wiki.ia.batchListingWikitext.name}}\", and \"$3\" will catch the page title ONLY IF the page wasn't caught in \"$2\" (such as in cases where there is only a bare link to the page).",

"deputy.setting.wiki.ia.batchListingWikitext.name": "Batch listing wikitext",

"deputy.setting.wiki.ia.batchListingWikitext.description": "Defines the wikitext that will be used when adding batch listings to a noticeboard page. You may use \"$1\" to denote the page being reported, and \"$2\" for the list of pages (as determined by \"{{int:deputy.setting.wiki.ia.batchListingPageWikitext.name}}\") and \"$3\" for user comments (which doesn't contain the signature).",

"deputy.setting.wiki.ia.batchListingPageWikitext.name": "Batch listing page wikitext",

"deputy.setting.wiki.ia.batchListingPageWikitext.description": "Wikitext to use for every row of text in \"{{int:deputy.setting.wiki.ia.batchListingWikitext.name}}\". No line breaks are automatically added; these must be added into this string.",

"deputy.setting.wiki.ia.hideTemplate.name": "Content hiding wikitext (top)",

"deputy.setting.wiki.ia.hideTemplate.description": "Wikitext to hide offending content with. On the English Wikipedia, this is a usage of {{Template:copyvio}}. Other wikis may have an equivalent template. This should go hand in hand with \"{{int:deputy.setting.wiki.ia.hideTemplateBottom.name}}\", as they are used as a pair.",

"deputy.setting.wiki.ia.hideTemplateBottom.name": "Content hiding wikitext (bottom)",

"deputy.setting.wiki.ia.hideTemplateBottom.description": "Placed at the end of hidden content to hide only part of a page. On the English Wikipedia, this is {{Template:copyvio/bottom}}. Other wikis may have an equivalent template.",

"deputy.setting.wiki.ia.entirePageAppendBottom.name": "Append content hiding wikitext (bottom) when hiding an entire page",

"deputy.setting.wiki.ia.entirePageAppendBottom.description": "If enabled, the content hiding wikitext (bottom) will be appended to the end of the page when hiding the entire page. This avoids the \"missing end tag\" lint error, if the template is properly formatted.",

"deputy.setting.wiki.ia.responses.name": "Responses",

"deputy.setting.wiki.ia.responses.description": "Quick responses for copyright problems listings. Used by clerks to resolve specific listings or provide more information about the progress of a given listing."

};

/**

* Handles resource fetching operations.

*/

class DeputyResources {

/**

* Loads a resource from the provided resource root.

*

* @param path A path relative to the resource root.

* @return A Promise that resolves to the resource's content as a UTF-8 string.

*/

static loadResource(path) {

return __awaiter(this, void 0, void 0, function* () {

switch (this.root.type) {

case 'url': {

const headers = new Headers();

headers.set('Origin', window.location.origin);

return fetch((new URL(path, this.root.url)).href, {

method: 'GET',

headers

}).then((r) => r.text());

}

case 'wiki': {

this.assertApi();

return getPageContent(this.root.prefix.replace(/\/$/, '') + '/' + path, {}, this.api);

}

}

});

}

/**

* Ensures that `this.api` is a valid ForeignApi.

*/

static assertApi() {

if (this.root.type !== 'wiki') {

return;

}

if (!this.api) {

this.api = new mw.ForeignApi(this.root.wiki.toString(), {

// Force anonymous mode. Deputy doesn't need user data anyway,

// so this should be fine.

anonymous: true

});

}

}

}

/**

* The root of all Deputy resources. This should serve static data that Deputy will

* use to load resources such as language files.

*/

DeputyResources.root = {

type: 'url',

url: new URL('https://tools-static.wmflabs.org/deputy/')

};

var _a;

const USER_LOCALE = (_a = window.deputyLang) !== null && _a !== void 0 ? _a : mw.config.get('wgUserLanguage');

/**

* Handles internationalization and localization for Deputy and sub-modules.

*/

class DeputyLanguage {

/**

* Loads the language for this Deputy interface.

*

* @param module The module to load a language pack for.

* @param fallback A fallback language pack to load. Since this is an actual

* `Record`, this relies on the language being bundled with the userscript. This ensures

* that a language pack is always available, even if a language file could not be loaded.

*/

static load(module, fallback) {

var _a;

return __awaiter(this, void 0, void 0, function* () {

const lang = (_a = window.deputyLang) !== null && _a !== void 0 ? _a : 'en';

// The loaded language resource file. Forced to `null` if using English, since English

// is our fallback language.

const langResource = lang === 'en' ? null :

yield DeputyResources.loadResource(`i18n/${module}/${lang}.json`)

.catch(() => {

// Could not find requested language file.

return null;

});

if (!langResource) {

// Fall back.

for (const key in fallback) {

mw.messages.set(key, fallback[key]);

}

return;

}

try {

const langData = JSON.parse(langResource);

for (const key in langData) {

mw.messages.set(key, langData[key]);

}

}

catch (e) {

error(e);

mw.notify(

// No languages to fall back on. Do not translate this string.

'Deputy: Requested language page is not a valid JSON file.', { type: 'error' });

// Fall back.

for (const key in fallback) {

mw.messages.set(key, fallback[key]);

}

}

if (lang !== mw.config.get('wgUserLanguage')) {

yield DeputyLanguage.loadSecondary();

}

});

}

/**

* Loads a specific moment.js locale. It's possible for nothing to be loaded (e.g. if the

* locale is not supported by moment.js), in which case nothing happens and English is

* likely used.

*

* @param locale The locale to load. `window.deputyLang` by default.

*/

static loadMomentLocale(locale = USER_LOCALE) {

return __awaiter(this, void 0, void 0, function* () {

if (locale === 'en') {

// Always loaded.

return;

}

if (mw.loader.getState('moment') !== 'ready') {

// moment.js is not yet loaded.

warn('Deputy tried loading moment.js locales but moment.js is not yet ready.');

return;

}

if (window.moment.locales().indexOf(locale) !== -1) {

// Already loaded.

return;

}

yield mw.loader.using('moment')

.then(() => true, () => null);

yield mw.loader.getScript(new URL(`resources/lib/moment/locale/${locale}.js`, new URL(mw.util.wikiScript('index'), window.location.href)).href).then(() => true, () => null);

});

}

/**

* There are times when the user interface language do not match the wiki content

* language. Since Deputy's edit summary and content strings are found in the

* i18n files, however, there are cases where the wrong language would be used.

*

* This solves this problem by manually overriding content-specific i18n keys with

* the correct language. By default, all keys that match `deputy.*.content.**` get

* overridden.

*

* There are no fallbacks for this. If it fails, the user interface language is

* used anyway. In the event that the user interface language is not English,

* this will cause awkward situations. Whether or not something should be done to

* catch this specific edge case will depend on how frequent it happens.

*

* @param locale

* @param match

*/

static loadSecondary(locale = mw.config.get('wgContentLanguage'), match = /^deputy\.(?:[^.]+)?\.content\./g) {

return __awaiter(this, void 0, void 0, function* () {

// The loaded language resource file. Forced to `null` if using English, since English

// is our fallback language.

const langResource = locale === 'en' ? null :

yield DeputyResources.loadResource(`i18n/${module}/${locale}.json`)

.catch(() => {

// Could not find requested language file.

return null;

});

if (!langResource) {

return;

}

try {

const langData = JSON.parse(langResource);

for (const key in langData) {

if (cloneRegex$1(match).test(key)) {

mw.messages.set(key, langData[key]);

}

}

}

catch (e) {

// Silent failure.

error('Deputy: Requested language page is not a valid JSON file.', e);

}

});

}

}

/**

* Get the nodes from a JQuery object and wraps it in an element.

*

* @param element The element to add the children into

* @param $j The JQuery object

* @return The original element, now with children

*/

function unwrapJQ(element = h_1("span", null), $j) {

$j.each((i, e) => element.append(e));

return element;

}

let InternalConfigurationGroupTabPanel;

/**

* Initializes the process element.

*/

function initConfigurationGroupTabPanel() {

var _a;

InternalConfigurationGroupTabPanel = (_a = class ConfigurationGroupTabPanel extends OO.ui.TabPanelLayout {

/**

* @return The {@Link Setting}s for this group.

*/

get settings() {

return this.config.config.all[this.config.group];

}

/**

*/

constructor() {

super('configurationGroupPage_About');

this.$element.append(h_1("div", null,

h_1("div", { class: "deputy-about" },

h_1("div", { style: "flex: 0" },

h_1("img", { src: ConfigurationGroupTabPanel.logoUrl, alt: "Deputy logo" })),

h_1("div", { style: "flex: 1" },

h_1("div", null,

h_1("div", null, mw.msg('deputy.name')),

h_1("div", null, mw.msg('deputy.about.version', version, gitAbbrevHash))),

h_1("div", null, mw.msg('deputy.description')))),

h_1("div", null,

h_1("a", { href: "https://w.wiki/7NWR", target: "_blank" }, unwrapWidget(new OO.ui.ButtonWidget({

label: mw.msg('deputy.about.homepage'),

flags: ['progressive']

}))),

h_1("a", { href: "https://github.com/ChlodAlejandro/deputy", target: "_blank" }, unwrapWidget(new OO.ui.ButtonWidget({

label: mw.msg('deputy.about.openSource'),

flags: ['progressive']

}))),

h_1("a", { href: "https://w.wiki/7NWS", target: "_blank" }, unwrapWidget(new OO.ui.ButtonWidget({

label: mw.msg('deputy.about.contact'),

flags: ['progressive']

})))),

unwrapJQ(h_1("p", null), mw.message('deputy.about.credit').parseDom()),

unwrapJQ(h_1("p", null), mw.message('deputy.about.license', 'https://www.apache.org/licenses/LICENSE-2.0', 'https://github.com/ChlodAlejandro/deputy').parseDom()),

unwrapJQ(h_1("p", null), mw.message('deputy.about.thirdParty', 'https://github.com/ChlodAlejandro/deputy#licensing').parseDom()),

unwrapJQ(h_1("p", { style: { fontSize: '0.9em', color: 'darkgray' } }), mw.message('deputy.about.buildInfo', gitVersion, gitBranch, new Date(gitDate).toLocaleString()).parseDom()),

unwrapJQ(h_1("p", { style: { fontSize: '0.9em', color: 'darkgray' } }), mw.message('deputy.about.footer').parseDom())));

}

/**

* Sets up the tab item

*/

setupTabItem() {

this.tabItem.setLabel(mw.msg('deputy.about'));

return this;

}

},

_a.logoUrl = 'https://upload.wikimedia.org/wikipedia/commons/2/2b/Deputy_logo.svg',

_a);

}

/**

* Creates a new ConfigurationGroupTabPanel.

*

* @return A ConfigurationGroupTabPanel object

*/

function ConfigurationAboutTabPanel () {

if (!InternalConfigurationGroupTabPanel) {

initConfigurationGroupTabPanel();

}

return new InternalConfigurationGroupTabPanel();

}

let InternalConfigurationDialog;

/**

* Initializes the process element.

*/

function initConfigurationDialog() {

var _a;

InternalConfigurationDialog = (_a = class ConfigurationDialog extends OO.ui.ProcessDialog {

/**

* @param data

*/

constructor(data) {

super();

this.config = data.config;

}

/**

* @return The body height of this dialog.

*/

getBodyHeight() {

return 900;

}

/**

* Initializes the dialog.

*/

initialize() {

super.initialize();

this.layout = new OO.ui.IndexLayout();

this.layout.addTabPanels(this.generateGroupLayouts());

if (this.config instanceof UserConfiguration) {

this.layout.addTabPanels([ConfigurationAboutTabPanel()]);

}

this.$body.append(this.layout.$element);

return this;

}

/**

* Generate TabPanelLayouts for each configuration group.

*

* @return An array of TabPanelLayouts

*/

generateGroupLayouts() {

return Object.keys(this.config.all).map((group) => ConfigurationGroupTabPanel({

$overlay: this.$overlay,

config: this.config,

group

}));

}

/**

* @param action

* @return An OOUI Process.

*/

getActionProcess(action) {

const process = super.getActionProcess();

if (action === 'save') {

process.next(this.config.save());

process.next(() => {

var _a, _b;

mw.notify(mw.msg('deputy.settings.saved'), {

type: 'success'

});

if (this.config.type === 'user') {

// Override local Deputy option, just in case the user wishes to

// change the configuration again.

mw.user.options.set(UserConfiguration.optionKey, this.config.serialize());

if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {

window.deputy.comms.send({

type: 'userConfigUpdate',

config: this.config.serialize()

});

}

}

else if (this.config.type === 'wiki') {

// We know it is a WikiConfiguration, the instanceof check here

// is just for type safety.

if ((_b = window.deputy) === null || _b === void 0 ? void 0 : _b.comms) {

window.deputy.comms.send({

type: 'wikiConfigUpdate',

config: {

title: this.config.sourcePage.getPrefixedText(),

editable: this.config.editable,

wt: this.config.serialize()

}

});

}

// Reload the page.

window.location.reload();

}

});

}

process.next(() => {

this.close();

});

return process;

}

},

_a.static = Object.assign(Object.assign({}, OO.ui.ProcessDialog.static), { name: 'configurationDialog', title: mw.msg('deputy.settings.dialog.title'), size: 'large', actions: [

{

flags: ['safe', 'close'],

icon: 'close',

label: mw.msg('deputy.ante.close'),

title: mw.msg('deputy.ante.close'),

invisibleLabel: true,

action: 'close'

},

{

action: 'save',

label: mw.msg('deputy.save'),

flags: ['progressive', 'primary']

}

] }),

_a);

}

/**

* Creates a new ConfigurationDialog.

*

* @param data

* @return A ConfigurationDialog object

*/

function ConfigurationDialogBuilder(data) {

if (!InternalConfigurationDialog) {

initConfigurationDialog();

}

return new InternalConfigurationDialog(data);

}

let attached = false;

/**

* Spawns a new configuration dialog.

*

* @param config

*/

function spawnConfigurationDialog(config) {

mw.loader.using([

'oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets', 'mediawiki.widgets'

], () => {

const dialog = ConfigurationDialogBuilder({ config });

openWindow(dialog);

});

}

/**

* Attaches the "Deputy preferences" portlet link in the toolbox. Ensures that it doesn't

* get attached twice.

*/

function attachConfigurationDialogPortletLink() {

return __awaiter(this, void 0, void 0, function* () {

if (document.querySelector('#p-deputy-config') || attached) {

return;

}

attached = true;

mw.util.addCSS(deputySettingsStyles);

yield DeputyLanguage.load('settings', deputySettingsEnglish);

mw.util.addPortletLink('p-tb', '#', mw.msg('deputy.settings.portlet'), 'deputy-config', mw.msg('deputy.settings.portlet.tooltip')).addEventListener('click', () => {

// Load a fresh version of the configuration - this way we can make

// modifications live to the configuration without actually affecting

// tool usage.

spawnConfigurationDialog(UserConfiguration.load());

});

});

}

let InternalDeputyMessageWidget;

/**

* Initializes the process element.

*/

function initDeputyMessageWidget() {

InternalDeputyMessageWidget = class DeputyMessageWidget extends OO.ui.MessageWidget {

/**

* @param config Configuration to be passed to the element.

*/

constructor(config) {

var _a;

super(config);

this.$element.addClass('dp-messageWidget');

const elLabel = this.$label[0];

if (!config.label) {

if (config.title) {

elLabel.appendChild(h_1("b", { style: { display: 'block' } }, config.title));

}

if (config.message) {

elLabel.appendChild(h_1("p", { class: "dp-messageWidget-message" }, config.message));

}

}

if (config.actions || config.closable) {

const actionContainer = h_1("div", { class: "dp-messageWidget-actions" });

for (const action of ((_a = config.actions) !== null && _a !== void 0 ? _a : [])) {

if (action instanceof OO.ui.Element) {

actionContainer.appendChild(unwrapWidget(action));

}

else {

actionContainer.appendChild(action);

}

}

if (config.closable) {

const closeButton = new OO.ui.ButtonWidget({

label: mw.msg('deputy.dismiss')

});

closeButton.on('click', () => {

removeElement(unwrapWidget(this));

this.emit('close');

});

actionContainer.appendChild(unwrapWidget(closeButton));

}

elLabel.appendChild(actionContainer);

}

}

};

}

/**

* Creates a new DeputyMessageWidget. This is an extension of the default

* OOUI MessageWidget. It includes support for a title, a message, and button

* actions.

*

* @param config Configuration to be passed to the element.

* @return A DeputyMessageWidget object

*/

function DeputyMessageWidget (config) {

if (!InternalDeputyMessageWidget) {

initDeputyMessageWidget();

}

return new InternalDeputyMessageWidget(config);

}

/**

* @param config The current configuration (actively loaded, not the one being viewed)

* @return An HTML element consisting of an OOUI MessageWidget

*/

function WikiConfigurationEditIntro(config) {

const current = config.onConfigurationPage();

let buttons;

if (current) {

const editCurrent = new OO.ui.ButtonWidget({

flags: ['progressive', 'primary'],

label: mw.msg('deputy.settings.wikiEditIntro.edit.current'),

disabled: !mw.config.get('wgIsProbablyEditable'),

title: mw.config.get('wgIsProbablyEditable') ?

undefined : mw.msg('deputy.settings.wikiEditIntro.edit.protected')

});

editCurrent.on('click', () => {

spawnConfigurationDialog(config);

});

buttons = [editCurrent];

}

else {

const editCurrent = new OO.ui.ButtonWidget({

flags: ['progressive', 'primary'],

label: mw.msg('deputy.settings.wikiEditIntro.edit.otherCurrent'),

disabled: !config.editable,

title: config.editable ?

undefined : mw.msg('deputy.settings.wikiEditIntro.edit.protected')

});

editCurrent.on('click', () => __awaiter(this, void 0, void 0, function* () {

spawnConfigurationDialog(config);

}));

const editOther = new OO.ui.ButtonWidget({

flags: ['progressive'],

label: mw.msg('deputy.settings.wikiEditIntro.edit.other'),

disabled: !mw.config.get('wgIsProbablyEditable'),

title: mw.config.get('wgIsProbablyEditable') ?

undefined : mw.msg('deputy.settings.wikiEditIntro.edit.protected')

});

editOther.on('click', () => __awaiter(this, void 0, void 0, function* () {

spawnConfigurationDialog(yield config.static.load(normalizeTitle()));

}));

buttons = [editCurrent, editOther];

}

const messageBox = DeputyMessageWidget({

classes: [

'deputy', 'dp-mb'

],

type: 'notice',

title: mw.msg('deputy.settings.wikiEditIntro.title'),

message: current ?

mw.msg('deputy.settings.wikiEditIntro.current') :

unwrapJQ(h_1("span", null), mw.message('deputy.settings.wikiEditIntro.other', config.sourcePage.getPrefixedText()).parseDom()),

actions: buttons

});

const box = unwrapWidget(messageBox);

box.classList.add('deputy', 'deputy-wikiConfig-intro');

return box;

}

/* eslint-disable max-len */

/*

* Replacement polyfills for wikis that have no configured templates.

* Used in WikiConfiguration, to permit a seamless OOB experience.

*/

/** `{{collapse top}}` equivalent */

const collapseTop = `

class="mw-collapsible mw-collapsed" style="border:1px solid #C0C0C0;width:100%"

!

$1
`.trimStart();

/** `{{collapse bottom}}` equivalent */

const collapseBottom = `

`;

/** `* {{subst:article-cv|1=$1}} $2 ~~~~` equivalent */

const listingWikitext = '* $1 $2 ~~~~';

/**

* Polyfill for the following:

* `; {{anchor|1={{delink|$1}}}} $1

* $2

* $3 ~~~~`

*/

const batchListingWikitext = `*; $1

$2

$3`;

/**

* Inserted and chained as part of $2 in `batchListingWikitext`.

* Equivalent of `* {{subst:article-cv|1=$1}}\n`. Newline is intentional.

*/

const batchListingPageWikitext = '* $1\n';

/**

* `{{subst:copyvio|url=$1|fullpage=$2}}` equivalent

*/

const copyvioTop = `

{{int:deputy.ia.content.copyvio}}

{{int:deputy.ia.content.copyvio.help}}

{{if:$1|

{{if:$presumptive|{{int:deputy.ia.content.copyvio.from.pd}} $1|{{int:deputy.ia.content.copyvio.from}} $1}}
}}

`;

/**

* @return A MessageWidget for reloading a page with an outdated configuration.

*/

function ConfigurationReloadBanner() {

const reloadButton = new OO.ui.ButtonWidget({

flags: ['progressive', 'primary'],

label: mw.msg('deputy.settings.wikiOutdated.reload')

});

const messageBox = DeputyMessageWidget({

classes: [

'deputy', 'dp-mb', 'dp-wikiConfigUpdateMessage'

],

type: 'notice',

title: mw.msg('deputy.settings.wikiOutdated'),

message: mw.msg('deputy.settings.wikiOutdated.help'),

actions: [reloadButton]

});

reloadButton.on('click', () => __awaiter(this, void 0, void 0, function* () {

window.location.reload();

}));

const box = unwrapWidget(messageBox);

box.style.fontSize = 'calc(1em * 0.875)';

return box;

}

var WikiConfigurationLocations = [

'MediaWiki:Deputy-config.json',

// Prioritize interface protected page over Project namespace

'User:Chlod/Scripts/Deputy/configuration.json',

'Project:Deputy/configuration.json'

];

/**

* Automatically applies a change tag to edits made by the user if

* a change tag was provided in the configuration.

*

* @param config

* @return A spreadable Object containing the `tags` parameter for `action=edit`.

*/

function changeTag(config) {

return config.core.changeTag.get() ?

{ tags: config.core.changeTag.get() } :

{};

}

/**

* Wiki-wide configuration. This is applied to all users of the wiki, and has

* the potential to break things for EVERYONE if not set to proper values.

*

* As much as possible, the correct configuration location should be protected

* to avoid vandalism or bad-faith changes.

*

* This configuration works if specific settings are set. In other words, some

* features of Deputy are disabled unless Deputy has been configured. This is

* to avoid messing with existing on-wiki processes.

*/

class WikiConfiguration extends ConfigurationBase {

/**

* Loads the configuration from a set of possible sources.

*

* @param sourcePage The specific page to load from

* @return A WikiConfiguration object

*/

static load(sourcePage) {

return __awaiter(this, void 0, void 0, function* () {

if (sourcePage) {

// Explicit source given. Do not load from local cache.

return this.loadFromWiki(sourcePage);

}

else {

return this.loadFromLocal();

}

});

}

/**

* Loads the wiki configuration from localStorage and/or MediaWiki

* settings. This allows for faster loads at the expense of a (small)

* chance of outdated configuration.

*

* The localStorage layer allows fast browser-based caching. If a user

* is logging in again on another device, the user configuration

* will automatically be sent to the client, lessening turnaround time.

* If all else fails, the configuration will be loaded from the wiki.

*

* @return A WikiConfiguration object.

*/

static loadFromLocal() {

return __awaiter(this, void 0, void 0, function* () {

let configInfo;

// If `mw.storage.get` returns `false` or `null`, it'll be thrown up.

let rawConfigInfo = mw.storage.get(WikiConfiguration.optionKey);

// Try to grab it from user options, if it exists.

if (!rawConfigInfo) {

rawConfigInfo = mw.user.options.get(WikiConfiguration.optionKey);

}

if (typeof rawConfigInfo === 'string') {

try {

configInfo = JSON.parse(rawConfigInfo);

}

catch (e) {

// Bad local! Switch to non-local.

error('Failed to get Deputy wiki configuration', e);

return this.loadFromWiki();

}

}

else {

log('No locally-cached Deputy configuration, pulling from wiki.');

return this.loadFromWiki();

}

if (configInfo) {

return new WikiConfiguration(new mw.Title(configInfo.title.title, configInfo.title.namespace), JSON.parse(configInfo.wt), configInfo.editable);

}

else {

return this.loadFromWiki();

}

});

}

/**

* Loads the configuration from the current wiki.

*

* @param sourcePage The specific page to load from

* @return A WikiConfiguration object

*/

static loadFromWiki(sourcePage) {

return __awaiter(this, void 0, void 0, function* () {

const configPage = sourcePage ? Object.assign({ title: sourcePage }, yield (() => __awaiter(this, void 0, void 0, function* () {

const content = yield getPageContent(sourcePage, {

prop: 'revisions|info',

intestactions: 'edit',

fallbacktext: '{}'

});

return {

wt: content,

editable: content.page.actions.edit

};

}))()) : yield this.loadConfigurationWikitext();

try {

// Attempt save of configuration to local options (if not explicitly loaded)

if (sourcePage == null) {

mw.storage.set(WikiConfiguration.optionKey, JSON.stringify(configPage));

}

return new WikiConfiguration(configPage.title, JSON.parse(configPage.wt), configPage.editable);

}

catch (e) {

error(e, configPage);

mw.hook('deputy.i18nDone').add(function notifyConfigFailure() {

mw.notify(mw.msg('deputy.loadError.wikiConfig'), {

type: 'error'

});

mw.hook('deputy.i18nDone').remove(notifyConfigFailure);

});

return null;

}

});

}

/**

* Loads the wiki-wide configuration from a set of predefined locations.

* See {@link WikiConfiguration#configLocations} for a full list.

*

* @return The string text of the raw configuration, or `null` if a configuration was not found.

*/

static loadConfigurationWikitext() {

return __awaiter(this, void 0, void 0, function* () {

const response = yield MwApi.action.get({

action: 'query',

prop: 'revisions|info',

rvprop: 'content',

rvslots: 'main',

rvlimit: 1,

intestactions: 'edit',

redirects: true,

titles: WikiConfiguration.configLocations.join('|')

});

const redirects = toRedirectsObject(response.query.redirects, response.query.normalized);

for (const page of WikiConfiguration.configLocations) {

const title = normalizeTitle(redirects[page] || page).getPrefixedText();

const pageInfo = response.query.pages.find((p) => p.title === title);

if (!pageInfo.missing) {

return {

title: normalizeTitle(pageInfo.title),

wt: pageInfo.revisions[0].slots.main.content,

editable: pageInfo.actions.edit

};

}

}

return null;

});

}

/**

* Check if the current page being viewed is a valid configuration page.

*

* @param page

* @return `true` if the current page is a valid configuration page.

*/

static isConfigurationPage(page) {

if (page == null) {

page = new mw.Title(mw.config.get('wgPageName'));

}

return this.configLocations.some((v) => equalTitle(page, normalizeTitle(v)));

}

/**

* @param sourcePage

* @param serializedData

* @param editable Whether the configuration is editable by the current user or not.

*/

constructor(sourcePage, serializedData, editable) {

var _a;

super();

this.sourcePage = sourcePage;

this.serializedData = serializedData;

this.editable = editable;

// Used to avoid circular dependencies.

this.static = WikiConfiguration;

this.core = {

/**

* Numerical code that identifies this config version. Increments for every breaking

* configuration file change.

*/

configVersion: new Setting({

defaultValue: WikiConfiguration.configVersion,

displayOptions: { hidden: true },

alwaysSave: true

}),

lastEdited: new Setting({

defaultValue: 0,

displayOptions: { hidden: true },

alwaysSave: true

}),

dispatchRoot: new Setting({

serialize: (v) => v.href,

deserialize: (v) => new URL(v),

defaultValue: new URL('https://deputy.toolforge.org/'),

displayOptions: { type: 'text' },

alwaysSave: true

}),

changeTag: new Setting({

defaultValue: null,

displayOptions: { type: 'text' }

})

};

this.cci = {

enabled: new Setting({

defaultValue: false,

displayOptions: { type: 'checkbox' }

}),

rootPage: new Setting({

serialize: (v) => v === null || v === void 0 ? void 0 : v.getPrefixedText(),

deserialize: (v) => new mw.Title(v),

defaultValue: null,

displayOptions: { type: 'page' }

}),

headingMatch: new Setting({

defaultValue: '(Page|Article|Local file|File)s? \\d+ (to|through) \\d+',

displayOptions: { type: 'text' }

}),

collapseTop: new Setting({

defaultValue: collapseTop,

displayOptions: { type: 'code' }

}),

collapseBottom: new Setting({

defaultValue: collapseBottom,

displayOptions: { type: 'code' }

}),

earwigRoot: new Setting({

serialize: (v) => v.href,

deserialize: (v) => new URL(v),

defaultValue: new URL('https://copyvios.toolforge.org/'),

displayOptions: { type: 'text' },

alwaysSave: true

}),

resortRows: new Setting({

defaultValue: true,

displayOptions: { type: 'checkbox' }

})

};

this.ante = {

enabled: new Setting({

defaultValue: false,

displayOptions: { type: 'checkbox' }

})

};

this.ia = {

enabled: new Setting({

defaultValue: false,

displayOptions: { type: 'checkbox' }

}),

rootPage: new Setting({

serialize: (v) => v === null || v === void 0 ? void 0 : v.getPrefixedText(),

deserialize: (v) => new mw.Title(v),

defaultValue: null,

displayOptions: { type: 'page' }

}),

subpageFormat: new Setting({

defaultValue: 'YYYY MMMM D',

displayOptions: { type: 'text' }

}),

preload: new Setting({

serialize: (v) => { var _a, _b; return ((_b = (_a = v === null || v === void 0 ? void 0 : v.trim()) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) === 0 ? null : v.trim(); },

defaultValue: null,

displayOptions: { type: 'page' }

}),

allowPresumptive: new Setting({

defaultValue: true,

displayOptions: { type: 'checkbox' }

}),

listingWikitext: new Setting({

defaultValue: listingWikitext,

displayOptions: { type: 'code' }

}),

/**

* $1 - Title of the batch

* $2 - List of pages (newlines should be added in batchListingPageWikitext).

* $3 - User comment

*/

batchListingWikitext: new Setting({

defaultValue: batchListingWikitext,

displayOptions: { type: 'code' }

}),

/**

* $1 - Page to include

*/

batchListingPageWikitext: new Setting({

defaultValue: batchListingPageWikitext,

displayOptions: { type: 'code' }

}),

/**

* @see {@link CopyrightProblemsListing#articleCvRegex}

*

* This should match both normal and batch listings.

*/

listingWikitextMatch: new Setting({

defaultValue: '(\\*\\s*)?\\[\\[([^\\]]+)\\]\\]',

displayOptions: { type: 'code' }

}),

hideTemplate: new Setting({

defaultValue: copyvioTop,

displayOptions: { type: 'code' }

}),

hideTemplateBottom: new Setting({

defaultValue: copyvioBottom,

displayOptions: { type: 'code' }

}),

entirePageAppendBottom: new Setting({

defaultValue: true,

displayOptions: { type: 'checkbox' }

}),

responses: new Setting(Object.assign(Object.assign({}, Setting.basicSerializers), { defaultValue: null, displayOptions: { type: 'unimplemented' } }))

};

this.type = 'wiki';

this.all = { core: this.core, cci: this.cci, ante: this.ante, ia: this.ia };

/**

* Set to true when this configuration is outdated based on latest data. Usually adds banners

* to UI interfaces saying a new version of the configuration is available, and that it should

* be used whenever possible.

*

* TODO: This doesn't do what the documentations says yet.

*/

this.outdated = false;

if (serializedData) {

this.deserialize(serializedData);

}

if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {

// Communications is available. Register a listener.

window.deputy.comms.addEventListener('wikiConfigUpdate', (e) => {

this.update(Object.assign({}, e.data.config, {

title: normalizeTitle(e.data.config.title)

}));

});

}

}

/**

* Check for local updates, and update the local configuration as needed.

*

* @param sourceConfig A serialized version of the configuration based on a wiki

* page configuration load.

*/

update(sourceConfig) {

return __awaiter(this, void 0, void 0, function* () {

// Asynchronously load from the wiki.

let fromWiki;

if (sourceConfig) {

fromWiki = sourceConfig;

}

else {

// Asynchronously load from the wiki.

fromWiki = yield WikiConfiguration.loadConfigurationWikitext();

if (fromWiki == null) {

// No configuration found on the wiki.

return;

}

}

const liveWikiConfig = JSON.parse(fromWiki.wt);

// Attempt save if on-wiki config found and doesn't match local.

// Doesn't need to be from the same config page, since this usually means a new config

// page was made, and we need to switch to it.

if (this.core.lastEdited.get() < liveWikiConfig.core.lastEdited) {

if (liveWikiConfig.core.configVersion > WikiConfiguration.configVersion) {

// Don't update if the config version is higher than ours. We don't want

// to load in the config of a newer version, as it may break things.

// Deputy should load in the newer version of the script soon enough,

// and the config will be parsed by a version that supports it.

warn(`Expected wiki configuration version ${this.core.configVersion.get()}, but found ${liveWikiConfig.core.configVersion}. New configuration will not be loaded.`);

return;

}

else if (liveWikiConfig.core.configVersion < WikiConfiguration.configVersion) {

// Version change detected.

// Do nothing... for now.

// HINT: Update configuration

warn(`Expected wiki configuration version ${this.core.configVersion.get()}, but found ${liveWikiConfig.core.configVersion}. Proceeding anyway...`);

}

const onSuccess = () => {

var _a;

// Only mark outdated after saving, so we don't indirectly cause a save operation

// to cancel.

this.outdated = true;

// Attempt to add site notice.

if (document.querySelector('.dp-wikiConfigUpdateMessage') == null) {

(_a = document.getElementById('siteNotice')) === null || _a === void 0 ? void 0 : _a.insertAdjacentElement('afterend', ConfigurationReloadBanner());

}

};

// If updated from a source config (other Deputy tab), do not attempt to save

// to MediaWiki settings. This is most likely already saved by the original tab

// that sent the comms message.

if (!sourceConfig) {

// Use `liveWikiConfig`, since this contains the compressed version and is more

// bandwidth-friendly.

const rawConfigInfo = JSON.stringify({

title: fromWiki.title,

editable: fromWiki.editable,

wt: JSON.stringify(liveWikiConfig)

});

// Save to local storage.

mw.storage.set(WikiConfiguration.optionKey, rawConfigInfo);

// Save to user options (for faster first-load times).

yield MwApi.action.saveOption(WikiConfiguration.optionKey, rawConfigInfo).then(() => {

var _a;

if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {

// Broadcast the update to other tabs.

window.deputy.comms.send({

type: 'wikiConfigUpdate',

config: {

title: fromWiki.title.getPrefixedText(),

editable: fromWiki.editable,

wt: liveWikiConfig

}

});

}

onSuccess();

}).catch(() => {

// silently fail

});

}

else {

onSuccess();

}

}

});

}

/**

* Saves the configuration on-wiki. Does not automatically generate overrides.

*/

save() {

return __awaiter(this, void 0, void 0, function* () {

// Update last edited number

this.core.lastEdited.set(Date.now());

yield MwApi.action.postWithEditToken(Object.assign(Object.assign({}, changeTag(yield window.deputy.getWikiConfig())), { action: 'edit', title: this.sourcePage.getPrefixedText(), text: JSON.stringify(this.serialize()) }));

});

}

/**

* Check if the current page being viewed is the active configuration page.

*

* @param page

* @return `true` if the current page is the active configuration page.

*/

onConfigurationPage(page) {

return equalTitle(page !== null && page !== void 0 ? page : mw.config.get('wgPageName'), this.sourcePage);

}

/**

* Actually displays the banner which allows an editor to modify the configuration.

*/

displayEditBanner() {

return __awaiter(this, void 0, void 0, function* () {

mw.loader.using(['oojs', 'oojs-ui'], () => {

if (document.getElementsByClassName('deputy-wikiConfig-intro').length > 0) {

return;

}

document.getElementById('mw-content-text').insertAdjacentElement('afterbegin', WikiConfigurationEditIntro(this));

});

});

}

/**

* Shows the configuration edit intro banner, if applicable on this page.

*

* @return void

*/

prepareEditBanners() {

return __awaiter(this, void 0, void 0, function* () {

if (['view', 'diff'].indexOf(mw.config.get('wgAction')) === -1) {

return;

}

if (document.getElementsByClassName('deputy-wikiConfig-intro').length > 0) {

return;

}

if (this.onConfigurationPage()) {

return this.displayEditBanner();

}

else if (WikiConfiguration.isConfigurationPage()) {

return this.displayEditBanner();

}

});

}

}

WikiConfiguration.configVersion = 2;

WikiConfiguration.optionKey = 'userjs-deputy-wiki';

WikiConfiguration.configLocations = WikiConfigurationLocations;

/**

* API communication class

*/

class Dispatch {

/**

* Creates a Deputy API instance.

*/

constructor() { }

/**

* Logs the user out of the API.

*/

logout() {

return __awaiter(this, void 0, void 0, function* () {

// TODO: Make logout API request

yield window.deputy.storage.setKV('api-token', null);

});

}

/**

* Logs in the user. Optional: only used for getting data on deleted revisions.

*/

login() {

return __awaiter(this, void 0, void 0, function* () {

Dispatch.token = yield window.deputy.storage.getKV('api-token');

// TODO: If token, set token

// TODO: If no token, start OAuth flow and make login API request

throw new Error('Unimplemented method.');

});

}

/**

* Returns a fully-formed HTTP URL from a given endpoint. This uses the wiki's

* set Dispatch endpoint and a given target (such as `/v1/revisions`) to get

* the full URL.

*

* @param endpoint The endpoint to get

*/

getEndpoint(endpoint) {

return __awaiter(this, void 0, void 0, function* () {

return new URL(endpoint.replace(/^\/+/, ''), (yield WikiConfiguration.load()).core.dispatchRoot.get()

.href

.replace(/\/+$/, ''));

});

}

}

/**

* Singleton instance.

*/

Dispatch.i = new Dispatch();

/**

*

*/

class DispatchRevisions {

/**

*

*/

constructor() { }

/**

* Gets expanded revision data from the API. This returns a response similar to the

* `revisions` object provided by action=query, but also includes additional information

* relevant (such as the parsed (HTML) comment, diff size, etc.)

*

* @param revisions The revisions to get the data for

* @return An object of expanded revision data mapped by revision IDs

*/

get(revisions) {

return __awaiter(this, void 0, void 0, function* () {

return Requester.fetch(yield Dispatch.i.getEndpoint(`v1/revisions/${mw.config.get('wgWikiID')}`), {

method: 'POST',

headers: {

'Content-Type': 'application/x-www-form-urlencoded',

'Api-User-Agent': `Deputy/${version} (${window.location.hostname})`

},

body: 'revisions=' + revisions.join('|')

})

.then((r) => r.json())

.then((j) => {

if (j.error) {

throw new Error(j.error.info);

}

return j;

})

.then((j) => j.revisions);

});

}

}

/**

* Singleton instance

*/

DispatchRevisions.i = new DispatchRevisions();

var ContributionSurveyRowStatus;

(function (ContributionSurveyRowStatus) {

// The row has not been processed yet.

ContributionSurveyRowStatus[ContributionSurveyRowStatus["Unfinished"] = 0] = "Unfinished";

// The row has a comment but cannot be parsed

ContributionSurveyRowStatus[ContributionSurveyRowStatus["Unknown"] = 1] = "Unknown";

// The row has been processed and violations were found ({{y}})

ContributionSurveyRowStatus[ContributionSurveyRowStatus["WithViolations"] = 2] = "WithViolations";

// The row has been processed and violations were not found ({{n}})

ContributionSurveyRowStatus[ContributionSurveyRowStatus["WithoutViolations"] = 3] = "WithoutViolations";

// The row has been found but the added text is no longer in the existing revision

ContributionSurveyRowStatus[ContributionSurveyRowStatus["Missing"] = 4] = "Missing";

// The row has been processed and text was presumptively removed ({{x}}),

ContributionSurveyRowStatus[ContributionSurveyRowStatus["PresumptiveRemoval"] = 5] = "PresumptiveRemoval";

})(ContributionSurveyRowStatus || (ContributionSurveyRowStatus = {}));

/**

* Represents a contribution survey row. This is an abstraction of the row that can

* be seen on contribution survey pages, which acts as an intermediate between raw

* wikitext and actual HTML content.

*/

class ContributionSurveyRow {

/**

* Identifies a row's current status based on the comment's contents.

*

* @param comment The comment to process

* @return The status of the row

*/

static identifyCommentStatus(comment) {

for (const status in ContributionSurveyRow.commentMatchRegex) {

if (cloneRegex$1(ContributionSurveyRow.commentMatchRegex[+status]).test(comment)) {

return +status;

}

}

return ContributionSurveyRowStatus.Unknown;

}

/**

* Guesses the sort order for a given set of revisions.

*

* @param diffs The diffs to guess from.

* @return The sort order

*/

static guessSortOrder(diffs) {

let last = null;

let dateScore = 1;

let dateReverseScore = 1;

let byteScore = 1;

let dateStreak = 0;

let dateReverseStreak = 0;

let byteStreak = 0;

for (const diff of diffs) {

if (last == null) {

last = diff;

}

else {

const diffTimestamp = new Date(diff.timestamp).getTime();

const lastTimestamp = new Date(last.timestamp).getTime();

// The use of the OR operator here has a specific purpose:

// * On the first iteration, we want all streak values to be 1

// * On any other iteration, we want it to increment the streak by 1 if a streak

// exists, or set it to 1 if a streak was broken.

dateStreak =

diffTimestamp > lastTimestamp ? dateStreak + 1 : 0;

dateReverseStreak =

diffTimestamp < lastTimestamp ? dateReverseStreak + 1 : 0;

byteStreak =

diff.diffsize <= last.diffsize ? byteStreak + 1 : 0;

dateScore = (dateScore + ((diffTimestamp > lastTimestamp ? 1 : 0) * (1 + dateStreak * 0.3))) / 2;

dateReverseScore = (dateReverseScore + ((diffTimestamp < lastTimestamp ? 1 : 0) * (1 + dateReverseStreak * 0.3))) / 2;

byteScore = (byteScore + ((diff.diffsize <= last.diffsize ? 1 : 0) * (1 + byteStreak * 0.3))) / 2;

last = diff;

}

}

// Multiply by weights to remove ties

dateScore *= 1.05;

dateReverseScore *= 1.025;

switch (Math.max(dateScore, dateReverseScore, byteScore)) {

case byteScore:

return ContributionSurveyRowSort.Bytes;

case dateScore:

return ContributionSurveyRowSort.Date;

case dateReverseScore:

return ContributionSurveyRowSort.DateReverse;

}

}

/**

* Gets the sorter function which will sort a set of diffs based on a given

* sort order.

*

* @param sort

* @param mode The sort mode to use. If `array`, the returned function sorts an

* array of revisions. If `key`, the returned function sorts entries with the first

* entry element (`entry[0]`) being a revision. If `value`, the returned function

* sorts values with the second entry element (`entry[1]`) being a revision.

* @return The sorted array

*/

static getSorterFunction(sort, mode = 'array') {

return (_a, _b) => {

let a, b;

switch (mode) {

case 'array':

a = _a;

b = _b;

break;

case 'key':

a = _a[0];

b = _b[0];

break;

case 'value':

a = _a[1];

b = _b[1];

break;

}

switch (sort) {

case ContributionSurveyRowSort.Date:

return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();

case ContributionSurveyRowSort.DateReverse:

return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();

case ContributionSurveyRowSort.Bytes:

return b.diffsize - a.diffsize;

}

};

}

/**

* This variable returns true when

* (a) the row has a non-unfinished status, and

* (b) there are no outstanding diffs in this row

*

* @return See description.

*/

get completed() {

if (this.diffs == null) {

throw new Error('Diffs have not been pulled yet');

}

return this.status !== ContributionSurveyRowStatus.Unfinished &&

this.diffs.size === 0;

}

/**

* Creates a new contribution survey row from MediaWiki parser output.

*

* @param casePage The case page of this row

* @param wikitext The wikitext of the row

*/

constructor(casePage, wikitext) {

this.data = new ContributionSurveyRowParser(wikitext).parse();

this.type = this.data.type;

this.casePage = casePage;

this.wikitext = wikitext;

this.title = new mw.Title(this.data.page);

this.extras = this.data.extras;

this.comment = this.data.comments;

this.status = this.originalStatus = this.data.comments == null ?

ContributionSurveyRowStatus.Unfinished :

ContributionSurveyRow.identifyCommentStatus(this.data.comments);

if (ContributionSurveyRow.commentMatchRegex[this.status] != null) {

if (cloneRegex$1((ContributionSurveyRow.commentMatchRegex)[this.status], { pre: '^' }).test(this.comment)) {

this.statusIsolated = 'start';

}

else if (cloneRegex$1((ContributionSurveyRow.commentMatchRegex)[this.status], { post: '$' }).test(this.comment)) {

this.statusIsolated = 'end';

}

else {

this.statusIsolated = false;

}

}

}

/**

* Get the ContributionSurveyRowRevisions of this row.

*

* @param forceReload

*/

getDiffs(forceReload = false) {

return __awaiter(this, void 0, void 0, function* () {

if (this.diffs != null && !forceReload) {

return this.diffs;

}

const revisionData = new Map();

const revids = this.data.revids;

// Load revision information

const toCache = [];

for (const revisionID of revids) {

const cachedDiff = yield window.deputy.storage.db.get('diffCache', revisionID);

if (cachedDiff) {

revisionData.set(revisionID, new ContributionSurveyRevision(this, cachedDiff));

}

else {

toCache.push(revisionID);

}

}

if (toCache.length > 0) {

const expandedData = yield DispatchRevisions.i.get(toCache);

for (const revisionID in expandedData) {

revisionData.set(+revisionID, new ContributionSurveyRevision(this, expandedData[revisionID]));

}

for (const revisionID in expandedData) {

yield window.deputy.storage.db.put('diffCache', expandedData[revisionID]);

}

}

// Load tag messages

// First gather all tags mentioned, and then load messages.

const tags = Array.from(revisionData.values()).reduce((acc, cur) => {

if (cur.tags) {

for (const tag of cur.tags) {

if (acc.indexOf(tag) === -1) {

acc.push(tag);

}

}

}

return acc;

}, ['list-wrapper']);

yield MwApi.action.loadMessagesIfMissing(tags.map((v) => 'tag-' + v), {

amenableparser: true

});

// Sort the rows (if rearranging is enabled)

if (window.deputy.wikiConfig.cci.resortRows.get()) {

const sortOrder = ContributionSurveyRow.guessSortOrder(revisionData.values());

return this.diffs = new Map([...revisionData.entries()].sort(ContributionSurveyRow.getSorterFunction(sortOrder, 'value')));

}

else {

return this.diffs = new Map([...revisionData.entries()]);

}

});

}

/**

* Gets the comment with status indicator removed.

*

* @return The comment with the status indicator removed.

*/

getActualComment() {

if (this.originalStatus === ContributionSurveyRowStatus.Unfinished) {

return '';

}

else if (this.statusIsolated === false ||

this.originalStatus === ContributionSurveyRowStatus.Unknown) {

return this.comment;

}

else if (this.statusIsolated === 'start') {

return this.comment.replace(cloneRegex$1((ContributionSurveyRow.commentMatchRegex)[this.originalStatus], { pre: '^' }), '').trim();

}

else if (this.statusIsolated === 'end') {

return this.comment.replace(cloneRegex$1((ContributionSurveyRow.commentMatchRegex)[this.originalStatus], { post: '$' }), '').trim();

}

return '';

}

}

ContributionSurveyRow.Parser = ContributionSurveyRowParser;

/**

* A set of regular expressions that will match a specific contribution survey row

* comment. Used to determine the status of the comment.

*/

ContributionSurveyRow.commentMatchRegex = {

// TODO: Wiki localization

[ContributionSurveyRowStatus.WithViolations]: /\{\{(aye|y)}}/gi,

[ContributionSurveyRowStatus.WithoutViolations]: /\{\{n(ay)?}}/gi,

[ContributionSurveyRowStatus.Missing]: /\{\{\?}}/gi,

[ContributionSurveyRowStatus.PresumptiveRemoval]: /\{\{x}}/gi

};

/**

* Swaps two elements in the DOM. Element 1 will be removed from the DOM, Element 2 will

* be added in its place.

*

* @param element1 The element to remove

* @param element2 The element to insert

* @return `element2`, for chaining

*/

function swapElements (element1, element2) {

try {

element1.insertAdjacentElement('afterend', element2);

element1.parentElement.removeChild(element1);

return element2;

}

catch (e) {

error(e, { element1, element2 });

// Caught for debug only. Rethrow.

throw e;

}

}

/**

* Loading dots.

*

* @return Loading dots.

*/

function DeputyLoadingDots () {

return h_1("div", { class: "dp--loadingDots" },

h_1("span", { class: "dp-loadingDots-1" }),

h_1("span", { class: "dp-loadingDots-2" }),

h_1("span", { class: "dp-loadingDots-3" }));

}

/**

* Gets the URL of a permanent link page.

*

* @param revid The revision ID to link to

* @param page The title of the page to compare to

* @param includeCurrentParams `true` if the current query parameters should be included

* @return The URL of the diff page

*/

function getRevisionURL (revid, page, includeCurrentParams = false) {

const url = new URL(window.location.href);

url.pathname = mw.util.wikiScript('index');

const searchParams = url.searchParams;

if (!includeCurrentParams) {

for (const key of Array.from(searchParams.keys())) {

searchParams.delete(key);

}

}

searchParams.set('title', normalizeTitle(page).getPrefixedText());

searchParams.set('oldid', `${revid}`);

url.search = '?' + searchParams.toString();

url.hash = '';

return url.toString();

}

/**

* Gets the URL of a diff page.

*

* @param from The revision to compare with

* @param to The revision to compare from

* @param includeCurrentParams `true` if the current query parameters should be included

* @return The URL of the diff page

*/

function getRevisionDiffURL (from, to, includeCurrentParams = false) {

const url = new URL(window.location.href);

url.pathname = mw.util.wikiScript('index');

const searchParams = url.searchParams;

if (!includeCurrentParams) {

for (const key of Array.from(searchParams.keys())) {

searchParams.delete(key);

}

}

if (to != null) {

searchParams.set('diff', to.toString());

searchParams.set('oldid', from.toString());

}

else {

searchParams.set('diff', from.toString());

// Strip oldid from URL.

if (searchParams.has('oldid')) {

searchParams.delete('oldid');

}

}

url.search = '?' + searchParams.toString();

url.hash = '';

return url.toString();

}

/**

* Gets the namespace ID from a canonical (not localized) namespace name.

*

* @param namespace The namespace to get

* @return The namespace ID

*/

function nsId(namespace) {

return mw.config.get('wgNamespaceIds')[namespace.toLowerCase().replace(/ /g, '_')];

}

/**

* Evaluates any string using `mw.msg`. This handles internationalization of strings

* that are loaded outside the script or asynchronously.

*

* @param string The string to evaluate

* @param {...any} parameters Parameters to pass, if any

* @return A mw.Message

*/

function msgEval(string, ...parameters) {

// Named parameters

let named = {};

if (typeof parameters[0] === 'object') {

named = parameters.shift();

}

const m = new mw.Map();

for (const [from, to] of Object.entries(named)) {

string = string.replace(new RegExp(`\\$${from}`, 'g'), to);

}

m.set('msg', string);

return new mw.Message(m, 'msg', parameters);

}

/**

* Mixes values together into a string for the `class` attribute.

*

* @param {...any} classes

* @return string

*/

function classMix (...classes) {

const processedClasses = [];

for (const _class of classes) {

if (Array.isArray(_class)) {

processedClasses.push(..._class);

}

else {

processedClasses.push(_class);

}

}

return processedClasses.filter((v) => v != null && !!v).join(' ');

}

/**

* @param root0

* @param root0.revid

* @param root0.parentid

* @param root0.missing

* @return HTML element

*/

function ChangesListLinks({ revid: _revid, parentid: _parentid, missing }) {

const cur = getRevisionDiffURL(_revid, 'cur');

const prev = missing ?

getRevisionDiffURL(_revid, 'prev') :

getRevisionDiffURL(_parentid, _revid);

let cv;

if (window.deputy &&

window.deputy.config.cci.showCvLink &&

window.deputy.wikiConfig.cci.earwigRoot) {

cv = new URL('', window.deputy.wikiConfig.cci.earwigRoot.get());

const selfUrl = new URL(window.location.href);

const urlSplit = selfUrl.hostname.split('.').reverse();

const proj = urlSplit[1]; // wikipedia

const lang = urlSplit[2]; // en

// Cases where the project/lang is unsupported (e.g. proj = "facebook", for example)

// should be handled by Earwig's.

cv.searchParams.set('action', 'search');

cv.searchParams.set('lang', lang);

cv.searchParams.set('project', proj);

cv.searchParams.set('oldid', `${_revid}`);

cv.searchParams.set('use_engine', '0');

cv.searchParams.set('use_links', '1');

}

return h_1("span", { class: "mw-changeslist-links" },

h_1("span", null,

h_1("a", { rel: "noopener", href: cur, title: mw.msg('deputy.session.revision.cur.tooltip'), target: "_blank" }, mw.msg('deputy.revision.cur'))),

h_1("span", null, (!_parentid && !missing) ?

mw.msg('deputy.session.revision.prev') :

h_1("a", { rel: "noopener", href: prev, title: mw.msg('deputy.session.revision.prev.tooltip'), target: "_blank" }, mw.msg('deputy.revision.prev'))),

!!window.deputy.config.cci.showCvLink &&

cv &&

h_1("span", null,

h_1("a", { rel: "noopener", href: cv.toString(), title: mw.msg('deputy.session.revision.cv.tooltip'), target: "_blank" }, mw.msg('deputy.session.revision.cv'))));

}

/**

* @return HTML element

*/

function NewPageIndicator() {

return h_1("abbr", { class: "newpage", title: mw.msg('deputy.revision.new.tooltip') }, mw.msg('deputy.revision.new'));

}

/**

* @param root0

* @param root0.timestamp

* @return HTML element

*/

function ChangesListTime({ timestamp }) {

const time = new Date(timestamp);

const formattedTime = time.toLocaleTimeString(USER_LOCALE, {

hourCycle: 'h24',

timeStyle: mw.user.options.get('date') === 'ISO 8601' ? 'long' : 'short'

});

return h_1("span", { class: "mw-changeslist-time" }, formattedTime);

}

/**

* @param root0

* @param root0.revision

* @param root0.link

* @return HTML element

*/

function ChangesListDate({ revision, link }) {

var _a;

// `texthidden` would be indeterminate if the `{timestamp}` type was used

if (revision.texthidden) {

// Don't give out a link if the revision was deleted

link = false;

}

const time = new Date(revision.timestamp);

let now = window.moment(time);

if (window.deputy && window.deputy.config.cci.forceUtc.get()) {

now = now.utc();

}

const formattedTime = time.toLocaleTimeString(USER_LOCALE, {

hourCycle: 'h24',

timeStyle: mw.user.options.get('date') === 'ISO 8601' ? 'long' : 'short',

timeZone: ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.config.cci.forceUtc.get()) ? 'UTC' : undefined

});

const formattedDate = now.locale(USER_LOCALE).format({

dmy: 'D MMMM YYYY',

mdy: 'MMMM D, Y',

ymd: 'YYYY MMMM D',

'ISO 8601': 'YYYY:MM:DD[T]HH:mm:SS'

}[mw.user.options.get('date')]);

const comma = mw.msg('comma-separator');

return link !== false ?

h_1("a", { class: "mw-changeslist-date", href: getRevisionURL(revision.revid, revision.page.title) },

formattedTime,

comma,

formattedDate) :

h_1("span", { class: classMix('mw-changeslist-date', revision.texthidden && 'history-deleted') },

formattedTime,

comma,

formattedDate);

}

/**

* @param root0

* @param root0.revision

* @return HTML element

*/

function ChangesListUser({ revision }) {

const { user, userhidden } = revision;

if (userhidden) {

return h_1("span", { class: "history-user" },

h_1("span", { class: "history-deleted mw-userlink" }, mw.msg('deputy.revision.removed.user')));

}

const userPage = new mw.Title(user, nsId('user'));

const userTalkPage = new mw.Title(user, nsId('user_talk'));

const userContribsPage = new mw.Title('Special:Contributions/' + user);

return h_1("span", { class: "history-user" },

h_1("a", { class: "mw-userlink", target: "_blank", rel: "noopener", href: mw.format(mw.config.get('wgArticlePath'), userPage.getPrefixedDb()), title: userPage.getPrefixedText() }, userPage.getMainText()),

" ",

h_1("span", { class: "mw-usertoollinks mw-changeslist-links" },

h_1("span", null,

h_1("a", { class: "mw-usertoollinks-talk", target: "_blank", rel: "noopener", href: mw.format(mw.config.get('wgArticlePath'), userTalkPage.getPrefixedDb()), title: userTalkPage.getPrefixedText() }, mw.msg('deputy.revision.talk'))),

" ",

h_1("span", null,

h_1("a", { class: "mw-usertoollinks-contribs", target: "_blank", rel: "noopener", href: mw.format(mw.config.get('wgArticlePath'), userContribsPage.getPrefixedDb()), title: userContribsPage.getPrefixedText() }, mw.msg('deputy.revision.contribs')))));

}

/**

* @param root0

* @param root0.size

* @return HTML element

*/

function ChangesListBytes({ size }) {

return h_1("span", { class: "history-size mw-diff-bytes", "data-mw-bytes": size }, mw.message('deputy.revision.bytes', size.toString()).text());

}

/**

* @param root0

* @param root0.diffsize

* @param root0.size

* @return HTML element

*/

function ChangesListDiff({ diffsize, size }) {

const DiffTag = (Math.abs(diffsize) > 500 ?

'strong' :

'span');

return h_1(DiffTag, { class: `mw-plusminus-${!diffsize ? 'null' :

(diffsize > 0 ? 'pos' : 'neg')} mw-diff-bytes`, title: diffsize == null ?

mw.msg('deputy.brokenDiff.explain') :

mw.message('deputy.revision.byteChange', size.toString()).text() }, diffsize == null ?

mw.msg('deputy.brokenDiff') :

// Messages that can be used here:

// * deputy.negativeDiff

// * deputy.positiveDiff

// * deputy.zeroDiff

mw.message(`deputy.${{

'-1': 'negative',

1: 'positive',

0: 'zero'

}[Math.sign(diffsize)]}Diff`, diffsize.toString()).text());

}

/**

* @param root0

* @param root0.page

* @param root0.page.title

* @param root0.page.ns

* @return HTML element

*/

function ChangesListPage({ page }) {

const pageTitle = new mw.Title(page.title, page.ns).getPrefixedText();

return h_1("a", { class: "mw-contributions-title", href: mw.util.getUrl(pageTitle), title: pageTitle }, pageTitle);

}

/**

* @param root0

* @param root0.tags

* @return HTML element

*/

function ChangesListTags({ tags }) {

return h_1("span", { class: "mw-tag-markers" },

h_1("a", { rel: "noopener", href: mw.format(mw.config.get('wgArticlePath'), 'Special:Tags'), title: "Special:Tags", target: "_blank" }, mw.message('deputy.revision.tags', tags.length.toString()).text()),

tags.map((v) => {

// eslint-disable-next-line mediawiki/msg-doc

const tagMessage = mw.message(`tag-${v}`).parseDom();

return [

' ',

tagMessage.text() !== '-' && unwrapJQ(h_1("span", { class: `mw-tag-marker mw-tag-marker-${v}` }), tagMessage)

];

}));

}

/**

*

* @param root0

* @param root0.revision

*/

function ChangesListMissingRow({ revision }) {

return h_1("span", null,

h_1(ChangesListLinks, { revid: revision.revid, parentid: revision.parentid, missing: true }),

' ',

h_1("i", { dangerouslySetInnerHTML: mw.message('deputy.session.revision.missing', revision.revid).parse() }));

}

/**

* @param root0

* @param root0.revision

* @param root0.format

* @return A changes list row.

*/

function ChangesListRow({ revision, format }) {

var _a, _b;

if (!format) {

format = 'history';

}

let commentElement = '';

if (revision.commenthidden) {

commentElement = h_1("span", { class: "history-deleted comment" }, mw.msg('deputy.revision.removed.comment'));

}

else if (revision.parsedcomment) {

commentElement = h_1("span", { class: "comment comment--without-parentheses",

/** Stranger danger! Yes. */

dangerouslySetInnerHTML: revision.parsedcomment });

}

else if (revision.comment) {

const comment = revision.comment

// Insert Word-Joiner to avoid parsing "templates".

.replace(/{/g, '{\u2060')

.replace(/}/g, '\u2060}');

commentElement = unwrapJQ(h_1("span", { class: "comment comment--without-parentheses" }), msgEval(comment).parseDom());

}

return h_1("span", null,

h_1(ChangesListLinks, { revid: revision.revid, parentid: revision.parentid }),

" ",

!revision.parentid && h_1(NewPageIndicator, null),

h_1(ChangesListTime, { timestamp: revision.timestamp }),

h_1(ChangesListDate, { revision: revision }),

" ",

format === 'history' && h_1(ChangesListUser, { revision: revision }),

" ",

h_1("span", { class: "mw-changeslist-separator" }),

" ",

format === 'history' && h_1(ChangesListBytes, { size: revision.size }),

" ",

h_1(ChangesListDiff, { size: revision.size, diffsize: revision.diffsize }),

" ",

h_1("span", { class: "mw-changeslist-separator" }),

" ",

format === 'contribs' && h_1(ChangesListPage, { page: revision.page }),

" ",

commentElement,

" ",

((_b = (_a = revision.tags) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : -1) > 0 &&

h_1(ChangesListTags, { tags: revision.tags }));

}

/**

* Get the API error text from an API response.

*

* @param errorData

* @param n Get the `n`th error. Defaults to 0 (first error).

*/

function getApiErrorText(errorData, n = 0) {

var _a, _b, _c, _d, _e, _f, _g;

// errorformat=html

return ((_b = (_a = errorData.errors) === null || _a === void 0 ? void 0 : _a[n]) === null || _b === void 0 ? void 0 : _b.html) ?

h_1("span", { dangerouslySetInnerHTML: (_d = (_c = errorData.errors) === null || _c === void 0 ? void 0 : _c[n]) === null || _d === void 0 ? void 0 : _d.html }) :

(

// errorformat=plaintext/wikitext

(_g = (_f = (_e = errorData.errors) === null || _e === void 0 ? void 0 : _e[n]) === null || _f === void 0 ? void 0 : _f.text) !== null && _g !== void 0 ? _g :

// errorformat=bc

errorData.info);

}

/**

* A specific revision for a section row.

*/

class DeputyContributionSurveyRevision extends EventTarget {

/**

* @return `true` the current revision has been checked by the user or `false` if not.

*/

get completed() {

var _a, _b;

return (_b = (_a = this.completedCheckbox) === null || _a === void 0 ? void 0 : _a.isSelected()) !== null && _b !== void 0 ? _b : false;

}

/**

* Set the value of the completed checkbox.

*

* @param value The new value

*/

set completed(value) {

var _a;

(_a = this.completedCheckbox) === null || _a === void 0 ? void 0 : _a.setSelected(value);

}

/**

* @return The hash used for autosave keys

*/

get autosaveHash() {

return `CASE--${this.uiRow.row.casePage.title.getPrefixedDb()}+PAGE--${this.uiRow.row.title.getPrefixedDb()}+REVISION--${this.revision.revid}`;

}

/**

* @param revision

* @param row

* @param options

* @param options.expanded

*/

constructor(revision, row, options = {}) {

var _a;

super();

this.revisionStatusUpdateListener = this.onRevisionStatusUpdate.bind(this);

/**

* The diff view of the given revision. May also be "loading" text, or

* null if the diff view has not yet been set.

*

* @private

*/

this.diff = null;

this.revision = revision;

this.uiRow = row;

this.autoExpanded = (_a = options.expanded) !== null && _a !== void 0 ? _a : false;

if (this.statusAutosaveFunction == null) {

// TODO: types-mediawiki limitation

this.statusAutosaveFunction = mw.util.throttle(() => __awaiter(this, void 0, void 0, function* () {

yield this.saveStatus();

}), 500);

}

}

/**

* Save the status and comment for this row to DeputyStorage.

*/

saveStatus() {

return __awaiter(this, void 0, void 0, function* () {

if (this.completed) {

yield window.deputy.storage.db.put('diffStatus', {

hash: this.autosaveHash

});

}

else {

yield window.deputy.storage.db.delete('diffStatus', this.autosaveHash);

}

});

}

/**

* Gets the database-saved status. Used for getting the autosaved values of the status and

* closing comments.

*/

getSavedStatus() {

return __awaiter(this, void 0, void 0, function* () {

return (yield window.deputy.storage.db.get('diffStatus', this.autosaveHash)) != null;

});

}

/**

* Listener for revision status updates from the root session.

*

* @param root0

* @param root0.data

*/

onRevisionStatusUpdate({ data }) {

if (this.uiRow.row.casePage.pageId === data.caseId &&

this.uiRow.row.title.getPrefixedText() === data.page &&

this.revision.revid === data.revision) {

this.completed = data.status;

window.deputy.comms.reply(data, {

type: 'acknowledge'

});

}

}

/**

* Performs cleanup before removal.

*/

close() {

window.deputy.comms.removeEventListener('revisionStatusUpdate', this.revisionStatusUpdateListener);

}

/**

* Prepares the completed checkbox (and preload it with a check if it's been saved in

* the cache).

*/

prepare() {

return __awaiter(this, void 0, void 0, function* () {

this.completedCheckbox = new OO.ui.CheckboxInputWidget({

title: mw.msg('deputy.session.revision.assessed'),

selected: yield this.getSavedStatus(),

classes: ['dp-cs-rev-checkbox']

});

this.completedCheckbox.on('change', (checked) => {

var _a, _b, _c;

this.dispatchEvent(new CustomEvent('update', {

detail: {

checked: checked,

revision: this.revision

}

}));

window.deputy.comms.send({

type: 'revisionStatusUpdate',

caseId: this.uiRow.row.casePage.pageId,

page: this.uiRow.row.title.getPrefixedText(),

revision: this.revision.revid,

status: checked,

nextRevision: (_c = (_b = (_a = this.uiRow.revisions) === null || _a === void 0 ? void 0 : _a.find((revision) => !revision.completed &&

revision.revision.revid !== this.revision.revid)) === null || _b === void 0 ? void 0 : _b.revision.revid) !== null && _c !== void 0 ? _c : null

});

this.statusAutosaveFunction();

});

this.diffToggle = new OO.ui.ToggleButtonWidget({

label: mw.msg('deputy.session.revision.diff.toggle'),

invisibleLabel: true,

indicator: 'down',

framed: false,

classes: ['dp-cs-rev-toggleDiff'],

value: this.autoExpanded

});

this.diff = h_1("div", { class: "dp-cs-rev-diff" });

let loaded = false;

const handleDiffToggle = (active) => {

this.diffToggle.setIndicator(active ? 'up' : 'down');

if (!active) {

this.diff.classList.toggle('dp-cs-rev-diff--hidden', true);

return;

}

if (this.diff.classList.contains('dp-cs-rev-diff--errored')) {

// Error occurred previously, remake diff panel

this.diff = swapElements(this.diff, h_1("div", { class: "dp-cs-rev-diff" }));

}

else if (loaded) {

this.diff.classList.toggle('dp-cs-rev-diff--hidden', false);

}

if (active && !loaded) {

// Going active, clear the element out

Array.from(this.diff.children).forEach((child) => this.diff.removeChild(child));

this.diff.setAttribute('class', 'dp-cs-rev-diff');

this.diff.appendChild(h_1(DeputyLoadingDots, null));

const comparePromise = MwApi.action.get({

action: 'compare',

fromrev: this.revision.revid,

torelative: 'prev',

prop: 'diff'

});

const stylePromise = mw.loader.using('mediawiki.diff.styles');

// Promise.all not used here since we need to use JQuery.Promise#then

// if we want to access the underlying error response.

$.when([comparePromise, stylePromise])

.then((results) => results[0])

.then((data) => {

unwrapWidget(this.diffToggle).classList.add('dp-cs-rev-toggleDiff--loaded');

// Clear element out again

Array.from(this.diff.children).forEach((child) => this.diff.removeChild(child));

// https://youtrack.jetbrains.com/issue/WEB-61047

// noinspection JSXDomNesting

const diffTable = h_1("table", { class: classMix('diff', `diff-editfont-${mw.user.options.get('editfont')}`) },

h_1("colgroup", null,

h_1("col", { class: "diff-marker" }),

h_1("col", { class: "diff-content" }),

h_1("col", { class: "diff-marker" }),

h_1("col", { class: "diff-content" })));

// Trusted .innerHTML (data always comes from MediaWiki Action API)

diffTable.innerHTML += data.compare.body;

diffTable.querySelectorAll('tr').forEach((tr) => {

// Delete all header rows

if (tr.querySelector('.diff-lineno')) {

removeElement(tr);

return;

}

// Delete all no-change rows (gray rows)

if (tr.querySelector('td.diff-context')) {

removeElement(tr);

}

});

this.diff.classList.toggle('dp-cs-rev-diff--loaded', true);

this.diff.classList.toggle('dp-cs-rev-diff--errored', false);

this.diff.appendChild(diffTable);

loaded = true;

}, (_error, errorData) => {

// Clear element out again

Array.from(this.diff.children).map((child) => this.diff.removeChild(child));

this.diff.classList.toggle('dp-cs-rev-diff--loaded', true);

this.diff.classList.toggle('dp-cs-rev-diff--errored', true);

this.diff.appendChild(unwrapWidget(DeputyMessageWidget({

type: 'error',

message: mw.msg('deputy.session.revision.diff.error', errorData ?

getApiErrorText(errorData) :

_error.message)

})));

});

}

};

this.diffToggle.on('change', (checked) => {

handleDiffToggle(checked);

});

if (this.autoExpanded) {

handleDiffToggle(true);

}

});

}

/**

* @inheritDoc

*/

render() {

var _a;

window.deputy.comms.addEventListener('revisionStatusUpdate', this.revisionStatusUpdateListener);

// Be wary of the spaces between tags.

return this.element = h_1("div", { class: ((_a = this.revision.tags) !== null && _a !== void 0 ? _a : []).map((v) => 'mw-tag-' + v

.replace(/[^A-Z0-9-]/gi, '')

.replace(/\s/g, '_')).join(' ') },

unwrapWidget(this.completedCheckbox),

unwrapWidget(this.diffToggle),

this.revision.missing ?

h_1(ChangesListMissingRow, { revision: this.revision }) :

h_1(ChangesListRow, { revision: this.revision }),

this.diff);

}

/**

* Sets the disabled state of this section.

*

* @param disabled

*/

setDisabled(disabled) {

var _a;

(_a = this.completedCheckbox) === null || _a === void 0 ? void 0 : _a.setDisabled(disabled);

this.disabled = disabled;

}

}

/**

* Attempt to guess the author of a comment from the comment signature.

*

* @param comment The comment to read.

* @return The author of the comment

*/

function guessAuthor (comment) {

const userRegex = /\[\[User(?:[ _]talk)?:([^|\]]+?)(?:\|[^\]]+?|]])/g;

const matches = [];

let match = userRegex.exec(comment);

while (match != null) {

matches.push(match[1]);

match = userRegex.exec(comment);

}

return matches.length === 0 ? null : matches[matches.length - 1];

}

/**

* Used for detecting Deputy traces.

*/

const traceRegex = /\s*$/g;

/**

* Generates the Deputy trace, used to determine who assessed a row.

*

* @return the Deputy trace

*/

function generateTrace() {

return ``;

}

/**

* Attempts to extract the Deputy trace from wikitext.

*

* @param wikitext

* @return The trace author and timestamp (if available), or null if a trace was not found.

*/

function guessTrace(wikitext) {

const traceExec = cloneRegex$1(traceRegex).exec(wikitext);

if (traceExec) {

return {

author: traceExec[1],

timestamp: new Date(traceExec[2])

};

}

else {

return null;

}

}

/**

* Displayed when a ContributionSurveyRow has no remaining diffs. Deputy is not able

* to perform the contribution survey itself, so there is no revision list.

*/

class DeputyFinishedContributionSurveyRow {

/**

* @param props Element properties

* @param props.row The reference row

* @param props.originalElement

* @return An HTML element

*/

constructor(props) {

this.props = props;

}

/**

* Checks if this row has a signature.

*

* @return `true` if this row's comment has a signature

*/

hasSignature() {

return this.author ? (this.timestamp ? true : 'maybe') : false;

}

/**

* Renders the element.

*

* @return The rendered row content

*/

render() {

var _a, _b;

const props = this.props;

const parser = window.deputy.session.rootSession.parser;

// Use DiscussionTools to identify the user and timestamp.

let parsedComment;

try {

parsedComment = (_b = (_a = parser.parse(props.originalElement)) === null || _a === void 0 ? void 0 : _a.commentItems) === null || _b === void 0 ? void 0 : _b[0];

}

catch (e) {

warn('Failed to parse user signature.', e);

}

if (!parsedComment) {

// See if the Deputy trace exists.

const fromTrace = guessTrace(props.row.wikitext);

if (fromTrace) {

this.author = fromTrace.author;

this.timestamp = fromTrace.timestamp && !isNaN(fromTrace.timestamp.getTime()) ?

window.moment(fromTrace.timestamp) :

undefined;

}

else {

// Fall back to guessing the author based on an in-house parser.

this.author = guessAuthor(props.row.comment);

// Don't even try to guess the timestamp.

}

}

else {

this.author = parsedComment.author;

this.timestamp = parsedComment.timestamp;

}

if (this.author) {

const userPage = new mw.Title(this.author, nsId('user'));

const talkPage = userPage.getTalkPage();

const contribsPage = new mw.Title('Special:Contributions/' + this.author);

const params = [

(h_1("span", null,

h_1("a", { target: "_blank", rel: "noopener", href: mw.util.getUrl(userPage.getPrefixedDb()), title: userPage.getPrefixedText() }, this.author),

" ",

h_1("span", { class: "mw-usertoollinks mw-changeslist-links" },

h_1("span", null,

h_1("a", { class: "mw-usertoollinks-talk", target: "_blank", rel: "noopener", href: mw.util.getUrl(talkPage.getPrefixedDb()), title: talkPage.getPrefixedText() }, mw.msg('deputy.revision.talk'))),

h_1("span", null,

h_1("a", { class: "mw-usertoollinks-contribs", target: "_blank", rel: "noopener", href: mw.util.getUrl(contribsPage.getPrefixedDb()), title: contribsPage.getPrefixedText() }, mw.msg('deputy.revision.contribs'))))))

];

if (this.timestamp) {

params.push(this.timestamp.toDate()

.toLocaleString('en-US', { dateStyle: 'long', timeStyle: 'short' }), this.timestamp.toNow(true));

}

return unwrapJQ(h_1("i", null), mw.message(this.timestamp ?

'deputy.session.row.checkedComplete' :

'deputy.session.row.checked', ...params).parseDom());

}

else {

return null;

}

}

}

/**

*

*/

class DeputyCCIStatusDropdown extends EventTarget {

/**

* @return The currently-selected status of this dropdown.

*/

get status() {

var _a, _b;

return (_b = (_a = this.dropdown.getMenu().findSelectedItem()) === null || _a === void 0 ? void 0 : _a.getData()) !== null && _b !== void 0 ? _b : null;

}

/**

* Sets the currently-selected status of this dropdown.

*/

set status(status) {

this.dropdown.getMenu().selectItemByData(status);

this.setOptionDisabled(ContributionSurveyRowStatus.Unknown, status !== ContributionSurveyRowStatus.Unknown, false);

this.refresh();

}

/**

* Create a new DeputyCCIStatusDropdown object.

*

* @param row The origin row of this dropdown.

* For the root session, this is simply the `row` field of the

* DeputyContributionSurveyRow that is handling the row.

* For dependent sessions, this is a much simpler version which includes

* only the case page info and the row title.

* @param row.casePage The DeputyCase for this dropdown

* @param row.title The title of the row (page) that this dropdown accesses

* @param options Additional construction options, usually used by the root session.

* @param row.type

*/

constructor(row, options = {}) {

var _a, _b;

super();

this.row = row;

this.options = new Map();

for (const status in ContributionSurveyRowStatus) {

if (isNaN(+status)) {

// String key, skip.

continue;

}

const statusName = ContributionSurveyRowStatus[status];

// The following classes are used here:

// * dp-cs-row-status--unfinished

// * dp-cs-row-status--unknown

// * dp-cs-row-status--withviolations

// * dp-cs-row-status--withoutviolations

// * dp-cs-row-status--missing

const option = new OO.ui.MenuOptionWidget({

classes: ['dp-cs-row-status--' + statusName.toLowerCase()],

data: +status,

label: mw.message('deputy.session.row.status.' +

statusName[0].toLowerCase() +

statusName.slice(1)).text(),

icon: DeputyCCIStatusDropdown.menuOptionIcons[+status],

// Always disable if Unknown, as Unknown is merely a placeholder value.

disabled: +status === ContributionSurveyRowStatus.Unknown

});

this.options.set(+status, option);

}

this.dropdown = new OO.ui.DropdownWidget(Object.assign({

classes: ['dp-cs-row-status'],

label: mw.msg('deputy.session.row.status')

}, (_a = options.widgetOptions) !== null && _a !== void 0 ? _a : {}, {

menu: {

items: Array.from(this.options.values())

}

}));

// Place before event listeners to prevent them from firing too early.

if (options.status != null) {

this.status = options.status;

}

if (options.enabled != null) {

this.setEnabledOptions(options.enabled);

}

const requireAcknowledge = (_b = options.requireAcknowledge) !== null && _b !== void 0 ? _b : true;

let pastStatus = this.status;

let processing = false;

let incommunicable = false;

this.dropdownChangeListener = () => __awaiter(this, void 0, void 0, function* () {

if (incommunicable) {

// Reset flag.

incommunicable = false;

return;

}

else if (processing) {

return;

}

processing = true;

this.dispatchEvent(Object.assign(new Event('change'), {

status: this.status

}));

this.refresh();

const message = yield window.deputy.comms[requireAcknowledge ? 'sendAndWait' : 'send']({

type: 'pageStatusUpdate',

caseId: this.row.casePage.pageId,

page: this.row.title.getPrefixedText(),

status: this.status

});

if (requireAcknowledge && message == null) {

// Broadcast failure as an event and restore to the past value.

// This will cause an infinite loop, so set `incommunicable` to true to

// avoid that.

this.dispatchEvent(Object.assign(new Event('updateFail'), {

data: {

former: pastStatus,

target: this.status

}

}));

incommunicable = true;

this.status = pastStatus;

}

else {

// Overwrite the past status.

pastStatus = this.status;

}

processing = false;

});

this.dropdownUpdateListener = (event) => {

var _a, _b;

const { data: message } = event;

if (message.caseId === this.row.casePage.pageId &&

message.page === this.row.title.getPrefixedText()) {

// Update the enabled and disabled options.

for (const enabled of (_a = message.enabledOptions) !== null && _a !== void 0 ? _a : []) {

this.setOptionDisabled(enabled, false, false);

}

for (const disabled of (_b = message.disabledOptions) !== null && _b !== void 0 ? _b : []) {

this.setOptionDisabled(disabled, true, false);

}

// Update the status.

this.status = message.status;

window.deputy.comms.reply(message, { type: 'acknowledge' });

}

};

window.deputy.comms.addEventListener('pageStatusUpdate', this.dropdownUpdateListener);

// Change the icon of the dropdown when the value changes.

this.dropdown.getMenu().on('select', this.dropdownChangeListener);

// Make the menu larger than the actual dropdown.

this.dropdown.getMenu().on('ready', () => {

this.dropdown.getMenu().toggleClipping(false);

unwrapWidget(this.dropdown.getMenu()).style.width = '20em';

});

}

/**

* Performs cleanup

*/

close() {

window.deputy.comms.removeEventListener('pageStatusUpdate', this.dropdownUpdateListener);

}

/**

* @inheritDoc

*/

addEventListener(type, callback, options) {

super.addEventListener(type, callback, options);

}

/**

* Refreshes the status dropdown for any changes. This function must NOT

* modify `this.status`, or else it will cause a stack overflow.

*/

refresh() {

const icon = DeputyCCIStatusDropdown.menuOptionIcons[this.status];

this.dropdown.setIcon(icon);

}

/**

* Gets a list of enabled options.

*

* @return An array of {@link ContributionSurveyRowStatus}es

*/

getEnabledOptions() {

return Array.from(this.options.keys()).filter((status) => {

return !this.options.get(status).isDisabled();

});

}

/**

*

* @param enabledOptions

* @param broadcast

*/

setEnabledOptions(enabledOptions, broadcast = false) {

for (const status in ContributionSurveyRowStatus) {

const option = this.options.get(+status);

if (option == null) {

// Skip if null.

continue;

}

const toEnable = enabledOptions.indexOf(+status) !== -1;

const optionDisabled = option.isDisabled();

if (toEnable && optionDisabled) {

this.setOptionDisabled(+status, false, broadcast);

}

else if (!toEnable && !optionDisabled) {

this.setOptionDisabled(+status, true, broadcast);

}

}

}

/**

* Sets the disabled state of the dropdown. Does not affect menu options.

*

* @param disabled

*/

setDisabled(disabled) {

this.dropdown.setDisabled(disabled);

}

/**

* Sets the 'disable state' of specific menu options. For the `Unknown` option, a

* specific case is made which removes the option from the menu entirely.

*

* @param status

* @param disabled

* @param broadcast

*/

setOptionDisabled(status, disabled, broadcast = false) {

if (status === ContributionSurveyRowStatus.Unknown) {

// Special treatment. This hides the entire option from display.

this.options.get(status).toggle(disabled);

}

else {

// Disable the disable flag.

this.options.get(status).setDisabled(disabled);

}

if (this.status === status && disabled) {

this.selectNextBestValue(status);

}

if (broadcast) {

window.deputy.comms.send({

type: 'pageStatusUpdate',

caseId: this.row.casePage.pageId,

page: this.row.title.getPrefixedText(),

status: this.status,

[disabled ? 'disabledOptions' : 'enabledOptions']: [status]

});

}

}

/**

* When an option is about to be closed and the current status matches that option,

* this function will find the next best option and select it. The next best value

* is as follows:

*

* For

* - Unfinished: WithoutViolations, unless it's `pageonly`, on which it'll be kept as is.

* - Unknown: Unfinished

* - WithViolations: _usually not disabled, kept as is_

* - WithoutViolations: _usually not disabled, kept as is_

* - Missing: _usually not disabled, kept as is_

* - PresumptiveRemoval: _usually not disabled, kept as is_

*

* @param status The status that was changed into

*/

selectNextBestValue(status) {

if (status === ContributionSurveyRowStatus.Unfinished) {

if (this.row.type === 'pageonly') {

// Leave it alone.

return;

}

this.status = ContributionSurveyRowStatus.WithoutViolations;

}

else if (status === ContributionSurveyRowStatus.Unknown) {

this.status = ContributionSurveyRowStatus.Unfinished;

}

}

}

DeputyCCIStatusDropdown.menuOptionIcons = {

[ContributionSurveyRowStatus.Unfinished]: null,

[ContributionSurveyRowStatus.Unknown]: 'alert',

[ContributionSurveyRowStatus.WithViolations]: 'check',

[ContributionSurveyRowStatus.WithoutViolations]: 'close',

[ContributionSurveyRowStatus.Missing]: 'help',

[ContributionSurveyRowStatus.PresumptiveRemoval]: 'trash'

};

/**

* Shows a confirmation dialog, if the user does not have danger mode enabled.

* If the user has danger mode enabled, this immediately resolves to true, letting

* the action run immediately.

*

* Do not use this with any action that can potentially break templates, user data,

* or cause irreversible data loss.

*

* @param config The user's configuration

* @param message See {@link OO.ui.MessageDialog}'s parameters.

* @param options See {@link OO.ui.MessageDialog}'s parameters.

* @return Promise resolving to a true/false boolean.

*/

function dangerModeConfirm(config, message, options) {

if (config.all.core.dangerMode.get()) {

return $.Deferred().resolve(true);

}

else {

return OO.ui.confirm(message, options);

}

}

var DeputyContributionSurveyRowState;

(function (DeputyContributionSurveyRowState) {

/*

* Special boolean that gets set to true if the supposed data from `this.wikitext`

* should not be trusted. This is usually due to UI element failures or network

* issues that cause the revision list to be loaded improperly (or to be not

* loaded at all). `this.wikitext` will return the original wikitext, if capable.

*/

DeputyContributionSurveyRowState[DeputyContributionSurveyRowState["Broken"] = -1] = "Broken";

// Data not loaded, may be appended.

DeputyContributionSurveyRowState[DeputyContributionSurveyRowState["Loading"] = 0] = "Loading";

// Data loaded, ready for use.

DeputyContributionSurveyRowState[DeputyContributionSurveyRowState["Ready"] = 1] = "Ready";

// Closed by `close()`.

DeputyContributionSurveyRowState[DeputyContributionSurveyRowState["Closed"] = 2] = "Closed";

})(DeputyContributionSurveyRowState || (DeputyContributionSurveyRowState = {}));

/**

* A UI element used for denoting the following aspects of a page in the contribution

* survey:

* (a) the current status of the page (violations found, no violations found, unchecked, etc.)

* (b) the name of the page

* (c) special page tags

* (d) the number of edits within that specific row

* (e) the byte size of the largest-change diff

* (f) a list of revisions related to this page (as DeputyContributionSurveyRowRevision classes)

* (g) closing comments

*/

class DeputyContributionSurveyRow extends EventTarget {

/**

* @return `true` if:

* (a) this row's status changed OR

* (b) this row's comment changed

*

* This does not check if the revisions themselves were modified.

*/

get statusModified() {

return (this.status !== this.row.originalStatus ||

this.comments !== this.row.getActualComment());

}

/**

* @return `true` if:

* (a) `statusModified` is true OR

* (b) diffs were marked as completed

*

* This does not check if the revisions themselves were modified.

*/

get modified() {

var _a;

return this.statusModified ||

(

// This is assumed as a modification, since all diffs are automatically removed

// from the page whenever marked as complete. Therefore, there can never be a

// situation where a row's revisions have been modified but there are no completed

// revisions.

(_a = this.revisions) === null || _a === void 0 ? void 0 : _a.some((v) => v.completed));

}

/**

* @return The current status of this row.

*/

get status() {

return this.row.status;

}

/**

* Set the current status of this row.

*

* @param status The new status to apply

*/

set status(status) {

this.row.status = status;

}

/**

* @return `true` if this row has all diffs marked as completed.

*/

get completed() {

if (this.revisions == null) {

return true;

}

return this.revisions

.every((v) => v.completed);

}

/**

* @return `true` if this element is broken.

*/

get broken() {

return this.state === DeputyContributionSurveyRowState.Broken;

}

/**

* @return The comments for this row (as added by a user)

*/

get comments() {

var _a;

return (_a = this.commentsTextInput) === null || _a === void 0 ? void 0 : _a.getValue();

}

/**

* Generates a wikitext string representation of this row, preserving existing wikitext

* whenever possible.

*

* @return Wikitext

*/

get wikitext() {

var _a, _b, _c, _d;

// Broken, loading, or closed. Just return the original wikitext.

if (this.state !== DeputyContributionSurveyRowState.Ready) {

return this.originalWikitext;

}

if (this.wasFinished == null) {

warn('Could not determine if this is an originally-finished or ' +

'originally-unfinished row. Assuming unfinished and moving on...');

}

// "* "

let result = this.row.data.bullet;

if (this.row.data.creation) {

result += "N ";

}

// :Example

result += `${this.row.data.page}`;

// "{bullet}{creation}{page}{extras}{diffs}{comments}"

if (this.row.extras) {

result += `${this.row.extras}`;

}

const unfinishedDiffs = (_c = (_b = (_a = this.revisions) === null || _a === void 0 ? void 0 : _a.filter((v) => !v.completed)) === null || _b === void 0 ? void 0 : _b.sort((a, b) => ContributionSurveyRow.getSorterFunction(this.sortOrder)(a.revision, b.revision))) !== null && _c !== void 0 ? _c : [];

let diffsText = '';

if (unfinishedDiffs.length > 0) {

diffsText += unfinishedDiffs.map((v) => {

return mw.format(this.row.data.diffTemplate, String(v.revision.revid), v.revision.diffsize == null ?

// For whatever reason, diffsize is missing. Fall back to the text we had

// previously.

v.uiRow.row.data.revidText[v.revision.revid] :

String(v.revision.diffsize > 0 ?

'+' + v.revision.diffsize : v.revision.diffsize));

}).join('');

result += mw.format(this.row.data.diffsTemplate, diffsText);

if (this.row.data.comments) {

// Comments existed despite not being finished yet. Allow anyway.

result += this.row.data.comments;

}

}

else {

/**

* Function will apply the current user values to the row.

*/

const useUserData = () => {

let addComments = false;

switch (this.status) {

// TODO: l10n

case ContributionSurveyRowStatus.Unfinished:

// This state should not exist. Just add signature (done outside of switch).

break;

case ContributionSurveyRowStatus.Unknown:

// This state should not exist. Try to append comments (because if this

// branch is running, the comment must have not been added by the positive

// branch of this if statement). Don't append user-provided comments.

result += this.row.comment;

break;

case ContributionSurveyRowStatus.WithViolations:

result += '{{y}}';

addComments = true;

break;

case ContributionSurveyRowStatus.WithoutViolations:

result += '{{n}}';

addComments = true;

break;

case ContributionSurveyRowStatus.Missing:

result += '{{?}}';

addComments = true;

break;

case ContributionSurveyRowStatus.PresumptiveRemoval:

result += '{{x}}';

addComments = true;

break;

}

const userComments = this.comments

.replace(/~~~~\s*$/g, '')

.trim();

if (addComments && userComments.length > 0) {

result += ' ' + userComments;

}

// Sign.

result += ' ~~~~';

};

if (this.statusModified) {

// Modified. Use user data.

useUserData();

}

else if ((_d = this.wasFinished) !== null && _d !== void 0 ? _d : false) {

// No changes. Just append original closure comments.

result += this.row.comment;

}

// Otherwise, leave this row unchanged.

}

return result;

}

/**

* @return The hash used for autosave keys

*/

get autosaveHash() {

return `CASE--${this.row.casePage.title.getPrefixedDb()}+H--${this.section.headingName}-${this.section.headingN}+PAGE--${this.row.title.getPrefixedDb()}`;

}

/**

* Creates a new DeputyContributionSurveyRow object.

*

* @param row The contribution survey row data

* @param originalElement

* @param originalWikitext

* @param section The section that this row belongs to

*/

constructor(row, originalElement, originalWikitext, section) {

super();

/**

* The state of this element.

*/

this.state = DeputyContributionSurveyRowState.Loading;

/**

* Responder for session requests.

*/

this.statusRequestResponder = this.sendStatusResponse.bind(this);

this.nextRevisionRequestResponder = this.sendNextRevisionResponse.bind(this);

this.row = row;

this.originalElement = originalElement;

this.additionalComments = this.extractAdditionalComments();

this.originalWikitext = originalWikitext;

this.section = section;

}

/**

* Extracts HTML elements which may be additional comments left by others.

* The general qualification for this is that it has to be a list block

* element that comes after the main line (in this case, it's detected after

* the last .

* This appears in the following form in wikitext:

*

* ```

* * Page (...) ...

* *: Hello! <-- definition list block

* ** What!? <-- sub ul

* *# Yes. <-- sub ol

* * Page (...) ...

...
<-- inline div

* ```

*

* Everything else (`*

...`, `*'''...`, `*`, etc.) is considered

* not to be an additional comment.

*

* If no elements were found, this returns an empty array.

*

* @return An array of HTMLElements

*/

extractAdditionalComments() {

// COMPAT: Specific to MER-C contribution surveyor

// Initialize to first successive diff link.

let lastSuccessiveDiffLink = this.originalElement.querySelector('a[href^="/wiki/Special:Diff/"]');

const elements = [];

if (!lastSuccessiveDiffLink) {

// No diff links. Get last element, check if block element, and crawl backwards.

let nextDiscussionElement = this.originalElement.lastElementChild;

while (nextDiscussionElement &&

window.getComputedStyle(nextDiscussionElement, '').display === 'block') {

elements.push(nextDiscussionElement);

nextDiscussionElement = nextDiscussionElement.previousElementSibling;

}

}

else {

while (lastSuccessiveDiffLink.nextElementSibling &&

lastSuccessiveDiffLink.nextElementSibling.tagName === 'A' &&

lastSuccessiveDiffLink

.nextElementSibling

.getAttribute('href')

.startsWith('/wiki/Special:Diff')) {

lastSuccessiveDiffLink = lastSuccessiveDiffLink.nextElementSibling;

}

// The first block element after `lastSuccessiveDiffLink` is likely discussion,

// and everything after it is likely part of such discussion.

let pushing = false;

let nextDiscussionElement = lastSuccessiveDiffLink.nextElementSibling;

while (nextDiscussionElement != null) {

if (!pushing &&

window.getComputedStyle(nextDiscussionElement).display === 'block') {

pushing = true;

elements.push(nextDiscussionElement);

}

else if (pushing) {

elements.push(nextDiscussionElement);

}

nextDiscussionElement = nextDiscussionElement.nextElementSibling;

}

}

return elements;

}

/**

* Load the revision data in and change the UI element respectively.

*/

loadData() {

return __awaiter(this, void 0, void 0, function* () {

try {

const diffs = yield this.row.getDiffs();

this.sortOrder = ContributionSurveyRow.guessSortOrder(diffs.values());

this.wasFinished = this.row.completed;

if (this.row.completed) {

this.renderRow(diffs, this.renderFinished());

}

else {

this.renderRow(diffs, yield this.renderUnfinished(diffs));

const savedStatus = yield this.getSavedStatus();

if (!this.wasFinished && savedStatus) {

// An autosaved status exists. Let's use that.

this.commentsTextInput.setValue(savedStatus.comments);

this.statusDropdown.status = savedStatus.status;

this.onUpdate();

}

}

window.deputy.comms.addEventListener('pageStatusRequest', this.statusRequestResponder);

window.deputy.comms.addEventListener('pageNextRevisionRequest', this.nextRevisionRequestResponder);

this.state = DeputyContributionSurveyRowState.Ready;

}

catch (e) {

error('Caught exception while loading data', e);

this.state = DeputyContributionSurveyRowState.Broken;

this.renderRow(null, unwrapWidget(new OO.ui.MessageWidget({

type: 'error',

label: mw.msg('deputy.session.row.error', e.message)

})));

this.setDisabled(true);

}

});

}

/**

* Perform UI updates and recheck possible values.

*/

onUpdate() {

if (this.statusAutosaveFunction == null) {

// TODO: types-mediawiki limitation

this.statusAutosaveFunction = mw.util.throttle(() => __awaiter(this, void 0, void 0, function* () {

yield this.saveStatus();

}), 500);

}

if (this.revisions && this.statusDropdown) {

if (this.row.type !== 'pageonly') {

// Only disable this option if the row isn't already finished.

this.statusDropdown.setOptionDisabled(ContributionSurveyRowStatus.Unfinished, this.completed, true);

}

const unfinishedWithStatus = this.statusModified && !this.completed;

if (this.unfinishedMessageBox) {

this.unfinishedMessageBox.toggle(

// If using danger mode, this should always be enabled.

!window.deputy.config.core.dangerMode.get() &&

unfinishedWithStatus);

}

this.statusAutosaveFunction();

}

if (this.wasFinished && this.statusModified && this.commentsField && this.finishedRow) {

this.commentsField.setNotices({

true: [mw.msg('deputy.session.row.close.sigFound')],

maybe: [mw.msg('deputy.session.row.close.sigFound.maybe')],

false: []

}[`${this.finishedRow.hasSignature()}`]);

}

else if (this.commentsField) {

this.commentsField.setNotices([]);

}

// Emit "update" event

this.dispatchEvent(new CustomEvent('update'));

}

/**

* Gets the database-saved status. Used for getting the autosaved values of the status and

* closing comments.

*/

getSavedStatus() {

var _a;

return __awaiter(this, void 0, void 0, function* () {

return (_a = yield window.deputy.storage.db.get('pageStatus', this.autosaveHash)) !== null && _a !== void 0 ? _a :

// Old hash (< v0.9.0)

yield window.deputy.storage.db.get('pageStatus', `CASE--${this.row.casePage.title.getPrefixedDb()}+PAGE--${this.row.title.getPrefixedDb()}`);

});

}

/**

* Save the status and comment for this row to DeputyStorage.

*/

saveStatus() {

return __awaiter(this, void 0, void 0, function* () {

if (this.statusModified) {

yield window.deputy.storage.db.put('pageStatus', {

hash: this.autosaveHash,

status: this.status,

comments: this.comments

});

}

});

}

/**

* Mark all revisions of this section as finished.

*/

markAllAsFinished() {

if (!this.revisions) {

// If `renderUnfinished` was never called, this will be undefined.

// We want to skip over instead.

return;

}

this.revisions.forEach((revision) => {

revision.completed = true;

});

this.onUpdate();

}

/**

* Renders the `commentsTextInput` variable (closing comments OOUI TextInputWidget)

*

* @param value

* @return The OOUI TextInputWidget

*/

renderCommentsTextInput(value) {

this.commentsTextInput = new OO.ui.MultilineTextInputWidget({

classes: ['dp-cs-row-closeComments'],

placeholder: mw.msg('deputy.session.row.closeComments'),

value: value !== null && value !== void 0 ? value : '',

autosize: true,

rows: 1

});

this.commentsTextInput.on('change', () => {

this.onUpdate();

});

return this.commentsTextInput;

}

/**

* Render the row with the "finished" state (has info

* on closer and closing comments).

*

* @return HTML element

*/

renderFinished() {

this.finishedRow = new DeputyFinishedContributionSurveyRow({

originalElement: this.originalElement,

row: this.row

});

return h_1("div", { class: "dp-cs-row-finished" },

this.finishedRow.render(),

unwrapWidget(this.commentsField = new OO.ui.FieldLayout(this.renderCommentsTextInput(this.row.getActualComment()), {

align: 'top',

invisibleLabel: true,

label: mw.msg('deputy.session.row.closeComments')

})));

}

/**

* Render the row with the "unfinished" state (has

* revision list, etc.)

*

* @param diffs

* @return HTML element

*/

renderUnfinished(diffs) {

return __awaiter(this, void 0, void 0, function* () {

this.revisions = [];

const revisionList = document.createElement('div');

revisionList.classList.add('dp-cs-row-revisions');

this.unfinishedMessageBox = new OO.ui.MessageWidget({

classes: ['dp-cs-row-unfinishedWarning'],

type: 'warning',

label: mw.msg('deputy.session.row.unfinishedWarning')

});

this.unfinishedMessageBox.toggle(false);

revisionList.appendChild(unwrapWidget(this.unfinishedMessageBox));

revisionList.appendChild(unwrapWidget(this.renderCommentsTextInput(this.row.comment)));

if (this.row.type === 'pageonly') {

revisionList.appendChild(h_1("div", { class: "dp-cs-row-pageonly" },

h_1("i", null, mw.msg('deputy.session.row.pageonly'))));

}

else {

const cciConfig = window.deputy.config.cci;

const maxSize = cciConfig.maxSizeToAutoShowDiff.get();

for (const revision of diffs.values()) {

const revisionUIEl = new DeputyContributionSurveyRevision(revision, this, {

expanded: cciConfig.autoShowDiff.get() &&

diffs.size < cciConfig.maxRevisionsToAutoShowDiff.get() &&

(maxSize === -1 || Math.abs(revision.diffsize) < maxSize)

});

revisionUIEl.addEventListener('update', () => {

// Recheck options first to avoid "Unfinished" being selected when done.

this.onUpdate();

});

yield revisionUIEl.prepare();

revisionList.appendChild(revisionUIEl.render());

this.revisions.push(revisionUIEl);

}

}

return revisionList;

});

}

/**

* Renders action button links.

*

* @return An HTML element

*/

renderLinks() {

return h_1("span", { class: "dp-cs-row-links" },

h_1("a", { class: "dp-cs-row-link dp-cs-row-edit", target: "_blank", rel: "noopener", href: mw.util.getUrl(this.row.title.getPrefixedDb(), { action: 'edit' }) }, unwrapWidget(new OO.ui.ButtonWidget({

invisibleLabel: true,

label: mw.msg('deputy.session.row.edit'),

title: mw.msg('deputy.session.row.edit'),

icon: 'edit',

framed: false

}))),

h_1("a", { class: "dp-cs-row-link dp-cs-row-talk", target: "_blank", rel: "noopener", href: mw.util.getUrl(this.row.title.getTalkPage().getPrefixedDb()) }, unwrapWidget(new OO.ui.ButtonWidget({

invisibleLabel: true,

label: mw.msg('deputy.session.row.talk'),

title: mw.msg('deputy.session.row.talk'),

icon: 'speechBubbles',

framed: false

}))),

h_1("a", { class: "dp-cs-row-link dp-cs-row-history", target: "_blank", rel: "noopener", href: mw.util.getUrl(this.row.title.getPrefixedDb(), { action: 'history' }) }, unwrapWidget(new OO.ui.ButtonWidget({

invisibleLabel: true,

label: mw.msg('deputy.session.row.history'),

title: mw.msg('deputy.session.row.history'),

icon: 'history',

framed: false

}))));

}

/**

* Renders the details of the row. Includes details such as largest diff size, diffs

* remaining, etc.

*

* @param diffs

* @return The row details as an element (or `false`, if no details are to be shown).

*/

renderDetails(diffs) {

const parts = [];

// Timestamp is always found in a non-missing diff, suppressed or not.

const validDiffs = Array.from(diffs.values()).filter((v) => v.timestamp);

if (validDiffs.length > 0) {

const diffArray = Array.from(diffs.values());

if (diffArray.some((v) => !v.parentid)) {

parts.push(mw.message('deputy.session.row.details.new', diffs.size.toString()).text());

}

// Number of edits

parts.push(mw.message('deputy.session.row.details.edits', diffs.size.toString()).text());

// Identify largest diff

const largestDiff = diffs.get(Array.from(diffs.values())

.sort(ContributionSurveyRow.getSorterFunction(ContributionSurveyRowSort.Bytes))[0]

.revid);

parts.push(

// Messages that can be used here:

// * deputy.negativeDiff

// * deputy.positiveDiff

// * deputy.zeroDiff

mw.message(`deputy.${{

'-1': 'negative',

1: 'positive',

0: 'zero'

}[Math.sign(largestDiff.diffsize)]}Diff`, largestDiff.diffsize.toString()).text());

}

const spliced = [];

for (let index = 0; index < parts.length; index++) {

spliced.push(h_1("span", { class: "dp-cs-row-detail" }, parts[index]));

if (index !== parts.length - 1) {

spliced.push(mw.msg('comma-separator'));

}

}

return parts.length === 0 ? false : h_1("span", { class: "dp-cs-row-details" },

"(",

spliced,

")");

}

/**

* Renders the "head" part of the row. Contains the status, page name, and details.

*

* @param diffs

* @param contentContainer

* @return The head of the row as an element

*/

renderHead(diffs, contentContainer) {

const possibleStatus = this.row.status;

// Build status dropdown

this.statusDropdown = new DeputyCCIStatusDropdown(this.row, {

status: possibleStatus,

requireAcknowledge: false

});

if (this.row.type !== 'pageonly' &&

((diffs && diffs.size === 0) || this.wasFinished)) {

// If there are no diffs found or `this.wasFinished` is set (both meaning there are

// no diffs and this is an already-assessed row), then the "Unfinished" option will

// be disabled. This does not apply for page-only rows, which never have diffs.

this.statusDropdown.setOptionDisabled(ContributionSurveyRowStatus.Unfinished, true);

}

this.statusDropdown.addEventListener('change', (event) => {

this.status = event.status;

this.onUpdate();

});

// Build mass checker

this.checkAllButton = new OO.ui.ButtonWidget({

icon: 'checkAll',

label: mw.msg('deputy.session.row.checkAll'),

title: mw.msg('deputy.session.row.checkAll'),

invisibleLabel: true,

framed: false

});

this.checkAllButton.on('click', () => {

dangerModeConfirm(window.deputy.config, mw.msg('deputy.session.row.checkAll.confirm')).done((confirmed) => {

if (confirmed) {

this.markAllAsFinished();

}

});

});

// Build content toggler

const contentToggle = new OO.ui.ButtonWidget({

classes: ['dp-cs-row-toggle'],

// Will be set by toggle function. Blank for now.

label: '',

invisibleLabel: true,

framed: false

});

let contentToggled = !window.deputy.config.cci.autoCollapseRows.get();

/**

* Toggles the content.

*

* @param show Whether to show the content or not.

*/

const toggleContent = (show = !contentToggled) => {

contentToggle.setIcon(show ? 'collapse' : 'expand');

contentToggle.setLabel(mw.message(show ?

'deputy.session.row.content.close' :

'deputy.session.row.content.open').text());

contentToggle.setTitle(mw.message(show ?

'deputy.session.row.content.close' :

'deputy.session.row.content.open').text());

contentContainer.style.display = show ? 'block' : 'none';

contentToggled = show;

};

toggleContent(contentToggled);

contentToggle.on('click', () => {

toggleContent();

});

return h_1("div", { class: "dp-cs-row-head" },

unwrapWidget(this.statusDropdown.dropdown),

h_1("a", { class: "dp-cs-row-title", target: "_blank", rel: "noopener", href: mw.format(mw.config.get('wgArticlePath'), this.row.title.getPrefixedDb()) }, this.row.title.getPrefixedText()),

diffs && this.renderDetails(diffs),

this.renderLinks(),

!this.wasFinished && diffs && diffs.size > 0 && unwrapWidget(this.checkAllButton),

!contentContainer.classList.contains('dp-cs-row-content-empty') &&

unwrapWidget(contentToggle));

}

/**

* Renders additional comments that became part of this row.

*

* @return An HTML element.

*/

renderAdditionalComments() {

const additionalComments = h_1("div", { class: "dp-cs-row-comments" },

h_1("b", null, mw.msg('deputy.session.row.additionalComments')),

h_1("hr", null),

h_1("div", { class: "dp-cs-row-comments-content", dangerouslySetInnerHTML: this.additionalComments.map(e => e.innerHTML).join('') }));

// Open all links in new tabs.

additionalComments.querySelectorAll('.dp-cs-row-comments-content a')

.forEach(a => a.setAttribute('target', '_blank'));

return additionalComments;

}

/**

* @param diffs

* @param content

*/

renderRow(diffs, content) {

var _a;

const contentContainer = h_1("div", { class: classMix([

'dp-cs-row-content',

!content && 'dp-cs-row-content-empty'

]) }, content);

this.element = swapElements(this.element, h_1("div", null,

this.renderHead(diffs, contentContainer),

((_a = this.additionalComments) === null || _a === void 0 ? void 0 : _a.length) > 0 && this.renderAdditionalComments(),

contentContainer));

}

/**

* @inheritDoc

*/

render() {

this.element = h_1(DeputyLoadingDots, null);

this.rootElement = h_1("div", { class: "dp-cs-row" }, this.element);

return this.rootElement;

}

/**

* Performs cleanup before removal.

*/

close() {

var _a;

this.state = DeputyContributionSurveyRowState.Closed;

window.deputy.comms.removeEventListener('pageStatusRequest', this.statusRequestResponder);

window.deputy.comms.removeEventListener('pageNextRevisionRequest', this.nextRevisionRequestResponder);

(_a = this.revisions) === null || _a === void 0 ? void 0 : _a.forEach((revision) => {

revision.close();

});

}

/**

* Sets the disabled state of this section.

*

* @param disabled

*/

setDisabled(disabled) {

var _a, _b, _c, _d;

(_a = this.statusDropdown) === null || _a === void 0 ? void 0 : _a.setDisabled(disabled);

(_b = this.commentsTextInput) === null || _b === void 0 ? void 0 : _b.setDisabled(disabled);

(_c = this.checkAllButton) === null || _c === void 0 ? void 0 : _c.setDisabled(disabled);

(_d = this.revisions) === null || _d === void 0 ? void 0 : _d.forEach((revision) => revision.setDisabled(disabled));

this.disabled = disabled;

}

/**

* Responds to a status request.

*

* @param event

*/

sendStatusResponse(event) {

var _a, _b, _c, _d;

const rev = (_a = this.revisions) === null || _a === void 0 ? void 0 : _a.find((r) => r.revision.revid === event.data.revision);

// Handles the cases:

// * Page title and revision ID (if supplied) match

// * Page title matches

// * Page revision ID (if supplied) matches

if (event.data.page === this.row.title.getPrefixedText() ||

(rev && event.data.revision)) {

window.deputy.comms.reply(event.data, {

type: 'pageStatusResponse',

caseId: this.row.casePage.pageId,

caseTitle: this.row.casePage.title.getPrefixedText(),

title: this.row.title.getPrefixedText(),

status: this.status,

enabledStatuses: this.statusDropdown.getEnabledOptions(),

rowType: this.row.type,

revisionStatus: rev ? rev.completed : undefined,

revision: event.data.revision,

nextRevision: (_d = (_c = (_b = this.revisions) === null || _b === void 0 ? void 0 : _b.find((revision) => !revision.completed &&

revision.revision.revid !== event.data.revision)) === null || _c === void 0 ? void 0 : _c.revision.revid) !== null && _d !== void 0 ? _d : null

});

}

}

/**

* @param event

*/

sendNextRevisionResponse(event) {

var _a, _b, _c;

if (event.data.caseId === this.row.casePage.pageId &&

event.data.page === this.row.title.getPrefixedText()) {

if (!this.revisions) {

window.deputy.comms.reply(event.data, {

type: 'pageNextRevisionResponse',

revid: null

});

}

else {

// If `event.data.after` == null, this will be `undefined`.

const baseRevision = this.revisions

.find((r) => r.revision.revid === event.data.after);

const baseRevisionIndex = baseRevision == null ?

0 : this.revisions.indexOf(baseRevision);

// Find the next revision that is not completed.

const exactRevision = event.data.reverse ?

last(this.revisions.filter((r, i) => i < baseRevisionIndex && !r.completed)) :

this.revisions.find((r, i) => i > baseRevisionIndex && !r.completed);

const firstRevision = exactRevision == null ?

this.revisions.find((r) => !r.completed) : null;

// Returns `null` if an `exactRevision` or a `firstRevision` were not found.

window.deputy.comms.reply(event.data, {

type: 'pageNextRevisionResponse',

revid: (_c = (_b = (_a = (exactRevision !== null && exactRevision !== void 0 ? exactRevision : firstRevision)) === null || _a === void 0 ? void 0 : _a.revision) === null || _b === void 0 ? void 0 : _b.revid) !== null && _c !== void 0 ? _c : null

});

}

}

}

}

DeputyContributionSurveyRow.menuOptionIcon = {

[ContributionSurveyRowStatus.Unfinished]: false,

[ContributionSurveyRowStatus.Unknown]: 'alert',

[ContributionSurveyRowStatus.WithViolations]: 'check',

[ContributionSurveyRowStatus.WithoutViolations]: 'close',

[ContributionSurveyRowStatus.Missing]: 'help',

[ContributionSurveyRowStatus.PresumptiveRemoval]: 'trash'

};

/**

* Represents a ContributionSurveySection. Contains a list of {@link ContributionSurveyRow}s

* that make up a section generated by the contribution survey.

*/

class ContributionSurveySection {

/**

* @param casePage The case page of this section

* @param name The name of this section (based on the heading)

* @param closed Whether this section has been closed (wrapped in collapse templates)

* @param closingComments Closing comments for this section

* @param wikitext The original wikitext of this section

* @param revid The revision ID of the wikitext attached to this section.

*/

constructor(casePage, name, closed, closingComments, wikitext, revid) {

this.casePage = casePage;

this.name = name;

this.closed = closed;

this.closingComments = closingComments;

this.originalWikitext = wikitext;

this.originallyClosed = closed;

this.revid = revid;

}

}

let InternalDeputyReviewDialog;

/**

* Initializes the process dialog.

*/

function initDeputyReviewDialog() {

var _a;

InternalDeputyReviewDialog = (_a = class DeputyReviewDialog extends OO.ui.ProcessDialog {

/**

*

* @param config

*/

constructor(config) {

config.size = config.size || 'larger';

super(config);

this.data = config;

}

/**

* @return The body height of this dialog.

*/

getBodyHeight() {

return 500;

}

/**

*

* @param {...any} args

*/

initialize(...args) {

super.initialize.apply(this, args);

this.element = h_1("div", { style: {

display: 'flex',

flexDirection: 'column',

alignItems: 'center',

textAlign: 'center'

} },

h_1("div", { style: { marginBottom: '8px' } }, mw.msg('deputy.diff.load')),

unwrapWidget(new OO.ui.ProgressBarWidget({

classes: ['dp-review-progress'],

progress: false

})));

this.content = new OO.ui.PanelLayout({ expanded: true, padded: true });

unwrapWidget(this.content).appendChild(this.element);

this.$body.append(this.content.$element);

return this;

}

/**

* @param data

* @return The ready process for this object.

*/

getReadyProcess(data) {

return super.getReadyProcess.call(this, data)

.next(new Promise((res) => {

// Load MediaWiki diff styles

mw.loader.using('mediawiki.diff.styles', () => res());

}))

.next(() => __awaiter(this, void 0, void 0, function* () {

// Load diff HTML

const compareRequest = yield MwApi.action.post({

action: 'compare',

fromtitle: this.data.title.getPrefixedText(),

fromslots: 'main',

totitle: this.data.title.getPrefixedText(),

toslots: 'main',

topst: 1,

prop: 'diff',

slots: 'main',

'fromtext-main': this.data.from,

'fromcontentformat-main': 'text/x-wiki',

'fromcontentmodel-main': 'wikitext',

'totext-main': this.data.to,

'tocontentformat-main': 'text/x-wiki',

'tocontentmodel-main': 'wikitext'

});

if (compareRequest.error) {

swapElements(this.element, unwrapWidget(new OO.ui.MessageWidget({

type: 'error',

label: mw.msg('deputy.diff.error')

})));

}

const diffHTML = compareRequest.compare.bodies.main;

if (!diffHTML) {

this.element = swapElements(this.element, h_1("div", { style: { textAlign: 'center' } }, mw.msg('deputy.diff.no-changes')));

}

else {

// noinspection JSXDomNesting

this.element = swapElements(this.element, h_1("table", { class: "diff" },

h_1("colgroup", null,

h_1("col", { class: "diff-marker" }),

h_1("col", { class: "diff-content" }),

h_1("col", { class: "diff-marker" }),

h_1("col", { class: "diff-content" })),

h_1("tbody", { dangerouslySetInnerHTML: diffHTML })));

}

}), this);

}

/**

* @param action

* @return The action process

*/

getActionProcess(action) {

if (action === 'close') {

return new OO.ui.Process(function () {

this.close({

action: action

});

}, this);

}

// Fallback to parent handler

return super.getActionProcess.call(this, action);

}

},

_a.static = Object.assign(Object.assign({}, OO.ui.ProcessDialog.static), { name: 'deputyReviewDialog', title: mw.msg('deputy.diff'), actions: [

{

flags: ['safe', 'close'],

icon: 'close',

label: mw.msg('deputy.ante.close'),

title: mw.msg('deputy.ante.close'),

invisibleLabel: true,

action: 'close'

}

] }),

_a);

}

/**

* Creates a new DeputyReviewDialog.

*

* @param config

* @return A DeputyReviewDialog

*/

function DeputyReviewDialog (config) {

if (!InternalDeputyReviewDialog) {

initDeputyReviewDialog();

}

return new InternalDeputyReviewDialog(config);

}

/**

* Get the ID of a section from its heading.

*

* @param page The page to check for

* @param sectionName The section name to get the ID of

* @param n The `n`th occurrence of a section with the same name

*/

function getSectionId (page, sectionName, n = 1) {

return __awaiter(this, void 0, void 0, function* () {

const parseRequest = yield MwApi.action.get({

action: 'parse',

page: normalizeTitle(page).getPrefixedText(),

prop: 'sections'

});

if (parseRequest.error) {

throw new Error('Error finding section ID: ' + parseRequest.error.info);

}

let indexSection;

let currentN = 1;

for (const section of parseRequest.parse.sections) {

if (section.line === sectionName) {

if (currentN < n) {

currentN++;

}

else {

indexSection = section;

break;

}

}

}

if (indexSection) {

return isNaN(+indexSection.index) ? null : +indexSection.index;

}

else {

return null;

}

});

}

/**

* Get the parser output HTML of a specific page section.

*

* @param page

* @param section

* @param extraOptions

* @return A promise resolving to the `

` element.

*/

function getSectionHTML (page, section, extraOptions = {}) {

return __awaiter(this, void 0, void 0, function* () {

if (typeof section === 'string') {

section = yield getSectionId(page, section);

}

return MwApi.action.get(Object.assign({ action: 'parse', prop: 'text|wikitext|revid', page: normalizeTitle(page).getPrefixedText(), section: section, disablelimitreport: true }, extraOptions)).then((data) => {

const temp = document.createElement('span');

temp.innerHTML = data.parse.text;

return {

element: temp.children[0],

wikitext: data.parse.wikitext,

revid: data.parse.revid

};

});

});

}

/**

* Appends extra information to an edit summary (also known as the "advert").

*

* @param editSummary The edit summary

* @param config The user's configuration. Used to get the "danger mode" setting.

* @return The decorated edit summary (in wikitext)

*/

function decorateEditSummary (editSummary, config) {

var _a;

const dangerMode = (_a = config === null || config === void 0 ? void 0 : config.core.dangerMode.get()) !== null && _a !== void 0 ? _a : false;

return `${editSummary} (Deputy v${version}${dangerMode ? '!' : ''})`;

}

/**

* Checks the n of a given element, that is to say the `n`th occurrence of a section

* with this exact heading name in the entire page.

*

* This is purely string- and element-based, with no additional metadata or parsing

* information required.

*

* This function detects the `n` using the following conditions:

* - If the heading ID does not have an n suffix, the n is always 1.

* - If the heading ID does have an n suffix, and the detected heading name does not end

* with a number, the n is always the last number on the ID.

* - If the heading ID and heading name both end with a number,

* - The n is 1 if the ID has an equal number of ending number patterns (sequences of "_n",

* e.g. "_20_30_40" has three) with the heading name.

* - Otherwise, the n is the last number on the ID if the ID than the heading name.

*

* @param heading The heading to check

* @return The n, a number

*/

function sectionHeadingN(heading) {

try {

const headingNameEndPattern = /(?:\s|_)(\d+)/g;

const headingIdEndPattern = /_(\d+)/g;

const headingId = heading.id;

const headingIdMatches = headingId.match(headingIdEndPattern);

const headingNameMatches = heading.title.match(headingNameEndPattern);

if (headingIdMatches == null) {

return 1;

}

else if (headingNameMatches == null) {

// Last number of the ID

return +(headingIdEndPattern.exec(last(headingIdMatches))[1]);

}

else if (headingIdMatches.length === headingNameMatches.length) {

return 1;

}

else {

// Last number of the ID

return +(headingIdEndPattern.exec(last(headingIdMatches))[1]);

}

}

catch (e) {

error('Error getting section number', e, heading);

throw e;

}

}

/**

* Wraps a set of nodes in a div.dp-cs-extraneous element.

*

* @param children The nodes to wrap

*/

function DeputyExtraneousElement(children) {

const container = document.createElement('div');

container.classList.add('dp-cs-extraneous');

children = Array.isArray(children) ? children : [children];

children.forEach(child => container.appendChild(child.cloneNode(true)));

return container;

}

/**

* What it says on the tin. Attempt to parse out a `title`, `diff`,

* or `oldid` from a URL. This is useful for converting diff URLs into actual

* diff information, and especially useful for {{copied}} templates.

*

* If diff parameters were not found (no `diff` or `oldid`), they will be `null`.

*

* @param url The URL to parse

* @return Parsed info: `diff` or `oldid` revision IDs, and/or the page title.

*/

function parseDiffUrl(url) {

if (typeof url === 'string') {

url = new URL(url);

}

// Attempt to get values from URL parameters (when using `/w/index.php?action=diff`)

let oldid = url.searchParams.get('oldid');

let diff = url.searchParams.get('diff');

let title = url.searchParams.get('title');

// Attempt to get information from this URL.

tryConvert: {

if (title && oldid && diff) {

// Skip if there's nothing else we need to get.

break tryConvert;

}

// Attempt to get values from Special:Diff short-link

const diffSpecialPageCheck =

// eslint-disable-next-line security/detect-unsafe-regex

/\/wiki\/Special:Diff\/(prev|next|\d+)(?:\/(prev|next|\d+))?/i.exec(url.pathname);

if (diffSpecialPageCheck != null) {

if (diffSpecialPageCheck[1] != null &&

diffSpecialPageCheck[2] == null) {

// Special:Diff/diff

diff = diffSpecialPageCheck[1];

}

else if (diffSpecialPageCheck[1] != null &&

diffSpecialPageCheck[2] != null) {

// Special:Diff/oldid/diff

oldid = diffSpecialPageCheck[1];

diff = diffSpecialPageCheck[2];

}

break tryConvert;

}

// Attempt to get values from Special:PermanentLink short-link

const permanentLinkCheck = /\/wiki\/Special:Perma(nent)?link\/(\d+)/i.exec(url.pathname);

if (permanentLinkCheck != null) {

oldid = permanentLinkCheck[2];

break tryConvert;

}

// Attempt to get values from article path with ?oldid or ?diff

// eslint-disable-next-line security/detect-non-literal-regexp

const articlePathRegex = new RegExp(mw.util.getUrl('(.*)'))

.exec(url.pathname);

if (articlePathRegex != null) {

title = decodeURIComponent(articlePathRegex[1]);

break tryConvert;

}

}

// Convert numbers to numbers

if (oldid != null && !isNaN(+oldid)) {

oldid = +oldid;

}

if (diff != null && !isNaN(+diff)) {

diff = +diff;

}

// Try to convert a page title

try {

title = new mw.Title(title).getPrefixedText();

}

catch (e) {

warn('Failed to normalize page title during diff URL conversion.');

}

return {

diff: diff,

oldid: oldid,

title: title

};

}

/**

* The contribution survey section UI element. This includes a list of revisions

* (which are {@link DeputyContributionSurveyRow} objects), a "close section"

* checkbox, a "comments" input box (for additional comments when closing the

* section), a "cancel" button and a "save" button.

*/

class DeputyContributionSurveySection {

/**

* @return `true` if this section has been modified

*/

get modified() {

return this.rows && this.rows.length > 0 &&

this.rows.some((row) => row.modified) || (this._section && this._section.originallyClosed !== this.closed);

}

/**

* @return `true` if this section is (or will be) closed

*/

get closed() {

var _a;

return (_a = this._section) === null || _a === void 0 ? void 0 : _a.closed;

}

/**

* Sets the close state of this section

*/

set closed(value) {

var _a;

if (((_a = this._section) === null || _a === void 0 ? void 0 : _a.closed) == null) {

throw new Error('Section has not been loaded yet.');

}

this._section.closed = value;

}

/**

* @return The closing comments for this section

*/

get comments() {

var _a;

return (_a = this._section) === null || _a === void 0 ? void 0 : _a.closingComments;

}

/**

* Sets the comments of a section.

*/

set comments(value) {

if (this._section == null) {

throw new Error('Section has not been loaded yet.');

}

this._section.closingComments = value;

}

/**

* @return The wikitext for this section.

*/

get wikitext() {

var _a;

let final = [];

for (const obj of this.wikitextLines) {

if (typeof obj === 'string') {

final.push(obj);

}

else {

final.push(obj.wikitext);

}

}

let lastModifiedRowIndex;

for (const i in final) {

const wikitext = final[+i];

if (wikitext.indexOf(' ~~~~') !== -1) {

lastModifiedRowIndex = +i;

}

}

const trace = ` ${generateTrace()}`;

if (lastModifiedRowIndex != null) {

// If `lastModifiedRowIndex` exists, we can assume that a modified row exists.

// This prevents the following from running on unmodified rows, which is

// wasteful.

switch (window.deputy.config.cci.signingBehavior.get()) {

case ContributionSurveyRowSigningBehavior.AlwaysTrace:

final = final.map((line) => {

return line.replace(/ ~~~~$/, trace);

});

break;

case ContributionSurveyRowSigningBehavior.AlwaysTraceLastOnly:

final = final.map((line, i) => {

if (i !== lastModifiedRowIndex) {

return line.replace(/ ~~~~$/, trace);

}

else {

return line;

}

});

break;

case ContributionSurveyRowSigningBehavior.LastOnly:

final = final.map((line, i) => {

if (i !== lastModifiedRowIndex) {

return line.replace(/ ~~~~$/, '');

}

else {

return line;

}

});

break;

case ContributionSurveyRowSigningBehavior.Never:

final = final.map((line) => {

return line.replace(/ ~~~~$/, '');

});

break;

}

}

if (this.closed) {

if (!this._section.originallyClosed) {

let closingComments = ((_a = this.comments) !== null && _a !== void 0 ? _a : '').trim();

if (this.closingCommentsSign.isSelected()) {

closingComments += ' ~~~~';

}

final.splice(1, 0, msgEval(window.deputy.wikiConfig.cci.collapseTop.get(), closingComments).plain());

if (final[final.length - 1].trim().length === 0) {

final.pop();

}

final.push(window.deputy.wikiConfig.cci.collapseBottom.get());

}

// If the section was originally closed, don't allow the archiving

// message to be edited.

}

return final.join('\n');

}

/**

* @return The edit summary for this section's changes.

*/

get editSummary() {

var _a;

if (this.modified) {

const modified = this.rows.filter((row) => row.modified);

let worked = 0;

let assessed = 0;

let finished = 0;

let reworked = 0;

for (const row of modified) {

if (!row.wasFinished) {

worked++;

assessed += (_a = row.revisions) === null || _a === void 0 ? void 0 : _a.filter((rev) => rev.completed).length;

if (row.completed) {

finished++;

}

}

else {

reworked++;

}

}

const message = [];

if (assessed > 0) {

message.push(mw.msg('deputy.content.assessed', `${assessed}`, `${worked}`));

}

if (finished > 0) {

message.push(mw.msg('deputy.content.assessed.finished', `${finished}`));

}

if (reworked > 0) {

message.push(mw.msg('deputy.content.assessed.reworked', `${reworked}`));

}

const nowClosed = !this._section.originallyClosed && this.closed;

if (nowClosed) {

message.push(mw.msg('deputy.content.assessed.sectionClosed'));

}

const m = message.join(mw.msg('deputy.content.assessed.comma'));

if (m.length === 0) {

return mw.msg('deputy.content.reformat');

}

const summary = mw.msg(nowClosed ?

'deputy.content.summary.sectionClosed' :

(finished === 0 && assessed > 0 ?

'deputy.content.summary.partial' :

'deputy.content.summary'), this.headingName, finished);

return summary + m[0].toUpperCase() + m.slice(1);

}

else {

return mw.msg('deputy.content.reformat');

}

}

/**

* @return the name of the section heading.

*/

get headingName() {

return this.heading.title;

}

/**

* @return the `n` of the section heading, if applicable.

*/

get headingN() {

return sectionHeadingN(this.heading);

}

/**

* Creates a DeputyContributionSurveySection from a given heading.

*

* @param casePage

* @param heading

*/

constructor(casePage, heading) {

this.casePage = casePage;

this.heading = normalizeWikiHeading(heading);

this.sectionNodes = casePage.getContributionSurveySection(heading);

}

/**

* Get the ContributionSurveySection for this section

*

* @param wikitext Internal use only. Used to skip section loading using existing wikitext.

* @return The ContributionSurveySection for this section

*/

getSection(wikitext) {

var _a, _b, _c;

return __awaiter(this, void 0, void 0, function* () {

const collapsible = (_b = (_a = this.sectionNodes.find((v) => v instanceof HTMLElement && v.querySelector('.mw-collapsible'))) === null || _a === void 0 ? void 0 : _a.querySelector('.mw-collapsible')) !== null && _b !== void 0 ? _b : null;

const sectionWikitext = yield this.casePage.wikitext.getSectionWikitext(this.headingName, this.headingN);

return (_c = this._section) !== null && _c !== void 0 ? _c : (this._section = new ContributionSurveySection(this.casePage, this.headingName, collapsible != null, collapsible === null || collapsible === void 0 ? void 0 : collapsible.querySelector('th > div').innerText, wikitext !== null && wikitext !== void 0 ? wikitext : sectionWikitext, wikitext ? wikitext.revid : sectionWikitext.revid));

});

}

/**

* Perform any required pre-render operations.

*

* @return `true` if prepared successfully.

* `false` if not (invalid section, already closed, etc.)

*/

prepare() {

var _a, _b;

return __awaiter(this, void 0, void 0, function* () {

let targetSectionNodes = this.sectionNodes;

let listElements = this.sectionNodes.filter((el) => el instanceof HTMLElement && el.tagName === 'UL');

if (listElements.length === 0) {

// No list found ! Is this a valid section?

// Check for a collapsible section.

const collapsible = (_b = (_a = this.sectionNodes.find((v) => v instanceof HTMLElement && v.querySelector('.mw-collapsible'))) === null || _a === void 0 ? void 0 : _a.querySelector('.mw-collapsible')) !== null && _b !== void 0 ? _b : null;

if (collapsible) {

// This section has a collapsible. It's possible that it's a closed section.

// From here, use a different `sectionNodes` (specifically targeting all nodes

// inside that collapsible), and then locate all ULs inside that collapsible.

targetSectionNodes = Array.from(collapsible.childNodes);

listElements = Array.from(collapsible.querySelectorAll('ul'));

}

else {

// No collapsible found. Give up.

warn('Could not find valid ULs in CCI section.', targetSectionNodes);

return false;

}

}

const rowElements = {};

for (const listElement of listElements) {

for (let i = 0; i < listElement.children.length; i++) {

const li = listElement.children.item(i);

if (li.tagName !== 'LI') {

// Skip this element.

continue;

}

const anchor = li.querySelector('a:first-of-type');

// Avoid enlisting if the anchor can't be found (invalid row).

if (anchor) {

const anchorLinkTarget = parseDiffUrl(new URL(anchor.getAttribute('href'), window.location.href)).title;

if (!anchorLinkTarget) {

warn('Could not parse target of anchor', anchor);

}

else {

rowElements[new mw.Title(anchorLinkTarget).getPrefixedText()] =

li;

}

}

}

}

const section = yield this.getSection();

const sectionWikitext = section.originalWikitext;

this.revid = section.revid;

const wikitextLines = sectionWikitext.split('\n');

this.rows = [];

this.rowElements = [];

this.wikitextLines = [];

let rowElement;

for (let i = 0; i < wikitextLines.length; i++) {

const line = wikitextLines[i];

try {

const csr = new ContributionSurveyRow(this.casePage, line);

const originalElement = rowElements[csr.title.getPrefixedText()];

if (originalElement) {

rowElement = new DeputyContributionSurveyRow(csr, originalElement, line, this);

}

else {

// Element somehow not in list. Just keep line as-is.

warn(`Could not find row element for "${csr.title.getPrefixedText()}"`);

rowElement = line;

}

}

catch (e) {

// This is not a contribution surveyor row.

if (/^\*[^*:]+/.test(line)) {

// Only trigger on actual bulleted lists.

warn('Could not parse row.', line, e);

// For debugging and tests.

mw.hook('deputy.errors.cciRowParse').fire({

line, error: e.toString()

});

}

if (rowElement instanceof DeputyContributionSurveyRow &&

rowElement.originalElement.nextSibling == null &&

rowElement.originalElement.parentNode.nextSibling != null &&

// Just a blank line. Don't try to do anything else.

line !== '') {

// The previous row element was the last in the list. The

// list probably broke somewhere. (comment with wrong

// bullet?)

// In any case, let's try show it anyway. The user might

// miss some context otherwise.

// We'll only begin reading proper section data once we hit

// another bullet. So let's grab all nodes from the erring

// one until the next bullet list.

const extraneousNodes = [];

let lastNode = rowElement.originalElement.parentElement.nextSibling;

while (

// Another node exists next

lastNode != null &&

// The node is part of this section

targetSectionNodes.includes(lastNode) &&

(

// The node is not an element

!(lastNode instanceof HTMLElement) ||

// The element is not a bullet list

lastNode.tagName !== 'UL')) {

extraneousNodes.push(lastNode);

lastNode = lastNode.nextSibling;

}

rowElement = extraneousNodes;

}

else {

rowElement = line;

}

}

if (rowElement instanceof DeputyContributionSurveyRow) {

this.rows.push(rowElement);

this.rowElements.push(rowElement);

this.wikitextLines.push(rowElement);

}

else if (Array.isArray(rowElement)) {

// Array of Nodes

this.wikitextLines.push(line);

if (rowElement.length !== 0) {

// Only append the row element if it has contents.

// Otherwise, there will be a blank blue box.

this.rowElements.push(DeputyExtraneousElement(rowElement));

}

}

else if (typeof rowElement === 'string') {

this.wikitextLines.push(rowElement);

}

}

// Hide all section elements

this.toggleSectionElements(false);

return true;

});

}

/**

* Toggle section elements. Removes the section elements (but preservers them in

* `this.sectionElements`) if `false`, re-appends them to the DOM if `true`.

*

* @param toggle

*/

toggleSectionElements(toggle) {

var _a;

const bottom = (_a = this.heading.root.nextSibling) !== null && _a !== void 0 ? _a : null;

for (const sectionElement of this.sectionNodes) {

if (toggle) {

this.heading.root.parentNode.insertBefore(sectionElement, bottom);

}

else {

removeElement(sectionElement);

}

}

}

/**

* Destroys the element from the DOM and re-inserts in its place the original list.

* This *should* return the section back to its original look. This does *NOT*

* remove the section from the session or cache. Use `DeputySession.closeSection`

* instead.

*/

close() {

removeElement(this.container);

this.toggleSectionElements(true);

// Detach listeners to stop listening to events.

this.rows.forEach((row) => {

row.close();

});

}

/**

* Toggles the closing comments input box and signature checkbox.

* This will disable the input box AND hide the element from view.

*

* @param show

*/

toggleClosingElements(show) {

this.closingComments.setDisabled(!show);

this.closingComments.toggle(show);

this.closingCommentsSign.setDisabled(!show);

this.closingCommentsSign.toggle(show);

}

/**

* Sets the disabled state of this section.

*

* @param disabled

*/

setDisabled(disabled) {

var _a, _b, _c, _d, _e, _f, _g;

(_a = this.closeButton) === null || _a === void 0 ? void 0 : _a.setDisabled(disabled);

(_b = this.reviewButton) === null || _b === void 0 ? void 0 : _b.setDisabled(disabled);

(_c = this.saveButton) === null || _c === void 0 ? void 0 : _c.setDisabled(disabled);

(_d = this.closingCheckbox) === null || _d === void 0 ? void 0 : _d.setDisabled(disabled);

(_e = this.closingComments) === null || _e === void 0 ? void 0 : _e.setDisabled(disabled);

(_f = this.closingCommentsSign) === null || _f === void 0 ? void 0 : _f.setDisabled(disabled);

(_g = this.rows) === null || _g === void 0 ? void 0 : _g.forEach((row) => row.setDisabled(disabled));

this.disabled = disabled;

}

/**

* Saves the current section to the case page.

*

* @param sectionId

* @return Save data, or `false` if the save hit an error

*/

save(sectionId) {

return __awaiter(this, void 0, void 0, function* () {

if (sectionId == null) {

throw new Error(mw.msg('deputy.session.section.missingSection'));

}

if (this.closed &&

!this._section.originallyClosed &&

!window.deputy.config.core.dangerMode.get() &&

this.rows.some(r => !r.completed)) {

throw new Error(mw.msg('deputy.session.section.sectionIncomplete'));

}

return MwApi.action.postWithEditToken(Object.assign(Object.assign({}, changeTag(yield window.deputy.getWikiConfig())), { action: 'edit', pageid: this.casePage.pageId, section: sectionId, text: this.wikitext, baserevid: this.revid, summary: decorateEditSummary(this.editSummary, window.deputy.config) })).then(function (data) {

return data;

}, (code, data) => {

if (code === 'editconflict') {

// Wipe cache.

this.casePage.wikitext.resetCachedWikitext();

OO.ui.alert(mw.msg('deputy.session.section.conflict.help'), {

title: mw.msg('deputy.session.section.conflict.title')

}).then(() => {

window.deputy.session.rootSession.restartSession();

});

return false;

}

mw.notify(h_1("span", { dangerouslySetInnerHTML: data.errors[0].html }), {

autoHide: false,

title: mw.msg('deputy.session.section.failed'),

type: 'error'

});

return false;

});

});

}

/**

* Makes all rows of this section being loading data.

*

* @return A Promise that resolves when all rows have finished loading data.

*/

loadData() {

return __awaiter(this, void 0, void 0, function* () {

// For debugging and tests.

// noinspection JSUnresolvedReference

if (window.deputy.NO_ROW_LOADING !== true) {

yield Promise.all(this.rows.map(row => row.loadData()));

}

});

}

/**

* @inheritDoc

*/

render() {

const dangerMode = window.deputy.config.core.dangerMode.get();

this.closingCheckbox = new OO.ui.CheckboxInputWidget({

selected: this._section.originallyClosed,

disabled: this._section.originallyClosed

});

this.closingComments = new OO.ui.TextInputWidget({

placeholder: mw.msg('deputy.session.section.closeComments'),

value: this._section.closingComments,

disabled: true

});

this.closingCommentsSign = new OO.ui.CheckboxInputWidget({

selected: window.deputy.config.cci.signSectionArchive.get(),

disabled: true

});

this.closeButton = new OO.ui.ButtonWidget(Object.assign({ label: mw.msg('deputy.session.section.stop'), title: mw.msg('deputy.session.section.stop.title') }, (dangerMode ? { invisibleLabel: true, icon: 'pause' } : {})));

this.reviewButton = new OO.ui.ButtonWidget(Object.assign({ label: mw.msg('deputy.review'), title: mw.msg('deputy.review.title') }, (dangerMode ? { invisibleLabel: true, icon: 'eye' } : {})));

this.saveButton = new OO.ui.ButtonWidget({

label: mw.msg('deputy.save'),

flags: ['primary', 'progressive']

});

const saveContainer = h_1("div", { class: "dp-cs-section-progress" }, unwrapWidget(new OO.ui.ProgressBarWidget({

progress: false

})));

this.closeButton.on('click', () => __awaiter(this, void 0, void 0, function* () {

if (this.wikitext !== (yield this.getSection()).originalWikitext) {

dangerModeConfirm(window.deputy.config, mw.msg('deputy.session.section.closeWarn')).done((confirmed) => {

if (confirmed) {

this.close();

window.deputy.session.rootSession.closeSection(this);

}

});

}

else {

this.close();

yield window.deputy.session.rootSession.closeSection(this);

}

}));

this.reviewButton.on('click', () => __awaiter(this, void 0, void 0, function* () {

const reviewDialog = DeputyReviewDialog({

from: (yield this.getSection()).originalWikitext,

to: this.wikitext,

title: this.casePage.title

});

window.deputy.windowManager.addWindows([reviewDialog]);

yield window.deputy.windowManager.openWindow(reviewDialog).opened;

}));

this.saveButton.on('click', () => __awaiter(this, void 0, void 0, function* () {

this.setDisabled(true);

saveContainer.classList.add('active');

const sectionId = yield getSectionId(this.casePage.title, this.headingName, this.headingN);

yield this.save(sectionId).then((result) => __awaiter(this, void 0, void 0, function* () {

var _a, _b;

if (result) {

mw.notify(mw.msg('deputy.session.section.saved'));

// Rebuild the entire section to HTML, and then reopen.

const { element, wikitext, revid } = yield getSectionHTML(this.casePage.title, sectionId);

removeElement(this.container);

// Remove whatever section elements are still there.

// They may have been greatly modified by the save.

const sectionElements = this.casePage.getContributionSurveySection(this.heading.root);

sectionElements.forEach((el) => removeElement(el));

// Clear out section elements and re-append new ones to the DOM.

this.sectionNodes = [];

// Heading is preserved to avoid messing with IDs.

const heading = this.heading.root;

const insertRef = (_a = heading.nextSibling) !== null && _a !== void 0 ? _a : null;

for (const child of Array.from(element.childNodes)) {

if (!this.casePage.isContributionSurveyHeading((_b = normalizeWikiHeading(child,

// We're using elements that aren't currently appended to the

// DOM, so we have to manually set the ceiling. Otherwise, we'll

// get the wrong element and ceiling checks will always be false.

element)) === null || _b === void 0 ? void 0 : _b.h)) {

heading.parentNode.insertBefore(child, insertRef);

this.sectionNodes.push(child);

// noinspection JSUnresolvedReference

$(child).children('.mw-collapsible').makeCollapsible();

}

}

if (!this._section.closed) {

this._section = null;

yield this.getSection(Object.assign(wikitext, { revid }));

yield this.prepare();

heading.insertAdjacentElement('afterend', this.render());

// Run this asynchronously.

setTimeout(this.loadData.bind(this), 0);

}

else {

this.close();

yield window.deputy.session.rootSession.closeSection(this);

}

}

}), (err) => {

OO.ui.alert(err.message, {

title: mw.msg('deputy.session.section.failed')

});

error(err);

saveContainer.classList.remove('active');

this.setDisabled(false);

});

saveContainer.classList.remove('active');

this.setDisabled(false);

}));

// Section closing (archive/ctop) elements

const closingWarning = DeputyMessageWidget({

classes: ['dp-cs-section-unfinishedWarning'],

type: 'error',

label: mw.msg('deputy.session.section.closeError')

});

closingWarning.toggle(false);

const updateClosingWarning = (() => {

const incomplete = this.rows.some((row) => !row.completed);

if (window.deputy.config.core.dangerMode.get()) {

this.saveButton.setDisabled(false);

closingWarning.setLabel(mw.msg('deputy.session.section.closeError.danger'));

}

else {

closingWarning.setLabel(mw.msg('deputy.session.section.closeError'));

this.saveButton.setDisabled(incomplete);

}

closingWarning.toggle(incomplete);

});

const closingCommentsField = new OO.ui.FieldLayout(this.closingComments, {

align: 'top',

label: mw.msg('deputy.session.section.closeComments'),

invisibleLabel: true,

helpInline: true,

classes: ['dp-cs-section-closingCommentsField']

});

const closingCommentsSignField = new OO.ui.FieldLayout(this.closingCommentsSign, {

align: 'inline',

label: mw.msg('deputy.session.section.closeCommentsSign')

});

const closingFields = h_1("div", { class: "dp-cs-section-closing", style: { display: 'none' } },

unwrapWidget(closingWarning),

unwrapWidget(closingCommentsField),

unwrapWidget(closingCommentsSignField));

const updateClosingFields = (v) => {

this.closed = v;

if (this._section.originallyClosed) {

// This section was originally closed. Hide everything.

v = false;

}

closingFields.style.display = v ? '' : 'none';

this.toggleClosingElements(v);

if (v) {

updateClosingWarning();

this.rows.forEach((row) => {

row.addEventListener('update', updateClosingWarning);

});

}

else {

closingWarning.toggle(false);

this.saveButton.setDisabled(false);

this.rows.forEach((row) => {

row.removeEventListener('update', updateClosingWarning);

});

}

};

this.closingCheckbox.on('change', updateClosingFields);

updateClosingFields(this.closed);

this.closingComments.on('change', (v) => {

this.comments = v;

});

// Danger mode buttons

const dangerModeElements = [];

if (dangerMode) {

const markAllFinishedButton = new OO.ui.ButtonWidget({

flags: ['destructive'],

icon: 'checkAll',

label: mw.msg('deputy.session.section.markAllFinished'),

title: mw.msg('deputy.session.section.markAllFinished'),

invisibleLabel: true

});

markAllFinishedButton.on('click', () => {

this.rows.forEach(v => v.markAllAsFinished());

});

const instantArchiveButton = new OO.ui.ButtonWidget({

flags: ['destructive', 'primary'],

label: mw.msg('deputy.session.section.instantArchive'),

title: mw.msg('deputy.session.section.instantArchive.title')

});

instantArchiveButton.on('click', () => {

this.closingCheckbox.setSelected(true);

this.saveButton.emit('click');

});

const dangerModeButtons = [

unwrapWidget(markAllFinishedButton),

unwrapWidget(instantArchiveButton)

];

dangerModeElements.push(h_1("div", { class: "dp-cs-section-danger--separator" }, mw.msg('deputy.session.section.danger')), dangerModeButtons);

// Remove spacing from save button

unwrapWidget(this.saveButton).style.marginRight = '0';

}

// Actual element

return this.container = h_1("div", { class: classMix('deputy', 'dp-cs-section', this._section.originallyClosed && 'dp-cs-section-archived') },

this._section.originallyClosed && h_1("div", { class: "dp-cs-section-archived-warn" }, unwrapWidget(new OO.ui.MessageWidget({

type: 'warning',

label: mw.msg('deputy.session.section.closed')

}))),

h_1("div", null, this.rowElements.map((row) => row instanceof HTMLElement ? row : row.render())),

h_1("div", { class: "dp-cs-section-footer" },

h_1("div", { style: { display: 'flex' } },

h_1("div", { style: {

flex: '1 1 100%',

display: 'flex',

flexDirection: 'column'

} },

unwrapWidget(new OO.ui.FieldLayout(this.closingCheckbox, {

align: 'inline',

label: mw.msg('deputy.session.section.close')

})),

closingFields),

h_1("div", { style: {

display: 'flex',

alignContent: 'end',

justifyContent: 'end',

flexWrap: dangerMode ? 'wrap' : 'nowrap',

maxWidth: '320px'

} },

unwrapWidget(this.closeButton),

unwrapWidget(this.reviewButton),

unwrapWidget(this.saveButton),

dangerModeElements)),

saveContainer));

}

}

/**

*

* @param element

*/

function findNextSiblingElement(element) {

if (element == null) {

return null;

}

let anchor = element.nextSibling;

while (anchor && !(anchor instanceof Element)) {

anchor = anchor.nextSibling;

}

return anchor;

}

/**

* The DeputyRootSession. Instantiated only when:

* (a) the page is a CCI case page, and

* (b) a session is currently active

*/

class DeputyRootSession {

/*

* =========================================================================

* STATIC AND SESSION-LESS FUNCTIONS

* =========================================================================

*/

/**

* Initialize interface components for *starting* a session. This includes

* the `[start CCI session]` notice at the top of each CCI page section heading.

*

* @param _casePage The current case page

*/

static initEntryInterface(_casePage) {

return __awaiter(this, void 0, void 0, function* () {

const continuing = _casePage != null;

const casePage = continuing ? _casePage : yield DeputyCasePage.build();

const startLink = [];

casePage.findContributionSurveyHeadings()

.forEach((heading) => {

const normalizedHeading = normalizeWikiHeading(heading);

const link = DeputyCCISessionStartLink(normalizedHeading, casePage);

startLink.push(link);

normalizedHeading.root.appendChild(link);

});

window.deputy.comms.addEventListener('sessionStarted', () => {

// Re-build interface.

startLink.forEach((link) => {

removeElement(link);

});

window.deputy.session.init();

}, { once: true });

});

}

/**

* Shows the interface for overwriting an existing session. The provided

* action button will close the other section. This does not start a new

* session; the user must start the session on this page on their own.

*

* @param casePage The case page to continue with

*/

static initOverwriteMessage(casePage) {

return __awaiter(this, void 0, void 0, function* () {

yield mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-content'], () => {

const firstHeading = casePage.findFirstContributionSurveyHeadingElement();

if (firstHeading) {

const stopButton = new OO.ui.ButtonWidget({

label: mw.msg('deputy.session.otherActive.button'),

flags: ['primary', 'destructive']

});

const messageBox = DeputyMessageWidget({

classes: [

'deputy', 'dp-cs-session-notice', 'dp-cs-session-otherActive'

],

type: 'notice',

icon: 'alert',

title: mw.msg('deputy.session.otherActive.head'),

message: mw.msg('deputy.session.otherActive.help'),

actions: [stopButton],

closable: true

});

stopButton.on('click', () => __awaiter(this, void 0, void 0, function* () {

const session = yield window.deputy.comms.sendAndWait({

type: 'sessionStop'

});

if (session === null) {

// Session did not close cleanly. Tab must be closed. Force-stop

// the session.

yield window.deputy.session.clearSession();

removeElement(unwrapWidget(messageBox));

yield window.deputy.session.init();

}

}));

window.deputy.comms.addEventListener('sessionClosed', () => {

// Closed externally. Re-build interface.

removeElement(unwrapWidget(messageBox));

window.deputy.session.init();

});

normalizeWikiHeading(firstHeading).root.insertAdjacentElement('beforebegin', unwrapWidget(messageBox));

}

});

});

}

/**

* Shows the interface for continuing a previous session. This includes

* the `[continue CCI session]` notice at the top of each CCI page section heading

* and a single message box showing when the page was last worked on top of the

* first CCI heading found.

*

* @param casePage The case page to continue with

*/

static initContinueInterface(casePage) {

return __awaiter(this, void 0, void 0, function* () {

yield Promise.all([

DeputyRootSession.initEntryInterface(),

mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-content'], () => {

const lastActiveSection = DeputyRootSession.findFirstLastActiveSection(casePage);

const firstSection = normalizeWikiHeading(casePage.findFirstContributionSurveyHeadingElement());

// Insert element directly into widget (not as text, or else event

// handlers will be destroyed).

const continueButton = new OO.ui.ButtonWidget({

label: mw.msg('deputy.session.continue.button'),

flags: ['primary', 'progressive']

});

const messageBox = DeputyMessageWidget({

classes: [

'deputy', 'dp-cs-session-notice', 'dp-cs-session-lastActive'

],

type: 'notice',

icon: 'history',

title: mw.msg('deputy.session.continue.head', new Date().toLocaleString(mw.config.get('wgUserLanguage'), { dateStyle: 'long', timeStyle: 'medium' })),

message: mw.msg(lastActiveSection ?

'deputy.session.continue.help' :

'deputy.session.continue.help.fromStart', lastActiveSection ?

normalizeWikiHeading(lastActiveSection).title :

casePage.lastActiveSections[0]

.replace(/_/g, ' '), firstSection.title),

actions: [continueButton],

closable: true

});

const sessionStartListener = () => __awaiter(this, void 0, void 0, function* () {

removeElement(unwrapWidget(messageBox));

yield this.initTabActiveInterface();

});

continueButton.on('click', () => {

removeElement(unwrapWidget(messageBox));

if (lastActiveSection) {

DeputyRootSession.continueSession(casePage);

}

else {

DeputyRootSession.continueSession(casePage, [

firstSection.id

]);

}

window.deputy.comms.removeEventListener('sessionStarted', sessionStartListener);

});

firstSection.root.insertAdjacentElement('beforebegin', unwrapWidget(messageBox));

window.deputy.comms.addEventListener('sessionStarted', sessionStartListener, { once: true });

})

]);

});

}

/**

* Shows the interface for an attempted Deputy execution on a different tab than

* expected. This prevents Deputy from running entirely to avoid loss of progress

* and desynchronization.

*

* @param _casePage The current case page (not the active one)

*/

static initTabActiveInterface(_casePage) {

return __awaiter(this, void 0, void 0, function* () {

const casePage = _casePage !== null && _casePage !== void 0 ? _casePage : yield DeputyCasePage.build();

yield mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-content'], () => {

const firstHeading = casePage.findFirstContributionSurveyHeadingElement();

if (firstHeading) {

const messageBox = DeputyMessageWidget({

classes: [

'deputy', 'dp-cs-session-notice', 'dp-cs-session-tabActive'

],

type: 'notice',

title: mw.msg('deputy.session.tabActive.head'),

message: mw.msg('deputy.session.tabActive.help'),

closable: true

});

normalizeWikiHeading(firstHeading).root.insertAdjacentElement('beforebegin', unwrapWidget(messageBox));

window.deputy.comms.addEventListener('sessionClosed', () => __awaiter(this, void 0, void 0, function* () {

removeElement(unwrapWidget(messageBox));

yield window.deputy.session.init();

}), { once: true });

}

});

});

}

/**

* Finds the first last active section that exists on the page.

* If a last active section that still exists on the page could not be found,

* `null` is returned.

*

* @param casePage The case page to use

* @return The last active session's heading element.

*/

static findFirstLastActiveSection(casePage) {

const csHeadings = casePage.findContributionSurveyHeadings();

for (const lastActiveSection of casePage.lastActiveSections) {

for (const heading of csHeadings) {

if (normalizeWikiHeading(heading).id === lastActiveSection) {

return heading;

}

}

}

return null;

}

/**

* Starts a Deputy session.

*

* @param section

* @param _casePage

*/

static startSession(section, _casePage) {

return __awaiter(this, void 0, void 0, function* () {

const sectionIds = (Array.isArray(section) ? section : [section]).map((_section) => normalizeWikiHeading(_section).id);

// Save session to storage

const casePage = _casePage !== null && _casePage !== void 0 ? _casePage : yield DeputyCasePage.build();

const session = yield this.setSession({

casePageId: casePage.pageId,

caseSections: sectionIds

});

const rootSession = window.deputy.session.rootSession =

new DeputyRootSession(session, casePage);

yield casePage.bumpActive();

yield rootSession.initSessionInterface();

});

}

/**

* Continue a session from a DeputyCasePage.

*

* @param casePage The case page to continue with

* @param sectionIds The section IDs to load on startup. If not provided, this will be

* taken from the cache. If provided, this overrides the cache, discarding any

* sections cached previously.

*/

static continueSession(casePage, sectionIds) {

return __awaiter(this, void 0, void 0, function* () {

// Save session to storage

if (sectionIds) {

casePage.lastActiveSections = sectionIds;

}

const session = yield this.setSession({

casePageId: casePage.pageId,

// Shallow array copy

caseSections: [...casePage.lastActiveSections]

});

const rootSession = window.deputy.session.rootSession =

new DeputyRootSession(session, casePage);

yield casePage.bumpActive();

yield rootSession.initSessionInterface();

});

}

/**

* Sets the current active session information.

*

* @param session The session to save.

* @return SessionInformation object if successful, `null` if not.

*/

static setSession(session) {

return __awaiter(this, void 0, void 0, function* () {

return (yield window.deputy.storage.setKV('session', session)) ? session : null;

});

}

/*

* =========================================================================

* INSTANCE AND ACTIVE SESSION FUNCTIONS

* =========================================================================

*/

/**

* @param session

* @param casePage

*/

constructor(session, casePage) {

/**

* Responder for session requests.

*/

this.sessionRequestResponder = this.sendSessionResponse.bind(this);

this.sessionStopResponder = this.handleStopRequest.bind(this);

this.session = session;

this.casePage = casePage;

}

/**

* Initialize interface components for an active session. This will always run in the

* context of a CCI case page.

*/

initSessionInterface() {

return __awaiter(this, void 0, void 0, function* () {

if (window.location.search.indexOf('action=edit') !== -1) {

// User is editing, don't load interface.

return;

}

if (yield window.deputy.session.checkForActiveSessionTabs()) {

// User is on another tab, don't load interface.

mw.loader.using(['oojs-ui-core', 'oojs-ui-windows'], () => {

OO.ui.alert(mw.msg('deputy.session.tabActive.help'), { title: mw.msg('deputy.session.tabActive.head') });

});

return;

}

removeElement(this.casePage.document.querySelector('.dp-cs-session-lastActive'));

this.casePage.document.querySelectorAll('.dp-sessionStarter')

.forEach((el) => {

removeElement(el);

});

window.deputy.comms.addEventListener('sessionRequest', this.sessionRequestResponder);

window.deputy.comms.addEventListener('sessionStop', this.sessionStopResponder);

window.deputy.comms.send({ type: 'sessionStarted', caseId: this.session.casePageId });

yield new Promise((res) => {

mw.loader.using([

'mediawiki.special.changeslist',

'mediawiki.interface.helpers.styles',

'mediawiki.pager.styles',

'oojs-ui-core',

'oojs-ui-windows',

'oojs-ui-widgets',

'oojs-ui.styles.icons-alerts',

'oojs-ui.styles.icons-content',

'oojs-ui.styles.icons-editing-core',

'oojs-ui.styles.icons-interactions',

'oojs-ui.styles.icons-media',

'oojs-ui.styles.icons-movement',

'ext.discussionTools.init',

'jquery.makeCollapsible'

], (require) => __awaiter(this, void 0, void 0, function* () {

// Instantiate the parser

const dt = require('ext.discussionTools.init');

this.parser = new dt.Parser(dt.parserData);

document.getElementsByTagName('body')[0]

.appendChild(window.deputy.windowManager.$element[0]);

// TODO: Do interface functions

this.sections = [];

const activeSectionPromises = [];

for (const heading of this.casePage.findContributionSurveyHeadings()) {

const headingId = normalizeWikiHeading(heading).id;

if (this.session.caseSections.indexOf(headingId) !== -1) {

activeSectionPromises.push(this.activateSection(this.casePage, heading)

.then(v => v ? headingId : null));

}

else {

this.addSectionOverlay(this.casePage, heading);

}

}

// Strip missing sections from caseSections.

this.session.caseSections = (yield Promise.all(activeSectionPromises))

.filter(v => !!v);

yield DeputyRootSession.setSession(this.session);

if (this.session.caseSections.length === 0) {

// No sections re-opened. All of them might have been removed or closed already.

// Close this entire session.

yield this.closeSession();

}

mw.hook('deputy.load.cci.root').fire();

res();

}));

});

});

}

/**

* Responds to session requests through the Deputy communicator. This prevents two

* tabs from having the same session opened.

*

* @param event

*/

sendSessionResponse(event) {

window.deputy.comms.reply(event.data, {

type: 'sessionResponse',

caseId: this.session.casePageId,

sections: this.session.caseSections

});

}

/**

* Handles a session stop request.

*

* @param event

*/

handleStopRequest(event) {

return __awaiter(this, void 0, void 0, function* () {

yield this.closeSession();

window.deputy.comms.reply(event.data, {

type: 'acknowledge'

});

});

}

/**

* Adds the "start working on this section" or "reload page" overlay and button

* to a given section.

*

* @param casePage

* @param heading

*/

addSectionOverlay(casePage, heading) {

var _a, _b, _c;

const normalizedHeading = normalizeWikiHeading(heading).root;

const section = casePage.getContributionSurveySection(normalizedHeading);

const list = section.find((v) => v instanceof HTMLElement && v.tagName === 'UL');

const headingTop = window.scrollY +

normalizedHeading.getBoundingClientRect().bottom;

const sectionBottom = window.scrollY + ((_c = (_b = (_a = findNextSiblingElement(last(section))) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect()) === null || _b === void 0 ? void 0 : _b.top) !== null && _c !== void 0 ? _c : normalizedHeading.parentElement.getBoundingClientRect().bottom);

const overlayHeight = sectionBottom - headingTop;

if (list != null) {

list.style.position = 'relative';

list.appendChild(DeputyCCISessionAddSection({

casePage, heading,

height: overlayHeight

}));

}

}

/**

* Closes all active session-related UI components. Done prior to closing

* a section or reloading the interface.

*/

closeSessionUI() {

if (this.sections) {

for (const section of this.sections) {

section.close();

}

}

this.casePage.document.querySelectorAll('.dp-cs-section-add')

.forEach((el) => removeElement(el));

}

/**

* Closes the current session.

*/

closeSession() {

return __awaiter(this, void 0, void 0, function* () {

this.closeSessionUI();

yield this.casePage.saveToCache();

const oldSessionId = this.session.casePageId;

window.deputy.comms.removeEventListener('sessionRequest', this.sessionRequestResponder);

yield window.deputy.session.clearSession();

window.deputy.comms.send({ type: 'sessionClosed', caseId: oldSessionId });

// Re-initialize session interface objects.

yield window.deputy.session.init();

});

}

/**

* Activates a section. This appends the section UI, adds the section to the

* cache (if not already added), and internally stores the section for a

* graceful exit.

*

* @param casePage

* @param heading

* @return `true` if the section was activated successfully

*/

activateSection(casePage, heading) {

return __awaiter(this, void 0, void 0, function* () {

const el = new DeputyContributionSurveySection(casePage, heading);

if (!(yield el.prepare())) {

return false;

}

const sectionId = normalizeWikiHeading(heading).id;

this.sections.push(el);

const lastActiveSession = this.session.caseSections.indexOf(sectionId);

if (lastActiveSession === -1) {

this.session.caseSections.push(sectionId);

yield DeputyRootSession.setSession(this.session);

}

yield casePage.addActiveSection(sectionId);

normalizeWikiHeading(heading).root.insertAdjacentElement('afterend', el.render());

yield el.loadData();

mw.hook('deputy.load.cci.session').fire();

return true;

});

}

/**

* Closes a section. This removes the section from both the session data and from

* the case page cache.

*

* @param e0

* @param e1

*/

closeSection(e0, e1) {

return __awaiter(this, void 0, void 0, function* () {

const el = e0 instanceof DeputyContributionSurveySection ?

e0 : null;

const casePage = e0 instanceof DeputyContributionSurveySection ?

e0.casePage : e0;

const heading = e0 instanceof DeputyContributionSurveySection ?

e0.heading : normalizeWikiHeading(e1);

const sectionId = heading.id;

const sectionListIndex = this.sections.indexOf(el);

if (el != null && sectionListIndex !== -1) {

this.sections.splice(sectionListIndex, 1);

}

const lastActiveSection = this.session.caseSections.indexOf(sectionId);

if (lastActiveSection !== -1) {

this.session.caseSections.splice(lastActiveSection, 1);

// If no sections remain, clear the session.

if (this.session.caseSections.length === 0) {

yield this.closeSession();

// Don't remove from casePage if there are no sections left, or

// else "continue where you left off" won't work.

}

else {

yield DeputyRootSession.setSession(this.session);

yield casePage.removeActiveSection(sectionId);

this.addSectionOverlay(casePage, heading.h);

}

}

});

}

/**

* Restarts the section. This rebuilds *everything* from the ground up, which may

* be required when there's an edit conflict.

*/

restartSession() {

return __awaiter(this, void 0, void 0, function* () {

const casePage = this.casePage;

yield this.closeSession();

yield window.deputy.session.DeputyRootSession.continueSession(casePage);

});

}

}

/**

* Fake document. Used to load in entire HTML pages without having to append them to the

* actual DOM or use JQuery.

*/

class FakeDocument {

/**

* Creates a fake document and waits for the `document` to be ready.

*

* @param data Data to include in the iframe

*/

static build(data) {

return __awaiter(this, void 0, void 0, function* () {

const fakeDoc = new FakeDocument(data);

yield fakeDoc.waitForDocument();

return fakeDoc;

});

}

/**

* @return The document of the iframe

*/

get document() {

return this.iframe.contentDocument;

}

/**

* @param data Data to include in the iframe

*/

constructor(data) {

this.ready = false;

this.iframe = document.createElement('iframe');

this.iframe.style.display = 'none';

this.iframe.addEventListener('load', () => {

this.ready = true;

});

this.iframe.src = URL.createObjectURL(data instanceof Blob ? data : new Blob(data));

// Disables JavaScript, modals, popups, etc., but allows same-origin access.

this.iframe.setAttribute('sandbox', 'allow-same-origin');

document.getElementsByTagName('body')[0].appendChild(this.iframe);

}

/**

* Returns the Document of the iframe when ready.

*/

waitForDocument() {

return __awaiter(this, void 0, void 0, function* () {

while (!this.ready ||

this.document == null ||

!this.document.getElementsByTagName('body')[0]

.classList.contains('mediawiki')) {

yield new Promise((res) => {

setTimeout(res, 10);

});

}

return this.document;

});

}

/**

* Performs cleanup

*/

close() {

removeElement(this.iframe);

}

}

/**

*

*/

class DiffPage {

/**

* Reloads the current diff page. Takes inspiration from Extension:RevisionSlider.

*

* @param diff

* @param options

* @see https://w.wiki/5Roy

*/

static loadNewDiff(diff, options = {}) {

var _a, _b;

return __awaiter(this, void 0, void 0, function* () {

const diffUrl = getRevisionDiffURL(diff, (_a = options.oldid) !== null && _a !== void 0 ? _a : null, true);

const contentText = document.querySelector('#mw-content-text');

contentText.classList.add('dp-reloading');

const diffDoc = yield fetch(diffUrl)

.then((r) => r.blob(), () => {

mw.loader.using([

'oojs-ui-core', 'oojs-ui-windows'

], () => {

OO.ui.alert(mw.msg('deputy.session.page.diff.loadFail'));

});

return null;

})

.then((b) => b == null ? null : FakeDocument.build(b))

.then((d) => d);

if (diffDoc == null) {

return;

}

const newContentText = diffDoc.document.querySelector('#mw-content-text');

swapElements(contentText, newContentText);

document.querySelectorAll('#ca-edit a, #ca-ve-edit a').forEach((e) => {

const newEditUrl = new URL(e.getAttribute('href'), window.location.href);

newEditUrl.searchParams.set('oldid', `${diff}`);

e.setAttribute('href', newEditUrl.href);

});

// Extract wgDiffOldId from HTML (because JavaScript remains unparsed and oldid

// (from parameters) might be a null value.

const oldid = (_b = /"wgDiffOldId":\s*(\d+)/g.exec(diffDoc.document.head.outerHTML)) === null || _b === void 0 ? void 0 : _b[1];

// T161257

mw.config.set({

wgRevisionId: diff,

wgDiffOldId: +oldid,

wgDiffNewId: diff

});

// Forgetting JQuery ban for now. Backwards-compat reasons.

mw.hook('wikipage.content').fire($(newContentText));

mw.hook('wikipage.diff').fire($(document.querySelector('body > table.diff')));

history.pushState({}, null, diffUrl);

});

}

}

/**

* Utility class for generating URLs to Earwig's Copyvio Detector.

*/

class EarwigCopyvioDetector {

/**

* Guesses the current project and language.

*

* @param project

* @param language

* @return The (guessed) project and language

*/

static guessProject(project, language) {

// Attempt to guess the language and project.

const splitHost = window.location.host.split('.');

if (!project && splitHost[splitHost.length - 2]) {

// Project (e.g. wikipedia)

project = splitHost[splitHost.length - 2];

}

if (!language && splitHost[splitHost.length - 3]) {

// Language (e.g. en)

language = splitHost[splitHost.length - 3];

}

return { project, language };

}

/**

* Get Earwig's Copyvio Detector's supported languages and projects.

*/

static getSupported() {

return __awaiter(this, void 0, void 0, function* () {

if (!!this.supportedLanguages && !!this.supportedProjects) {

// Already loaded.

return;

}

const cachedSupportedRaw = window.sessionStorage.getItem('dp-earwig-supported');

if (cachedSupportedRaw) {

const cachedSupported = JSON.parse(cachedSupportedRaw);

this.supportedLanguages = cachedSupported.languages;

this.supportedProjects = cachedSupported.projects;

}

const sites = yield fetch(`${(yield window.deputy.getWikiConfig()).cci.earwigRoot.get()}/api.json?action=sites&version=1`)

.then((r) => r.json());

this.supportedLanguages = [];

for (const lang of sites.langs) {

this.supportedLanguages.push(lang[0]);

}

this.supportedProjects = [];

for (const project of sites.projects) {

this.supportedProjects.push(project[0]);

}

window.sessionStorage.setItem('dp-earwig-supported', JSON.stringify({

languages: this.supportedLanguages,

projects: this.supportedProjects

}));

});

}

/**

* Checks if this wiki is supported by Earwig's Copyvio Detector.

*

* @param _project The project to check for

* @param _language The language to check for

*/

static supports(_project, _language) {

return __awaiter(this, void 0, void 0, function* () {

yield this.getSupported();

const { project, language } = this.guessProject(_project, _language);

return EarwigCopyvioDetector.supportedProjects.indexOf(project) !== -1 &&

EarwigCopyvioDetector.supportedLanguages.indexOf(language) !== -1;

});

}

/**

* Generates a URL for Earwig's Copyvio Detector.

*

* @param target

* @param options

* @return An Earwig Copyvio Detector execution URL. `null` if wiki is not supported.

*/

static getUrl(target, options = {}) {

var _a, _b, _c;

return __awaiter(this, void 0, void 0, function* () {

if (!(yield this.supports())) {

return null;

}

const { project, language } = this.guessProject(options.project, options.language);

return `${(yield window.deputy.getWikiConfig()).cci.earwigRoot.get()}?action=search&lang=${language}&project=${project}&${typeof target === 'number' ?

'oldid=' + target :

'title=' + target.getPrefixedText()}&use_engine=${((_a = options.useEngine) !== null && _a !== void 0 ? _a : true) ? 1 : 0}&use_links=${((_b = options.useLinks) !== null && _b !== void 0 ? _b : true) ? 1 : 0}&turnitin=${((_c = options.turnItIn) !== null && _c !== void 0 ? _c : false) ? 1 : 0}`;

});

}

}

/**

* Renders a MenuLayout responsible for displaying analysis options or tools.

*/

class DeputyPageMenu {

/**

* @param options

* @param toolbar

* @param baseWidget

*/

constructor(options, toolbar, baseWidget) {

this.options = options;

this.toolbar = toolbar;

this.baseWidget = baseWidget;

}

/**

* @inheritDoc

*/

render() {

const menuItems = new Map();

const menuSelectWidget = new OO.ui.MenuSelectWidget({

autoHide: false,

hideWhenOutOfView: false,

verticalPosition: 'above',

horizontalPosition: 'start',

widget: this.baseWidget,

$floatableContainer: this.baseWidget.$element,

items: this.options.map((option, i) => {

const item = new OO.ui.MenuOptionWidget({

data: i,

disabled: option.condition ? !(option.condition(this.toolbar)) : false,

icon: option.icon,

label: option.label

});

menuItems.set(item, option);

return item;

})

});

menuSelectWidget.on('select', () => {

// Not a multiselect MenuSelectWidget

const selected = menuSelectWidget.findSelectedItem();

if (selected) {

this.options[selected.getData()].action(this.toolbar);

// Clear selections.

menuSelectWidget.selectItem();

this.baseWidget.setValue(false);

}

});

// Disables clipping (allows the menu to be wider than the button)

menuSelectWidget.toggleClipping(false);

this.baseWidget.on('change', (toggled) => {

// Recalculate disabled condition

menuItems.forEach((option, item) => {

item.setDisabled(option.condition ? !(option.condition(this.toolbar)) : false);

});

menuSelectWidget.toggle(toggled);

});

return unwrapWidget(menuSelectWidget);

}

}

var deputyPageAnalysisOptions = () => [

{

icon: 'eye',

label: mw.msg('deputy.session.page.earwigLatest'),

action: (toolbar) => __awaiter(void 0, void 0, void 0, function* () {

const url = yield EarwigCopyvioDetector.getUrl(toolbar.row.title);

window.open(url, '_blank', 'noopener');

if (url == null) {

mw.notify(mw.msg('deputy.session.page.earwigUnsupported'), {

type: 'error'

});

}

else {

window.open(url, '_blank', 'noopener');

}

})

},

{

icon: 'eye',

label: mw.msg('deputy.session.page.earwigRevision'),

condition: (toolbar) => toolbar.revision != null,

action: (toolbar) => __awaiter(void 0, void 0, void 0, function* () {

const url = yield EarwigCopyvioDetector.getUrl(toolbar.revision);

if (url == null) {

mw.notify(mw.msg('deputy.session.page.earwigUnsupported'), {

type: 'error'

});

}

else {

window.open(url, '_blank', 'noopener');

}

})

},

{

icon: 'references',

label: mw.msg('deputy.session.page.iabot'),

action: (toolbar) => __awaiter(void 0, void 0, void 0, function* () {

const url = new URL('https://iabot.toolforge.org/index.php');

url.searchParams.set('page', 'runbotsingle');

url.searchParams.set('pagesearch', toolbar.row.title.getPrefixedText());

url.searchParams.set('archiveall', 'on');

url.searchParams.set('wiki', 'enwiki');

url.searchParams.set('reason', mw.msg('deputy.session.page.iabot.reason'));

window.open(url, '_blank', 'noopener');

})

}

];

var deputyPageTools = () => [

{

icon: 'copy',

label: mw.msg('deputy.ante'),

condition: () => window.deputy.ante.startState,

action: () => __awaiter(void 0, void 0, void 0, function* () {

window.deputy.ante.openEditDialog();

})

},

{

icon: 'flag',

label: mw.msg('deputy.ia'),

condition: () => true,

action: () => __awaiter(void 0, void 0, void 0, function* () {

yield window.deputy.ia.openWorkflowDialog();

})

}

];

/**

* The DeputyPageToolbar is appended to all pages (outside the mw-parser-output block)

* that are part of the currently-active case page. It includes the status dropdown,

* page name, basic case info, and analysis tools.

*

* The toolbar automatically connects with an existing session through the use of

* inter-tab communication (facilitated by DeputyCommunications).

*/

class DeputyPageToolbar {

/**

* @param options The data received from a page status request.

* Used to initialize some values.

*/

constructor(options) {

var _a;

this.state = DeputyPageToolbarState.Open;

this.instanceId = generateId();

this.revisionStatusUpdateListener = this.onRevisionStatusUpdate.bind(this);

this.options = options;

if (options.revisionStatus != null) {

this.revision = (_a = options.revision) !== null && _a !== void 0 ? _a : mw.config.get('wgRevisionId');

}

this.state = window.deputy.config.cci.toolbarInitialState.get();

this.runAsyncJobs();

}

/**

* Runs asynchronous preparation jobs. Makes loading more seamless later in execution,

* as this will run functions that cache data in the background.

*/

runAsyncJobs() {

return __awaiter(this, void 0, void 0, function* () {

yield EarwigCopyvioDetector.getSupported();

});

}

/**

* @inheritDoc

*/

prepare() {

return __awaiter(this, void 0, void 0, function* () {

this.row = {

casePage: yield DeputyCase.build(this.options.caseId, normalizeTitle(this.options.caseTitle)),

title: normalizeTitle(this.options.title),

type: this.options.rowType

};

});

}

/**

* Instantiates a DeputyCCIStatusDropdown and returns the HTML element for it.

*

* @return The OOUI dropdown's HTMLElement

*/

renderStatusDropdown() {

this.statusDropdown = new DeputyCCIStatusDropdown(this.row, {

status: this.options.status,

enabled: this.options.enabledStatuses

});

this.statusDropdown.addEventListener('updateFail', () => {

OO.ui.alert(mw.msg('deputy.session.page.incommunicable'));

});

return unwrapWidget(this.statusDropdown.dropdown);

}

/**

* Renders the "current case" section on the toolbar.

*

* @return The "current case" section

*/

renderCaseInfo() {

return h_1("div", { class: "dp-pt-section" },

h_1("div", { class: "dp-pt-section-label" }, mw.msg('deputy.session.page.caseInfo.label')),

h_1("a", { class: "dp-pt-section-content dp-pt-caseInfo" }, this.row.casePage.getCaseName()));

}

/**

* Renders the "revision" section on the toolbar.

*

* @return The "Revision #XXXXXXXXXX" section

*/

renderRevisionInfo() {

var _a;

if (this.revision == null) {

if (

// Show if forced, or if we're not looking at the latest revision.

mw.config.get('wgRevisionId') !== mw.config.get('wgCurRevisionId') ||

((_a = this.options.forceRevision) !== null && _a !== void 0 ? _a : true)) {

return this.renderMissingRevisionInfo();

}

else {

return null;

}

}

this.revisionCheckbox = new OO.ui.CheckboxInputWidget({

title: mw.msg('deputy.session.revision.assessed'),

selected: this.options.revisionStatus

});

let lastStatus = this.revisionCheckbox.isSelected();

// State variables

let processing = false;

let incommunicable = false;

this.revisionCheckbox.on('change', (selected) => __awaiter(this, void 0, void 0, function* () {

if (incommunicable) {

incommunicable = false;

return;

}

else if (processing) {

return;

}

processing = true;

const response = yield window.deputy.comms.sendAndWait({

type: 'revisionStatusUpdate',

caseId: this.row.casePage.pageId,

page: this.row.title.getPrefixedText(),

revision: this.revision,

status: selected,

nextRevision: null

});

if (response == null) {

OO.ui.alert(mw.msg('deputy.session.page.incommunicable'));

// Sets flag to avoid running this listener twice.

incommunicable = true;

this.revisionCheckbox.setSelected(lastStatus);

}

else {

// Replace the last status for "undo".

lastStatus = this.revisionCheckbox.isSelected();

}

processing = false;

}));

window.deputy.comms.addEventListener('revisionStatusUpdate', this.revisionStatusUpdateListener);

return h_1("div", { class: "dp-pt-section" },

h_1("div", { class: "dp-pt-section-label" }, mw.message('deputy.session.page.caseInfo.revision', `${this.revision}`).text()),

h_1("div", { class: "dp-pt-section-content" }, unwrapWidget(new OO.ui.FieldLayout(this.revisionCheckbox, {

align: 'inline',

label: mw.msg('deputy.session.page.caseInfo.assessed')

}))));

}

/**

* Replaces `renderRevisionInfo` if a revision does not exist. Placeholder to

* allow tools to be used anyway, even without having an active revision associated.

*

* @return The "Revision out of scope" section

*/

renderMissingRevisionInfo() {

const helpPopup = new OO.ui.PopupButtonWidget({

icon: 'info',

framed: false,

label: mw.msg('deputy.moreInfo'),

invisibleLabel: true,

popup: {

head: true,

padded: true,

label: mw.msg('deputy.moreInfo'),

align: 'forwards'

}

});

unwrapWidget(helpPopup).querySelector('.oo-ui-popupWidget-body')

.appendChild(h_1("p", null, mw.msg('deputy.session.page.caseInfo.revision.help')));

return h_1("div", { class: "dp-pt-section" },

h_1("div", { class: "dp-pt-section-label" }, mw.msg('deputy.session.page.caseInfo.revision.none')),

h_1("div", { class: "dp-pt-section-content dp-pt-missingRevision" }, unwrapWidget(helpPopup)));

}

/**

* Renders the next revision button. Used to navigate to the next unassessed revision

* for a row.

*

* @return The OOUI ButtonWidget element.

*/

renderRevisionNavigationButtons() {

if (this.row.type === 'pageonly') {

return h_1("div", { class: "dp-pt-section" }, unwrapWidget(new OO.ui.PopupButtonWidget({

icon: 'info',

framed: false,

label: mw.msg('deputy.session.page.pageonly.title'),

popup: {

head: true,

icon: 'infoFilled',

label: mw.msg('deputy.session.page.pageonly.title'),

$content: $(h_1("p", null, mw.msg('deputy.session.page.pageonly.help'))),

padded: true

}

})));

}

const getButtonClickHandler = (button, reverse) => {

return () => __awaiter(this, void 0, void 0, function* () {

this.setDisabled(true);

if (this.options.nextRevision) {

// No need to worry about swapping elements here, since `loadNewDiff`

// will fire the `wikipage.diff` MW hook. This means this element will

// be rebuilt from scratch anyway.

try {

const nextRevisionData = yield window.deputy.comms.sendAndWait({

type: 'pageNextRevisionRequest',

caseId: this.options.caseId,

page: this.row.title.getPrefixedText(),

after: this.revision,

reverse

});

if (nextRevisionData == null) {

OO.ui.alert(mw.msg('deputy.session.page.incommunicable'));

this.setDisabled(false);

}

else if (nextRevisionData.revid != null) {

yield DiffPage.loadNewDiff(nextRevisionData.revid);

}

else {

this.setDisabled(false);

button.setDisabled(true);

}

}

catch (e) {

error(e);

this.setDisabled(false);

}

}

else if (this.options.nextRevision !== false) {

// Sets disabled to false if the value is null.

this.setDisabled(false);

}

});

};

this.previousRevisionButton = new OO.ui.ButtonWidget({

invisibleLabel: true,

label: mw.msg('deputy.session.page.diff.previous'),

title: mw.msg('deputy.session.page.diff.previous'),

icon: 'previous',

disabled: this.options.nextRevision == null

});

this.previousRevisionButton.on('click', getButtonClickHandler(this.nextRevisionButton, true));

this.nextRevisionButton = new OO.ui.ButtonWidget({

invisibleLabel: true,

label: mw.msg('deputy.session.page.diff.next'),

title: mw.msg('deputy.session.page.diff.next'),

icon: this.revision == null ? 'play' : 'next',

disabled: this.options.nextRevision == null

});

this.nextRevisionButton.on('click', getButtonClickHandler(this.nextRevisionButton, false));

return h_1("div", { class: "dp-pt-section" },

h_1("div", { class: "dp-pt-section-content" },

this.revision != null && unwrapWidget(this.previousRevisionButton),

unwrapWidget(this.nextRevisionButton)));

}

/**

* Renders a OOUI PopupButtonWidget and a menu, which contains a given set of

* menu options.

*

* @param label The label of the section

* @param options The section menu options

* @return The section HTML

*/

renderMenu(label, options) {

const popupButton = new OO.ui.ToggleButtonWidget({

label: label,

framed: false,

indicator: 'up'

});

return [

new DeputyPageMenu(options, this, popupButton).render(),

unwrapWidget(popupButton)

];

}

/**

* Renders the "Analysis" and "Tools" sections.

*

* @return The section HTML

*/

renderMenus() {

return h_1("div", { class: "dp-pt-section" },

h_1("div", { class: "dp-pt-section-content dp-pt-menu" },

this.renderMenu(mw.msg('deputy.session.page.analysis'), deputyPageAnalysisOptions()),

this.renderMenu(mw.msg('deputy.session.page.tools'), deputyPageTools())));

}

/**

* Rends the page toolbar actions and main section, if the dropdown is open.

*/

renderOpen() {

return [

h_1("div", { class: "dp-pageToolbar-actions" },

h_1("div", { class: "dp-pageToolbar-close", role: "button", title: mw.msg('deputy.session.page.close'), onClick: () => this.setState(DeputyPageToolbarState.Hidden) }),

h_1("div", { class: "dp-pageToolbar-collapse", role: "button", title: mw.msg('deputy.session.page.collapse'), onClick: () => this.setState(DeputyPageToolbarState.Collapsed) })),

h_1("div", { class: "dp-pageToolbar-main" },

this.renderStatusDropdown(),

this.renderCaseInfo(),

this.renderRevisionInfo(),

this.revisionNavigationSection =

this.renderRevisionNavigationButtons(),

this.renderMenus())

];

}

/**

* Renders the collapsed toolbar button.

*

* @return The render button, to be included in the main toolbar.

*/

renderCollapsed() {

return h_1("div", { class: "dp-pageToolbar-collapsed", role: "button", title: mw.msg('deputy.session.page.expand'), onClick: () => this.setState(DeputyPageToolbarState.Open) });

}

/**

* @inheritDoc

*/

render() {

console.log(this.state);

if (this.state === DeputyPageToolbarState.Hidden) {

const portletLink = mw.util.addPortletLink('p-tb', '#', mw.msg('deputy.session.page.open'), 'pt-dp-pt', mw.msg('deputy.session.page.open.tooltip'));

portletLink.querySelector('a').addEventListener('click', (event) => {

event.preventDefault();

this.setState(DeputyPageToolbarState.Open);

return false;

});

// Placeholder element

return this.element = h_1("div", { class: "deputy" });

}

else {

const toolbar = document.getElementById('pt-dp-pt');

if (toolbar) {

removeElement(toolbar);

}

}

return this.element = h_1("div", { class: "deputy dp-pageToolbar" },

this.state === DeputyPageToolbarState.Open && this.renderOpen(),

this.state === DeputyPageToolbarState.Collapsed && this.renderCollapsed());

}

/**

* Sets the disabled state of the toolbar.

*

* @param disabled

*/

setDisabled(disabled) {

var _a, _b, _c, _d;

(_a = this.statusDropdown) === null || _a === void 0 ? void 0 : _a.setDisabled(disabled);

(_b = this.revisionCheckbox) === null || _b === void 0 ? void 0 : _b.setDisabled(disabled);

(_c = this.previousRevisionButton) === null || _c === void 0 ? void 0 : _c.setDisabled(disabled);

(_d = this.nextRevisionButton) === null || _d === void 0 ? void 0 : _d.setDisabled(disabled);

}

/**

* Sets the display state of the toolbar. This will also set the

* initial state configuration option for the user.

*

* @param state

*/

setState(state) {

this.state = state;

window.deputy.config.cci.toolbarInitialState.set(state);

window.deputy.config.save();

swapElements(this.element, this.render());

}

/**

* Performs cleanup and removes the element from the DOM.

*/

close() {

window.deputy.comms.removeEventListener('revisionStatusUpdate', this.revisionStatusUpdateListener);

this.statusDropdown.close();

removeElement(this.element);

}

/**

* Listener for revision status updates from the root session.

*

* @param root0

* @param root0.data

*/

onRevisionStatusUpdate({ data }) {

if (this.row.casePage.pageId === data.caseId &&

this.row.title.getPrefixedText() === data.page) {

if (this.revision === data.revision &&

this.revisionCheckbox.isSelected() !== data.status) {

this.revisionCheckbox.setSelected(data.status);

}

this.options.nextRevision = data.nextRevision;

// Re-render button.

swapElements(this.revisionNavigationSection, this.revisionNavigationSection =

this.renderRevisionNavigationButtons());

}

}

}

/**

* Controls everything related to a page that is the subject of an active

* Deputy row.

*/

class DeputyPageSession {

constructor() {

this.sessionCloseHandler = this.onSessionClosed.bind(this);

}

/**

* Attempts to grab page details from a session. If a session does not exist,

* this will return null.

*

* @param revision The revision of the page to get information for.

* If the page is being viewed normally (not in a diff or permanent link), then

* this value should be set to null. This ensures that a generic toolbar is

* used instead of the revision-specific toolbar.

* @param title The title of the page to get information for. Defaults to current.

* @param timeout Timeout for the page detail request.

*/

static getPageDetails(revision = mw.config.get('wgRevisionId'), title = window.deputy.currentPage, timeout = 500) {

return __awaiter(this, void 0, void 0, function* () {

return window.deputy.comms.sendAndWait({

type: 'pageStatusRequest',

page: title.getPrefixedText(),

revision: revision

}, timeout);

});

}

/**

* @param data

*/

init(data) {

window.deputy.comms.addEventListener('sessionClosed', this.sessionCloseHandler);

// Spawn toolbar

if (window.deputy.config.cci.enablePageToolbar.get()) {

mw.loader.using([

'oojs-ui-core',

'oojs-ui-windows',

'oojs-ui-widgets',

'oojs-ui.styles.icons-interactions',

'oojs-ui.styles.icons-movement',

'oojs-ui.styles.icons-moderation',

'oojs-ui.styles.icons-media',

'oojs-ui.styles.icons-editing-advanced',

'oojs-ui.styles.icons-editing-citation'

], () => {

if (mw.config.get('wgDiffNewId') === null) {

// Not on a diff page, but wgRevisionId is populated nonetheless.

this.initInterface(data);

}

else {

mw.hook('wikipage.diff').add(() => __awaiter(this, void 0, void 0, function* () {

yield this.initInterface(data);

}));

}

});

}

}

/**

* Initialize the interface.

*

* @param data

*/

initInterface(data) {

return __awaiter(this, void 0, void 0, function* () {

// Attempt to get new revision data *with revision ID*.

const isCurrentDiff = /[?&]diff=0+(\D|$)/.test(window.location.search);

data = yield DeputyPageSession.getPageDetails((isCurrentDiff ?

// On a "cur" diff page

mw.config.get('wgDiffOldId') :

// On a "prev" diff page

mw.config.get('wgDiffNewId')) ||

mw.config.get('wgRevisionId'), window.deputy.currentPage,

// Relatively low-stakes branch, we can handle a bit of a delay.

2000);

const openPromise = this.appendToolbar(Object.assign(Object.assign({}, data), { forceRevision: this.toolbar != null ||

// Is a diff page.

mw.config.get('wgDiffNewId') != null }));

if (

// Previous toolbar exists. Close it before moving on.

this.toolbar &&

this.toolbar.revision !== mw.config.get('wgRevisionId')) {

const oldToolbar = this.toolbar;

openPromise.then(() => {

oldToolbar.close();

});

}

this.toolbar = yield openPromise;

});

}

/**

* Creates the Deputy page toolbar and appends it to the DOM.

*

* @param data Data for constructing the toolbar

*/

appendToolbar(data) {

return __awaiter(this, void 0, void 0, function* () {

const toolbar = new DeputyPageToolbar(data);

yield toolbar.prepare();

document.getElementsByTagName('body')[0]

.appendChild(toolbar.render());

return toolbar;

});

}

/**

* Cleanup toolbar, remove event listeners, and remove from DOM.

*/

close() {

var _a;

(_a = this.toolbar) === null || _a === void 0 ? void 0 : _a.close();

window.deputy.comms.removeEventListener('sessionClosed', this.sessionCloseHandler);

}

/**

* Handler for when a session is closed.

*/

onSessionClosed() {

this.close();

}

}

/**

* Handles the active Deputy session.

*

* A "Session" is a period wherein Deputy exercises a majority of its features,

* namely the use of inter-tab communication and database transactions for

* page and revision caching. Other tabs that load Deputy will recognize the

* started session and begin communicating with the root tab (the tab with the

* CCI page, and therefore the main Deputy session handler, open). The handler

* for root tab session activities is {@link DeputyRootSession}.

*/

class DeputySession {

constructor() {

this.DeputyRootSession = DeputyRootSession;

this.DeputyPageSession = DeputyPageSession;

}

/**

* Initialize session-related information. If an active session was detected,

* restart it.

*/

init() {

return __awaiter(this, void 0, void 0, function* () {

// Check if there is an active session.

const session = yield this.getSession();

// Ensure wiki config is loaded

const wikiConfig = yield window.deputy.getWikiConfig();

if (!wikiConfig.cci.enabled.get() || wikiConfig.cci.rootPage.get() == null) {

// Not configured. Exit.

return;

}

if (session) {

const viewingCurrent =

// Page is being viewed.

mw.config.get('wgAction') === 'view' &&

// Revision is current revision. Also handles wgRevisionId = 0

// (which happens when viewing a diff).

mw.config.get('wgRevisionId') ===

mw.config.get('wgCurRevisionId');

if (session.caseSections.length === 0) {

// No more sections. Discard session.

yield this.clearSession();

yield this.init();

}

else if (session.casePageId === window.deputy.currentPageId) {

// Definitely a case page, no need to question.

const casePage = yield DeputyCasePage.build();

this.rootSession = new DeputyRootSession(session, casePage);

if (viewingCurrent && (yield this.checkForActiveSessionTabs())) {

// Session is active in another tab. Defer to other tab.

yield DeputyRootSession.initTabActiveInterface(casePage);

}

else if (viewingCurrent) {

// Page reloaded or exited without proper session close.

// Continue where we left off.

yield this.rootSession.initSessionInterface();

yield casePage.bumpActive();

}

}

else if (DeputyCasePage.isCasePage()) {

if (mw.config.get('wgCurRevisionId') !==

mw.config.get('wgRevisionId')) {

// This is an old revision. Don't show the interface.

return;

}

const casePage = yield DeputyCasePage.build();

yield DeputyRootSession.initOverwriteMessage(casePage);

}

else if (mw.config.get('wgAction') === 'view') {

yield this.normalPageInitialization();

window.deputy.comms.addEventListener('sessionStarted', () => {

// This misses by a few seconds right now since sessionStarted is

// called when the sessionStarts but not when it is ready.

// TODO: Fix that.

this.normalPageInitialization();

});

}

}

else {

// No active session

if (DeputyCasePage.isCasePage()) {

const casePage = yield DeputyCasePage.build();

if (yield casePage.isCached()) {

yield DeputyRootSession.initContinueInterface(casePage);

}

else {

// Show session start buttons

yield DeputyRootSession.initEntryInterface(casePage);

}

}

}

});

}

/**

* Broadcasts a `sessionRequest` message to the Deputy communicator to find other

* tabs with open sessions. This prevents two tabs from opening the same session

* at the same time.

*/

checkForActiveSessionTabs() {

return __awaiter(this, void 0, void 0, function* () {

return yield window.deputy.comms.sendAndWait({ type: 'sessionRequest' })

.then((res) => {

return res != null;

});

});

}

/**

* Detects if a session is currently active, attempt to get page details, and

* start a page session if details have been found.

*

* @return `true` if a session was started, `false` otherwise.

*/

normalPageInitialization() {

return __awaiter(this, void 0, void 0, function* () {

// Normal page. Determine if this is being worked on, and then

// start a new session if it is.

const pageSession = yield DeputyPageSession.getPageDetails();

if (pageSession) {

// This page is being worked on, create a session.

this.pageSession = new DeputyPageSession();

this.pageSession.init(pageSession);

return true;

}

else {

return false;

}

});

}

/**

* Gets the current active session information. Session mutation functions (besides

* `clearSession`) are only available in {@link DeputyRootSession}.

*

* @return {Promise}

* A promise that resolves with the session information or `undefined` if session

* information is not available.

*/

getSession() {

return __awaiter(this, void 0, void 0, function* () {

return (yield window.deputy.storage.getKV('session'));

});

}

/**

* Sets the current active session information.

*

* @return boolean `true` if successful.

*/

clearSession() {

if (this.rootSession) {

this.rootSession.session = null;

}

return window.deputy.storage.setKV('session', null);

}

}

/**

* MediaWiki core contains a lot of quirks in the code. Other extensions

* also have their own quirks. To prevent these quirks from affecting Deputy's

* functionality, we need to perform a few hacks.

*/

function performHacks () {

var _a;

const HtmlEmitter = (_a = mw.jqueryMsg.HtmlEmitter) !== null && _a !== void 0 ? _a : {

prototype: Object.getPrototypeOf(new mw.jqueryMsg.Parser().emitter)

};

// This applies the {{int:message}} parser function with "MediaWiki:". This

// is due to VisualEditor using "MediaWiki:" in message values instead of "int:"

HtmlEmitter.prototype.mediawiki =

HtmlEmitter.prototype.int;

/**

* Performs a simple if check. Works just like the Extension:ParserFunctions

* version; it checks if the first parameter is blank and returns the second

* parameter if true. The latter parameter is passed if false.

*

* UNLIKE the Extension:ParserFunctions version, this version does not trim

* the parameters.

*

* @see https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#if

* @param nodes

* @return see function description

*/

HtmlEmitter.prototype.if = function (nodes) {

var _a, _b;

return (nodes[0].trim() ? ((_a = nodes[1]) !== null && _a !== void 0 ? _a : ) : ((_b = nodes[2]) !== null && _b !== void 0 ? _b : ));

};

// "#if" is unsupported due to the parsing done by jqueryMsg.

/**

* Simple function to avoid parsing errors during message expansion. Drops the "Template:"

* prefix before a link.

*

* @param nodes

* @return `{{text}}`

*/

HtmlEmitter.prototype.template = function (nodes) {

return `{{${nodes.join('|')}}}`;

};

/**

* Allows `{{subst:...}}` to work. Does not actually change anything.

*

* @param nodes

* @return `{{text}}`

*/

HtmlEmitter.prototype.subst = function (nodes) {

return `{{subst:${nodes.map((v) => typeof v === 'string' ? v : v.text()).join('|')}}}`;

};

/**

* Works exactly like the localurl magic word. Returns the local href to a page.

* Also adds query strings if given.

*

* @see https://www.mediawiki.org/wiki/Help:Magic_words#URL_data

* @param nodes

* @return `/wiki/{page}?{query}`

*/

HtmlEmitter.prototype.localurl = function (nodes) {

return mw.util.getUrl(nodes[0]) + '?' + nodes[1];

};

}

/**

* Copies text to the clipboard. Relies on the old style of clipboard copying

* (using `document.execCommand` due to a lack of support for `navigator`-based

* clipboard handling).

*

* @param text The text to copy to the clipboard.

*/

function copyToClipboard (text) {

const body = document.getElementsByTagName('body')[0];

const textarea = document.createElement('textarea');

textarea.value = text;

textarea.style.position = 'fixed';

textarea.style.left = '-100vw';

textarea.style.top = '-100vh';

body.appendChild(textarea);

textarea.select();

// noinspection JSDeprecatedSymbols

document.execCommand('copy');

body.removeChild(textarea);

}

/**

* Performs {{yesno}}-based string interpretation.

*

* @param value The value to check

* @param pull Depends which direction to pull unspecified values.

* @return If `pull` is true,

* any value that isn't explicitly a negative value will return `true`. Otherwise,

* any value that isn't explicitly a positive value will return `false`.

*/

function yesNo (value, pull = true) {

if (pull) {

return value !== false &&

value !== 'no' &&

value !== 'n' &&

value !== 'false' &&

value !== 'f' &&

value !== 'off' &&

+value !== 0;

}

else {

return !(value !== true &&

value !== 'yes' &&

value !== 'y' &&

value !== 't' &&

value !== 'true' &&

value !== 'on' &&

+value !== 1);

}

}

let InternalRevisionDateGetButton;

/**

* Initializes the process element.

*/

function initRevisionDateGetButton() {

InternalRevisionDateGetButton = class RevisionDateGetButton extends OO.ui.ButtonWidget {

/**

* @param config Configuration to be passed to the element.

*/

constructor(config) {

super(Object.assign({

icon: 'download',

invisibleLabel: true,

disabled: true

}, config));

this.revisionInputWidget = config.revisionInputWidget;

this.dateInputWidget = config.dateInputWidget;

this.revisionInputWidget.on('change', this.updateButton.bind(this));

this.dateInputWidget.on('change', this.updateButton.bind(this));

this.on('click', this.setDateFromRevision.bind(this));

this.updateButton();

}

/**

* Update the disabled state of the button.

*/

updateButton() {

this.setDisabled(isNaN(+this.revisionInputWidget.getValue()) ||

!!this.dateInputWidget.getValue());

}

/**

* Set the date from the revision ID provided in the value of

* `this.revisionInputWidget`.

*/

setDateFromRevision() {

return __awaiter(this, void 0, void 0, function* () {

const revid = this.revisionInputWidget.getValue();

if (isNaN(+revid)) {

mw.notify(mw.msg('deputy.ante.dateAuto.invalid'), { type: 'error' });

this.updateButton();

return;

}

this

.setIcon('ellipsis')

.setDisabled(true);

this.dateInputWidget.setDisabled(true);

yield MwApi.action.get({

action: 'query',

prop: 'revisions',

revids: revid,

rvprop: 'timestamp'

}).then((data) => {

if (data.query.badrevids != null) {

mw.notify(mw.msg('deputy.ante.dateAuto.missing', revid), { type: 'error' });

this.updateButton();

return;

}

this.dateInputWidget.setValue(

// ISO-format date

data.query.pages[0].revisions[0].timestamp.split('T')[0]);

this.dateInputWidget.setDisabled(false);

this.setIcon('download');

this.updateButton();

}, (_error, errorData) => {

mw.notify(mw.msg('deputy.ante.dateAuto.failed', getApiErrorText(errorData)), { type: 'error' });

this.dateInputWidget.setDisabled(false);

this.setIcon('download');

this.updateButton();

});

});

}

};

}

/**

* Creates a new RevisionDateGetButton.

*

* @param config Configuration to be passed to the element.

* @return A RevisionDateGetButton object

*/

function RevisionDateGetButton (config) {

if (!InternalRevisionDateGetButton) {

initRevisionDateGetButton();

}

return new InternalRevisionDateGetButton(config);

}

let InternalSmartTitleInputWidget;

/**

* Initializes the process element.

*/

function initSmartTitleInputWidget() {

InternalSmartTitleInputWidget = class SmartTitleInputWidget extends mw.widgets.TitleInputWidget {

/**

* @param config Configuration to be passed to the element.

*/

constructor(config) {

super(Object.assign(config, {

// Force this to be true

allowSuggestionsWhenEmpty: true

}));

}

/**

* @inheritDoc

*/

getRequestQuery() {

const v = super.getRequestQuery();

return v || normalizeTitle().getSubjectPage().getPrefixedText();

}

/**

* @inheritDoc

*/

getQueryValue() {

const v = super.getQueryValue();

return v || normalizeTitle().getSubjectPage().getPrefixedText();

}

};

}

/**

* Creates a new SmartTitleInputWidget.

*

* @param config Configuration to be passed to the element.

* @return A SmartTitleInputWidget object

*/

function SmartTitleInputWidget (config) {

if (!InternalSmartTitleInputWidget) {

initSmartTitleInputWidget();

}

return new InternalSmartTitleInputWidget(config);

}

let InternalPageLatestRevisionGetButton;

/**

* Initializes the process element.

*/

function initPageLatestRevisionGetButton() {

InternalPageLatestRevisionGetButton = class PageLatestRevisionGetButton extends OO.ui.ButtonWidget {

/**

* @param config Configuration to be passed to the element.

*/

constructor(config) {

super(Object.assign({

icon: 'download',

invisibleLabel: true,

disabled: true

}, config));

this.titleInputWidget = config.titleInputWidget;

this.revisionInputWidget = config.revisionInputWidget;

this.titleInputWidget.on('change', this.updateButton.bind(this));

this.revisionInputWidget.on('change', this.updateButton.bind(this));

this.on('click', this.setRevisionFromPageLatestRevision.bind(this));

this.updateButton();

}

/**

* Update the disabled state of the button.

*/

updateButton() {

this.setDisabled(this.titleInputWidget.getValue().trim().length === 0 ||

this.revisionInputWidget.getValue().trim().length !== 0 ||

!this.titleInputWidget.isQueryValid());

}

/**

* Set the revision ID from the page provided in the value of

* `this.titleInputWidget`.

*/

setRevisionFromPageLatestRevision() {

return __awaiter(this, void 0, void 0, function* () {

this

.setIcon('ellipsis')

.setDisabled(true);

this.revisionInputWidget.setDisabled(true);

const title = this.titleInputWidget.getValue();

yield MwApi.action.get({

action: 'query',

prop: 'revisions',

titles: title,

rvprop: 'ids'

}).then((data) => {

if (data.query.pages[0].missing) {

mw.notify(mw.msg('deputy.ante.revisionAuto.missing', title), { type: 'error' });

this.updateButton();

return;

}

this.revisionInputWidget.setValue(data.query.pages[0].revisions[0].revid);

this.revisionInputWidget.setDisabled(false);

this.setIcon('download');

this.updateButton();

}, (_error, errorData) => {

mw.notify(mw.msg('deputy.ante.revisionAuto.failed', getApiErrorText(errorData)), { type: 'error' });

this.revisionInputWidget.setDisabled(false);

this.setIcon('download');

this.updateButton();

});

});

}

};

}

/**

* Creates a new PageLatestRevisionGetButton.

*

* @param config Configuration to be passed to the element.

* @return A PageLatestRevisionGetButton object

*/

function PageLatestRevisionGetButton (config) {

if (!InternalPageLatestRevisionGetButton) {

initPageLatestRevisionGetButton();

}

return new InternalPageLatestRevisionGetButton(config);

}

let InternalCopiedTemplateRowPage;

/**

* The UI representation of a {{copied}} template row. This refers to a set of `diff`, `to`,

* or `from` parameters on each {{copied}} template.

*

* Note that "Page" in the class title does not refer to a MediaWiki page, but rather

* a OOUI PageLayout.

*/

function initCopiedTemplateRowPage() {

InternalCopiedTemplateRowPage = class CopiedTemplateRowPage extends OO.ui.PageLayout {

/**

* @param config Configuration to be passed to the element.

*/

constructor(config) {

const { copiedTemplateRow, parent } = config;

if (parent == null) {

throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');

}

else if (copiedTemplateRow == null) {

throw new Error('Reference row (CopiedTemplateRow) is required');

}

const finalConfig = {

classes: ['cte-page-row']

};

super(copiedTemplateRow.id, finalConfig);

this.parent = parent;

this.copiedTemplateRow = copiedTemplateRow;

this.refreshLabel();

this.copiedTemplateRow.parent.addEventListener('destroy', () => {

parent.rebuildPages();

});

this.copiedTemplateRow.parent.addEventListener('rowDelete', () => {

parent.rebuildPages();

});

this.$element.append(this.render().$element);

}

/**

* Refreshes the page's label

*/

refreshLabel() {

if (this.copiedTemplateRow.from && equalTitle(this.copiedTemplateRow.from, normalizeTitle(this.copiedTemplateRow.parent.parsoid.getPage())

.getSubjectPage())) {

this.label = mw.message('deputy.ante.copied.entry.shortTo', this.copiedTemplateRow.to || '???').text();

}

else if (this.copiedTemplateRow.to && equalTitle(this.copiedTemplateRow.to, normalizeTitle(this.copiedTemplateRow.parent.parsoid.getPage())

.getSubjectPage())) {

this.label = mw.message('deputy.ante.copied.entry.shortFrom', this.copiedTemplateRow.from || '???').text();

}

else {

this.label = mw.message('deputy.ante.copied.entry.short', this.copiedTemplateRow.from || '???', this.copiedTemplateRow.to || '???').text();

}

if (this.outlineItem) {

this.outlineItem.setLabel(this.label);

}

}

/**

* Renders this page. Returns a FieldsetLayout OOUI widget.

*

* @return An OOUI FieldsetLayout

*/

render() {

this.layout = new OO.ui.FieldsetLayout({

icon: 'parameter',

label: mw.msg('deputy.ante.copied.entry.label'),

classes: ['cte-fieldset']

});

this.layout.$element.append(this.renderButtons());

this.layout.addItems(this.renderFields());

return this.layout;

}

/**

* Renders a set of buttons used to modify a specific {{copied}} template row.

*

* @return An array of OOUI FieldLayouts

*/

renderButtons() {

const deleteButton = new OO.ui.ButtonWidget({

icon: 'trash',

title: mw.msg('deputy.ante.copied.entry.remove'),

framed: false,

flags: ['destructive']

});

deleteButton.on('click', () => {

this.copiedTemplateRow.parent.deleteRow(this.copiedTemplateRow);

});

const copyButton = new OO.ui.ButtonWidget({

icon: 'quotes',

title: mw.msg('deputy.ante.copied.entry.copy'),

framed: false

});

copyButton.on('click', () => {

// TODO: Find out a way to l10n-ize this.

let attributionString = `Attribution: Content ${this.copiedTemplateRow.merge ? 'merged' : 'partially copied'}`;

let lacking = false;

if (this.copiedTemplateRow.from != null &&

this.copiedTemplateRow.from.length !== 0) {

attributionString += ` from ${this.copiedTemplateRow.from}`;

}

else {

lacking = true;

if (this.copiedTemplateRow.from_oldid != null) {

attributionString += ' from a page';

}

}

if (this.copiedTemplateRow.from_oldid != null) {

attributionString += ` as of revision ${this.copiedTemplateRow.from_oldid}`;

}

if (this.copiedTemplateRow.to_diff != null ||

this.copiedTemplateRow.to_oldid != null) {

// Shifting will ensure that `to_oldid` will be used if `to_diff` is missing.

const diffPart1 = this.copiedTemplateRow.to_oldid ||

this.copiedTemplateRow.to_diff;

const diffPart2 = this.copiedTemplateRow.to_diff ||

this.copiedTemplateRow.to_oldid;

attributionString += ` with this edit`;

}

if (this.copiedTemplateRow.from != null &&

this.copiedTemplateRow.from.length !== 0) {

attributionString += `; refer to that page's edit history for additional attribution`;

}

attributionString += '.';

copyToClipboard(attributionString);

if (lacking) {

mw.notify(mw.msg('deputy.ante.copied.entry.copy.lacking'), { title: mw.msg('deputy.ante'), type: 'warn' });

}

else {

mw.notify(mw.msg('deputy.ante.copied.entry.copy.success'), { title: mw.msg('deputy.ante') });

}

});

return h_1("div", { style: {

float: 'right',

position: 'absolute',

top: '0.5em',

right: '0.5em'

} },

unwrapWidget(copyButton),

unwrapWidget(deleteButton));

}

/**

* Renders a set of OOUI InputWidgets and FieldLayouts, eventually returning an

* array of each FieldLayout to append to the FieldsetLayout.

*

* @return An array of OOUI FieldLayouts

*/

renderFields() {

const copiedTemplateRow = this.copiedTemplateRow;

const parsedDate = (copiedTemplateRow.date == null || copiedTemplateRow.date.trim().length === 0) ?

undefined : (!isNaN(new Date(copiedTemplateRow.date.trim() + ' UTC').getTime()) ?

(new Date(copiedTemplateRow.date.trim() + ' UTC')) : (!isNaN(new Date(copiedTemplateRow.date.trim()).getTime()) ?

new Date(copiedTemplateRow.date.trim()) : null));

this.inputs = {

from: SmartTitleInputWidget({

$overlay: this.parent.$overlay,

placeholder: mw.msg('deputy.ante.copied.from.placeholder'),

value: copiedTemplateRow.from,

validate: /^.+$/g

}),

from_oldid: new OO.ui.TextInputWidget({

placeholder: mw.msg('deputy.ante.copied.from_oldid.placeholder'),

value: copiedTemplateRow.from_oldid,

validate: /^\d*$/

}),

to: SmartTitleInputWidget({

$overlay: this.parent.$overlay,

placeholder: mw.msg('deputy.ante.copied.to.placeholder'),

value: copiedTemplateRow.to

}),

to_diff: new OO.ui.TextInputWidget({

placeholder: mw.msg('deputy.ante.copied.to_diff.placeholder'),

value: copiedTemplateRow.to_diff,

validate: /^\d*$/

}),

// Advanced options

to_oldid: new OO.ui.TextInputWidget({

placeholder: mw.msg('deputy.ante.copied.to_oldid.placeholder'),

value: copiedTemplateRow.to_oldid,

validate: /^\d*$/

}),

diff: new OO.ui.TextInputWidget({

placeholder: mw.msg('deputy.ante.copied.diff.placeholder'),

value: copiedTemplateRow.diff

}),

merge: new OO.ui.CheckboxInputWidget({

selected: yesNo(copiedTemplateRow.merge)

}),

afd: new OO.ui.TextInputWidget({

placeholder: mw.msg('deputy.ante.copied.afd.placeholder'),

value: copiedTemplateRow.afd,

disabled: copiedTemplateRow.merge === undefined,

// Prevent people from adding the WP:AFD prefix.

validate: /^((?!W(iki)?p(edia)?:(A(rticles)?[ _]?f(or)?[ _]?d(eletion)?\/)).+|$)/gi

}),

date: new mw.widgets.DateInputWidget({

$overlay: this.parent.$overlay,

icon: 'calendar',

value: parsedDate ? `${parsedDate.getUTCFullYear()}-${parsedDate.getUTCMonth() + 1}-${parsedDate.getUTCDate()}` : undefined,

placeholder: mw.msg('deputy.ante.copied.date.placeholder'),

calendar: {

verticalPosition: 'above'

}

}),

toggle: new OO.ui.ToggleSwitchWidget()

};

const diffConvert = new OO.ui.ButtonWidget({

label: mw.msg('deputy.ante.copied.convert')

});

const dateAuto = RevisionDateGetButton({

label: mw.msg('deputy.ante.dateAuto', 'to_diff'),

revisionInputWidget: this.inputs.to_diff,

dateInputWidget: this.inputs.date

});

const revisionAutoFrom = PageLatestRevisionGetButton({

invisibleLabel: false,

label: mw.msg('deputy.ante.revisionAuto'),

title: mw.msg('deputy.ante.revisionAuto.title', 'from'),

titleInputWidget: this.inputs.from,

revisionInputWidget: this.inputs.from_oldid

});

const revisionAutoTo = PageLatestRevisionGetButton({

invisibleLabel: false,

label: mw.msg('deputy.ante.revisionAuto'),

title: mw.msg('deputy.ante.revisionAuto.title', 'to'),

titleInputWidget: this.inputs.to,

revisionInputWidget: this.inputs.to_diff

});

this.fieldLayouts = {

from: new OO.ui.FieldLayout(this.inputs.from, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.from.label'),

align: 'top',

help: mw.msg('deputy.ante.copied.from.help')

}),

from_oldid: new OO.ui.ActionFieldLayout(this.inputs.from_oldid, revisionAutoFrom, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.from_oldid.label'),

align: 'left',

help: mw.msg('deputy.ante.copied.from_oldid.help')

}),

to: new OO.ui.FieldLayout(this.inputs.to, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.to.label'),

align: 'top',

help: mw.msg('deputy.ante.copied.to.help')

}),

to_diff: new OO.ui.ActionFieldLayout(this.inputs.to_diff, revisionAutoTo, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.to_diff.label'),

align: 'left',

help: mw.msg('deputy.ante.copied.to_diff.help')

}),

// Advanced options

to_oldid: new OO.ui.FieldLayout(this.inputs.to_oldid, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.to_oldid.label'),

align: 'left',

help: mw.msg('deputy.ante.copied.to_oldid.help')

}),

diff: new OO.ui.ActionFieldLayout(this.inputs.diff, diffConvert, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.diff.label'),

align: 'inline',

help: new OO.ui.HtmlSnippet(mw.message('deputy.ante.copied.diff.help').plain())

}),

merge: new OO.ui.FieldLayout(this.inputs.merge, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.merge.label'),

align: 'inline',

help: mw.msg('deputy.ante.copied.merge.help')

}),

afd: new OO.ui.FieldLayout(this.inputs.afd, {

$overlay: this.parent.$overlay,

label: mw.msg('deputy.ante.copied.afd.label'),

align: 'left',

help: mw.msg('deputy.ante.copied.afd.help')

}),

date: new OO.ui.ActionFieldLayout(this.inputs.date, dateAuto, {

align: 'inline',

classes: ['cte-fieldset-date']

}),

toggle: new OO.ui.FieldLayout(this.inputs.toggle, {

label: mw.msg('deputy.ante.copied.advanced'),

align: 'inline',

classes: ['cte-fieldset-advswitch']

})

};

if (parsedDate === null) {

this.fieldLayouts.date.setWarnings([

mw.msg('deputy.ante.copied.dateInvalid', copiedTemplateRow.date)

]);

}

// Define options that get hidden when advanced options are toggled

const advancedOptions = [

this.fieldLayouts.to_oldid,

this.fieldLayouts.diff,

this.fieldLayouts.merge,

this.fieldLayouts.afd

];

// Self-imposed deprecation notice in order to steer away from plain URL

// diff links. This will, in the long term, make it easier to parse out

// and edit {{copied}} templates.

const diffDeprecatedNotice = new OO.ui.HtmlSnippet(mw.message('deputy.ante.copied.diffDeprecate').plain());

// Hide advanced options

advancedOptions.forEach((e) => {

e.toggle(false);

});

// ...except for `diff` if it's supplied (legacy reasons)

if (copiedTemplateRow.diff) {

this.fieldLayouts.diff.toggle(true);

this.fieldLayouts.diff.setWarnings([diffDeprecatedNotice]);

}

else {

diffConvert.setDisabled(true);

}

// Attach event listeners

this.inputs.diff.on('change', () => {

if (this.inputs.diff.getValue().length > 0) {

try {

// Check if the diff URL is from this wiki.

if (new URL(this.inputs.diff.getValue(), window.location.href).host === window.location.host) {

// Prefer `to_oldid` and `to_diff`

this.fieldLayouts.diff.setWarnings([diffDeprecatedNotice]);

diffConvert.setDisabled(false);

}

else {

this.fieldLayouts.diff.setWarnings([]);

diffConvert.setDisabled(true);

}

}

catch (e) {

// Clear warnings just to be safe.

this.fieldLayouts.diff.setWarnings([]);

diffConvert.setDisabled(true);

}

}

else {

this.fieldLayouts.diff.setWarnings([]);

diffConvert.setDisabled(true);

}

});

this.inputs.merge.on('change', (value) => {

this.inputs.afd.setDisabled(!value);

});

this.inputs.toggle.on('change', (value) => {

advancedOptions.forEach((e) => {

e.toggle(value);

});

this.fieldLayouts.to_diff.setLabel(value ? 'Ending revision ID' : 'Revision ID');

});

this.inputs.from.on('change', () => {

this.refreshLabel();

});

this.inputs.to.on('change', () => {

this.refreshLabel();

});

for (const _field in this.inputs) {

if (_field === 'toggle') {

continue;

}

const field = _field;

const input = this.inputs[field];

// Attach the change listener

input.on('change', (value) => {

if (input instanceof OO.ui.CheckboxInputWidget) {

// Specific to `merge`. Watch out before adding more checkboxes.

this.copiedTemplateRow[field] = value ? 'yes' : '';

}

else if (input instanceof mw.widgets.DateInputWidget) {

this.copiedTemplateRow[field] = value ?

window.moment(value, 'YYYY-MM-DD')

.locale(mw.config.get('wgContentLanguage'))

.format('D MMMM Y') : undefined;

if (value.length > 0) {

this.fieldLayouts[field].setWarnings([]);

}

}

else {

this.copiedTemplateRow[field] = value;

}

copiedTemplateRow.parent.save();

this.refreshLabel();

});

if (input instanceof OO.ui.TextInputWidget) {

// Rechecks the validity of the field.

input.setValidityFlag();

}

}

// Diff convert click handler

diffConvert.on('click', this.convertDeprecatedDiff.bind(this));

return getObjectValues(this.fieldLayouts);

}

/**

* Converts a raw diff URL on the same wiki as the current to use `to` and `to_oldid`

* (and `to_diff`, if available).

*/

convertDeprecatedDiff() {

return __awaiter(this, void 0, void 0, function* () {

const value = this.inputs.diff.getValue();

try {

const url = new URL(value, window.location.href);

if (!value) {

return;

}

if (url.host !== window.location.host) {

if (!(yield OO.ui.confirm(mw.msg('deputy.ante.copied.diffDeprecate.warnHost')))) {

return;

}

}

// From the same wiki, accept deprecation immediately.

// Parse out info from this diff URL

const parseInfo = parseDiffUrl(url);

let { diff, oldid } = parseInfo;

const { title } = parseInfo;

// If only an oldid was provided, and no diff

if (oldid && !diff) {

diff = oldid;

oldid = undefined;

}

const confirmProcess = new OO.ui.Process();

// Looping over the row name and the value that will replace it.

for (const [_rowName, newValue] of [

['to_oldid', oldid],

['to_diff', diff],

['to', title]

]) {

const rowName = _rowName;

if (newValue == null) {

continue;

}

if (

// Field has an existing value

this.copiedTemplateRow[rowName] != null &&

this.copiedTemplateRow[rowName].length > 0 &&

this.copiedTemplateRow[rowName] !== newValue) {

confirmProcess.next(() => __awaiter(this, void 0, void 0, function* () {

const confirmPromise = dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.copied.diffDeprecate.replace', rowName, this.copiedTemplateRow[rowName], newValue).text());

confirmPromise.done((confirmed) => {

if (confirmed) {

this.inputs[rowName].setValue(newValue);

this.fieldLayouts[rowName].toggle(true);

}

});

return confirmPromise;

}));

}

else {

this.inputs[rowName].setValue(newValue);

this.fieldLayouts[rowName].toggle(true);

}

}

confirmProcess.next(() => {

this.copiedTemplateRow.parent.save();

this.inputs.diff.setValue('');

if (!this.inputs.toggle.getValue()) {

this.fieldLayouts.diff.toggle(false);

}

});

confirmProcess.execute();

}

catch (e) {

error('Cannot convert `diff` parameter to URL.', e);

OO.ui.alert(mw.msg('deputy.ante.copied.diffDeprecate.failed'));

}

});

}

/**

* Sets up the outline item of this page. Used in the BookletLayout.

*/

setupOutlineItem() {

if (this.outlineItem !== undefined) {

this.outlineItem

.setMovable(true)

.setRemovable(true)

.setIcon('parameter')

.setLevel(1)

.setLabel(this.label);

}

}

};

}

/**

* Creates a new CopiedTemplateRowPage.

*

* @param config Configuration to be passed to the element.

* @return A CopiedTemplateRowPage object

*/

function CopiedTemplateRowPage (config) {

if (!InternalCopiedTemplateRowPage) {

initCopiedTemplateRowPage();

}

return new InternalCopiedTemplateRowPage(config);

}

/**

* An attribution notice's row or entry.

*/

class AttributionNoticeRow {

/**

* @return The parent of this attribution notice row.

*/

get parent() {

return this._parent;

}

/**

* Sets the parent. Automatically moves this template from one

* parent's row set to another.

*

* @param newParent The new parent.

*/

set parent(newParent) {

this._parent.deleteRow(this);

newParent.addRow(this);

this._parent = newParent;

}

/**

*

* @param parent

*/

constructor(parent) {

this._parent = parent;

const r = window.btoa((Math.random() * 10000).toString()).slice(0, 6);

this.name = this.parent.name + '#' + r;

this.id = window.btoa(parent.node.getTarget().wt) + '-' + this.name;

}

/**

* Clones this row.

*

* @param parent The parent of this new row.

* @return The cloned row

*/

clone(parent) {

// Odd constructor usage here allows cloning from subclasses without having

// to re-implement the cloning function.

// noinspection JSCheckFunctionSignatures

return new this.constructor(this, parent);

}

}

const copiedTemplateRowParameters = [

'from', 'from_oldid', 'to', 'to_diff',

'to_oldid', 'diff', 'date', 'afd', 'merge'

];

/**

* Represents a row/entry in a {{copied}} template.

*/

class CopiedTemplateRow extends AttributionNoticeRow {

// noinspection JSDeprecatedSymbols

/**

* Creates a new RawCopiedTemplateRow

*

* @param rowObjects

* @param parent

*/

constructor(rowObjects, parent) {

super(parent);

this.from = rowObjects.from;

// eslint-disable-next-line camelcase

this.from_oldid = rowObjects.from_oldid;

this.to = rowObjects.to;

// eslint-disable-next-line camelcase

this.to_diff = rowObjects.to_diff;

// eslint-disable-next-line camelcase

this.to_oldid = rowObjects.to_oldid;

this.diff = rowObjects.diff;

this.date = rowObjects.date;

this.afd = rowObjects.afd;

this.merge = rowObjects.merge;

}

/**

* @inheritDoc

*/

clone(parent) {

return super.clone(parent);

}

/**

* @inheritDoc

*/

generatePage(dialog) {

return CopiedTemplateRowPage({

copiedTemplateRow: this,

parent: dialog

});

}

}

/**

* Merges templates together. Its own class to avoid circular dependencies.

*/

class TemplateMerger {

/**

* Merge an array of CopiedTemplates into one big CopiedTemplate. Other templates

* will be destroyed.

*

* @param templateList The list of templates to merge

* @param pivot The template to merge into. If not supplied, the first template

* in the list will be used.

*/

static merge(templateList, pivot) {

pivot = pivot !== null && pivot !== void 0 ? pivot : templateList[0];

while (templateList.length > 0) {

const template = templateList[0];

if (template !== pivot) {

if (template.node.getTarget().href !== pivot.node.getTarget().href) {

throw new Error("Attempted to merge incompatible templates.");

}

pivot.merge(template, { delete: true });

}

// Pop the pivot template out of the list.

templateList.shift();

}

}

}

/**

* Renders the panel used to merge multiple {{split article}} templates.

*

* @param type

* @param parentTemplate

* @param mergeButton

* @return A

element

*/

function renderMergePanel(type, parentTemplate, mergeButton) {

const mergePanel = new OO.ui.FieldsetLayout({

classes: ['cte-merge-panel'],

icon: 'tableMergeCells',

label: mw.msg('deputy.ante.merge.title')

});

unwrapWidget(mergePanel).style.padding = '16px';

unwrapWidget(mergePanel).style.zIndex = '20';

// Hide by default

mergePanel.toggle(false);

//