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