Files
rappaurio-sae501_502/app/node_modules/wikiapi/docs/index.html
2023-09-25 13:27:24 +02:00

472 lines
18 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
<script src="scripts/nav.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav >
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Wikiapi.html">Wikiapi</a><ul class='methods'><li data-type='method'><a href="Wikiapi.html#category_tree">category_tree</a></li><li data-type='method'><a href="Wikiapi.html#convert_Chinese">convert_Chinese</a></li><li data-type='method'><a href="Wikiapi.html#data">data</a></li><li data-type='method'><a href="Wikiapi.html#delete">delete</a></li><li data-type='method'><a href="Wikiapi.html#download">download</a></li><li data-type='method'><a href="Wikiapi.html#edit">edit</a></li><li data-type='method'><a href="Wikiapi.html#edit_page">edit_page</a></li><li data-type='method'><a href="Wikiapi.html#for_each_page">for_each_page</a></li><li data-type='method'><a href="Wikiapi.html#get_featured_content">get_featured_content</a></li><li data-type='method'><a href="Wikiapi.html#listen">listen</a></li><li data-type='method'><a href="Wikiapi.html#login">login</a></li><li data-type='method'><a href="Wikiapi.html#move_page">move_page</a></li><li data-type='method'><a href="Wikiapi.html#move_to">move_to</a></li><li data-type='method'><a href="Wikiapi.html#new_data_entity">new_data_entity</a></li><li data-type='method'><a href="Wikiapi.html#page">page</a></li><li data-type='method'><a href="Wikiapi.html#purge">purge</a></li><li data-type='method'><a href="Wikiapi.html#query">query</a></li><li data-type='method'><a href="Wikiapi.html#redirects_here">redirects_here</a></li><li data-type='method'><a href="Wikiapi.html#redirects_root">redirects_root</a></li><li data-type='method'><a href="Wikiapi.html#register_redirects">register_redirects</a></li><li data-type='method'><a href="Wikiapi.html#search">search</a></li><li data-type='method'><a href="Wikiapi.html#site_name">site_name</a></li><li data-type='method'><a href="Wikiapi.html#SPARQL">SPARQL</a></li><li data-type='method'><a href="Wikiapi.html#tracking_revisions">tracking_revisions</a></li><li data-type='method'><a href="Wikiapi.html#upload">upload</a></li></ul></li></ul><h3>Global</h3><ul><li><a href="global.html#CeL">CeL</a></li><li><a href="global.html#KEY_SESSION">KEY_SESSION</a></li><li><a href="global.html#KEY_wiki_session">KEY_wiki_session</a></li><li><a href="global.html#modify_data_entity">modify_data_entity</a></li><li><a href="global.html#page_data_attributes">page_data_attributes</a></li><li><a href="global.html#reject_edit_error">reject_edit_error</a></li><li><a href="global.html#set_page_data_attributes">set_page_data_attributes</a></li><li><a href="global.html#setup_data_entity">setup_data_entity</a></li><li><a href="global.html#setup_wiki_session">setup_wiki_session</a></li><li><a href="global.html#wiki_API">wiki_API</a></li><li><a href="global.html#Wikiapi_list">Wikiapi_list</a></li></ul>
</nav>
<div id="main">
<section class="package">
<h3> </h3>
</section>
<section class="readme usertext">
<article><p><a href="https://www.npmjs.com/package/wikiapi"><img src="https://badge.fury.io/js/wikiapi.svg" alt="npm version"></a>
<a href="https://www.npmjs.com/package/wikiapi"><img src="https://img.shields.io/npm/dm/wikiapi.svg" alt="npm downloads"></a>
<a href="https://github.com/kanasimi/wikiapi/actions"><img src="https://github.com/kanasimi/wikiapi/actions/workflows/npm-test.yml/badge.svg" alt="GitHub Actions workflow build status"></a>
<a href="https://codecov.io/gh/kanasimi/wikiapi"><img src="https://codecov.io/gh/kanasimi/wikiapi/branch/master/graph/badge.svg" alt="codecov"></a></p>
<p><a href="https://snyk.io/test/github/kanasimi/wikiapi?targetFile=package.json"><img src="https://snyk.io/test/github/kanasimi/wikiapi/badge.svg?targetFile=package.json" alt="Known Vulnerabilities"></a>
<a href="https://codebeat.co/projects/github-com-kanasimi-wikiapi-master"><img src="https://codebeat.co/badges/47d3b442-fd49-4142-a69b-05171bf8fe36" alt="codebeat badge"></a>
<a href="https://www.codacy.com/gh/kanasimi/wikiapi/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=kanasimi/wikiapi&amp;utm_campaign=Badge_Grade"><img src="https://app.codacy.com/project/badge/Grade/bfb4fe9fe6e04346986f5cc83099369e" alt="Codacy Badge"></a>
<a href="https://deepscan.io/dashboard#view=project&amp;tid=4788&amp;pid=6757&amp;bid=58325"><img src="https://deepscan.io/api/teams/4788/projects/6757/branches/58325/badge/grade.svg" alt="DeepScan grade"></a></p>
<h1>JavaScript MediaWiki API</h1>
<p>A simple way to access MediaWiki API via JavaScript with <a href="https://kanasimi.github.io/CeJS/_test%20suite/wikitext_parser.html">wikitext parser</a>.
This is basically a modern syntax version of <a href="https://github.com/kanasimi/CeJS/blob/master/application/net/wiki">CeJS MediaWiki module</a>. For example, using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async functions</a>.</p>
<h2>Features</h2>
<ul>
<li>Read / edit pages.</li>
<li>Get list of categorymembers, pages transclude specified template, and more...</li>
<li>Auto-limited editing rate.</li>
<li>Parse wikitext / pages. You may modify parts of the wikitext, then regenerate the page just using .toString(). See <a href="https://kanasimi.github.io/CeJS/_test%20suite/wikitext_parser.html">wikitext parser examples</a>.</li>
</ul>
<h2>Installation</h2>
<p>This is a nodejs module. Please install <a href="https://nodejs.org/">node.js</a> first.</p>
<pre class="prettyprint source lang-bash"><code>npm install wikiapi
</code></pre>
<h2>Usage</h2>
<p>Here lists some examples of this module.</p>
<ul>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#login">Login to wiki site</a>
<ul>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#example__Login%20to%20wiki%20site%201">Login to wiki site #1</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#example__Login%20to%20wiki%20site%202">Login to wiki site #2</a></li>
</ul>
</li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#page">Get page data and parse the wikitext</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#listen">Listen to page modification</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#edit">Edit page</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#for_each_page">Edit multiple pages</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#category_tree">Get category tree</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#data">Get wikidata</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#upload">Upload file / media</a>
<ul>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#example__Upload%20file%20/%20media">Upload file / media</a></li>
</ul>
</li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#download">Download file / media</a></li>
<li><a href="https://kanasimi.github.io/wikiapi/Wikiapi.html#move_page">Move page</a></li>
</ul>
<h3>As node.js module</h3>
<pre class="prettyprint source lang-javascript"><code>// Load Wikiapi module
const Wikiapi = require('wikiapi');
// OPEN ASYNC DOMAIN:
(async () => {
// LOGIN IN: In any wiki, any language
const wiki = new Wikiapi('zh'); // or new Wikiapi('https://zh.wikipedia.org/w/api.php')
await wiki.login('user', 'password'); // get your own account and password on your target wiki.
/* ***************************************************** */
/* READ ONLY ******************************************* */
// load page
let page_data = await wiki.page('Universe', {});
console.log('page_data: ', page_data);
console.log('page_data.text: ', page_data.wikitext);
// Get multi revisions (ex: 2)
let page_data = await wiki.page('Universe', { revisions: 2 });
console.log('page_data: ', page_data);
console.log('page_data.text: ', page_data.wikitext);
/* ***************************************************** */
/* EDITING ********************************************* */
/* Note: .page() then .edit() ************************** */
// Edit page: append content, as bot.
let page_data = await wiki.page('Universe'),
newContent = page_data.wikitext + '\nTest edit using wikiapi.';
await enwiki.edit(
function (page_data) { return newContent; }, // new content
{ bot: 1, summary: 'Test edit.' } // options
);
// Edit page: replace content, with more options.
let page_data = await wiki.page('Universe'),
newContent = page_data.wikitext.replace(/Test edit using wikiapi/g, 'Test: replace content was successful!');
await enwiki.edit(
function (page_data) { return newContent; }, // new content
{ bot: 1, minor: 1, nocreate: 1, summary: 'Test: replace content.' } // more options
);
// Edit page: wipe clean, replace by string
let page_data = await wiki.page('Universe');
await wiki.edit(
'{{Speedy|reason=Vandalism}}',
{ bot: 1, minor: 0, nocreate: 1, summary: 'Test: wipe clean, please delete.' }
);
/* edit_page(): a more direct method ******************* */
// Edit page:
await wiki.edit_page('Wikipedia:Sandbox', function (page_data) {
return page_data.wikitext + '\nTest edit using {{GitHub|kanasimi/wikiapi}}.';
}, { bot: 1, nocreate: 1, minor: 1, summary: 'Test: edit page via .edit_page().' });
/* ***************************************************** */
/* PROVIDE MANY **************************************** */
// List of hand-picked target pages
let list = ['Wikipedia:Sandbox', 'Wikipedia:Sandbox2', 'Wikipedia:Sandbox/wikiapi'];
// List pages in [[Category:Chemical_elements]]
let listMembers = await wiki.categorymembers('Chemical elements'); // array of titles
// List intra-wiki links in [[ABC]]
let listLinks = await wiki.redirects_here('ABC'); // array of titles
// List of transcluded pages {{w:en:Periodic table}}
let listTranscluded = await wiki.embeddedin('Template:Periodic table');
// List of searched pages with expression in its title name
let listSearch = await wiki.search(' dragon'); // array of titles
/* ***************************************************** */
/* MULTI-read/edit ************************************* */
// Multi edit, members of category
await wiki.for_each_page(
listMembers,
page_data => { return `{{stub}}\n` + page_data.wikitext; },
{ summary: 'Test: multi-edits', minor: 1 }
);
// Multi read, following intra-wiki links
await wiki.for_each_page(
listLinks, // array of targets
page_data => {
console.log(page_data.title); // print page title
return Wikiapi.skip_edit; // skip edit, just read, return nothing to edit with.
}, // no edit therefore no options
);
/* ***************************************************** */
/* MOVE PAGE (RENAME) ********************************** */
// Move page once.
result = await wiki.move_page('Wikipedia:Sanbox/Wikiapi', 'Wikipedia:Sanbox/NewWikiapi',
{ reason: 'Test: move page (1).', noredirect: true, movetalk: true }
);
// Reverse move
result = await wiki.move_page('Wikipedia:Sanbox/NewWikiapi', 'Wikipedia:Sanbox/Wikiapi',
{ reason: 'Test: move page (2).', noredirect: true, movetalk: true }
);
/* ***************************************************** */
/* PARSE *********************************************** */
// Read Infobox templates, convert to JSON.
const page_data = await wiki.page('JavaScript');
// `page_data.parse(options)` will startup the parser process, create page_data.parsed. After .parse(), we can use parsed.each().
const parsed = page_data.parse();
let infobox;
parsed.each('template', template_token => {
if (template_token.name.startsWith('Infobox')) {
infobox = template_token.parameters;
return parsed.each.exit;
}
});
for (const [key, value] of Object.entries(infobox))
infobox[key] = value.toString();
// print json of the infobox
console.log(infobox);
// Edit page and parse
const parsed = await wiki.page('Wikipedia:Sandbox').parse();
parsed.each('template', template_token => {/* modify token */ });
await wiki.edit(parsed.toString(), { bot: 1, minor: 1, nocreate: 1 });
let page_data = await wiki.page('Universe');
// See all type in wiki_toString @ https://github.com/kanasimi/CeJS/tree/master/application/net/wiki/parser.js
// List all template name.
page_data.parse().each('template',
token => console.log(token.name));
/* ***************************************************** */
/* MONITORING ****************************************** */
// Listen to new edits, check every 2 minutes
wiki.listen(function for_each_row() {
// ...
}, {
// 檢查的延遲時間。
delay: '2m',
filter: function filter_row(row) {
// row is the same format as page_data
},
// also get diff
with_diff: { LCS: true, line: true },
// only for articles (0:main namespace) and talk pages
namespace: '0|talk',
});
/* ***************************************************** */
/* FILES *********************************************** */
// Set upload parameters, maily for licensing reasons.
// Note: parameter `text`, filled with the right wikicode `{{description|}}`, can replace most parameters.
let options = {
description: 'Photo of Osaka',
date: new Date() || '2021-01-01',
source_url: 'https://github.com/kanasimi/wikiapi',
author: '[[User:user]]',
permission: '{{cc-by-sa-2.5}}',
other_versions: '',
other_fields: '',
license: ['{{cc-by-sa-2.5}}'],
categories: ['[[Category:test images]]'],
bot: 1,
tags: &quot;tag1|tag2&quot;,
};
// Upload file from local path
let result = await wiki.upload({
file_path: '/local/file/path',
filename: 'New_Osaka_Photograph.jpg', // default : keep filename
comment: '',
ignorewarnings: 1, // overwrite
...options
});
// Upload file from URL
result = await wiki.upload({
media_url: 'https://media.url/Thunder-Dragon.ogg',
text: &quot;Her eis wikicode to replave the page's content instead of various other parameters.&quot;,
comment: 'Thunder Dragon audio from vacation in Philipines. Page uses custom template.',
ignorewarnings: 1, // overwrite
...options
});
/* ***************************************************** */
/* WIKIDATA, WIKIBASES ********************************* */
// Read Qid Q1 (Universe), print Chinese label
const wiki = new Wikiapi('https://wikidata.org/w/api.php')
let page_data = await wiki.data('Q1');
console.log(page_data.labels.zh) // '宇宙'
// Read, access by title (English), access property P1419
// Get P1419 of wikidata entity: 'Universe'
let data = await wiki.data('Universe', 'P1419');
// assert: {Array}data = [ 'shape of the universe', '...', ... ]
console.assert(data.includes('shape of the universe'));
// update wikidata
// Get https://test.wikidata.org/wiki/Q7
let entity = await wiki.data('Q7');
// search [ language, label ]
//entity = await wiki.data(['en', 'Earth']);
// Update claim
await entity.modify({ claims: [{ P17: 'Q213280' }] });
// Update claim: set country (P17) to 'Test Country 1' (Q213280) ([language, label] as entity)
await entity.modify({ claims: [{ language: 'en', country: [, 'Test Country 1'] }] });
// Remove country (P17) : 'Test Country 1' (Q213280)
await entity.modify({ claims: [{ language: 'en', country: [, 'Test Country 1'], remove: true }] });
// Update label
await entity.modify({ labels: [{ language: 'zh-tw', value: '地球' }] });
// CLOSE ASYNC DOMAIN:
})();
</code></pre>
<p>More examples: Please see <a href="https://github.com/kanasimi/wikiapi/blob/master/_test%20suite/test.js">test.js</a>.</p>
<h2>OS support</h2>
<table>
<thead>
<tr>
<th>Platform</th>
<th>support</th>
</tr>
</thead>
<tbody>
<tr>
<td>Windows</td>
<td>✔️</td>
</tr>
<tr>
<td>macOS</td>
<td>✔️</td>
</tr>
<tr>
<td>UNIX, Linux</td>
<td>✔️</td>
</tr>
</tbody>
</table>
<h2>See also</h2>
<p>For old style JavaScript, or general environment usage, please see <a href="https://github.com/kanasimi/wikibot">wikibot</a>.</p>
<h2>Contact</h2>
<p>Contact us at <a href="https://github.com/kanasimi/wikiapi/issues">GitHub</a>.</p>
<p><a href="http://lyrics.meicho.com.tw/"><img src="https://raw.githubusercontent.com/kanasimi/CeJS/master/_test%20suite/misc/logo.jpg" alt="logo"></a></p></article>
</section>
<section>
<header>
<h2>
wikiapi.js
</h2>
</header>
<article>
<div class="container-overview">
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="wikiapi.js.html">wikiapi.js</a>, <a href="wikiapi.js.html#line1">line 1</a>
</li></ul></dd>
</dl>
<div class="description usertext">Main codes of module wikiapi (class Wikiapi)</div>
</div>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.10</a> on Tue Sep 20 2022 04:45:46 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
</body>
</html>