summaryrefslogtreecommitdiff
path: root/maintenance/importTextFile.php
blob: 0a0068d7f372ae6a4f7b2b5fcf343b51582e2d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

/**
 * Maintenance script allows creating or editing pages using
 * the contents of a text file
 *
 * @file
 * @ingroup Maintenance
 * @author Rob Church <robchur@gmail.com>
 */

$options = array( 'help', 'nooverwrite', 'norc' ); 
$optionsWithArgs = array( 'title', 'user', 'comment' );
require_once( 'commandLine.inc' );
echo( "Import Text File\n\n" );

if( count( $args ) < 1 || isset( $options['help'] ) ) {
	showHelp();
} else {

	$filename = $args[0];
	echo( "Using {$filename}..." );
	if( is_file( $filename ) ) {
		
		$title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( $filename );
		$title = Title::newFromUrl( $title );
		
		if( is_object( $title ) ) {
			
			echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
			if( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
			
				$text = file_get_contents( $filename );
				$user = isset( $options['user'] ) ? $options['user'] : 'Maintenance script';
				$user = User::newFromName( $user );
				
				if( is_object( $user ) ) {
				
					echo( "\nUsing username '" . $user->getName() . "'..." );
					$wgUser =& $user;
					$comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
					$flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
					
					echo( "\nPerforming edit..." );
					$article = new Article( $title );
					$article->doEdit( $text, $comment, $flags );
					echo( "done.\n" );
				
				} else {
					echo( "invalid username.\n" );
				}
			
			} else {
				echo( "page exists.\n" );
			}
			
		} else {
			echo( "invalid title.\n" );
		}
		
	} else {
		echo( "does not exist.\n" );
	}

}

function titleFromFilename( $filename ) {
	$parts = explode( '/', $filename );
	$parts = explode( '.', $parts[ count( $parts ) - 1 ] );
	return $parts[0];
}

function showHelp() {
	echo( "Import the contents of a text file into a wiki page.\n" );
	echo( "USAGE: php importTextFile.php <options> <filename>\n\n" );
	echo( "<filename> : Path to the file containing page content to import\n\n" );
	echo( "Options:\n\n" );
	echo( "--title <title>\n\tTitle for the new page; default is to use the filename as a base\n" );
	echo( "--user <user>\n\tUser to be associated with the edit\n" );
	echo( "--comment <comment>\n\tEdit summary\n" );
	echo( "--nooverwrite\n\tDon't overwrite existing content\n" );
	echo( "--norc\n\tDon't update recent changes\n" );
	echo( "--help\n\tShow this information\n" );
	echo( "\n" );
}