User:Evad37/Covery.js: Difference between revisions

Content deleted Content added
.
.
Line 1:
// <nowiki>
function loadDepenedencies() {
return mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.RegExp', 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']);
Line 151 ⟶ 152:
* @param {File} file
* @param {String} text wikitext for the file description page
* @param {StringObject} title mw.Title object
* @returns {Promise} Promise of result object, or an error code and a jqxhr object
*/
var uploadFile = function uploadFile(api, file, text, title) {
var filename = title.getMain();
return api.postWithToken('csrf',
{
"action": "upload",
"format": "json",
"filename": titlefilename,
"comment": "Upload cover image (using [[User:Evad37/Covery|Covery]])",
"text": text,
Line 177 ⟶ 179:
*/
);
};
 
var createFileTalkpage = function (api, fileTitle) {
return api.postWithToken('csrf', {
action: 'edit',
format: 'json',
title: fileTitle.getTalkPage().toString(),
text: '{{WikiProject Video games}}',
summary: 'WikiProject tagging (using [[User:Evad37/Covery|Covery]])',
createonly: true
});
};
 
Line 244 ⟶ 257:
title: pageTitle,
text: wikitext,
summary: "AddAdded cover image (using [[User:Evad37/Covery|Covery]])",
basetimestamp: timestamp,
nocreate: true
Line 250 ⟶ 263:
};
 
var updatePage = function updatePage(api, pageTitlepage, infobox, filenamefileTitle, caption, alt) {
var filename = fileTitle.getMainText();
return getRevisionWikitext(api, pageTitle)
return getRevisionWikitext(api, page)
.then(function (revision) {
return $.when(
Line 259 ⟶ 273:
})
.then(function (updatedWikitext, timestamp) {
return editPage(api, pageTitlepage, updatedWikitext, timestamp);
});
};
var updateTalkpageWikitext = function updateTalkpageWikitext(revisionWikitext) {
/* Redirects to {{WikiProject Video games}} :
// Template:Cvgproj (redirect page) ‎ (links | edit)
// Template:WikiProject Video Games (redirect page) ‎ (links | edit)
// Template:WPVG (redirect page) ‎ (links | edit)
// Template:Vgproj (redirect page) ‎ (links | edit)
// Template:Wpvg (redirect page) ‎ (links | edit)
// Template:WP video games (redirect page) ‎ (links | edit)
// Template:WP cvg (redirect page) ‎ (links | edit)
// Template:WikiProject Rockstar Games (redirect page) ‎ (links | edit)
// Template:WGVG (redirect page) ‎ (links | edit)
// Template:WP Video games (redirect page) ‎ (links | edit)
// Template:WikiProject VG (redirect page) ‎ (links | edit)
// Template:WikiProject video games (redirect page)
*/
var bannerPattern = /\{\{\s*([Ww](?:P|p|G|ikiProject) ?c?[Vv](?:ideo )?[Gg](?:ames)?|[Cc]?[Vv]gproj|[Ww]ikiProject Rockstar Games)\s*(\|(?:.|\n)*?(?:(?:\{\{(?:.|\n)*?(?:(?:\{\{(?:.|\n)*?\}\})(?:.|\n)*?)*?\}\})(?:.|\n)*?)*|)\}\}\n?/;
var banner = bannerPattern.exec(revisionWikitext);
var noBannerPrersent = !banner || !banner[0];
if (noBannerPrersent) {
return '{{WikiProject Video games}}\n' + revisionWikitext;
}
var noParamsInBanner = !banner[2];
if (noParamsInBanner) {
return false;
}
var params = getTemplateParameters(banner[2]);
var coverParam = getTemplateParameters(banner[2]).find(paramByName('cover'));
if (!coverParam) {
return false;
}
var updatedBannerWikitext = banner[0].replace(coverParam.wikitext.name + coverParam.wikitext.value, '');
return revisionWikitext.replace(banner[0], updatedBannerWikitext);
}
 
var updateTalkpage = function updateTalkpage(api, page) {
var talkpageTitle = mw.Title.newFromText(page).getTalkPage();
var talkpage = talkpageTitle && talkpageTitle.toString();
return getRevisionWikitext(api, talkpage)
.then(function (revision) {
return $.when(
updateTalkpageWikitext(revision.wikitext),
revision.timestamp
);
})
.then(function (updatedWikitext, timestamp) {
if (!updatedWikitext) {
return 'Done';
}
return editPage(api, talkpage, updatedWikitext, timestamp);
})
};
 
 
 
 
/* ---------- Dialog ---------- */
// Make a subclass of ProcessDialog
var CoveryDialog = function CoveryDialog(config) {
CoveryDialog.super.call(this, config);
Line 274 ⟶ 334:
OO.inheritClass(CoveryDialog, OO.ui.ProcessDialog);
 
// Specify a name for .addWindows()
CoveryDialog.static.name = 'coveryDialog';
// Specify the CoveryDialog.static configurations: .title and action= set'Covery';
CoveryDialog.static.actions = [
{ flags: 'primary', label: 'Upload', action: 'upload' },
Line 287 ⟶ 346:
 
this.panel = new OO.ui.PanelLayout({ padded: true, expanded: false });
 
/* Form content: */
this.content = new OO.ui.FieldsetLayout();
 
Line 301 ⟶ 362:
this.publisherInput = new OO.ui.TextInputWidget();
 
this.fileSelectField = new OO.ui.FieldLayout(this.fileSelect, { label: 'Upload a file...', align: 'left' });
this.fileSelect.field = this.fileSelectField;
this.urlInputField = new OO.ui.FieldLayout(this.urlInput, { label: '...or enter a URL', align: 'left' });
this.urlInput.field = this.urlInputField;
this.imagePreviewField = new OO.ui.FieldLayout(this.imagePreview, { label: 'Preview:', align: 'top' });
Line 322 ⟶ 383:
this.publisherInputField
]);
this.panel.$element.append(this.content.$element);
this.$body.append(this.panel.$element);
 
/* Progress status content: */
this.progressStatusContent = new OO.ui.FieldsetLayout({ label: 'Status' });
this.progressBar = new OO.ui.ProgressBarWidget({
progress: 0
});
this.progressField = new OO.ui.FieldLayout(this.progressBar, { label: '', align: 'below' });
this.progressStatusContent.addItems([this.progressField]);
this.progressStatusContent.toggle(false); //hide
 
this.panel.$element.append([
this.content.$element,
this.progressStatusContent.$element
]);
this.$body.append(this.panel.$element);
 
this.fileSelect.connect(this, { 'change': 'onFileSelectChange' });
Line 459 ⟶ 532:
}, this);
};
 
CoveryDialog.prototype.setProgressStatus = function (label, progress) {
this.progressBar.setProgress(progress);
this.progressField.setLabel(label);
}
 
// Specify processes to handle the actions.
CoveryDialog.prototype.getActionProcess = function (action) {
if (action === 'upload') {
this.content.toggle(false); // hide
var filename = mw.Title.newFromFileName(this.titleInput.getValue());
this.progressStatusContent.toggle(true); // show
this.setProgressStatus('Uploading...', 1);
 
var fileTitle = mw.Title.newFromFileName(this.titleInput.getValue());
return new OO.ui.Process(function () {
return this.uploaded || uploadFile(
Line 473 ⟶ 555:
this.publisherInput.getValue()
),
filenamefileTitle
).then(
function () { return true; },
function (errorCode) { return $.Deferred().reject(new OO.ui.Error('UploadError erroruploading: ' + errorCode)) }
);
}, this)
.next(function () {
this.uploaded = true;
this.setProgressStatus('Uploaded file!', 25);
}, this)
.next(function () {
this.setProgressStatus('Uploaded file! Creating file talk page...', 26);
return updatePage(
return this.createdFileTalkpage || createFileTalkpage(
this.api,
fileTitle
).then(
function () { return true; },
function (errorCode) { return $.Deferred().reject(new OO.ui.Error('Error creating file talk page: ' + errorCode)) }
);
}, this)
.next(function () {
this.createdFileTalkpage = true;
this.setProgressStatus('Uploaded file! Created file talk page!', 50);
}, this)
.next(function () {
this.setProgressStatus('Uploaded file! Created file talk page! Updating article...', 51);
return this.updatedArticle || updatePage(
this.api,
this.pageName,
this.infobox,
filenamefileTitle,
this.captionInput.getValue(),
this.altTextInput.getValue()
).then(
function () { return true; },
function (errorCode) { return $.Deferred().reject(new OO.ui.Error('EditError errorediting article: ' + errorCode)) }
);
}, this)
.next(function () {
this.updatedArticle = true;
this.setProgressStatus('Uploaded file! Created file talk page! Updated article!', 75);
}, this)
.next(function () {
this.setProgressStatus('Uploaded file! Created file talk page! Updated article! Updating article talk page...', 76);
return this.createdFileTalkpage || updateTalkpage(
this.api,
fileTitle
).then(
function () { return true; },
function (errorCode) { return $.Deferred().reject(new OO.ui.Error('Error editing article talk page: ' + errorCode)) }
);
}, this)
.next(function () {
this.setProgressStatus('All done! Reloading article...', 100);
return 1200;
}, this)
.next(function () {
Line 537 ⟶ 653:
})
.then(function (data) {
if (data && data.sucess) {
___location.reload();
}
Line 587 ⟶ 703:
var portletLink = mw.util.addPortletLink('p-tb', '#', 'Test covery', 'tb-testcovery');
$(portletLink).click(function (e) {
e.preventDefault();
startCovery(new FakeApi, mw.config.get('wgPageName'));
});
Line 610 ⟶ 727:
})
}
// </nowiki>