API:Continue

MediaWiki version:
1.26

Very often you will not get all the data you want in one API query. When this happens, the API will append an additional element (titled continue) to the results to indicate there is more data.

The element that is returned will have two parts to it: One part will be a sub-element also titled continue. This will typically have an associated value that include the symbols || if there is data available to continue. The second part will be a sub-element (or multiple sub-elements) whose title is the abbreviation of whichever list is used in the query, prefixed to the word continue. (So, for example, the sub-element for a query using categorymembers would be cmcontinue, the sub-element for a query using allcategories would be accontinue, the sub-element for a query using protectedtitles would be ptcontinue, etc.)

To retrieve further data when the continue element is present, you must add the second sub-element as a parameter to the next API request. For example, if a query using the list allcategories contained additional results, then the next API query made should include accontinue= as parameter, along with whatever the value of accontinue was in the results of the first query. To get all results, this can be repeated until an API result does not have a continue element in the results, indicating there are no more data matching the query.

batchcomplete

edit

When you make an API request using a generator together with properties, the API result may signal to continue because there are more properties to retrieve for the pages so far (in which case the same set of pages is returned but with the next set of properties), because there are more pages from the generator, or both. From version 1.25 onwards, the API returns a batchcomplete element to indicate that all data for the current "batch" of pages has been returned and the continue element doesn't contain continuation data for properties, but instead can contain continuation data for the generator. This can be useful to avoid building a combined result set for thousands of pages when using a generator together with prop modules that may themselves need continuation.

Backwards compatibility of continue

edit
MediaWiki version:
1.9

From MediaWiki 1.21 to 1.25, it was required to specify continue= (i.e. with an empty string as the value) in the initial request to get continuation data in the format described above. Without doing that, API results would indicate there is additional data by returning a query-continue element, explained in Raw query continue . Prior to 1.21, that raw continuation was the only option.

If your application needs to use the raw continuation in MediaWiki 1.26 or later, you must specify rawcontinue= to request it.

Examples

edit

Example 1: Continuing allcategories

edit
The following example uses the allcategories list to request results that exceed the default limit , and thus need to be continued to return more results. Notice that because the allcategories list is used, the continue's second sub-element is titled accontinue.
Result
    "continue": {
        "accontinue": "List_of_19th_century_baseball_players",
        "continue": "-||"
    },
    "batchcomplete": "",
    "query": {
        "allcategories": [
            {
                "*": "List of"
            },
            {
                "*": "List of Palestinians"
            },
            {
                "*": "List of \"M\" series military vehicles"
            },
            {
                "*": "List of ''The Fast and the Furious'' characters"
            },
            {
                "*": "List of 100 Deeds for Eddie McDowd"
            },
            {
                "*": "List of 1919 Actors"
            },
            {
                "*": "List of 1972 births"
            },
            {
                "*": "List of 1999 ballet premieres"
            },
            {
                "*": "List of 19th-century Russian artists"
            },
            {
                "*": "List of 19th century Russian artists"
            }
        ]
    }
}


Example 2: Continuing categorymembers

edit
The following example uses the categorymembers list to request results that exceed the limit (cmlimit), and thus need to be continued to return more results. Notice that because the categorymembers list is used, the continue's second sub-element is titled cmcontinue.
Result
{
    "batchcomplete": true,
    "continue": {
        "cmcontinue": "page|2a2a4c4e2a402a443e382a403a30011201dc11|21583092",
        "continue": "-||"
    },
    "query": {
        "categorymembers": [
            {
                "pageid": 3801230,
                "ns": 10,
                "title": "Template:User history"
            },
            {
                "pageid": 7769732,
                "ns": 10,
                "title": "Template:User history2"
            },
            {
                "pageid": 56312624,
                "ns": 10,
                "title": "Template:User history3"
            },
            {
                "pageid": 8364349,
                "ns": 2,
                "title": "User:-1348-"
            },
            {
                "pageid": 12553892,
                "ns": 2,
                "title": "User:(RT)"
            },
            {
                "pageid": 60272884,
                "ns": 2,
                "title": "User:MPSHeritage"
            },
            {
                "pageid": 42055428,
                "ns": 2,
                "title": "User:Tyssil"
            },
            {
                "pageid": 10356432,
                "ns": 2,
                "title": "User:1GoodNight4Life"
            },
            {
                "pageid": 59180278,
                "ns": 2,
                "title": "User:1I0I1I0I1I0"
            },
            {
                "pageid": 51651783,
                "ns": 2,
                "title": "User:01iproject09"
            },
            {
                "pageid": 29595961,
                "ns": 2,
                "title": "User:3family6"
            },
            {
                "pageid": 43181830,
                "ns": 2,
                "title": "User:3family6/Education and interests"
            },
            {
                "pageid": 17919048,
                "ns": 2,
                "title": "User:03md"
            },
            {
                "pageid": 44202586,
                "ns": 2,
                "title": "User:4Sage Wiki"
            },
            {
                "pageid": 64607473,
                "ns": 2,
                "title": "User:05Pier"
            },
            {
                "pageid": 46626298,
                "ns": 2,
                "title": "User:16pedia2"
            },
            {
                "pageid": 33371141,
                "ns": 2,
                "title": "User:17adavis7"
            },
            {
                "pageid": 15797963,
                "ns": 2,
                "title": "User:21stCenturyGreenstuff"
            },
            {
                "pageid": 61571792,
                "ns": 2,
                "title": "User:28regiment"
            },
            {
                "pageid": 17053340,
                "ns": 2,
                "title": "User:37ophiuchi"
            },
            {
                "pageid": 9781448,
                "ns": 2,
                "title": "User:88wolfmaster"
            },
            {
                "pageid": 17120424,
                "ns": 2,
                "title": "User:88wolfmaster/Userboxes"
            },
            {
                "pageid": 16369371,
                "ns": 2,
                "title": "User:123w456t"
            },
            {
                "pageid": 69413683,
                "ns": 2,
                "title": "User:296cherry"
            },
            {
                "pageid": 41449363,
                "ns": 2,
                "title": "User:636Buster"
            },
            {
                "pageid": 8451054,
                "ns": 2,
                "title": "User:1549bcp"
            },
            {
                "pageid": 26153484,
                "ns": 2,
                "title": "User:1951bfrj"
            },
            {
                "pageid": 28820985,
                "ns": 2,
                "title": "User:11614soup"
            },
            {
                "pageid": 69602489,
                "ns": 2,
                "title": "User:16272js"
            },
            {
                "pageid": 9578722,
                "ns": 2,
                "title": "User:A Geographer"
            },
            {
                "pageid": 36155511,
                "ns": 2,
                "title": "User:A knyght ther was"
            },
            {
                "pageid": 6637293,
                "ns": 2,
                "title": "User:A student of history"
            },
            {
                "pageid": 22308356,
                "ns": 2,
                "title": "User:A Werewolf"
            },
            {
                "pageid": 39893010,
                "ns": 2,
                "title": "User:A. Pseudonym"
            },
            {
                "pageid": 36267514,
                "ns": 2,
                "title": "User:A.Aboumrad"
            },
            {
                "pageid": 8475873,
                "ns": 2,
                "title": "User:A.Arc"
            },
            {
                "pageid": 5283037,
                "ns": 2,
                "title": "User:A.J.Chesswas"
            },
            {
                "pageid": 26516505,
                "ns": 2,
                "title": "User:A.kamburov"
            },
            {
                "pageid": 40306753,
                "ns": 2,
                "title": "User:A.sky245"
            },
            {
                "pageid": 6320018,
                "ns": 2,
                "title": "User:A.Zhubrovsky"
            },
            {
                "pageid": 68777172,
                "ns": 2,
                "title": "User:A2Bros"
            },
            {
                "pageid": 26764821,
                "ns": 2,
                "title": "User:A2soup"
            },
            {
                "pageid": 68203375,
                "ns": 2,
                "title": "User:A0231050705"
            },
            {
                "pageid": 26356724,
                "ns": 2,
                "title": "User:Aaa8841"
            },
            {
                "pageid": 61127272,
                "ns": 2,
                "title": "User:Aaghi"
            },
            {
                "pageid": 5410981,
                "ns": 2,
                "title": "User:Aak044"
            },
            {
                "pageid": 23936323,
                "ns": 2,
                "title": "User:Aardvarkzz"
            },
            {
                "pageid": 2934321,
                "ns": 2,
                "title": "User:Aaron of Mpls"
            },
            {
                "pageid": 40974272,
                "ns": 2,
                "title": "User:Aaron-Kocourek"
            },
            {
                "pageid": 13618776,
                "ns": 2,
                "title": "User:Aaroncorey"
            }
        ]
    }
}

Example 3: Python code for iterating through all results

edit

Here is Python code showing how to iterate over query results (using the python requests lib). Note that you should not manipulate or depend on any specifics of the values returned inside the continue element, as they may change.

for result in query({'generator': 'allpages', 'prop': 'links'}):
    # process result data
...
def query(request):
    request['action'] = 'query'
    request['format'] = 'json'
    last_continue = {}
    while True:
        # Clone original request
        req = request.copy()
        # Modify it with the values returned in the 'continue' section of the last result.
        req.update(last_continue)
        # Call API
        result = requests.get('https://en.wikipedia.org/w/api.php', params=req).json()
        if 'error' in result:
            raise Exception(result['error'])
        if 'warnings' in result:
            print(result['warnings'])
        if 'query' in result:
            yield result['query']
        if 'continue' not in result:
            break
        last_continue = result['continue']