API:RecentChanges/Sample code 1
Appearance
From mediawiki.org
Python
[edit ]#!/usr/bin/python3 """ get_recent_changes.py MediaWiki API Demos Demo of `RecentChanges` module: Get the three most recent changes with sizes and flags MIT License """ importrequests S = requests.Session() URL = "https://en.wikipedia.org/w/api.php" PARAMS = { "format": "json", "rcprop": "title|ids|sizes|flags|user", "list": "recentchanges", "action": "query", "rclimit": "3" } R = S.get(url=URL, params=PARAMS) DATA = R.json() RECENTCHANGES = DATA['query']['recentchanges'] for rc in RECENTCHANGES: print(str(rc['title']))
PHP
[edit ]<?php /* get_recent_changes.php MediaWiki API Demos Demo of `RecentChanges` module: Get the three most recent changes with sizes and flags MIT License */ $endPoint = "https://en.wikipedia.org/w/api.php"; $params = [ "action" => "query", "list" => "recentchanges", "rcprop" => "title|ids|sizes|flags|user", "rclimit" => "3", "format" => "json" ]; $url = $endPoint . "?" . http_build_query( $params ); $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $output = curl_exec( $ch ); curl_close( $ch ); $result = json_decode( $output, true ); foreach( $result["query"]["recentchanges"] as $rc ){ echo( $rc["title"] . "\n" ); }
JavaScript
[edit ]/* get_recent_changes.js MediaWiki API Demos Demo of `RecentChanges` module: Get the three most recent changes with sizes and flags MIT License */ varurl="https://en.wikipedia.org/w/api.php"; varparams={ action:"query", list:"recentchanges", rcprop:"title|ids|sizes|flags|user", rclimit:"3", format:"json" }; url=url+"?origin=*"; Object.keys(params).forEach(function(key){url+="&"+key+"="+params[key];}); fetch(url) .then(function(response){returnresponse.json();}) .then(function(response){ varrecentchanges=response.query.recentchanges; for(varrcinrecentchanges){ console.log(recentchanges[rc].title); } }) .catch(function(error){console.log(error);});
MediaWiki JS
[edit ]/* get_recent_changes.js MediaWiki API Demos Demo of `RecentChanges` module: Get the three most recent changes with sizes and flags MIT License */ varparams={ action:'query', list:'recentchanges', rcprop:'title|ids|sizes|flags|user', rclimit:'3', format:'json' }, api=newmw.Api(); api.get(params).done(function(data){ varrecentchanges=data.query.recentchanges, rc; for(rcinrecentchanges){ console.log(recentchanges[rc].title); } });