User:Bilardi/ImportTextFile.php

My revision about importTextFile.php.

<?php

/**
 * Maintenance script allows creating or editing pages using
 * the contents of a text files
 *
 * @file
 * @ingroup Maintenance
 * @author Rob Church <robchur@gmail.com>, revised by Alessandra Bilardi <alessandra.bilardi@gmail.com>
 */

$options = array( 'help', 'nooverwrite', 'norc', 'morepages', 'fileslist' ); 
$optionsWithArgs = array( 'title', 'user', 'comment' );
require_once( 'commandLine.inc' );
$separator="<title>";
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 ) ) {
		$text = file_get_contents( $filename );
		$user = isset( $options['user'] ) ? $options['user'] : 'Maintenance scripts';
		$user = User::newFromName( $user );
		echo( "\nUsing username '" . $user->getName() . "'..." );
		if( is_object( $user ) ) {
			$wgUser =& $user;
			$comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file';
			$flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
			if (isset( $options['morepages']) && strstr( $text, $separator ) && !isset( $options['fileslist'])) {
				$pages = explode( $separator, $text );
				for ($i=1,$cnt=count($pages);$i<$cnt;$i+=2) {
					$title = $pages[$i];
					$text = $pages[$i+1];
					insertNewArticle( $title, $text, $comment, $flags );
				}
			} else if (isset( $options['fileslist']) && !strstr( $text, $separator ) && !isset( $options['morepages'])) {
				$pages = preg_split( "/\s+/", $text );
				for ($i=0,$cnt=count($pages);$pages[$i] && $i<$cnt;$i++) {
					$text = file_get_contents( $pages[$i]);
					$title = titleFromFilename($pages[$i]);
					insertNewArticle( $title, $text, $comment, $flags );
				}
			} else if (!strstr( $text, $separator ) && !isset( $options['morepages']) && !isset( $options['fileslist'])) {
				$title = isset( $options['title'] ) ? $options['title'] : titleFromFilename( 
$filename );
				insertNewArticle( $title, $text, $comment, $flags );
			} else {
				echo( "do you want use --morepages or --fileslist?\n" );
			}
		} else {
			echo( "invalid username.\n" );
		}
	} else {
		echo( "file does not exist.\n" );
	}

}

function insertNewArticle( $title, $text, $comment, $flags ) {
	$title = Title::newFromUrl( $title );
	echo( "\nUsing title '" . $title->getPrefixedText() . "'..." );
	if( is_object( $title ) ) {
		if( !$title->exists() || !isset( $options['nooverwrite'] ) ) {
			echo( "\nPerforming edit..." );
			$article = new Article( $title );
			$article->doEdit( $text, $comment, $flags );
			echo( "done.\n" );
		} else {
			echo( "page exists.\n" );
		}
	} else {
		echo( "invalid title.\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( "--morepages\n\t<filename> contents more wiki pages divide by <title>Title for the new page<title>\n" );
	echo( "--fileslist\n\t<filename> contents one file path for line\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" );
}