45 $userName = $this->
getOption(
'user',
false );
46 $summary = $this->
getOption(
'summary',
'Imported from text file' );
47 $useTimestamp = $this->
hasOption(
'use-timestamp' );
50 $overwrite = $this->
hasOption(
'overwrite' );
51 $prefix = $this->
getOption(
'prefix',
'' );
58 while ( $arg = $this->
getArg( $i++ ) ) {
59 if ( file_exists( $arg ) ) {
60 $files[$arg] = file_get_contents( $arg );
65 foreach ( glob( $arg ) as $filename ) {
67 $files[$filename] = file_get_contents( $filename );
70 $this->
fatalError(
"Fatal error: The file '$arg' does not exist!" );
75 $count = count( $files );
76 $this->
output(
"Importing $count pages...\n" );
78 if ( $userName ===
false ) {
79 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
81 $user = User::newFromName( $userName );
87 if ( $user->isAnon() ) {
88 $user->addToDatabase();
100 foreach ( $files as $file => $text ) {
101 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
104 $title = Title::newFromText( $pageName );
106 if ( !$title || $title->hasFragment() ) {
107 $this->
error(
"Invalid title $pageName. Skipping.\n" );
112 $exists = $title->exists();
113 $oldRevID = $title->getLatestRevID();
114 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) :
null;
115 $actualTitle = $title->getPrefixedText();
118 $touched =
wfTimestamp( TS::UNIX, $title->getTouched() );
120 $this->
output(
"Title $actualTitle already exists. Skipping.\n" );
123 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
124 $this->
output(
"File for title $actualTitle has not been modified since the " .
125 "destination page was touched. Skipping.\n" );
131 $content = ContentHandler::makeContent( rtrim( $text ), $title );
132 $rev =
new WikiRevision();
133 $rev->setContent( SlotRecord::MAIN, $content );
134 $rev->setTitle( $title );
135 $rev->setUserObj( $user );
136 $rev->setComment( $summary );
137 $rev->setTimestamp( $timestamp );
141 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
143 $this->
output(
"File for title $actualTitle contains no changes from the current " .
144 "revision. Skipping.\n" );
149 $status = $rev->importOldRevision();
150 $newId = $title->getLatestRevID();
153 $action = $exists ?
'updated' :
'created';
154 $this->
output(
"Successfully $action $actualTitle\n" );
157 $action = $exists ?
'update' :
'create';
158 $this->
output(
"Failed to $action $actualTitle\n" );
164 if ( $rc && $status ) {
166 if ( is_object( $oldRevRecord ) ) {
167 $recentChange = $recentChangeFactory->createEditRecentChange(
176 $oldRevRecord->getSize(),
183 $recentChangeFactory->insertRecentChange( $recentChange );
186 $recentChange = $recentChangeFactory->createNewPageRecentChange(
199 $recentChangeFactory->insertRecentChange( $recentChange );
204 $this->
output(
"Done! $successCount succeeded, $skipCount skipped.\n" );
206 $this->
fatalError(
"Import failed with $failCount failed pages.\n", $exit );