MediaWiki  master
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 
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                                         $content = ContentHandler::makeContent( $text, $title );
00060                                         $page->doEditContent( $content, $comment, $flags, false, $user );
00061                                         echo( "done.\n" );
00062 
00063                                 } else {
00064                                         echo( "invalid username.\n" );
00065                                 }
00066 
00067                         } else {
00068                                 echo( "page exists.\n" );
00069                         }
00070 
00071                 } else {
00072                         echo( "invalid title.\n" );
00073                 }
00074 
00075         } else {
00076                 echo( "does not exist.\n" );
00077         }
00078 
00079 }
00080 
00081 function titleFromFilename( $filename ) {
00082         $parts = explode( '/', $filename );
00083         $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
00084         return $parts[0];
00085 }
00086 
00087 function showHelp() {
00088 print <<<EOF
00089 USAGE: php importTextFile.php <options> <filename>
00090 
00091 <filename> : Path to the file containing page content to import
00092 
00093 Options:
00094 
00095 --title <title>
00096         Title for the new page; default is to use the filename as a base
00097 --user <user>
00098         User to be associated with the edit
00099 --comment <comment>
00100         Edit summary
00101 --nooverwrite
00102         Don't overwrite existing content
00103 --norc
00104         Don't update recent changes
00105 --help
00106         Show this information
00107 
00108 EOF;
00109 }