Topic on Project:Support desk

API: Parse json result titles to links?

7
Subfader (talkcontribs)

I receive titles using $.each but how can I pass them through the parser as links '[['+ item.title +']]'?

{
    "query-continue": {
        "recentchanges": {
            "rccontinue": "20141204064939|1459491"
        }
    },
    "warnings": {
        "query": {
            "*": "Formatting of continuation data will be changing soon. To continue using the current formatting, use the 'rawcontinue' parameter. To begin using the new format, pass an empty string for 'continue' in the initial query."
        }
    },
    "query": {
        "recentchanges": [
            {
                "type": "new",
                "ns": 0,
                "title": "Wikibase/Indexing/Data Model",
                "timestamp": "2014-12-04T22:28:21Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "HHVM/About/ar",
                "timestamp": "2014-12-04T21:59:09Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "MediaWiki Stakeholders' Group/Tasks/Feature wishlist/NAME OF STUDENT",
                "timestamp": "2014-12-04T21:28:58Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "Iwlinks/pl",
                "timestamp": "2014-12-04T20:30:39Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "RecentChanges.php/pl",
                "timestamp": "2014-12-04T20:26:01Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "Random page/pl",
                "timestamp": "2014-12-04T18:36:37Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "Redirects/pl",
                "timestamp": "2014-12-04T18:36:07Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "Martin luther",
                "timestamp": "2014-12-04T13:44:14Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "Design/Living style guide/Requirements",
                "timestamp": "2014-12-04T11:24:19Z"
            },
            {
                "type": "new",
                "ns": 0,
                "title": "Analytics/Server Admin Log/Archive/2014",
                "timestamp": "2014-12-04T08:17:09Z"
            }
        ]
    }
}
120.144.185.33 (talkcontribs)

Since the API should be returning well formed titles, you can just use mw.util.getUrl() to turn a title into a URL relative to the current page.

mw.loader.using( 'mediawiki.util', function() {
	var url = mw.util.getUrl( 'Wikibase/Indexing/Data Model' ); // Gives '/wiki/Wikibase/Indexing/Data_Model'
	
	// Then you could use the URL in a link
	var $link = $( '<a>' ).attr( 'href', url ).text( 'Wikibase/Indexing/Data Model' );
	console.log( $link );
} );


If you really need the exact link HTML used by MediaWiki links, you could construct the links in wikitext format ([[link]]), then send that wikitext to the parse API, then read the returned HTML ($.parseHTML()) and extract the links.

93.184.128.25 (talkcontribs)

Hi, yes I need the exact Parser output since I have an extension adding classes to the link.

I can't believe there is no easier way than passing the json result again through the API + through a jQuery extension :/

Isn't the API designed to be used via jQuery on the server itself?

Subfader (talkcontribs)

Ok, I'm only parsing the end result, not inside the each loop. Thanks!

Subfader (talkcontribs)

This leads to the next problem:

Request-URI Too Large
110.148.123.212 (talkcontribs)

If you want to parse wikitext, then you use the parse API. What could it do to make things easier? If you're having to run wikitext through the parser, then extract HTML from that, you're probably doing something wrong. I don't see why anything in the API would be specifically designed for use with jQuery. The API's design isn't even optimal for JSON format, rather being designed around XML format.

Use a POST request.

Subfader (talkcontribs)

You're right, I'm asking too much from the API. Using POST now. Thanks!

Reply to "API: Parse json result titles to links?"