Moved /0 prefix tests to valid...it just means "everything"
[mediawiki.git] / maintenance / importTextFile.php
blob3b77eb5f4a5eab357ff18212c456ca32bf690587
1 <?php
3 /**
4 * Maintenance script allows creating or editing pages using
5 * the contents of a text file
7 * @file
8 * @ingroup Maintenance
9 * @author Rob Church <robchur@gmail.com>
12 $options = array( 'help', 'nooverwrite', 'norc' );
13 $optionsWithArgs = array( 'title', 'user', 'comment' );
14 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
15 echo( "Import Text File\n\n" );
17 if ( count( $args ) < 1 || isset( $options['help'] ) ) {
18 showHelp();
19 } else {
21 $filename = $args[0];
22 echo( "Using {$filename}..." );
23 if ( is_file( $filename ) ) {
25 $title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
26 $title = Title::newFromURL( $title );
28 if ( is_object( $title ) ) {
30 echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
31 if ( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
33 $text = file_get_contents( $filename );
34 $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
35 $user = User::newFromName( $user );
37 if ( is_object( $user ) ) {
39 echo( "\nUsing username '" . $user->getName() . "'..." );
40 $wgUser =& $user;
41 $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
42 $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
44 echo( "\nPerforming edit..." );
45 $article = new Article( $title );
46 $article->doEdit( $text, $comment, $flags );
47 echo( "done.\n" );
49 } else {
50 echo( "invalid username.\n" );
53 } else {
54 echo( "page exists.\n" );
57 } else {
58 echo( "invalid title.\n" );
61 } else {
62 echo( "does not exist.\n" );
67 function titleFromFilename( $filename ) {
68 $parts = explode( '/', $filename );
69 $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
70 return $parts[0];
73 function showHelp() {
74 print <<<EOF
75 USAGE: php importTextFile.php <options> <filename>
77 <filename> : Path to the file containing page content to import
79 Options:
81 --title <title>
82 Title for the new page; default is to use the filename as a base
83 --user <user>
84 User to be associated with the edit
85 --comment <comment>
86 Edit summary
87 --nooverwrite
88 Don't overwrite existing content
89 --norc
90 Don't update recent changes
91 --help
92 Show this information
94 EOF;