kanasimi / wikiapi

JavaScript MediaWiki API for node.js
https://kanasimi.github.io/wikiapi/
BSD 3-Clause "New" or "Revised" License
50 stars 6 forks source link

Renaming file fails #27

Closed hugolpz closed 3 years ago

hugolpz commented 3 years ago

[Please close this issue. Already solved. Just here to create some record / documentation.]

I got an error message with another of my code...

commons-rename.js:

// PURPOSE: Script renames targets following hand-coded patters.
const Wikiapi= require('wikiapi');
const logins = require('./logins-ShufaBot.js');
const files  = require('./data/zi-reds.js'); 

// Edit login credentials
var USER = logins.commons.user,
    PASS = logins.commons.pass,
    API  = logins.commons.api;

(async () => {
    // Connect
    console.log('Connecting...');
    const targetWiki = new Wikiapi;
    await targetWiki.login(USER, PASS, API);
    console.log(`Username ${USER} is connected !`);

    // Renaming by patter
    for(i=0;i<files.length;i++){
        zi = files[i].zi;
        console.log(zi)
        // File page exist ?
        let pageData = await targetWiki.page(`File:${zi}.png`, {});
        console.log('pageExists: ',pageData.wikitext!=='')
        if(pageData.wikitext!=='') {
            var initialTitle=`File:${zi}.png`,
                newTitle=`File:${zi}-newname.png`,
                reason='ShufaBot test: renaming file.',
                revertReason='ShufaBot test: renaming file, revert.';
            console.log(initialTitle,newTitle);
            // Rename
            result = await targetWiki.move_page(initialTitle, newTitle, { reason: reason, noredirect: true, movetalk: true });
            // Revert rename
            await targetWiki.page(newTitle);
            result = await targetWiki.move_to(initialTitle, { reason: revertReason, noredirect: true, movetalk: true });
        }

    }
})();
$ node ./edit-Commons-filename.js 
Connecting...
get_API_parameters: Set enwiki: path=query+siteinfo
get_API_parameters: Set commonswiki: path=query+siteinfo
Username ShufaBot@ShufaBot is connected !
112379664-b9a79100-8ce8-11eb-981d-727884b32993
pageExists:  true
File:112379664-b9a79100-8ce8-11eb-981d-727884b32993.png File:112379664-b9a79100-8ce8-11eb-981d-727884b32993-newname.png
(node:71405) UnhandledPromiseRejectionWarning: #<Object>
(Use `node --trace-warnings ...` to show where the warning was created)
(node:71405) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:71405) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
hugolpz commented 3 years ago

Kanasimi recommended using :

try{ 
  // do your things
}catch(error){
  console.log(error)
}

I was able to get a more meaningful error message:

{
  code: 'cantmovefile',
  info: 'You do not have permission to move files.',
  '*': 'See https://commons.wikimedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes.'
}

So I requested file mover user-rights on Commons, should be ok ! 👍🏼

Issue can be closed ! 👯