MediaWiki  REL1_21
importTextFile.php
Go to the documentation of this file.
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                                 RequestContext::getMain()->setTitle( $title );
00046 
00047                                 $text = file_get_contents( $filename );
00048                                 $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
00049                                 $user = User::newFromName( $user );
00050 
00051                                 if ( is_object( $user ) ) {
00052 
00053                                         echo( "\nUsing username '" . $user->getName() . "'..." );
00054                                         $wgUser =& $user;
00055                                         $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
00056                                         $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
00057 
00058                                         echo( "\nPerforming edit..." );
00059                                         $page = WikiPage::factory( $title );
00060                                         $content = ContentHandler::makeContent( $text, $title );
00061                                         $page->doEditContent( $content, $comment, $flags, false, $user );
00062                                         echo( "done.\n" );
00063 
00064                                 } else {
00065                                         echo( "invalid username.\n" );
00066                                 }
00067 
00068                         } else {
00069                                 echo( "page exists.\n" );
00070                         }
00071 
00072                 } else {
00073                         echo( "invalid title.\n" );
00074                 }
00075 
00076         } else {
00077                 echo( "does not exist.\n" );
00078         }
00079 
00080 }
00081 
00082 function titleFromFilename( $filename ) {
00083         $parts = explode( '/', $filename );
00084         $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
00085         return $parts[0];
00086 }
00087 
00088 function showHelp() {
00089 print <<<EOF
00090 USAGE: php importTextFile.php <options> <filename>
00091 
00092 <filename> : Path to the file containing page content to import
00093 
00094 Options:
00095 
00096 --title <title>
00097         Title for the new page; default is to use the filename as a base
00098 --user <user>
00099         User to be associated with the edit
00100 --comment <comment>
00101         Edit summary
00102 --nooverwrite
00103         Don't overwrite existing content
00104 --norc
00105         Don't update recent changes
00106 --help
00107         Show this information
00108 
00109 EOF;
00110 }