c8b00cb2a3a18784eb2246920e1d080e4503fc1d
[lhc/web/wiklou.git] / maintenance / importTextFile.php
1 <?php
2
3 /**
4 * Maintenance script to insert an article, importing text from a file
5 *
6 * @package MediaWiki
7 * @subpackage Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
10
11 $options = array( 'help' );
12 $optionsWithArgs = array( 'title', 'user', 'comment' );
13 require_once( 'commandLine.inc' );
14 require_once( 'importTextFile.inc' );
15 echo( "Import Text File\n\n" );
16
17 if( !isset( $options['help'] ) || !$options['help'] ) {
18
19 # Check file existence
20 $filename = $args[0];
21 echo( "Using file '{$filename}'..." );
22 if( file_exists( $filename ) ) {
23 echo( "found.\n" );
24
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'] );
31 } else {
32 # Attempt to make a title out of the filename
33 echo( "Using title from filename..." );
34 $title = titleFromFilename( $filename );
35 }
36
37 # Check the title's valid
38 if( !is_null( $title ) && is_object( $title ) ) {
39 echo( "ok.\n" );
40
41 # Read in the text
42 $text = file_get_contents( $filename );
43
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'];
47 } else {
48 $username = 'MediaWiki default';
49 }
50 echo( "Using user '{$username}'..." );
51 $user = User::newFromName( $username );
52
53 # Check the user's valid
54 if( !is_null( $user ) && is_object( $user ) ) {
55 echo( "ok.\n" );
56
57 # If a comment was supplied, use it (replace _ with spaces ) else use a default
58 if( isset( $options['comment'] ) || trim( $options['comment'] != '' ) ) {
59 $comment = str_replace( '_', ' ', $options['comment'] );
60 } else {
61 $comment = 'Importing text file';
62 }
63 echo( "Using edit summary '{$comment}'.\n" );
64
65 # Attempt the insertion
66 echo( "Attempting to insert page..." );
67 $success = insertNewArticle( $title, $text, $user, $comment );
68 if( $success ) {
69 echo( "done.\n" );
70 } else {
71 echo( "failed. Title exists.\n" );
72 }
73
74 } else {
75 # Dud user
76 echo( "invalid username.\n" );
77 }
78
79 } else {
80 # Dud title
81 echo( "invalid title.\n" );
82 }
83
84 } else {
85 # File not found
86 echo( "not found.\n" );
87 }
88
89 } else {
90 # Show help
91 echo( "Imports the contents of a text file into a wiki page.\n\n" );
92 echo( "USAGE: php importTextFile.php [--help|--title <title>|--user <user>|--comment <comment>] <filename>\n\n" );
93 echo( " --help: Show this help information\n" );
94 echo( " --title <title> : Title for the new page; if not supplied, the filename is used as a base for the title\n" );
95 echo( " --user <user> : User to be associated with the edit; if not supplied, a default is used\n" );
96 echo( "--comment <comment> : Edit summary to be associated with the edit; underscores are transformed into spaces; if not supplied, a default is used\n" );
97 echo( " <filename> : Path to the file containing the wikitext to import\n" );
98
99 }
100 echo( "\n" );
101
102 ?>