X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22calendrier%22%2C%22type=semaine%22%29%20.%20%22?a=blobdiff_plain;f=maintenance%2FimportTextFiles.php;h=88ee9d7ecdda9a416544a6b0b017a9bfb822b3f3;hb=b46368a94cc411b8de9a6ad41f3767e729251b5c;hp=b26eaa576c2253eb1d17332f3094ac75319c381b;hpb=3092b9bf8eb5a6822578362fc09a6f538bd0e07e;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/importTextFiles.php b/maintenance/importTextFiles.php index b26eaa576c..88ee9d7ecd 100644 --- a/maintenance/importTextFiles.php +++ b/maintenance/importTextFiles.php @@ -32,23 +32,32 @@ 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' ); $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' ); + $this->addOption( 'overwrite', 'Overwrite existing pages. If --use-timestamp is passed, 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( 'files', 'Files to import' ); } public function execute() { $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. - $files = array(); + // support an arbitrary number of arguments. + $files = []; $i = 0; while ( $arg = $this->getArg( $i++ ) ) { if ( file_exists( $arg ) ) { @@ -59,10 +68,10 @@ class ImportTextFiles extends Maintenance { }; $count = count( $files ); - $this->output( "Creating $count pages...\n" ); + $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 ); } @@ -81,46 +90,103 @@ class ImportTextFiles extends Maintenance { $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(); + $oldRev = $oldRevID ? Revision::newFromId( $oldRevID ) : null; + 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(); + if ( $exists ) { + $touched = wfTimestamp( TS_UNIX, $title->getTouched() ); + if ( !$overwrite ) { + $this->output( "Title $actualTitle already exists. Skipping.\n" ); + $skipCount++; + continue; + } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) { + $this->output( "File for title $actualTitle has not been modified since the " . + "destination page was touched. Skipping.\n" ); + $skipCount++; + continue; + } + } + $rev = new WikiRevision( ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) ); - $rev->setText( $text ); + $rev->setText( rtrim( $text ) ); $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 ); + + if ( $exists && $overwrite && $rev->getContent()->equals( $oldRev->getContent() ) ) { + $this->output( "File for title $actualTitle contains no changes from the current " . + "revision. Skipping.\n" ); + $skipCount++; + continue; } $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 ) { + 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 succeeded, $skipCount skipped.\n" ); if ( $exit ) { $this->error( "Import failed with $failCount failed pages.\n", $exit ); }