From 40a6749e49fca38953da0818ad07cd247b57ff94 Mon Sep 17 00:00:00 2001 From: Rob Church Date: Sat, 2 Dec 2006 22:16:21 +0000 Subject: [PATCH] Rewrite the importTextFile.php script; remove un-needed include file, use Article::doEdit() to do the work, and allow overwriting to occur. Fixes these bugs: * (bug 7461) Allow overwriting pages using importTextFile.php * (bug 7946) importTextFile.php doesn't perform pre-save transform --- RELEASE-NOTES | 4 +- maintenance/importTextFile.inc | 75 ------------- maintenance/importTextFile.php | 193 ++++++++++++++------------------- 3 files changed, 86 insertions(+), 186 deletions(-) delete mode 100644 maintenance/importTextFile.inc diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 99d3841179..ea89aa74ec 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -234,7 +234,9 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * Added 'EditPage::attemptSave' hook before an article is saved. * (bug 8083) Applied patch for sk localisation * Add a backslash character to the edit token, to prevent edits via certain - broken proxies that mangle such characters in form submissions. + broken proxies that mangle such characters in form submissions +* (bug 7461) Allow overwriting pages using importTextFile.php +* (bug 7946) importTextFile.php doesn't perform pre-save transform == Languages updated == diff --git a/maintenance/importTextFile.inc b/maintenance/importTextFile.inc deleted file mode 100644 index 50b936c1f3..0000000000 --- a/maintenance/importTextFile.inc +++ /dev/null @@ -1,75 +0,0 @@ - - */ - -require_once( "$IP/includes/RecentChange.php" ); - -/** - * 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 - * @param $rc Whether or not to add a recent changes event - * @return bool - */ -function insertNewArticle( &$title, $text, &$user, $comment, $rc ) { - 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(); - # Update recent changes if appropriate - if( $rc ) - updateRecentChanges( $dbw, $title, $user, $comment, strlen( $text ), $articleId ); - # Touch links etc. - Article::onArticleCreate( $title ); - $article->editUpdates( $text, $comment, false, $dbw->timestamp(), $revisionId ); - 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] ); -} - -/** - * Update recent changes with the page creation event - * - * @param $dbw Database in use - * @param $title Title of the new page - * @param $user User responsible for the creation - * @param $comment Edit summary associated with the edit - * @param $size Size of the page - * @param $articleId Article identifier - */ -function updateRecentChanges( &$dbw, &$title, &$user, $comment, $size, $articleId ) { - RecentChange::notifyNew( $dbw->timestamp(), $title, false, $user, $comment, 'default', '', $size, $articleId ); -} - -?> \ No newline at end of file diff --git a/maintenance/importTextFile.php b/maintenance/importTextFile.php index 625763be29..a29d87967f 100644 --- a/maintenance/importTextFile.php +++ b/maintenance/importTextFile.php @@ -1,111 +1,84 @@ - - */ - -$options = array( 'help', 'norc' ); -$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" ); - $wgUser =& $user; - - # 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" ); - - # Do we need to update recent changes? - if( isset( $options['norc'] ) && $options['norc'] ) { - $rc = false; - } else { - $rc = true; - } - - # Attempt the insertion - echo( "Attempting to insert page..." ); - $success = insertNewArticle( $title, $text, $user, $comment, $rc ); - 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>|--norc] <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( " --norc : Do not add a page creation event to recent changes\n" ); - -} -echo( "\n" ); - +<?php + +/** + * Maintenance script allows creating or editing pages using + * the contents of a text file + * + * @package MediaWiki + * @subpackage Maintenance + * @author Rob Church <robchur@gmail.com> + */ + +$options = array( 'help', 'nooverwrite' ); +$optionsWithArgs = array( 'title', 'user', 'comment' ); +require_once( 'commandLine.inc' ); +echo( "Import Text File\n\n" ); + +if( 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 ); + echo( "\nUsing title '" . $title->getPrefixedText() . "'..." ); + + if( is_object( $title ) ) { + + if( !$title->exists() || !isset( $options['nooverwrite'] ) ) { + + $text = file_get_contents( $filename ); + $user = isset( $options['user'] ) ? $options['user'] : 'MediaWiki default'; + $user = User::newFromName( $user ); + echo( "\nUsing username '" . $user->getName() . "'..." ); + + if( is_object( $user ) ) { + + $wgUser =& $user; + $comment = isset( $options['comment'] ) ? $options['comment'] : 'Importing text file'; + $comment = str_replace( '_', ' ', $comment ); + + echo( "\nPerforming edit..." ); + $article = new Article( $title ); + $article->doEdit( $text, $comment ); + 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\n" ); + echo( "USAGE: php importTextFile.php [--help|--title <title>|--user <user>|--comment <comment>|--nooverwrite] <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( " --nooverwrite : Don't overwrite existing page content\n" ); + echo( " <filename> : Path to the file containing the wikitext to import\n\n" ); +} + ?> \ No newline at end of file -- 2.20.1