From c812fa3cb3555af7bf3d9d377fcad5477a3a61d2 Mon Sep 17 00:00:00 2001 From: Andrew H Date: Thu, 7 Jan 2016 03:09:47 +0000 Subject: [PATCH] Add maintenance script importTextFiles.php importTextFiles.php can be used to import pages from text files containing wikitext. Also, added $userObj to WikiRevision so that it can accept a User object instead of just a username. This is a GCI task. Change-Id: I20eaf2005bdd3d041f55d8c0b108f001c064d638 --- autoload.php | 1 + includes/import/WikiRevision.php | 28 +++++-- maintenance/importTextFiles.php | 131 +++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 maintenance/importTextFiles.php diff --git a/autoload.php b/autoload.php index 5ee73a5068..1f8c93a85c 100644 --- a/autoload.php +++ b/autoload.php @@ -575,6 +575,7 @@ $wgAutoloadLocalClasses = array( 'ImportSource' => __DIR__ . '/includes/import/ImportSource.php', 'ImportStreamSource' => __DIR__ . '/includes/import/ImportStreamSource.php', 'ImportStringSource' => __DIR__ . '/includes/import/ImportStringSource.php', + 'ImportTextFiles' => __DIR__ . '/maintenance/importTextFiles.php', 'ImportTitleFactory' => __DIR__ . '/includes/title/ImportTitleFactory.php', 'IncludableSpecialPage' => __DIR__ . '/includes/specialpage/IncludableSpecialPage.php', 'IndexPager' => __DIR__ . '/includes/pager/IndexPager.php', diff --git a/includes/import/WikiRevision.php b/includes/import/WikiRevision.php index 8705bb01c8..d2cf7f6443 100644 --- a/includes/import/WikiRevision.php +++ b/includes/import/WikiRevision.php @@ -51,6 +51,9 @@ class WikiRevision { /** @var string */ public $user_text = ""; + /** @var User */ + public $userObj = null; + /** @var string */ public $model = null; @@ -154,6 +157,13 @@ class WikiRevision { $this->user_text = $user; } + /** + * @param User $user + */ + function setUserObj( $user ) { + $this->userObj = $user; + } + /** * @param string $ip */ @@ -296,6 +306,13 @@ class WikiRevision { return $this->user_text; } + /** + * @return User + */ + function getUserObj() { + return $this->userObj; + } + /** * @return string * @@ -446,15 +463,14 @@ class WikiRevision { $dbw = wfGetDB( DB_MASTER ); # Sneak a single revision into place - $user = User::newFromName( $this->getUser() ); + $user = $this->getUserObj() ?: User::newFromName( $this->getUser() ); if ( $user ) { $userId = intval( $user->getId() ); $userText = $user->getName(); - $userObj = $user; } else { $userId = 0; $userText = $this->getUser(); - $userObj = new User; + $user = new User; } // avoid memory leak...? @@ -534,7 +550,7 @@ class WikiRevision { // countable/oldcountable stuff is handled in WikiImporter::finishImportPage $page->doEditUpdates( $revision, - $userObj, + $user, array( 'created' => $created, 'oldcountable' => 'no-change' ) ); } @@ -545,7 +561,7 @@ class WikiRevision { function importLogItem() { $dbw = wfGetDB( DB_MASTER ); - $user = User::newFromName( $this->getUser() ); + $user = $this->getUserObj() ?: User::newFromName( $this->getUser() ); if ( $user ) { $userId = intval( $user->getId() ); $userText = $user->getName(); @@ -644,7 +660,7 @@ class WikiRevision { return false; } - $user = User::newFromName( $this->user_text ); + $user = $this->getUserObj() ?: User::newFromName( $this->getUser() ); # Do the actual upload if ( $archiveName ) { diff --git a/maintenance/importTextFiles.php b/maintenance/importTextFiles.php new file mode 100644 index 0000000000..b26eaa576c --- /dev/null +++ b/maintenance/importTextFiles.php @@ -0,0 +1,131 @@ +mDescription = "Reads in text files and imports their content to pages of the wiki"; + $this->addOption( 'user', 'Username to which edits should be attributed. ' . + 'Default: "Maintenance script"', false, true, 'u' ); + $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->addArg( 'titles', 'Titles of article to edit' ); + } + + public function execute() { + $userName = $this->getOption( 'user', false ); + $summary = $this->getOption( 'summary', 'Imported from text file' ); + $useTimestamp = $this->hasOption( 'use-timestamp' ); + + // Get all the arguments. A loop is required since Maintenance doesn't + // suppport an arbitrary number of arguments. + $files = array(); + $i = 0; + while ( $arg = $this->getArg( $i++ ) ) { + if ( file_exists( $arg ) ) { + $files[$arg] = file_get_contents( $arg ); + } else { + $this->error( "Fatal error: The file '$arg' does not exist!", 1 ); + } + }; + + $count = count( $files ); + $this->output( "Creating $count pages...\n" ); + + if ( $userName === false ) { + $user = User::newSystemUser( 'Maintenance script', array( 'steal' => true ) ); + } else { + $user = User::newFromName( $userName ); + } + + if ( !$user ) { + $this->error( "Invalid username\n", true ); + } + if ( $user->isAnon() ) { + $user->addToDatabase(); + } + + $exit = 0; + + $successCount = 0; + $failCount = 0; + $skipCount = 0; + + foreach ( $files as $file => $text ) { + $pageName = pathinfo( $file, PATHINFO_FILENAME ); + $title = Title::newFromText( $pageName ); + 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; + } + + $actualTitle = $title->getPrefixedText(); + + $rev = new WikiRevision( ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) ); + $rev->setText( $text ); + $rev->setTitle( $title ); + $rev->setUserObj( $user ); + $rev->setComment( $summary ); + if ( $useTimestamp ) { + $rev->setTimestamp( wfTimestamp( TS_UNIX, filemtime( $file ) ) ); + } else { + $rev->setTimestamp( wfTimestampNow() ); + } + + $status = $rev->importOldRevision(); + if ( $status ) { + $this->output( "Successfully created $actualTitle\n" ); + $successCount++; + } else { + $actualTitle = $title->getPrefixedText(); + $this->output( "Failed to create $actualTitle\n" ); + $failCount++; + $exit = 1; + } + } + $this->output( "Done! $successCount successfully created, $skipCount skipped.\n" ); + if ( $exit ) { + $this->error( "Import failed with $failCount failed pages.\n", $exit ); + } + } +} + +$maintClass = "ImportTextFiles"; +require_once RUN_MAINTENANCE_IF_MAIN; -- 2.20.1