Merge "Send correct HTML when reporting a MWException object and the OuputPage object...
[lhc/web/wiklou.git] / maintenance / importTextFile.php
index c8b00cb..ec9ff00 100644 (file)
-<?php\r
-\r
-/**\r
- * Maintenance script to insert an article, importing text from a file\r
- *\r
- * @package MediaWiki\r
- * @subpackage Maintenance\r
- * @author Rob Church <robchur@gmail.com>\r
- */\r
-\r
-$options = array( 'help' ); \r
-$optionsWithArgs = array( 'title', 'user', 'comment' );\r
-require_once( 'commandLine.inc' );\r
-require_once( 'importTextFile.inc' );\r
-echo( "Import Text File\n\n" );\r
-\r
-if( !isset( $options['help'] ) || !$options['help'] ) {\r
-\r
-       # Check file existence\r
-       $filename = $args[0];\r
-       echo( "Using file '{$filename}'..." );\r
-       if( file_exists( $filename ) ) {\r
-               echo( "found.\n" );\r
-       \r
-               # Work out the title for the page       \r
-               if( isset( $option['title'] ) || trim( $options['title'] ) != '' ) {\r
-                       $titleText = $options['title'];\r
-                       # Use the supplied title\r
-                       echo( "Using title '{$titleText}'..." );\r
-                       $title = Title::newFromText( $options['title'] );\r
-               } else {\r
-                       # Attempt to make a title out of the filename\r
-                       echo( "Using title from filename..." );\r
-                       $title = titleFromFilename( $filename );\r
-               }\r
-               \r
-               # Check the title's valid\r
-               if( !is_null( $title ) && is_object( $title ) ) {\r
-                       echo( "ok.\n" );\r
-               \r
-                       # Read in the text\r
-                       $text = file_get_contents( $filename );\r
-                       \r
-                       # Check the supplied user and fall back to a default if needed\r
-                       if( isset( $options['user'] ) && trim( $options['user'] ) != '' ) {\r
-                               $username = $options['user'];\r
-                       } else {\r
-                               $username = 'MediaWiki default';\r
-                       }\r
-                       echo( "Using user '{$username}'..." );\r
-                       $user = User::newFromName( $username );\r
-                       \r
-                       # Check the user's valid\r
-                       if( !is_null( $user ) && is_object( $user ) ) {\r
-                               echo( "ok.\n" );\r
-                       \r
-                               # If a comment was supplied, use it (replace _ with spaces ) else use a default\r
-                               if( isset( $options['comment'] ) || trim( $options['comment'] != '' ) ) {\r
-                                       $comment = str_replace( '_', ' ', $options['comment'] );\r
-                               } else {\r
-                                       $comment = 'Importing text file';\r
-                               }\r
-                               echo( "Using edit summary '{$comment}'.\n" );\r
-                       \r
-                               # Attempt the insertion\r
-                               echo( "Attempting to insert page..." );\r
-                               $success = insertNewArticle( $title, $text, $user, $comment );\r
-                               if( $success ) {\r
-                                       echo( "done.\n" );\r
-                               } else {\r
-                                       echo( "failed. Title exists.\n" );\r
-                               }\r
-                       \r
-                       } else {\r
-                               # Dud user\r
-                               echo( "invalid username.\n" );\r
-                       }\r
-                       \r
-               } else {\r
-                       # Dud title\r
-                       echo( "invalid title.\n" );\r
-               }\r
-               \r
-       } else {\r
-               # File not found\r
-               echo( "not found.\n" );\r
-       }\r
-\r
-} else {\r
-       # Show help\r
-       echo( "Imports the contents of a text file into a wiki page.\n\n" );\r
-       echo( "USAGE: php importTextFile.php [--help|--title <title>|--user <user>|--comment <comment>] <filename>\n\n" );\r
-       echo( "              --help: Show this help information\n" );\r
-       echo( "    --title <title> : Title for the new page; if not supplied, the filename is used as a base for the title\n" );\r
-       echo( "      --user <user> : User to be associated with the edit; if not supplied, a default is used\n" );\r
-       echo( "--comment <comment> : Edit summary to be associated with the edit; underscores are transformed into spaces; if not supplied, a default is used\n" );\r
-       echo( "         <filename> : Path to the file containing the wikitext to import\n" );\r
-\r
-}\r
-echo( "\n" );  \r
-\r
-?>
\ No newline at end of file
+<?php
+
+/**
+ * Maintenance script allows creating or editing pages using
+ * the contents of a text file
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ * @author Rob Church <robchur@gmail.com>
+ */
+
+$options = array( 'help', 'nooverwrite', 'norc' );
+$optionsWithArgs = array( 'title', 'user', 'comment' );
+require_once( dirname( __FILE__ ) . '/commandLine.inc' );
+echo( "Import Text File\n\n" );
+
+if ( count( $args ) < 1 || isset( $options['help'] ) ) {
+       showHelp();
+} else {
+
+       $filename = $args[0];
+       echo( "Using {$filename}..." );
+       if ( is_file( $filename ) ) {
+
+               $title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
+               $title = Title::newFromURL( $title );
+
+               if ( is_object( $title ) ) {
+
+                       echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
+                       if ( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
+
+                               $text = file_get_contents( $filename );
+                               $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
+                               $user = User::newFromName( $user );
+
+                               if ( is_object( $user ) ) {
+
+                                       echo( "\nUsing username '" . $user->getName() . "'..." );
+                                       $wgUser =& $user;
+                                       $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
+                                       $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
+
+                                       echo( "\nPerforming edit..." );
+                                       $page = WikiPage::factory( $title );
+                                       $page->doEdit( $text, $comment, $flags, false, $user );
+                                       echo( "done.\n" );
+
+                               } else {
+                                       echo( "invalid username.\n" );
+                               }
+
+                       } else {
+                               echo( "page exists.\n" );
+                       }
+
+               } else {
+                       echo( "invalid title.\n" );
+               }
+
+       } else {
+               echo( "does not exist.\n" );
+       }
+
+}
+
+function titleFromFilename( $filename ) {
+       $parts = explode( '/', $filename );
+       $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
+       return $parts[0];
+}
+
+function showHelp() {
+print <<<EOF
+USAGE: php importTextFile.php <options> <filename>
+
+<filename> : Path to the file containing page content to import
+
+Options:
+
+--title <title>
+       Title for the new page; default is to use the filename as a base
+--user <user>
+       User to be associated with the edit
+--comment <comment>
+       Edit summary
+--nooverwrite
+       Don't overwrite existing content
+--norc
+       Don't update recent changes
+--help
+       Show this information
+
+EOF;
+}