1
0
Fork 1
mirror of https://codeberg.org/keyoxide/doipjs.git synced 2025-04-25 23:55:17 -06:00

Merge branch 'dev' into youtube

This commit is contained in:
Daniel Levi 2025-03-08 10:06:41 +02:00
commit 565807f4dc
No known key found for this signature in database
GPG key ID: D267DF29810DBDD5
2 changed files with 159 additions and 1 deletions
src/serviceProviders

View file

@ -43,6 +43,7 @@ import * as discord from './discord.js'
import * as bsky from './bsky.js'
import * as sourcehut from './sourcehut.js'
import * as pronounspage from './pronounspage.js'
import * as mediawiki from './mediawiki.js'
import * as youtube from './youtube.js'
const _data = {
@ -76,7 +77,8 @@ const _data = {
discord,
bsky,
sourcehut,
youtube
youtube,
mediawiki
}
export const list = Object.keys(_data)

View file

@ -0,0 +1,156 @@
/*
Copyright 2025 Daniel Levi
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.
*/
/**
* MediaWiki service provider ({@link https://docs.keyoxide.org/service-providers/mediawiki/|Keyoxide docs})
* @module serviceProviders/mediawiki
* @example
* import { ServiceProviderDefinitions } from 'doipjs';
* const sp = ServiceProviderDefinitions.data.matrix.processURI('http(s)://domain.tld/script_path/index.php?title=TITLE&revid=REVID');
*/
import * as E from "../enums.js";
import { ServiceProvider } from "../serviceProvider.js";
// [1] - MediaWiki script path
// [2] - Revision ID
export const reURI = /^(https?:\/\/[^/]+\/(?:[^/]+\/)*)index\.php\?(?:[^&]*&)*(?:oldid|revid)=([^&#]+)(?:&.*)?$/;
/**
* @function
* @param {string} uri - Claim URI to process
* @returns {ServiceProvider} The service provider information based on the claim URI
*/
export function processURI(uri) {
const match = uri.match(reURI);
return new ServiceProvider({
about: {
id: "mediawiki",
name: "MediaWiki Wiki",
homepage: null,
},
profile: {
display: "Unknown username",
uri: null,
qr: null,
},
claim: {
uriRegularExpression: reURI.toString(),
uriIsAmbiguous: true,
},
proof: {
request: {
uri: `${match[1]}index.php?oldid=${match[2]}`,
fetcher: E.Fetcher.HTTP,
accessRestriction: E.ProofAccessRestriction.NOCORS,
data: {
url: `${match[1]}api.php?action=query&format=json&formatversion=2&prop=revisions&&revids=${match[2]}&rvprop=user%7Ccontent%7Ccomment&meta=siteinfo&siprop=general%7Cnamespaces`,
format: E.ProofFormat.JSON,
},
},
response: {
format: E.ProofFormat.JSON,
},
target: [
{
format: E.ClaimFormat.URI,
encoding: E.EntityEncodingFormat.PLAIN,
relation: E.ClaimRelation.EQUALS,
path: ["query", "pages", "revisions", "content"],
},
{
format: E.ClaimFormat.URI,
encoding: E.EntityEncodingFormat.PLAIN,
relation: E.ClaimRelation.EQUALS,
path: ["query", "pages", "revisions", "comment"],
},
],
},
});
}
export const functions = {
postprocess: async (claimData, proofData, opts) => {
const match = claimData.proof.request.uri.match(reURI);
claimData.about.name = proofData.result.query.general.sitename;
claimData.about.homepage = proofData.result.query.general.base;
const userName = `${proofData.result.query.namespaces["2"].name}:${proofData.result.query.pages[0].revisions[0].user}`;
claimData.profile.display = userName;
claimData.profile.uri = `${match[1]}index.php?title=${encodeURIComponent(
userName.replace(/ /g, "_")
)}`;
return { claimData, proofData };
},
};
export const tests = [
{
uri: "https://somewiki.tld/w/index.php?oldid=1234",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/index.php?oldid=1234",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/w/index.php?title=Some_title&oldid=1234",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/w/index.php?oldid=1234&title=Some_title",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/w/index.php?revid=1234",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/index.php?revid=1234",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/w/index.php?title=Some_title&revid=1234",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/w/index.php?revid=1234&title=Some_title",
shouldMatch: true,
},
{
uri: "https://somewiki.tld/w/index.php?title=Some_title",
shouldMatch: false,
},
{
uri: "https://somewiki.tld/wiki/Some_title",
shouldMatch: false,
},
{
uri: "https://somewiki.tld/wiki/Some_title?oldid=1234",
shouldMatch: false,
},
{
uri: "https://somewiki.tld/wiki/Some_title?revid=1234",
shouldMatch: false,
},
{
uri: "https://somewiki.tld/",
shouldMatch: false,
},
];