MediaWiki:Gadget-Wyckibaat.js
From Khyentse Lineage - A Tsadra Foundation Project
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
if ( mw.config.get( 'wgPageName' ) === 'Wyckibaat' ) {
// Welcome to the main Wyckibaat JS script.
// This code creates the interface for processing
// wiki pages of unicode text into structured pages
// with metadata for a particular Tibetan collection.
// __
// _(\ |@@|
// (__/\__ \--/ __
// \___|----| | __
// \ }{ /\ )_ / _\
// /\__/\ \__O (__
// (--/\--) \__/
// _)( )(_
// ---''---
// Define api functions to use throughout
function createPage(page, content) {
var createPageParams = {
action: 'edit',
title: page,
text: content,
format: 'json',
createonly: true
},
createPageApi = new mw.Api();
createPageApi.postWithToken( 'csrf', createPageParams ).done( function (data) {
$('#alerts').append('<div class="col-lg-6"><div class="alert alert-success mt-2 mb-0 py-1 px-2" role="alert">Created: <a href="/index.php/' + page + '" title="' + page + '">' + page + '</a>.</div></div>');
purgePage(page);
});
};
function createFinalPage(page, content) {
var createPageParams = {
action: 'edit',
title: page,
text: content,
format: 'json'
},
createPageApi = new mw.Api();
createPageApi.postWithToken( 'csrf', createPageParams ).done( function (data) {
$('#alerts').append('<div class="col-lg-6"><div class="alert alert-success mt-2 mb-0 py-1 px-2" role="alert">Created: <a href="/index.php/' + page + '" title="' + page + '">' + page + '</a>.</div></div>');
purgePage(page);
});
};
function purgePage(page) {
var purgeParams = {
action: 'purge',
titles: page,
forcelinkupdate: true,
format: 'json'
},
purgeApi = new mw.Api();
purgeApi.post( purgeParams ).done( function ( data ) {
console.log(page + ' purged.');
} );
};
// Get unicode content:
function getUnicode(pagename) {
var paramsPageParse = {
action: 'parse',
page: pagename,
prop: 'wikitext',
formatversion: 2
},
parsePageApi = new mw.Api();
parsePageApi.get(paramsPageParse).done( function (data) {
// Storing parsed unicode from existing text page into variable
var unicodeContent = data.parse.wikitext;
getTemplateContent(pagename, unicodeContent);
});
};
// Get template content
function getTemplateContent(pagename, unicodeContent){
var paramsTemplateParse = {
action: 'parse',
page: 'WB-Text-Stub',
prop: 'wikitext',
formatversion: 2
},
parseTemplateApi = new mw.Api();
parseTemplateApi.get(paramsTemplateParse).done( function (data) {
// Storing parsed template content into variable
var templateContent = data.parse.wikitext ;
buildPage(pagename, unicodeContent, templateContent);
});
};
// Build final page
function buildPage(pagename, unicodeContent, templateContent){
// Splitting pagename into individual chunks to be reused later:
// wiki ID, collection ID, volume number, and text number.
if ( namingScheme === 'noVolume' ) {
var pageNameChunk = pagename.match(/^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([0-9]+)$/),
wikiID = pageNameChunk[1],
colID = pageNameChunk[2],
textNum = pageNameChunk[3],
volumePageName = wikiID + "-" + colID
} else if ( namingScheme === 'volNumOnly' ) {
var pageNameChunk = pagename.match(/^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([0-9]+)-([a-zA-Z0-9]+)$/),
wikiID = pageNameChunk[1],
colID = pageNameChunk[2],
volNum = pageNameChunk[3],
textNum = pageNameChunk[4],
volumePageName = wikiID + "-" + colID + "-Volume-" + volNum
} else if ( namingScheme === 'volNumAndLet' ) {
var pageNameChunk = pagename.match(/^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([0-9]+)-([a-zA-Z]+)-([a-zA-Z0-9]+)$/),
wikiID = pageNameChunk[1],
colID = pageNameChunk[2],
volNum = pageNameChunk[3],
volumeLetter = pageNameChunk[4],
textNum = pageNameChunk[5],
volumePageName = wikiID + "-" + colID + "-Volume-" + volNum + "-" + volumeLetter
}
var subclass;
if ( textNum === 'Karchag') {
subclass = 'Karchags';
} else {
subclass = 'Tibetan Texts';
};
// Build final page content by replacing strings in
// template content with actual page values and adding
// the unicode to the body of the page.
var pageContent = templateContent
.replace( '%subclass%', subclass )
.replace( '%collectiontitle%', collectionName.value )
.replace( '%collectiontitletib%', collectionNameTib.value )
.replace( '%totalvolumes%', totalVolumes.value )
.replace( '%author%', author.value )
.replace( '%authortib%', authorTib.value )
.replace( '%citation%', citation.value )
.replace( '%volumenumber%', volNum )
.replace( '%volumeLetter%', volumeLetter )
.replace( '%textnuminvol%', textNum )
.replace( '%pagename%', pagename )
.replace( '%InitCategories%', projectName.value + "; " + volumePageName )
.replace( '%pagecontent%', unicodeContent )
.replace( '[[Cate' + 'gory:Wyckibaat]]' , '' );
// Create final page
createFinalPage(pagename, pageContent);
};
// Building input elements for processing existing basic unicode pages:
var projectName = new OO.ui.TextInputWidget( {
icon: 'tag',
placeholder: 'Project name',
value: 'Khyentse Lineage Project'
} ),
collectionName = new OO.ui.TextInputWidget( {
icon: 'tag',
placeholder: 'Collection name',
value: "dpal sprul bka' 'bum"
} ),
collectionNameTib = new OO.ui.TextInputWidget( {
icon: 'tag',
placeholder: 'Collection name in Tibetan',
value: 'དཔལ་སྤྲུལ་བཀའ་འབུམ།'
} ),
totalVolumes = new OO.ui.NumberInputWidget( {
placeholder: 'Total volumes in collection',
value: '6'
}),
author = new OO.ui.TextInputWidget( {
icon: 'tag',
placeholder: 'Author pagename',
value: 'Dpal sprul rin po che'
} ),
authorTib = new OO.ui.TextInputWidget( {
icon: 'tag',
placeholder: 'Author name in Tibetan',
value: 'དཔལ་སྤྲུལ་རིན་པོ་ཆེ་'
} ),
citation = new OO.ui.TextInputWidget( {
icon: 'tag',
placeholder: 'Full citation for collection',
value: ""
} ),
namingScheme = new OO.ui.RadioSelectWidget( {
items: [
new OO.ui.RadioOptionWidget( {
data: 'noVolume',
label: 'No volume specified. E.g. ' + bold('DKYD-DZINDROL-001.')
} ),
new OO.ui.RadioOptionWidget( {
data: 'volNumOnly',
label: 'Volume number only, no letter specified. E.g. LCP-KABUM-06-002.'
} ),
new OO.ui.RadioOptionWidget( {
data: 'volNumAndLet',
label: 'Volume number and letter specified. E.g. DJYD-KABUM-01-KA-001.',
} )
]
} ),
dryRunButton = new OO.ui.ButtonWidget( {
label: 'Dry Run',
flags: [
'primary',
'progressive'
]
} ),
processButton = new OO.ui.ButtonWidget( {
label: 'Process Pages',
flags: [
'primary',
'progressive'
]
} );
// Placing input elements on the page:
$( '#wyckibaat' ).append( [
projectName.$element,
collectionName.$element,
collectionNameTib.$element,
totalVolumes.$element,
author.$element,
authorTib.$element,
citation.$element,
'<div class="my-2">File naming scheme:</div><div class="pl-3">',
namingScheme.$element,
'</div><div class="mb-3"></div>',
dryRunButton.$element,
processButton.$element,
'<div id="alerts" class="row mt-3"></div>'
] );
// DRY RUN
// Click action for main process button:
dryRunButton.on('click', function dryRunPages() {
// Check for existence of project pages:
new mw.Api().get( {
action: "query",
titles: [
'Category:' + projectName.value,
collectionName.value,
'Category:' + collectionName.value,
author.value,
'Category:' + author.value
],
} ).then( function( data ) {
$.each( data.query.pages, function() {
if ( this.missing !== "" ) {} else {
$('#alerts').append('<div class="col-lg-6"><div class="alert alert-info mt-2 mb-0 py-1 px-2" role="alert">Will create: <a class="new" href="/index.php/' + this.title + '" title="' + this.title + '">' + this.title + '</a>.</div></div>');
}
} );
}, function( error ) {
console.log('Error testing for page existence.')
});
// List pages to process
var queryUrl = 'https://' + mw.config.get( 'wgServerName' ) + '/api.php?action=query&list=categorymembers&cmtitle=Category:Wyckibaat&cmlimit=max&format=json'
var jsonData = queryUrl;
$.getJSON(jsonData, function(data){
$.each(data.query.categorymembers, function(work, field){
// console.log(field.title);
$('#alerts').append('<div class="col-lg-6"><div class="alert alert-info mt-2 mb-0 py-1 px-2" role="alert">Will process: <a href="/index.php/' + field.title + '" title="' + field.title + '">' + field.title + '</a> and <a href="/index.php/Category:' + field.title + '" title="Category:' + field.title + '">Category:' + field.title + '</a>.</div></div>');
});
});
});
// Click action for main process button:
processButton.on('click', function processPages() {
// Creation of project's main category page
var projectCatPageName = 'Category:' + projectName.value,
projectCatPageContent = "This the project's main category page.";
createPage(projectCatPageName, projectCatPageContent);
// Creation of collection page and collection category page
var collectionPageName = collectionName.value,
collectionPageContent = '{{Collection\n|author=' + author.value + '\n|title=' + collectionName.value + '\n}}';
createPage(collectionPageName, collectionPageContent);
var collectionCatPageName = 'Category:' + collectionName.value,
collectionCatPageContent = 'This is the category for the collection: {{PAGENAME}}';
createPage(collectionCatPageName, collectionCatPageContent);
// Creation of author page and author category page
var authorPageName = author.value,
authorPageContent = '{{PersonCall}}';
createPage(authorPageName, authorPageContent);
var authorCatPageName = 'Category:' + author.value,
authorCatPageContent = 'This is the category page for {{PAGENAME}}';
createPage(authorCatPageName, authorCatPageContent);
// Fetching pages using SMW's API's "ask" action, with result formatted in JSON:
var queryUrl = 'https://' + mw.config.get( 'wgServerName' ) + '/api.php?action=query&list=categorymembers&cmtitle=Category:Wyckibaat&cmlimit=max&format=json'
var jsonData = queryUrl;
// Extracting JSON data from file and parsing:
$.getJSON(jsonData, function(data){
// Defining what to do for each page returned by the query.
$.each(data.query.categorymembers, function(pagename, field){
// Splitting pagename into individual chunks to be reused later:
// wiki ID, collection ID, volume number, and text number.
var pagename = field.title
// Create volume pages
if ( namingScheme === 'noVolume' ) {
var pageNameChunk = pagename.match(/^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([0-9]+)$/),
wikiID = pageNameChunk[1],
colID = pageNameChunk[2],
volumePageName = wikiID + "-" + colID
} else if ( namingScheme === 'volNumOnly' ) {
var pageNameChunk = pagename.match(/^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([0-9]+)-([a-zA-Z0-9]+)$/),
wikiID = pageNameChunk[1],
colID = pageNameChunk[2],
volNum = pageNameChunk[3],
volumePageName = wikiID + "-" + colID + "-Volume-" + volNum
} else if ( namingScheme === 'volNumAndLet' ) {
var pageNameChunk = pagename.match(/^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([0-9]+)-([a-zA-Z]+)-([a-zA-Z0-9]+)$/),
wikiID = pageNameChunk[1],
colID = pageNameChunk[2],
volNum = pageNameChunk[3],
volumeLetter = pageNameChunk[4],
volumePageName = wikiID + "-" + colID + "-Volume-" + volNum + "-" + volumeLetter
}
var volumeStub = '{{VolumePage\n|collectiontitle=' + collectionName.value + '\n|collectiontitletib=' + collectionNameTib.value + '\n|volumenumber=' + volNum + '\n}}';
createPage(volumePageName, volumeStub);
var volumeCatPage = 'Category:' + volumePageName,
volumeCatPageContent = '{{VolumeCategoryPage}}';
createPage(volumeCatPage, volumeCatPageContent);
// Create category page
var catPageName = 'Category:' + field.title,
catPageContent = '{{TextCategoryPage}}';
createPage(catPageName, catPageContent);
// Build and process text page
getUnicode(field.title);
});
});
});
};