* Sync parserTests expected image results with r17524.
[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', 'norc' );
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 $wgUser =& $user;
57
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'] );
61 } else {
62 $comment = 'Importing text file';
63 }
64 echo( "Using edit summary '{$comment}'.\n" );
65
66 # Do we need to update recent changes?
67 if( isset( $options['norc'] ) && $options['norc'] ) {
68 $rc = false;
69 } else {
70 $rc = true;
71 }
72
73 # Attempt the insertion
74 echo( "Attempting to insert page..." );
75 $success = insertNewArticle( $title, $text, $user, $comment, $rc );
76 if( $success ) {
77 echo( "done.\n" );
78 } else {
79 echo( "failed. Title exists.\n" );
80 }
81
82 } else {
83 # Dud user
84 echo( "invalid username.\n" );
85 }
86
87 } else {
88 # Dud title
89 echo( "invalid title.\n" );
90 }
91
92 } else {
93 # File not found
94 echo( "not found.\n" );
95 }
96
97 } else {
98 # Show help
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" );
107
108 }
109 echo( "\n" );
110
111 ?>