54 $userName = $this->
getOption(
'user',
false );
55 $summary = $this->
getOption(
'summary',
'Imported from text file' );
56 $useTimestamp = $this->
hasOption(
'use-timestamp' );
59 $overwrite = $this->
hasOption(
'overwrite' );
60 $prefix = $this->
getOption(
'prefix',
'' );
66 while ( $arg = $this->
getArg( $i++ ) ) {
67 if ( file_exists( $arg ) ) {
68 $files[$arg] = file_get_contents( $arg );
73 foreach ( glob( $arg ) as $filename ) {
75 $files[$filename] = file_get_contents( $filename );
78 $this->
fatalError(
"Fatal error: The file '$arg' does not exist!" );
83 $count = count( $files );
84 $this->
output(
"Importing $count pages...\n" );
86 if ( $userName ===
false ) {
87 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
89 $user = User::newFromName( $userName );
95 if ( $user->isAnon() ) {
96 $user->addToDatabase();
106 foreach ( $files as
$file => $text ) {
107 $pageName = $prefix . pathinfo(
$file, PATHINFO_FILENAME );
110 $title = Title::newFromText( $pageName );
112 if ( !$title || $title->hasFragment() ) {
113 $this->
error(
"Invalid title $pageName. Skipping.\n" );
118 $exists = $title->exists();
119 $oldRevID = $title->getLatestRevID();
120 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) :
null;
121 $actualTitle = $title->getPrefixedText();
124 $touched =
wfTimestamp( TS_UNIX, $title->getTouched() );
126 $this->
output(
"Title $actualTitle already exists. Skipping.\n" );
129 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
130 $this->
output(
"File for title $actualTitle has not been modified since the " .
131 "destination page was touched. Skipping.\n" );
137 $content = ContentHandler::makeContent( rtrim( $text ), $title );
139 $rev->setContent( SlotRecord::MAIN,
$content );
140 $rev->setTitle( $title );
141 $rev->setUserObj( $user );
142 $rev->setComment( $summary );
143 $rev->setTimestamp( $timestamp );
147 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
149 $this->
output(
"File for title $actualTitle contains no changes from the current " .
150 "revision. Skipping.\n" );
155 $status = $rev->importOldRevision();
156 $newId = $title->getLatestRevID();
159 $action = $exists ?
'updated' :
'created';
160 $this->
output(
"Successfully $action $actualTitle\n" );
163 $action = $exists ?
'update' :
'create';
164 $this->
output(
"Failed to $action $actualTitle\n" );
170 if ( $rc && $status ) {
172 if ( is_object( $oldRevRecord ) ) {
173 RecentChange::notifyEdit(
180 $oldRevRecord->getTimestamp(),
183 $oldRevRecord->getSize(),
191 RecentChange::notifyNew(
207 $this->
output(
"Done! $successCount succeeded, $skipCount skipped.\n" );
209 $this->
fatalError(
"Import failed with $failCount failed pages.\n", $exit );