59 $userName = $this->
getOption(
'user',
false );
60 $summary = $this->
getOption(
'summary',
'Imported from text file' );
61 $useTimestamp = $this->
hasOption(
'use-timestamp' );
64 $overwrite = $this->
hasOption(
'overwrite' );
65 $prefix = $this->
getOption(
'prefix',
'' );
72 while ( $arg = $this->
getArg( $i++ ) ) {
73 if ( file_exists( $arg ) ) {
74 $files[$arg] = file_get_contents( $arg );
79 foreach ( glob( $arg ) as $filename ) {
81 $files[$filename] = file_get_contents( $filename );
84 $this->
fatalError(
"Fatal error: The file '$arg' does not exist!" );
89 $count = count( $files );
90 $this->
output(
"Importing $count pages...\n" );
92 if ( $userName ===
false ) {
93 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
95 $user = User::newFromName( $userName );
101 if ( $user->isAnon() ) {
102 $user->addToDatabase();
112 foreach ( $files as $file => $text ) {
113 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
116 $title = Title::newFromText( $pageName );
118 if ( !$title || $title->hasFragment() ) {
119 $this->
error(
"Invalid title $pageName. Skipping.\n" );
124 $exists = $title->exists();
125 $oldRevID = $title->getLatestRevID();
126 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) :
null;
127 $actualTitle = $title->getPrefixedText();
130 $touched =
wfTimestamp( TS_UNIX, $title->getTouched() );
132 $this->
output(
"Title $actualTitle already exists. Skipping.\n" );
135 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
136 $this->
output(
"File for title $actualTitle has not been modified since the " .
137 "destination page was touched. Skipping.\n" );
143 $content = ContentHandler::makeContent( rtrim( $text ), $title );
145 $rev->setContent( SlotRecord::MAIN, $content );
146 $rev->setTitle( $title );
147 $rev->setUserObj( $user );
148 $rev->setComment( $summary );
149 $rev->setTimestamp( $timestamp );
153 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
155 $this->
output(
"File for title $actualTitle contains no changes from the current " .
156 "revision. Skipping.\n" );
161 $status = $rev->importOldRevision();
162 $newId = $title->getLatestRevID();
165 $action = $exists ?
'updated' :
'created';
166 $this->
output(
"Successfully $action $actualTitle\n" );
169 $action = $exists ?
'update' :
'create';
170 $this->
output(
"Failed to $action $actualTitle\n" );
176 if ( $rc && $status ) {
178 if ( is_object( $oldRevRecord ) ) {
179 RecentChange::notifyEdit(
186 $oldRevRecord->getTimestamp(),
189 $oldRevRecord->getSize(),
197 RecentChange::notifyNew(
213 $this->
output(
"Done! $successCount succeeded, $skipCount skipped.\n" );
215 $this->
fatalError(
"Import failed with $failCount failed pages.\n", $exit );