X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2FimportTextFiles.php;h=c99aa155313b9a7321bda55aa57ca1853a6acb7f;hb=2ec769193eec7084c49ed4b4de04fbf023cb5d91;hp=14d8420ce41d7bfa31703c7d9991042452b0c494;hpb=0202c55e3a9460c814ddb9732fdf589d5e3bb773;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/importTextFiles.php b/maintenance/importTextFiles.php index 14d8420ce4..c99aa15531 100644 --- a/maintenance/importTextFiles.php +++ b/maintenance/importTextFiles.php @@ -21,6 +21,8 @@ * @ingroup Maintenance */ +use MediaWiki\MediaWikiServices; + require_once __DIR__ . '/Maintenance.php'; /** @@ -32,7 +34,7 @@ require_once __DIR__ . '/Maintenance.php'; class ImportTextFiles extends Maintenance { public function __construct() { parent::__construct(); - $this->mDescription = "Reads in text files and imports their content to pages of the wiki"; + $this->addDescription( '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' ); @@ -56,14 +58,23 @@ class ImportTextFiles extends Maintenance { $prefix = $this->getOption( 'prefix', '' ); // Get all the arguments. A loop is required since Maintenance doesn't - // suppport an arbitrary number of arguments. - $files = array(); + // support an arbitrary number of arguments. + $files = []; $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 ); + // use glob to support the Windows shell, which doesn't automatically + // expand wildcards + $found = false; + foreach ( glob( $arg ) as $filename ) { + $found = true; + $files[$filename] = file_get_contents( $filename ); + } + if ( !$found ) { + $this->fatalError( "Fatal error: The file '$arg' does not exist!" ); + } } }; @@ -71,13 +82,13 @@ class ImportTextFiles extends Maintenance { $this->output( "Importing $count pages...\n" ); if ( $userName === false ) { - $user = User::newSystemUser( 'Maintenance script', array( 'steal' => true ) ); + $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] ); } else { $user = User::newFromName( $userName ); } if ( !$user ) { - $this->error( "Invalid username\n", true ); + $this->fatalError( "Invalid username\n" ); } if ( $user->isAnon() ) { $user->addToDatabase(); @@ -94,16 +105,16 @@ class ImportTextFiles extends Maintenance { $timestamp = $useTimestamp ? wfTimestamp( TS_UNIX, filemtime( $file ) ) : wfTimestampNow(); $title = Title::newFromText( $pageName ); - $exists = $title->exists(); - $oldRevID = $title->getLatestRevID(); - $oldRev = $oldRevID ? Revision::newFromId( $oldRevID ) : null; - - if ( !$title ) { + // Have to check for # manually, since it gets interpreted as a fragment + if ( !$title || $title->hasFragment() ) { $this->error( "Invalid title $pageName. Skipping.\n" ); $skipCount++; continue; } + $exists = $title->exists(); + $oldRevID = $title->getLatestRevID(); + $oldRev = $oldRevID ? Revision::newFromId( $oldRevID ) : null; $actualTitle = $title->getPrefixedText(); if ( $exists ) { @@ -120,7 +131,7 @@ class ImportTextFiles extends Maintenance { } } - $rev = new WikiRevision( ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) ); + $rev = new WikiRevision( MediaWikiServices::getInstance()->getMainConfig() ); $rev->setText( rtrim( $text ) ); $rev->setTitle( $title ); $rev->setUserObj( $user ); @@ -188,10 +199,10 @@ class ImportTextFiles extends Maintenance { $this->output( "Done! $successCount succeeded, $skipCount skipped.\n" ); if ( $exit ) { - $this->error( "Import failed with $failCount failed pages.\n", $exit ); + $this->fatalError( "Import failed with $failCount failed pages.\n", $exit ); } } } -$maintClass = "ImportTextFiles"; +$maintClass = ImportTextFiles::class; require_once RUN_MAINTENANCE_IF_MAIN;