Unicornisaurous has uploaded a new change for review.
https://gerrit.wikimedia.org/r/263234
Change subject: Add parameters to importTextFiles.php
......................................................................
Add parameters to importTextFiles.php
- Add --bot to mark edits as bot edits when --rc is specified
- Add --overwrite to overwrite existing pages. This respects
the --use-timestamp option by only overwriting if the file is newer
than the latest revision on the destination page
- Add --prefix for specifying a prefix
- Add --rc to add an edit entry to Special:RecentChanges
This is a GCI task.
Change-Id: I5acf829409853e2b311ae6c1c75a009fef91ceeb
---
M maintenance/importTextFiles.php
1 file changed, 75 insertions(+), 17 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/34/263234/1
diff --git a/maintenance/importTextFiles.php b/maintenance/importTextFiles.php
index b26eaa5..6ba20ff 100644
--- a/maintenance/importTextFiles.php
+++ b/maintenance/importTextFiles.php
@@ -38,6 +38,11 @@
$this->addOption( 'summary', 'Specify edit summary for the
edits', false, true, 's' );
$this->addOption( 'use-timestamp', 'Use the modification date
of the text file ' .
'as the timestamp for the edit' );
+ $this->addOption( 'overwrite', 'Should existing pages be
overwritten? This will only ' .
+ 'overwrite pages if the file has been modified since
the page was last modified.' );
+ $this->addOption( 'prefix', 'A string to place in front of the
file name', false, true, 'p' );
+ $this->addOption( 'bot', 'Mark edits as bot edits in the recent
changes list.' );
+ $this->addOption( 'rc', 'Place revisions in RecentChanges.' );
$this->addArg( 'titles', 'Titles of article to edit' );
}
@@ -45,6 +50,10 @@
$userName = $this->getOption( 'user', false );
$summary = $this->getOption( 'summary', 'Imported from text
file' );
$useTimestamp = $this->hasOption( 'use-timestamp' );
+ $rc = $this->hasOption( 'rc' );
+ $bot = $this->hasOption( 'bot' );
+ $overwrite = $this->hasOption( 'overwrite' );
+ $prefix = $this->getOption( 'prefix', '' );
// Get all the arguments. A loop is required since Maintenance
doesn't
// suppport an arbitrary number of arguments.
@@ -59,7 +68,7 @@
};
$count = count( $files );
- $this->output( "Creating $count pages...\n" );
+ $this->output( "Creating/Updating $count pages...\n" );
if ( $userName === false ) {
$user = User::newSystemUser( 'Maintenance script',
array( 'steal' => true ) );
@@ -81,21 +90,32 @@
$skipCount = 0;
foreach ( $files as $file => $text ) {
- $pageName = pathinfo( $file, PATHINFO_FILENAME );
+ $pageName = $prefix . pathinfo( $file,
PATHINFO_FILENAME );
+ $timestamp = $useTimestamp ? wfTimestamp( TS_UNIX,
filemtime( $file ) ) : wfTimestampNow();
$title = Title::newFromText( $pageName );
+ $exists = $title->exists();
+ $oldRevID = $title->getLatestRevID();
if ( !$title ) {
$this->error( "Invalid title $pageName.
Skipping.\n" );
$skipCount++;
continue;
}
- if ( $title->exists() ) {
- $actualTitle = $title->getPrefixedText();
- $this->output( "Title $pageName already exists.
Skipping.\n" );
- $skipCount++;
- continue;
+ if ( $exists && ( !$overwrite || $title->getTouched() )
) {
+ $touched = wfTimestamp( TS_UNIX,
$title->getTouched() );
+ if ( !$overwrite ) {
+ $actualTitle =
$title->getPrefixedText();
+ $this->output( "Title $pageName already
exists. Skipping.\n" );
+ $skipCount++;
+ continue;
+ } elseif ( $useTimestamp && intval( $touched )
>= intval( $timestamp ) ) {
+ $actualTitle =
$title->getPrefixedText();
+ $this->output( "File for title
$pageName has not been modified since the " .
+ "destination page was touched.
Skipping.\n" );
+ $skipCount++;
+ continue;
+ }
}
-
$actualTitle = $title->getPrefixedText();
$rev = new WikiRevision(
ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
@@ -103,24 +123,62 @@
$rev->setTitle( $title );
$rev->setUserObj( $user );
$rev->setComment( $summary );
- if ( $useTimestamp ) {
- $rev->setTimestamp( wfTimestamp( TS_UNIX,
filemtime( $file ) ) );
- } else {
- $rev->setTimestamp( wfTimestampNow() );
- }
+ $rev->setTimestamp( $timestamp );
$status = $rev->importOldRevision();
+ $newId = $title->getLatestRevID();
+
if ( $status ) {
- $this->output( "Successfully created
$actualTitle\n" );
+ $action = $exists ? 'updated' : 'created';
+ $this->output( "Successfully $action
$actualTitle\n" );
$successCount++;
} else {
- $actualTitle = $title->getPrefixedText();
- $this->output( "Failed to create
$actualTitle\n" );
+ $action = $exists ? 'update' : 'create';
+ $this->output( "Failed to $action
$actualTitle\n" );
$failCount++;
$exit = 1;
}
+
+ // Create the RecentChanges entry if necessary
+ if ( $rc && $status ) {
+ if ( $exists ) {
+ $oldRev = Revision::newFromId(
$oldRevID );
+ if ( is_object( $oldRev ) ) {
+ $oldContent =
$oldRev->getContent();
+ RecentChange::notifyEdit(
+ $timestamp,
+ $title,
+ $rev->getMinor(),
+ $user,
+ $summary,
+ $oldRevID,
+ $oldRev->getTimestamp(),
+ $bot,
+ '',
+ $oldContent ?
$oldContent->getSize() : 0,
+
$rev->getContent()->getSize(),
+ $newId,
+ 1 /* the pages don't
need to be patrolled */
+ );
+ }
+ } else {
+ RecentChange::notifyNew(
+ $timestamp,
+ $title,
+ $rev->getMinor(),
+ $user,
+ $summary,
+ $bot,
+ '',
+ $rev->getContent()->getSize(),
+ $newId,
+ 1
+ );
+ }
+ }
}
- $this->output( "Done! $successCount successfully created,
$skipCount skipped.\n" );
+
+ $this->output( "Done! $successCount succeeeded, $skipCount
skipped.\n" );
if ( $exit ) {
$this->error( "Import failed with $failCount failed
pages.\n", $exit );
}
--
To view, visit https://gerrit.wikimedia.org/r/263234
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5acf829409853e2b311ae6c1c75a009fef91ceeb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Unicornisaurous <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits