4 * Maintenance script to insert an article, importing text from a file
7 * @subpackage Maintenance
8 * @author Rob Church <robchur@gmail.com>
11 $options = array( 'help', 'norc' );
12 $optionsWithArgs = array( 'title', 'user', 'comment' );
13 require_once( 'commandLine.inc' );
14 require_once( 'importTextFile.inc' );
15 echo( "Import Text File\n\n" );
17 if( !isset( $options['help'] ) ||
!$options['help'] ) {
19 # Check file existence
21 echo( "Using file '{$filename}'..." );
22 if( file_exists( $filename ) ) {
25 # Work out the title for the page
26 if( isset( $option['title'] ) ||
trim( $options['title'] ) != '' ) {
27 $titleText = $options['title'];
28 # Use the supplied title
29 echo( "Using title '{$titleText}'..." );
30 $title = Title
::newFromText( $options['title'] );
32 # Attempt to make a title out of the filename
33 echo( "Using title from filename..." );
34 $title = titleFromFilename( $filename );
37 # Check the title's valid
38 if( !is_null( $title ) && is_object( $title ) ) {
42 $text = file_get_contents( $filename );
44 # Check the supplied user and fall back to a default if needed
45 if( isset( $options['user'] ) && trim( $options['user'] ) != '' ) {
46 $username = $options['user'];
48 $username = 'MediaWiki default';
50 echo( "Using user '{$username}'..." );
51 $user = User
::newFromName( $username );
53 # Check the user's valid
54 if( !is_null( $user ) && is_object( $user ) ) {
58 # If a comment was supplied, use it (replace _ with spaces ) else use a default
59 if( isset( $options['comment'] ) ||
trim( $options['comment'] != '' ) ) {
60 $comment = str_replace( '_', ' ', $options['comment'] );
62 $comment = 'Importing text file';
64 echo( "Using edit summary '{$comment}'.\n" );
66 # Do we need to update recent changes?
67 if( isset( $options['norc'] ) && $options['norc'] ) {
73 # Attempt the insertion
74 echo( "Attempting to insert page..." );
75 $success = insertNewArticle( $title, $text, $user, $comment, $rc );
79 echo( "failed. Title exists.\n" );
84 echo( "invalid username.\n" );
89 echo( "invalid title.\n" );
94 echo( "not found.\n" );
99 echo( "Imports the contents of a text file into a wiki page.\n\n" );
100 echo( "USAGE: php importTextFile.php [--help|--title <title>|--user <user>|--comment <comment>|--norc] <filename>\n\n" );
101 echo( " --help: Show this help information\n" );
102 echo( " --title <title> : Title for the new page; if not supplied, the filename is used as a base for the title\n" );
103 echo( " --user <user> : User to be associated with the edit; if not supplied, a default is used\n" );
104 echo( "--comment <comment> : Edit summary to be associated with the edit; underscores are transformed into spaces; if not supplied, a default is used\n" );
105 echo( " <filename> : Path to the file containing the wikitext to import\n" );
106 echo( " --norc : Do not add a page creation event to recent changes\n" );