92c31fd0fa1aec22dfbfef7d8851982dcbdeadca
[lhc/web/wiklou.git] / maintenance / importTextFile.php
1 <?php
2
3 /**
4 * Maintenance script allows creating or editing pages using
5 * the contents of a text file
6 *
7 * @package MediaWiki
8 * @subpackage Maintenance
9 * @author Rob Church <robchur@gmail.com>
10 */
11
12 $options = array( 'help', 'nooverwrite' );
13 $optionsWithArgs = array( 'title', 'user', 'comment' );
14 require_once( 'commandLine.inc' );
15 echo( "Import Text File\n\n" );
16
17 if( isset( $options['help'] ) ) {
18 showHelp();
19 } else {
20
21 $filename = $args[0];
22 echo( "Using {$filename}..." );
23 if( is_file( $filename ) ) {
24
25 $title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
26 $title = Title::newFromUrl( $title );
27 echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
28
29 if( is_object( $title ) ) {
30
31 if( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
32
33 $text = file_get_contents( $filename );
34 $user = isset( $options['user'] ) ? $options['user'] : 'MediaWiki default';
35 $user = User::newFromName( $user );
36 echo( "\nUsing username '" . $user->getName() . "'..." );
37
38 if( is_object( $user ) ) {
39
40 $wgUser =& $user;
41 $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
42 $comment = str_replace( '_', ' ', $comment );
43
44 echo( "\nPerforming edit..." );
45 $article = new Article( $title );
46 $article->doEdit( $text, $comment );
47 echo( "done.\n" );
48
49 } else {
50 echo( "invalid username.\n" );
51 }
52
53 } else {
54 echo( "page exists.\n" );
55 }
56
57 } else {
58 echo( "invalid title.\n" );
59 }
60
61 } else {
62 echo( "does not exist.\n" );
63 }
64
65 }
66
67 function titleFromFilename( $filename ) {
68 $parts = explode( '/', $filename );
69 $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
70 return $parts[0];
71 }
72
73 function showHelp() {
74 echo( "Import the contents of a text file into a wiki page.\n\n" );
75 echo( "USAGE: php importTextFile.php [--help|--title <title>|--user <user>|--comment <comment>|--nooverwrite] <filename>\n\n" );
76 echo( " --help: Show this help information\n" );
77 echo( " --title <title> : Title for the new page; if not supplied, the filename is used as a base for the title\n" );
78 echo( " --user <user> : User to be associated with the edit; if not supplied, a default is used\n" );
79 echo( "--comment <comment> : Edit summary to be associated with the edit; underscores are transformed into spaces; if not supplied, a default is used\n" );
80 echo( " --nooverwrite : Don't overwrite existing page content\n" );
81 echo( " <filename> : Path to the file containing the wikitext to import\n\n" );
82 }
83
84 ?>