Manual:Wikibot/UploadBot

From Linux Web Expert

The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This code will upload all the files in a directory. It will ignore subdirectories. You will need to have wikibot.classes.php (see Manual:Wikibot) in the same directory as this file, UploadBot.php, in order for it to work.

Usage

After configuring the code, go to a command prompt and type:
php UploadBot.php

Make sure that the appropriate page, e.g. User:MyBot/Run, exists.

Configuration

Modify these lines in the source code:

$bot='YourBot';
$wiki='YourWiki';
$path = '../yourDirectory/';

Make sure you set up your bot and wiki in the wikibot.config.php file; see Manual:Wikibot.

Code

UploadBot.php

<?php
error_reporting(E_ALL | E_STRICT);
include 'wikibot.classes.php'; /* The wikipedia classes. */

# Configure these settings as you see fit.
$bot='YourBot';
$wiki='YourWiki';
$path = '../yourDirectory/';

$wpapi	= new wikipediaapi ('','','',$bot,$wiki,true);
$wpq	= new wikipediaquery ('','','',$bot,$wiki,true);
$wpi	= new wikipediaindex ('','','',$bot,$wiki,true);
$user=getWikibotSetting('user',$bot,$wiki);
$pass=getWikibotSetting('pass',$bot,$wiki);
if ($wpapi->login($user,$pass)!='true'){
    echo "Login failure";
    die();
}
sleep(1);

if ($handle = opendir( $path ) ) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";
    
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        $pathAndFile = $path.'/'.$file;
        if ( !is_dir ( $pathAndFile ) ) {
            echo "$file\n";
            var_dump ( $wpi->upload ( $file, $pathAndFile,'[[Category:Files from Libertarian Wiki]]' ) );
        }
    }
    
    closedir($handle);
}