58 $userName = $this->
getOption(
'user',
false );
59 $summary = $this->
getOption(
'summary',
'Imported from text file' );
60 $useTimestamp = $this->
hasOption(
'use-timestamp' );
63 $overwrite = $this->
hasOption(
'overwrite' );
64 $prefix = $this->
getOption(
'prefix',
'' );
71 while ( $arg = $this->
getArg( $i++ ) ) {
72 if ( file_exists( $arg ) ) {
73 $files[$arg] = file_get_contents( $arg );
78 foreach ( glob( $arg ) as $filename ) {
80 $files[$filename] = file_get_contents( $filename );
83 $this->
fatalError(
"Fatal error: The file '$arg' does not exist!" );
88 $count = count( $files );
89 $this->
output(
"Importing $count pages...\n" );
91 if ( $userName ===
false ) {
92 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
94 $user = User::newFromName( $userName );
100 if ( $user->isAnon() ) {
101 $user->addToDatabase();
111 foreach ( $files as $file => $text ) {
112 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
115 $title = Title::newFromText( $pageName );
117 if ( !$title || $title->hasFragment() ) {
118 $this->
error(
"Invalid title $pageName. Skipping.\n" );
123 $exists = $title->exists();
124 $oldRevID = $title->getLatestRevID();
125 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) :
null;
126 $actualTitle = $title->getPrefixedText();
129 $touched =
wfTimestamp( TS_UNIX, $title->getTouched() );
131 $this->
output(
"Title $actualTitle already exists. Skipping.\n" );
134 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
135 $this->
output(
"File for title $actualTitle has not been modified since the " .
136 "destination page was touched. Skipping.\n" );
142 $content = ContentHandler::makeContent( rtrim( $text ), $title );
144 $rev->setContent( SlotRecord::MAIN, $content );
145 $rev->setTitle( $title );
146 $rev->setUserObj( $user );
147 $rev->setComment( $summary );
148 $rev->setTimestamp( $timestamp );
152 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
154 $this->
output(
"File for title $actualTitle contains no changes from the current " .
155 "revision. Skipping.\n" );
160 $status = $rev->importOldRevision();
161 $newId = $title->getLatestRevID();
164 $action = $exists ?
'updated' :
'created';
165 $this->
output(
"Successfully $action $actualTitle\n" );
168 $action = $exists ?
'update' :
'create';
169 $this->
output(
"Failed to $action $actualTitle\n" );
175 if ( $rc && $status ) {
177 if ( is_object( $oldRevRecord ) ) {
178 RecentChange::notifyEdit(
185 $oldRevRecord->getTimestamp(),
188 $oldRevRecord->getSize(),
196 RecentChange::notifyNew(
212 $this->
output(
"Done! $successCount succeeded, $skipCount skipped.\n" );
214 $this->
fatalError(
"Import failed with $failCount failed pages.\n", $exit );