]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/importTextFile.php
MediaWiki 1.15.1
[autoinstallsdev/mediawiki.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  * @file
8  * @ingroup Maintenance
9  * @author Rob Church <robchur@gmail.com>
10  */
11
12 $options = array( 'help', 'nooverwrite', 'norc' );
13 $optionsWithArgs = array( 'title', 'user', 'comment' );
14 require_once( 'commandLine.inc' );
15 echo( "Import Text File\n\n" );
16
17 if( count( $args ) < 1 || 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
28                 if( is_object( $title ) ) {
29
30                         echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
31                         if( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
32
33                                 $text = file_get_contents( $filename );
34                                 $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
35                                 $user = User::newFromName( $user );
36
37                                 if( is_object( $user ) ) {
38
39                                         echo( "\nUsing username '" . $user->getName() . "'..." );
40                                         $wgUser =& $user;
41                                         $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
42                                         $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
43
44                                         echo( "\nPerforming edit..." );
45                                         $article = new Article( $title );
46                                         $article->doEdit( $text, $comment, $flags );
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 print <<<EOF
75 USAGE: php importTextFile.php <options> <filename>
76
77 <filename> : Path to the file containing page content to import
78
79 Options:
80
81 --title <title>
82         Title for the new page; default is to use the filename as a base
83 --user <user>
84         User to be associated with the edit
85 --comment <comment>
86         Edit summary
87 --nooverwrite
88         Don't overwrite existing content
89 --norc
90         Don't update recent changes
91 --help
92         Show this information
93
94 EOF;
95 }