PrefixSearch: Avoid notice when no subpage exists
[mediawiki.git] / maintenance / importTextFile.php
blobf73dd1c254cc081a3f86f177593aa5176f08b2a2
1 <?php
2 /**
3 * Create or edit pages using the contents of a text file.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
25 $options = array( 'help', 'nooverwrite', 'norc' );
26 $optionsWithArgs = array( 'title', 'user', 'comment' );
27 require_once __DIR__ . '/commandLine.inc';
28 echo "Import Text File\n\n";
30 if ( count( $args ) < 1 || isset( $options['help'] ) ) {
31 showHelp();
32 } else {
34 $filename = $args[0];
35 echo "Using {$filename}...";
36 if ( is_file( $filename ) ) {
38 $title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
39 $title = Title::newFromText( $title );
41 if ( is_object( $title ) ) {
43 echo "\nUsing title '" . $title->getPrefixedText() . "'...";
44 if ( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
45 RequestContext::getMain()->setTitle( $title );
47 $text = file_get_contents( $filename );
48 $user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
49 $user = User::newFromName( $user );
51 if ( is_object( $user ) ) {
53 echo "\nUsing username '" . $user->getName() . "'...";
54 $wgUser =& $user;
55 $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
56 $flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
58 echo "\nPerforming edit...";
59 $page = WikiPage::factory( $title );
60 $content = ContentHandler::makeContent( $text, $title );
61 $page->doEditContent( $content, $comment, $flags, false, $user );
62 echo "done.\n";
63 } else {
64 echo "invalid username.\n";
66 } else {
67 echo "page exists.\n";
69 } else {
70 echo "invalid title.\n";
72 } else {
73 echo "does not exist.\n";
77 function titleFromFilename( $filename ) {
78 $parts = explode( '/', $filename );
79 $parts = explode( '.', $parts[count( $parts ) - 1] );
81 return $parts[0];
84 function showHelp() {
85 print <<<EOF
86 USAGE: php importTextFile.php <options> <filename>
88 <filename> : Path to the file containing page content to import
90 Options:
92 --title <title>
93 Title for the new page; default is to use the filename as a base
94 --user <user>
95 User to be associated with the edit
96 --comment <comment>
97 Edit summary
98 --nooverwrite
99 Don't overwrite existing content
100 --norc
101 Don't update recent changes
102 --help
103 Show this information
105 EOF;