(bug 12214) Add a predefined list of delete reasons to the file deletion form
[lhc/web/wiklou.git] / maintenance / importTextFile.php
index 7f6d8ca..676acbd 100644 (file)
 <?php
 
 /**
- * Maintenance script to insert an article, importing text from a file
+ * Maintenance script allows creating or editing pages using
+ * the contents of a text file
  *
- * @package MediaWiki
- * @subpackage Maintenance
+ * @addtogroup Maintenance
  * @author Rob Church <robchur@gmail.com>
  */
 
-$options = array( 'help', 'norc' ); 
+$options = array( 'help', 'nooverwrite', 'norc' ); 
 $optionsWithArgs = array( 'title', 'user', 'comment' );
 require_once( 'commandLine.inc' );
-require_once( 'importTextFile.inc' );
 echo( "Import Text File\n\n" );
 
-if( !isset( $options['help'] ) || !$options['help'] ) {
+if( count( $args ) < 1 || isset( $options['help'] ) ) {
+       showHelp();
+} else {
 
-       # Check file existence
        $filename = $args[0];
-       echo( "Using file '{$filename}'..." );
-       if( file_exists( $filename ) ) {
-               echo( "found.\n" );
-       
-               # Work out the title for the page       
-               if( isset( $option['title'] ) || trim( $options['title'] ) != '' ) {
-                       $titleText = $options['title'];
-                       # Use the supplied title
-                       echo( "Using title '{$titleText}'..." );
-                       $title = Title::newFromText( $options['title'] );
-               } else {
-                       # Attempt to make a title out of the filename
-                       echo( "Using title from filename..." );
-                       $title = titleFromFilename( $filename );
-               }
+       echo( "Using {$filename}..." );
+       if( is_file( $filename ) ) {
                
-               # Check the title's valid
-               if( !is_null( $title ) && is_object( $title ) ) {
-                       echo( "ok.\n" );
+               $title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
+               $title = Title::newFromUrl( $title );
+               echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
                
-                       # Read in the text
-                       $text = file_get_contents( $filename );
-                       
-                       # Check the supplied user and fall back to a default if needed
-                       if( isset( $options['user'] ) && trim( $options['user'] ) != '' ) {
-                               $username = $options['user'];
-                       } else {
-                               $username = 'MediaWiki default';
-                       }
-                       echo( "Using user '{$username}'..." );
-                       $user = User::newFromName( $username );
-                       
-                       # Check the user's valid
-                       if( !is_null( $user ) && is_object( $user ) ) {
-                               echo( "ok.\n" );
-                       
-                               # If a comment was supplied, use it (replace _ with spaces ) else use a default
-                               if( isset( $options['comment'] ) || trim( $options['comment'] != '' ) ) {
-                                       $comment = str_replace( '_', ' ', $options['comment'] );
-                               } else {
-                                       $comment = 'Importing text file';
-                               }
-                               echo( "Using edit summary '{$comment}'.\n" );
+               if( is_object( $title ) ) {
                        
-                               # Do we need to update recent changes?
-                               if( isset( $options['norc'] ) && $options['norc'] ) {
-                                       $rc = false;
-                               } else {
-                                       $rc = true;
-                               }
+                       if( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
                        
-                               # Attempt the insertion
-                               echo( "Attempting to insert page..." );
-                               $success = insertNewArticle( $title, $text, $user, $comment, $rc );
-                               if( $success ) {
+                               $text = file_get_contents( $filename );
+                               $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
+                               $user = User::newFromName( $user );
+                               echo( "\nUsing username '" . $user->getName() . "'..." );
+                               
+                               if( is_object( $user ) ) {
+                               
+                                       $wgUser =& $user;
+                                       $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
+                                       $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
+                                       
+                                       echo( "\nPerforming edit..." );
+                                       $article = new Article( $title );
+                                       $article->doEdit( $text, $comment, $flags );
                                        echo( "done.\n" );
+                               
                                } else {
-                                       echo( "failed. Title exists.\n" );
+                                       echo( "invalid username.\n" );
                                }
                        
                        } else {
-                               # Dud user
-                               echo( "invalid username.\n" );
+                               echo( "page exists.\n" );
                        }
                        
                } else {
-                       # Dud title
                        echo( "invalid title.\n" );
                }
                
        } else {
-               # File not found
-               echo( "not found.\n" );
+               echo( "does not exist.\n" );
        }
 
-} else {
-       # Show help
-       echo( "Imports the contents of a text file into a wiki page.\n\n" );
-       echo( "USAGE: php importTextFile.php [--help|--title <title>|--user <user>|--comment <comment>|--norc] <filename>\n\n" );
-       echo( "              --help: Show this help information\n" );
-       echo( "    --title <title> : Title for the new page; if not supplied, the filename is used as a base for the title\n" );
-       echo( "      --user <user> : User to be associated with the edit; if not supplied, a default is used\n" );
-       echo( "--comment <comment> : Edit summary to be associated with the edit; underscores are transformed into spaces; if not supplied, a default is used\n" );
-       echo( "         <filename> : Path to the file containing the wikitext to import\n" );
-       echo( "             --norc : Do not add a page creation event to recent changes\n" );
+}
+
+function titleFromFilename( $filename ) {
+       $parts = explode( '/', $filename );
+       $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
+       return $parts[0];
+}
 
+function showHelp() {
+       echo( "Import the contents of a text file into a wiki page.\n" );
+       echo( "USAGE: php importTextFile.php <options> <filename>\n\n" );
+       echo( "<filename> : Path to the file containing page content to import\n\n" );
+       echo( "Options:\n\n" );
+       echo( "--title <title>\n\tTitle for the new page; default is to use the filename as a base\n" );
+       echo( "--user <user>\n\tUser to be associated with the edit\n" );
+       echo( "--comment <comment>\n\tEdit summary\n" );
+       echo( "--nooverwrite\n\tDon't overwrite existing content\n" );
+       echo( "--norc\n\tDon't update recent changes\n" );
+       echo( "--help\n\tShow this information\n" );
+       echo( "\n" );
 }
-echo( "\n" );  
 
-?>
\ No newline at end of file