00001 <?php
00002
00012 $options = array( 'help', 'nooverwrite', 'norc' );
00013 $optionsWithArgs = array( 'title', 'user', 'comment' );
00014 require_once( 'commandLine.inc' );
00015 echo( "Import Text File\n\n" );
00016
00017 if( count( $args ) < 1 || isset( $options['help'] ) ) {
00018 showHelp();
00019 } else {
00020
00021 $filename = $args[0];
00022 echo( "Using {$filename}..." );
00023 if( is_file( $filename ) ) {
00024
00025 $title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
00026 $title = Title::newFromUrl( $title );
00027
00028 if( is_object( $title ) ) {
00029
00030 echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
00031 if( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
00032
00033 $text = file_get_contents( $filename );
00034 $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
00035 $user = User::newFromName( $user );
00036
00037 if( is_object( $user ) ) {
00038
00039 echo( "\nUsing username '" . $user->getName() . "'..." );
00040 $wgUser =& $user;
00041 $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
00042 $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
00043
00044 echo( "\nPerforming edit..." );
00045 $article = new Article( $title );
00046 $article->doEdit( $text, $comment, $flags );
00047 echo( "done.\n" );
00048
00049 } else {
00050 echo( "invalid username.\n" );
00051 }
00052
00053 } else {
00054 echo( "page exists.\n" );
00055 }
00056
00057 } else {
00058 echo( "invalid title.\n" );
00059 }
00060
00061 } else {
00062 echo( "does not exist.\n" );
00063 }
00064
00065 }
00066
00067 function titleFromFilename( $filename ) {
00068 $parts = explode( '/', $filename );
00069 $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
00070 return $parts[0];
00071 }
00072
00073 function showHelp() {
00074 print <<<EOF
00075 USAGE: php importTextFile.php <options> <filename>
00076
00077 <filename> : Path to the file containing page content to import
00078
00079 Options:
00080
00081 --title <title>
00082 Title for the new page; default is to use the filename as a base
00083 --user <user>
00084 User to be associated with the edit
00085 --comment <comment>
00086 Edit summary
00087 --nooverwrite
00088 Don't overwrite existing content
00089 --norc
00090 Don't update recent changes
00091 --help
00092 Show this information
00093
00094 EOF;
00095 }