MediaWiki:Gadget-markreview.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 3: | Line 3: | ||
function run($content) { | function run($content) { | ||
var $docMeta = $content.find('.doc-meta').first(); | var $docMeta = $content.find('.doc-meta').first(); | ||
if (!$docMeta.length) { | if (!$docMeta.length) { | ||
| Line 14: | Line 13: | ||
var action = mw.config.get('wgAction') || 'view'; | var action = mw.config.get('wgAction') || 'view'; | ||
if (action !== 'view') return; | if (action !== 'view') return; | ||
if (!owner || !user || owner.toLowerCase() !== user.toLowerCase()) return; | if (!owner || !user || owner.toLowerCase() !== user.toLowerCase()) return; | ||
| Line 30: | Line 28: | ||
var api = new mw.Api(); | var api = new mw.Api(); | ||
api. | // Get current page content | ||
api.get({ | |||
action: 'query', | |||
prop: 'revisions', | |||
titles: page, | |||
rvslots: 'main', | |||
rvprop: 'content', | |||
formatversion: 2 | |||
}).done(function (data) { | |||
var content = data.query.pages[0].revisions[0].slots.main.content; | |||
var newContent; | |||
// Replace review_ping parameter if it exists, otherwise add it | |||
if (content.match(/(\|\s*review_ping\s*=\s*).*$/m)) { | |||
newContent = content.replace(/(\|\s*review_ping\s*=\s*).*$/m, '$1' + stamp); | |||
} else { | |||
newContent = content.replace(/(\{\{DocMeta[^\n]*)/, '$1\n | review_ping = ' + stamp); | |||
} | |||
api.postWithToken('csrf', { | |||
action: 'edit', | |||
title: page, | |||
text: newContent, | |||
summary: 'Mark as reviewed', | |||
minor: true, | |||
nocreate: true | |||
}).done(function () { | |||
location.reload(); | |||
}).fail(function (err) { | |||
mw.notify('Review failed: ' + (err && err.error && err.error.info ? err.error.info : err), { type: 'error' }); | |||
}); | |||
}); | }); | ||
} | } | ||
| Line 62: | Line 80: | ||
}); | }); | ||
// Place it | // Place it under the DocMeta table | ||
var $actions = $docMeta.closest('table').nextAll('.doc-meta-actions').first(); | var $actions = $docMeta.closest('table').nextAll('.doc-meta-actions').first(); | ||
if (!$actions.length) { | if (!$actions.length) { | ||
| Line 70: | Line 88: | ||
} | } | ||
mw.hook('wikipage.content').add(run); | mw.hook('wikipage.content').add(run); | ||
}); | }); | ||
Revision as of 08:53, 9 September 2025
/* Gadget: MarkReview — One‑click "Mark as reviewed" for page owners */
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'mediawiki.user']).then(function () {
function run($content) {
var $docMeta = $content.find('.doc-meta').first();
if (!$docMeta.length) {
$docMeta = $('.doc-meta').first();
if (!$docMeta.length) return;
}
var owner = $docMeta.attr('data-owner');
var user = mw.config.get('wgUserName');
var action = mw.config.get('wgAction') || 'view';
if (action !== 'view') return;
if (!owner || !user || owner.toLowerCase() !== user.toLowerCase()) return;
function doReview() {
var page = mw.config.get('wgPageName');
var now = new Date();
var stamp = now.getFullYear() + '-' +
String(now.getMonth() + 1).padStart(2, '0') + '-' +
String(now.getDate()).padStart(2, '0') + 'T' +
String(now.getHours()).padStart(2, '0') + ':' +
String(now.getMinutes()).padStart(2, '0') + ':' +
String(now.getSeconds()).padStart(2, '0');
var api = new mw.Api();
// Get current page content
api.get({
action: 'query',
prop: 'revisions',
titles: page,
rvslots: 'main',
rvprop: 'content',
formatversion: 2
}).done(function (data) {
var content = data.query.pages[0].revisions[0].slots.main.content;
var newContent;
// Replace review_ping parameter if it exists, otherwise add it
if (content.match(/(\|\s*review_ping\s*=\s*).*$/m)) {
newContent = content.replace(/(\|\s*review_ping\s*=\s*).*$/m, '$1' + stamp);
} else {
newContent = content.replace(/(\{\{DocMeta[^\n]*)/, '$1\n | review_ping = ' + stamp);
}
api.postWithToken('csrf', {
action: 'edit',
title: page,
text: newContent,
summary: 'Mark as reviewed',
minor: true,
nocreate: true
}).done(function () {
location.reload();
}).fail(function (err) {
mw.notify('Review failed: ' + (err && err.error && err.error.info ? err.error.info : err), { type: 'error' });
});
});
}
// Create the button
var $btn = $('<a>')
.attr('href', '#')
.text('Mark as reviewed')
.css({
display: 'inline-block',
marginTop: '8px',
padding: '4px 8px',
background: '#f0f0f0',
border: '1px solid #ccc',
borderRadius: '2px',
textDecoration: 'none'
})
.on('click', function (e) {
e.preventDefault();
doReview();
});
// Place it under the DocMeta table
var $actions = $docMeta.closest('table').nextAll('.doc-meta-actions').first();
if (!$actions.length) {
$actions = $('<div class="doc-meta-actions"></div>').insertAfter($docMeta.closest('table'));
}
$actions.empty().append($btn);
}
mw.hook('wikipage.content').add(run);
});