using .append() instead of innerHTML+=. The latter replaces the html and causes all...
[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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 * @author Rob Church <robchur@gmail.com>
25 */
26
27 $options = array( 'help', 'nooverwrite', 'norc' );
28 $optionsWithArgs = array( 'title', 'user', 'comment' );
29 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
30 echo( "Import Text File\n\n" );
31
32 if ( count( $args ) < 1 || isset( $options['help'] ) ) {
33 showHelp();
34 } else {
35
36 $filename = $args[0];
37 echo( "Using {$filename}..." );
38 if ( is_file( $filename ) ) {
39
40 $title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
41 $title = Title::newFromURL( $title );
42
43 if ( is_object( $title ) ) {
44
45 echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
46 if ( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
47
48 $text = file_get_contents( $filename );
49 $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
50 $user = User::newFromName( $user );
51
52 if ( is_object( $user ) ) {
53
54 echo( "\nUsing username '" . $user->getName() . "'..." );
55 $wgUser =& $user;
56 $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
57 $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
58
59 echo( "\nPerforming edit..." );
60 $article = new Article( $title );
61 $article->doEdit( $text, $comment, $flags );
62 echo( "done.\n" );
63
64 } else {
65 echo( "invalid username.\n" );
66 }
67
68 } else {
69 echo( "page exists.\n" );
70 }
71
72 } else {
73 echo( "invalid title.\n" );
74 }
75
76 } else {
77 echo( "does not exist.\n" );
78 }
79
80 }
81
82 function titleFromFilename( $filename ) {
83 $parts = explode( '/', $filename );
84 $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
85 return $parts[0];
86 }
87
88 function showHelp() {
89 print <<<EOF
90 USAGE: php importTextFile.php <options> <filename>
91
92 <filename> : Path to the file containing page content to import
93
94 Options:
95
96 --title <title>
97 Title for the new page; default is to use the filename as a base
98 --user <user>
99 User to be associated with the edit
100 --comment <comment>
101 Edit summary
102 --nooverwrite
103 Don't overwrite existing content
104 --norc
105 Don't update recent changes
106 --help
107 Show this information
108
109 EOF;
110 }