Advertisement
freephile

MediaWiki - list all pages + properties in all namespaces.

Sep 29th, 2021
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const results = {
  2.     pages: []
  3. };
  4.  
  5. const promises = [];
  6.  
  7. const defaultParams = {
  8.     action: "query",
  9.     format: "json",
  10. };
  11.  
  12. // Object.keys(mw.config.values.wgFormattedNamespaces).forEach((key) => {
  13.  
  14. const namespaces = [0,1,2,3,4,5,6,7,8,9];
  15.  
  16. namespaces.forEach((key) => {
  17.     const queryParams = {
  18.         generator: "allpages",
  19.         gaplimit: 5000,
  20.         gapfilterredir: "nonredirects",
  21.         prop: "info|pageprops",
  22.         gapnamespace: key,
  23.         ...defaultParams,
  24.     };
  25.     promises.push(getInfo('pages', queryParams));
  26. });
  27.  
  28.  
  29. Promise.all(promises).then((values) => {
  30.     console.log("Done, the file will be downloaded");
  31.  
  32.     download("results.txt", JSON.stringify(results, null, 2));
  33. });
  34.  
  35. function getInfo(key, queryParams) {
  36.     return new Promise((resolve) => {
  37.         const api = new mw.Api();
  38.  
  39.         api.post(queryParams).done((data) => {
  40.             if (data.continue) {
  41.                 const [next] = Object.entries(data.continue);
  42.                 const [continueKey, continueValue] = next;
  43.                 queryParams[continueKey] = continueValue;
  44.  
  45.                 resolve(getInfo(key, queryParams));
  46.             } else {
  47.                 resolve();
  48.             }
  49.             console.log("fetching data ...");
  50.             results[key].push(data.query);
  51.         });
  52.     });
  53. }
  54.  
  55. function download(filename, text) {
  56.     var element = document.createElement("a");
  57.     element.setAttribute(
  58.         "href",
  59.         "data:text/plain;charset=utf-8," + encodeURIComponent(text)
  60.     );
  61.     element.setAttribute("download", filename);
  62.  
  63.     element.style.display = "none";
  64.     document.body.appendChild(element);
  65.  
  66.     element.click();
  67.  
  68.     document.body.removeChild(element);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement