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