From eff2fa3b36e0488f636dffa92570ac6110f0c86b Mon Sep 17 00:00:00 2001 From: Rob Church Date: Fri, 14 Apr 2006 17:24:43 +0000 Subject: [PATCH] Maintenance script to import the contents of a text file into a wiki page --- maintenance/README | 3 + maintenance/importTextFile.inc | 52 +++++++++++++++++ maintenance/importTextFile.php | 102 +++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 maintenance/importTextFile.inc create mode 100644 maintenance/importTextFile.php diff --git a/maintenance/README b/maintenance/README index c9f92eef74..1b49ed3b3d 100644 --- a/maintenance/README +++ b/maintenance/README @@ -50,6 +50,9 @@ installations. importDump.php XML dump importer + + importTextFile.php + Imports the contents of a text file into a wiki page nukePage.php Wipe a page and all revisions from the database diff --git a/maintenance/importTextFile.inc b/maintenance/importTextFile.inc new file mode 100644 index 0000000000..7794827068 --- /dev/null +++ b/maintenance/importTextFile.inc @@ -0,0 +1,52 @@ + + */ + +/** + * Insert a new article + * + * @param $title Title of the article + * @param $text Text of the article + * @param $user User associated with the edit + * @param $comment Edit summary + * @return bool + */ +function insertNewArticle( &$title, $text, &$user, $comment ) { + if( !$title->exists() ) { + # Create the article + $dbw =& wfGetDB( DB_MASTER ); + $dbw->immediateBegin(); + $article = new Article( $title ); + $articleId = $article->insertOn( $dbw ); + # Prepare and save associated revision + $revision = new Revision( array( 'page' => $articleId, 'text' => $text, 'user' => $user->mId, 'user_text' => $user->getName(), 'comment' => $comment ) ); + $revisionId = $revision->insertOn( $dbw ); + # Make it the current revision + $article->updateRevisionOn( $dbw, $revision ); + $dbw->immediateCommit(); + return( true ); + } else { + # Title exists; touch nothing + return( false ); + } +} + +/** + * Turn a filename into a title + * + * @param $filename Filename to be transformed + * @return Title + */ +function titleFromFilename( $filename ) { + $parts = explode( '/', $filename ); + $parts = explode( '.', $parts[ count( $parts ) - 1 ] ); + return( Title::newFromText( $parts[0] ) ); +} + +?> \ No newline at end of file diff --git a/maintenance/importTextFile.php b/maintenance/importTextFile.php new file mode 100644 index 0000000000..c8b00cb2a3 --- /dev/null +++ b/maintenance/importTextFile.php @@ -0,0 +1,102 @@ + + */ + +$options = array( 'help' ); +$optionsWithArgs = array( 'title', 'user', 'comment' ); +require_once( 'commandLine.inc' ); +require_once( 'importTextFile.inc' ); +echo( "Import Text File\n\n" ); + +if( !isset( $options['help'] ) || !$options['help'] ) { + + # Check file existence + $filename = $args[0]; + echo( "Using file '{$filename}'..." ); + if( file_exists( $filename ) ) { + echo( "found.\n" ); + + # Work out the title for the page + if( isset( $option['title'] ) || trim( $options['title'] ) != '' ) { + $titleText = $options['title']; + # Use the supplied title + echo( "Using title '{$titleText}'..." ); + $title = Title::newFromText( $options['title'] ); + } else { + # Attempt to make a title out of the filename + echo( "Using title from filename..." ); + $title = titleFromFilename( $filename ); + } + + # Check the title's valid + if( !is_null( $title ) && is_object( $title ) ) { + echo( "ok.\n" ); + + # Read in the text + $text = file_get_contents( $filename ); + + # Check the supplied user and fall back to a default if needed + if( isset( $options['user'] ) && trim( $options['user'] ) != '' ) { + $username = $options['user']; + } else { + $username = 'MediaWiki default'; + } + echo( "Using user '{$username}'..." ); + $user = User::newFromName( $username ); + + # Check the user's valid + if( !is_null( $user ) && is_object( $user ) ) { + echo( "ok.\n" ); + + # If a comment was supplied, use it (replace _ with spaces ) else use a default + if( isset( $options['comment'] ) || trim( $options['comment'] != '' ) ) { + $comment = str_replace( '_', ' ', $options['comment'] ); + } else { + $comment = 'Importing text file'; + } + echo( "Using edit summary '{$comment}'.\n" ); + + # Attempt the insertion + echo( "Attempting to insert page..." ); + $success = insertNewArticle( $title, $text, $user, $comment ); + if( $success ) { + echo( "done.\n" ); + } else { + echo( "failed. Title exists.\n" ); + } + + } else { + # Dud user + echo( "invalid username.\n" ); + } + + } else { + # Dud title + echo( "invalid title.\n" ); + } + + } else { + # File not found + echo( "not found.\n" ); + } + +} else { + # Show help + echo( "Imports the contents of a text file into a wiki page.\n\n" ); + echo( "USAGE: php importTextFile.php [--help|--title |--user <user>|--comment <comment>] <filename>\n\n" ); + echo( " --help: Show this help information\n" ); + echo( " --title <title> : Title for the new page; if not supplied, the filename is used as a base for the title\n" ); + echo( " --user <user> : User to be associated with the edit; if not supplied, a default is used\n" ); + echo( "--comment <comment> : Edit summary to be associated with the edit; underscores are transformed into spaces; if not supplied, a default is used\n" ); + echo( " <filename> : Path to the file containing the wikitext to import\n" ); + +} +echo( "\n" ); + +?> \ No newline at end of file -- 2.20.1