Index: trunk/tools/wikibot/RPEDFileReader.php |
— | — | @@ -0,0 +1,80 @@ |
| 2 | +<?php |
| 3 | +# RPEDFileReader.php by Tisane, http://www.mediawiki.org/wiki/User:Tisane |
| 4 | +# |
| 5 | +# This script is free software that is available under the terms of the Creative Commons |
| 6 | +# Attribution-ShareAlike 3.0 license and the current version of the GNU General Public License. |
| 7 | +# |
| 8 | +# The purpose of this script is to read a text file (specifically, the list of page titles from |
| 9 | +# Wikipedia's data dump) and add each page title to a database table. |
| 10 | + |
| 11 | +error_reporting( E_ALL | E_STRICT ); |
| 12 | +include 'wikibot.classes.php'; /* The wikipedia classes. */ |
| 13 | + |
| 14 | +$bot = $argv[1]; |
| 15 | +$wiki = $argv[2]; |
| 16 | +$fileName = $argv[3]; |
| 17 | +$searching = false; |
| 18 | +if ( isset( $argv[4] ) && $argv[4] != '' ) { |
| 19 | + $searching = true; |
| 20 | +} |
| 21 | +if ( !isset( $argv[5] ) || $argv[5] != "nodaemon" ) { |
| 22 | + $daemonize = true; |
| 23 | +} |
| 24 | + |
| 25 | +$user = getWikibotSetting( 'user', $bot, $wiki ); |
| 26 | +$pass = getWikibotSetting( 'pass', $bot, $wiki ); |
| 27 | +$maxURLLength = getWikibotSetting( 'maxurllength', $bot, $wiki ); |
| 28 | + |
| 29 | +$wpapi = new wikipediaapi ( '', '', '', $bot, $wiki, true ); |
| 30 | +$wpq = new wikipediaquery ( '', '', '', $bot, $wiki, true ); |
| 31 | +$wpi = new wikipediaindex ( '', '', '', $bot, $wiki, true ); |
| 32 | +if ( $wpapi->login( $user, $pass ) != 'true' ) { |
| 33 | + echo "Login failure\n"; |
| 34 | + die(); |
| 35 | +} |
| 36 | + |
| 37 | +$handle = @fopen( $fileName, "r" ); |
| 38 | +$lineNumber = 0; |
| 39 | +$line = ""; |
| 40 | + |
| 41 | +if ( isset( $daemonize ) ) { |
| 42 | + |
| 43 | + $pid = pcntl_fork(); // fork |
| 44 | + if ( $pid < 0 ) { |
| 45 | + exit; |
| 46 | + } |
| 47 | + else if ( $pid ) { // parent |
| 48 | + exit; |
| 49 | + } |
| 50 | + // child |
| 51 | + $sid = posix_setsid(); |
| 52 | + if ( $sid < 0 ) { |
| 53 | + exit; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +if ( $handle ) { |
| 58 | + while ( !feof( $handle ) ) { |
| 59 | + $buffer = fgets( $handle, 4096 ); |
| 60 | + $buffer = str_replace( "\n", "", $buffer ); |
| 61 | + if ( $searching == true ) { |
| 62 | + if ( $buffer == $argv[4] ) { |
| 63 | + $searching = false; |
| 64 | + } |
| 65 | + } else { |
| 66 | + $buffer = urlencode ( $buffer ); |
| 67 | + if ( $line != "" ) { |
| 68 | + $line .= "|"; |
| 69 | + } |
| 70 | + if ( strlen( $line ) + strlen( $buffer ) > $maxURLLength ) { |
| 71 | + $wpapi->rpedInsert( $line ); |
| 72 | + $line = ""; |
| 73 | + } |
| 74 | + $line .= $buffer; |
| 75 | + } |
| 76 | + } |
| 77 | + $wpapi->rpedInsert( $line ); |
| 78 | + fclose( $handle ); |
| 79 | +} else { |
| 80 | + echo "No handle!\n"; |
| 81 | +} |