MediaWiki:Gadget-markreview.js: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
 
(6 intermediate revisions by the same user not shown)
Line 3: Line 3:


     function run($content) {
     function run($content) {
        // Find the DocMeta table
         var $docMeta = $content.find('.doc-meta').first();
         var $docMeta = $content.find('.doc-meta').first();
         if (!$docMeta.length) {
         if (!$docMeta.length) {
Line 13: Line 14:
         var action = mw.config.get('wgAction') || 'view';
         var action = mw.config.get('wgAction') || 'view';


        // Only run on view action and if current user is the owner
         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 18: Line 20:
         function doReview() {
         function doReview() {
             var page = mw.config.get('wgPageName');
             var page = mw.config.get('wgPageName');
             var now = new Date();
             var stamp = new Date().toISOString();
            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();
             var api = new mw.Api();


            // Get current page content
             api.postWithToken('csrf', {
             api.get({
                 action: 'edit',
                action: 'query',
                 title: page,
                 prop: 'revisions',
                 summary: 'Mark as reviewed',
                 titles: page,
                 appendtext: '\n<!-- review-ping: ' + stamp + ' -->',
                 rvslots: 'main',
                minor: true,
                 rvprop: 'content',
                nocreate: true
                formatversion: 2
            }).done(function () {
            }).done(function (data) {
                location.reload();
                var content = data.query.pages[0].revisions[0].slots.main.content;
            }).fail(function (err) {
                var newContent;
                mw.notify('Review failed: ' + (err && err.error && err.error.info ? err.error.info : err), { type: 'error' });
 
                // 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 80: Line 55:
             });
             });


         // Place it under the DocMeta table
         // Place it in .doc-meta-actions if present, otherwise create it after the 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 88: Line 63:
     }
     }


    // Run on initial load and after AJAX loads
     mw.hook('wikipage.content').add(run);
     mw.hook('wikipage.content').add(run);
});
});