summaryrefslogtreecommitdiff
path: root/maintenance/formatInstallDoc.php
blob: b3bb50ca1726a4d3782de06fd8f0689910d8bfbd (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
<?php
/**
 * @ingroup Maintenance
 */

require_once( dirname( __FILE__ ) .'/Maintenance.php' );

class MaintenanceFormatInstallDoc extends Maintenance {
	function __construct() {
		parent::__construct();
		$this->addArg( 'path', 'The file name to format', false );
		$this->addOption( 'outfile', 'The output file name', false, true );
		$this->addOption( 'html', 'Use HTML output format. By default, wikitext is used.' );
	}

	function execute() {
		if ( $this->hasArg( 0 ) ) {
			$fileName = $this->getArg( 0 );
			$inFile = fopen( $fileName, 'r' );
			if ( !$inFile ) {
				$this->error( "Unable to open input file \"$fileName\"" );
				exit( 1 );
			}
		} else {
			$inFile = STDIN;
		}

		if ( $this->hasOption( 'outfile' ) ) {
			$fileName = $this->getOption( 'outfile' );
			$outFile = fopen( $fileName, 'w' );
			if ( !$outFile ) {
				$this->error( "Unable to open output file \"$fileName\"" );
				exit( 1 );
			}
		} else {
			$outFile = STDOUT;
		}

		$inText = stream_get_contents( $inFile );
		$outText = InstallDocFormatter::format( $inText );

		if ( $this->hasOption( 'html' ) ) {
			global $wgParser;
			$opt = new ParserOptions;
			$title = Title::newFromText( 'Text file' );
			$out = $wgParser->parse( $outText, $title, $opt );
			$outText = "<html><body>\n" . $out->getText() . "\n</body></html>\n";
		}

		fwrite( $outFile, $outText );
	}
}

$maintClass = 'MaintenanceFormatInstallDoc';
require_once( RUN_MAINTENANCE_IF_MAIN );