MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 10: Line 10:
     });
     });


    // ---- Utility: robust API URL resolver ----
     function getApiUrl() {
     function getApiUrl() {
         // Try wgScript first (often '/log/index.php')
         // With wgScriptPath = "", api.php is at root
        var script = mw.config.get('wgScript') || ''; // e.g. '/log/index.php'
         return '/api.php';
         if (script) {
    }
            return script.replace(/\/index\.php$/, '') + '/api.php';
        }


        // Fallback: derive from wgArticlePath (often '/log/$1')
    // Resolve a title to a pageid (or null if missing)
         var articlePath = mw.config.get('wgArticlePath') || '';
    function resolveTitleToPageId(title, cb) {
         if (articlePath) {
         $.getJSON(getApiUrl(), {
             return articlePath.replace(/\/\$1.*/, '') + '/api.php';
            action: 'query',
        }
            prop: 'info',
 
            titles: title,
        // Last resort: infer from current URL path (handles being under /log/)
            format: 'json',
        var path = window.location.pathname;         // e.g. '/log/Main'
            formatversion: 2
         var m = path.match(/^\/[^/]+/);              // '/log'
         }).done(function (res) {
        var base = m ? m[0] : '';
            if (!res || !res.query || !res.query.pages || !res.query.pages.length) {
         return base + '/api.php';
             console.warn('[print] query returned no pages for title:', title, res);
            cb(null);
            return;
            }
            var page = res.query.pages[0];
            if (page.missing) {
            console.warn('[print] page is missing for title:', title);
            cb(null);
            return;
            }
            cb(page.pageid);
         }).fail(function (xhr) {
            console.warn('[print] query fail:', xhr && xhr.status, xhr && xhr.statusText);
            cb(null);
         });
     }
     }


     // Loop through each card to format related articles
     // Loop through each card to format related articles
Line 1,070: Line 1,080:
     // Print current entry only
     // Print current entry only
     $('#print-button').on('click', function () {
     $('#print-button').on('click', function () {
         var pageTitle = window.currentEntryTitle;
         var title = window.currentEntryTitle;
         if (!pageTitle) { console.warn('[print] no currentEntryTitle'); window.print(); return; }
         if (!title) { console.warn('[print] no currentEntryTitle'); window.print(); return; }
 
        console.log('[print] resolving title:', title);


         var apiUrl = getApiUrl();
         resolveTitleToPageId(title, function (pageid) {
        console.log('[print] fetching:', pageTitle, 'via', apiUrl);
            if (!pageid) {
            console.warn('[print] could not resolve title to pageid; fallback print.');
            window.print();
            return;
            }


        $.getJSON(apiUrl, {
            var apiUrl = getApiUrl();
            console.log('[print] parsing pageid:', pageid, 'via', apiUrl);
 
            $.getJSON(apiUrl, {
             action: 'parse',
             action: 'parse',
             page: pageTitle,       // e.g. "090"
             pageid: pageid,         // use pageid, not 'page'
             prop: 'text',
             prop: 'text',
             format: 'json',
             format: 'json',
             formatversion: 2
             formatversion: 2
        }).done(function (res) {
            }).done(function (res) {
            if (res && res.error) {
            console.warn('[print] API error:', res.error);
            }
            if (res && res.warnings) {
            console.warn('[print] API warnings:', res.warnings);
            }
             if (!res || !res.parse || !res.parse.text) {
             if (!res || !res.parse || !res.parse.text) {
            console.warn('[print] parse API returned no text for:', pageTitle);
                console.warn('[print] parse returned no text for pageid:', pageid, res);
            window.print();
                window.print();
            return;
                return;
             }
             }
             var html = res.parse.text;
             var html = res.parse.text;
             var $tmp = $('<div>').html(html);
             var $tmp = $('<div>').html(html);
Line 1,114: Line 1,126:
             var doc = iframe.contentDocument || iframe.contentWindow.document;
             var doc = iframe.contentDocument || iframe.contentWindow.document;


             // Raw Print.css URL (works regardless of script path)
             // Raw Print.css (works with wgScriptPath = "")
             var printCssUrl = (mw.config.get('wgScriptPath') || getApiUrl().replace(/\/api\.php$/, '')) +
             var printCssUrl = '/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css';
            '/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css';


             doc.open();
             doc.open();
             doc.write(
             doc.write(
            '<!doctype html><html><head><meta charset="utf-8">' +
                '<!doctype html><html><head><meta charset="utf-8">' +
            '<title>Print</title>' +
                '<title>Print</title>' +
            '<link rel="stylesheet" href="' + printCssUrl + '">' +
                '<link rel="stylesheet" href="' + printCssUrl + '">' +
            '</head><body>' + $print.prop('outerHTML') + '</body></html>'
                '</head><body>' + $print.prop('outerHTML') + '</body></html>'
             );
             );
             doc.close();
             doc.close();


             iframe.onload = function () {
             iframe.onload = function () {
            try { iframe.contentWindow.focus(); iframe.contentWindow.print(); }
                try { iframe.contentWindow.focus(); iframe.contentWindow.print(); }
            finally { setTimeout(function(){ document.body.removeChild(iframe); }, 500); }
                finally { setTimeout(function () { document.body.removeChild(iframe); }, 500); }
             };
             };
        }).fail(function (xhr) {
            }).fail(function (xhr) {
             console.warn('[print] API fail:', xhr && xhr.status, xhr && xhr.statusText);
             console.warn('[print] parse fail:', xhr && xhr.status, xhr && xhr.statusText);
             window.print();
             window.print();
            });
         });
         });
     });
     });




Navigation menu