46 $userName = $this->
getOption(
'user',
false );
47 $summary = $this->
getOption(
'summary',
'Imported from text file' );
48 $useTimestamp = $this->
hasOption(
'use-timestamp' );
51 $overwrite = $this->
hasOption(
'overwrite' );
52 $prefix = $this->
getOption(
'prefix',
'' );
59 while ( $arg = $this->
getArg( $i++ ) ) {
60 if ( file_exists( $arg ) ) {
61 $files[$arg] = file_get_contents( $arg );
66 foreach ( glob( $arg ) as $filename ) {
68 $files[$filename] = file_get_contents( $filename );
71 $this->
fatalError(
"Fatal error: The file '$arg' does not exist!" );
76 $count = count( $files );
77 $this->
output(
"Importing $count pages...\n" );
79 if ( $userName ===
false ) {
80 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
82 $user = User::newFromName( $userName );
88 if ( $user->isAnon() ) {
89 $user->addToDatabase();
101 foreach ( $files as $file => $text ) {
102 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
105 $title = Title::newFromText( $pageName );
107 if ( !$title || $title->hasFragment() ) {
108 $this->
error(
"Invalid title $pageName. Skipping.\n" );
113 $exists = $title->exists();
114 $oldRevID = $title->getLatestRevID();
115 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) :
null;
116 $actualTitle = $title->getPrefixedText();
119 $touched =
wfTimestamp( TS::UNIX, $title->getTouched() );
121 $this->
output(
"Title $actualTitle already exists. Skipping.\n" );
124 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
125 $this->
output(
"File for title $actualTitle has not been modified since the " .
126 "destination page was touched. Skipping.\n" );
132 $content = ContentHandler::makeContent( rtrim( $text ), $title );
134 $rev->setContent( SlotRecord::MAIN, $content );
135 $rev->setTitle( $title );
136 $rev->setUserObj( $user );
137 $rev->setComment( $summary );
138 $rev->setTimestamp( $timestamp );
142 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
144 $this->
output(
"File for title $actualTitle contains no changes from the current " .
145 "revision. Skipping.\n" );
150 $status = $rev->importOldRevision();
151 $newId = $title->getLatestRevID();
154 $action = $exists ?
'updated' :
'created';
155 $this->
output(
"Successfully $action $actualTitle\n" );
158 $action = $exists ?
'update' :
'create';
159 $this->
output(
"Failed to $action $actualTitle\n" );
165 if ( $rc && $status ) {
167 if ( is_object( $oldRevRecord ) ) {
168 $recentChange = $recentChangeFactory->createEditRecentChange(
177 $oldRevRecord->getSize(),
184 $recentChangeFactory->insertRecentChange( $recentChange );
187 $recentChange = $recentChangeFactory->createNewPageRecentChange(
200 $recentChangeFactory->insertRecentChange( $recentChange );
205 $this->
output(
"Done! $successCount succeeded, $skipCount skipped.\n" );
207 $this->
fatalError(
"Import failed with $failCount failed pages.\n", $exit );