MediaWiki master
importTextFiles.php
Go to the documentation of this file.
1<?php
30
31// @codeCoverageIgnoreStart
32require_once __DIR__ . '/Maintenance.php';
33// @codeCoverageIgnoreEnd
34
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Reads in text files and imports their content to pages of the wiki' );
45 $this->addOption( 'user', 'Username to which edits should be attributed. ' .
46 'Default: "Maintenance script"', false, true, 'u' );
47 $this->addOption( 'summary', 'Specify edit summary for the edits', false, true, 's' );
48 $this->addOption( 'use-timestamp', 'Use the modification date of the text file ' .
49 'as the timestamp for the edit' );
50 $this->addOption( 'overwrite', 'Overwrite existing pages. If --use-timestamp is passed, this ' .
51 'will only overwrite pages if the file has been modified since the page was last modified.' );
52 $this->addOption( 'prefix', 'A string to place in front of the file name', false, true, 'p' );
53 $this->addOption( 'bot', 'Mark edits as bot edits in the recent changes list.' );
54 $this->addOption( 'rc', 'Place revisions in RecentChanges.' );
55 $this->addArg( 'files', 'Files to import' );
56 }
57
58 public function execute() {
59 $userName = $this->getOption( 'user', false );
60 $summary = $this->getOption( 'summary', 'Imported from text file' );
61 $useTimestamp = $this->hasOption( 'use-timestamp' );
62 $rc = $this->hasOption( 'rc' );
63 $bot = $this->hasOption( 'bot' );
64 $overwrite = $this->hasOption( 'overwrite' );
65 $prefix = $this->getOption( 'prefix', '' );
66
67 // Get all the arguments. A loop is required since Maintenance doesn't
68 // support an arbitrary number of arguments.
69 $files = [];
70 $i = 0;
71 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
72 while ( $arg = $this->getArg( $i++ ) ) {
73 if ( file_exists( $arg ) ) {
74 $files[$arg] = file_get_contents( $arg );
75 } else {
76 // use glob to support the Windows shell, which doesn't automatically
77 // expand wildcards
78 $found = false;
79 foreach ( glob( $arg ) as $filename ) {
80 $found = true;
81 $files[$filename] = file_get_contents( $filename );
82 }
83 if ( !$found ) {
84 $this->fatalError( "Fatal error: The file '$arg' does not exist!" );
85 }
86 }
87 }
88
89 $count = count( $files );
90 $this->output( "Importing $count pages...\n" );
91
92 if ( $userName === false ) {
93 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
94 } else {
95 $user = User::newFromName( $userName );
96 }
97
98 if ( !$user ) {
99 $this->fatalError( "Invalid username\n" );
100 }
101 if ( $user->isAnon() ) {
102 $user->addToDatabase();
103 }
104
105 $exit = 0;
106
107 $successCount = 0;
108 $failCount = 0;
109 $skipCount = 0;
110
111 $revLookup = $this->getServiceContainer()->getRevisionLookup();
112 foreach ( $files as $file => $text ) {
113 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
114 $timestamp = $useTimestamp ? wfTimestamp( TS_UNIX, filemtime( $file ) ) : wfTimestampNow();
115
116 $title = Title::newFromText( $pageName );
117 // Have to check for # manually, since it gets interpreted as a fragment
118 if ( !$title || $title->hasFragment() ) {
119 $this->error( "Invalid title $pageName. Skipping.\n" );
120 $skipCount++;
121 continue;
122 }
123
124 $exists = $title->exists();
125 $oldRevID = $title->getLatestRevID();
126 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) : null;
127 $actualTitle = $title->getPrefixedText();
128
129 if ( $exists ) {
130 $touched = wfTimestamp( TS_UNIX, $title->getTouched() );
131 if ( !$overwrite ) {
132 $this->output( "Title $actualTitle already exists. Skipping.\n" );
133 $skipCount++;
134 continue;
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" );
138 $skipCount++;
139 continue;
140 }
141 }
142
143 $content = ContentHandler::makeContent( rtrim( $text ), $title );
144 $rev = new WikiRevision();
145 $rev->setContent( SlotRecord::MAIN, $content );
146 $rev->setTitle( $title );
147 $rev->setUserObj( $user );
148 $rev->setComment( $summary );
149 $rev->setTimestamp( $timestamp );
150
151 if ( $exists &&
152 $overwrite &&
153 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
154 ) {
155 $this->output( "File for title $actualTitle contains no changes from the current " .
156 "revision. Skipping.\n" );
157 $skipCount++;
158 continue;
159 }
160
161 $status = $rev->importOldRevision();
162 $newId = $title->getLatestRevID();
163
164 if ( $status ) {
165 $action = $exists ? 'updated' : 'created';
166 $this->output( "Successfully $action $actualTitle\n" );
167 $successCount++;
168 } else {
169 $action = $exists ? 'update' : 'create';
170 $this->output( "Failed to $action $actualTitle\n" );
171 $failCount++;
172 $exit = 1;
173 }
174
175 // Create the RecentChanges entry if necessary
176 if ( $rc && $status ) {
177 if ( $exists ) {
178 if ( is_object( $oldRevRecord ) ) {
179 RecentChange::notifyEdit(
180 $timestamp,
181 $title,
182 $rev->getMinor(),
183 $user,
184 $summary,
185 $oldRevID,
186 $oldRevRecord->getTimestamp(),
187 $bot,
188 '',
189 $oldRevRecord->getSize(),
190 $rev->getSize(),
191 $newId,
192 // the pages don't need to be patrolled
193 1
194 );
195 }
196 } else {
197 RecentChange::notifyNew(
198 $timestamp,
199 $title,
200 $rev->getMinor(),
201 $user,
202 $summary,
203 $bot,
204 '',
205 $rev->getSize(),
206 $newId,
207 1
208 );
209 }
210 }
211 }
212
213 $this->output( "Done! $successCount succeeded, $skipCount skipped.\n" );
214 if ( $exit ) {
215 $this->fatalError( "Import failed with $failCount failed pages.\n", $exit );
216 }
217 }
218}
219
220// @codeCoverageIgnoreStart
221$maintClass = ImportTextFiles::class;
222require_once RUN_MAINTENANCE_IF_MAIN;
223// @codeCoverageIgnoreEnd
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script which reads in text files and imports their content to a page of the wiki.
execute()
Do the actual work.
__construct()
Default constructor.
Base class for content handling.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Utility class for creating and reading rows in the recentchanges table.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:78
User class for the MediaWiki software.
Definition User.php:121
Represents a revision, log entry or upload during the import process.