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