56 $userName = $this->
getOption(
'user',
false );
57 $summary = $this->
getOption(
'summary',
'Imported from text file' );
58 $useTimestamp = $this->
hasOption(
'use-timestamp' );
61 $overwrite = $this->
hasOption(
'overwrite' );
62 $prefix = $this->
getOption(
'prefix',
'' );
69 while ( $arg = $this->
getArg( $i++ ) ) {
70 if ( file_exists( $arg ) ) {
71 $files[$arg] = file_get_contents( $arg );
76 foreach ( glob( $arg ) as $filename ) {
78 $files[$filename] = file_get_contents( $filename );
81 $this->
fatalError(
"Fatal error: The file '$arg' does not exist!" );
86 $count = count( $files );
87 $this->
output(
"Importing $count pages...\n" );
89 if ( $userName ===
false ) {
90 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
92 $user = User::newFromName( $userName );
98 if ( $user->isAnon() ) {
99 $user->addToDatabase();
109 foreach ( $files as $file => $text ) {
110 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
113 $title = Title::newFromText( $pageName );
115 if ( !$title || $title->hasFragment() ) {
116 $this->
error(
"Invalid title $pageName. Skipping.\n" );
121 $exists = $title->exists();
122 $oldRevID = $title->getLatestRevID();
123 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) :
null;
124 $actualTitle = $title->getPrefixedText();
127 $touched =
wfTimestamp( TS_UNIX, $title->getTouched() );
129 $this->
output(
"Title $actualTitle already exists. Skipping.\n" );
132 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
133 $this->
output(
"File for title $actualTitle has not been modified since the " .
134 "destination page was touched. Skipping.\n" );
140 $content = ContentHandler::makeContent( rtrim( $text ), $title );
142 $rev->setContent( SlotRecord::MAIN, $content );
143 $rev->setTitle( $title );
144 $rev->setUserObj( $user );
145 $rev->setComment( $summary );
146 $rev->setTimestamp( $timestamp );
150 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
152 $this->
output(
"File for title $actualTitle contains no changes from the current " .
153 "revision. Skipping.\n" );
158 $status = $rev->importOldRevision();
159 $newId = $title->getLatestRevID();
162 $action = $exists ?
'updated' :
'created';
163 $this->
output(
"Successfully $action $actualTitle\n" );
166 $action = $exists ?
'update' :
'create';
167 $this->
output(
"Failed to $action $actualTitle\n" );
173 if ( $rc && $status ) {
175 if ( is_object( $oldRevRecord ) ) {
176 RecentChange::notifyEdit(
183 $oldRevRecord->getTimestamp(),
186 $oldRevRecord->getSize(),
194 RecentChange::notifyNew(
210 $this->
output(
"Done! $successCount succeeded, $skipCount skipped.\n" );
212 $this->
fatalError(
"Import failed with $failCount failed pages.\n", $exit );