28 require_once __DIR__ .
'/Maintenance.php';
38 parent::__construct();
39 $this->
addDescription(
'Reads in text files and imports their content to pages of the wiki' );
40 $this->
addOption(
'user',
'Username to which edits should be attributed. ' .
41 'Default: "Maintenance script"',
false,
true,
'u' );
42 $this->
addOption(
'summary',
'Specify edit summary for the edits',
false,
true,
's' );
43 $this->
addOption(
'use-timestamp',
'Use the modification date of the text file ' .
44 'as the timestamp for the edit' );
45 $this->
addOption(
'overwrite',
'Overwrite existing pages. If --use-timestamp is passed, this ' .
46 'will only overwrite pages if the file has been modified since the page was last modified.' );
47 $this->
addOption(
'prefix',
'A string to place in front of the file name',
false,
true,
'p' );
48 $this->
addOption(
'bot',
'Mark edits as bot edits in the recent changes list.' );
49 $this->
addOption(
'rc',
'Place revisions in RecentChanges.' );
50 $this->
addArg(
'files',
'Files to import' );
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" );
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 ) ) {
180 $oldRevRecord->getTimestamp(),
183 $oldRevRecord->getSize(),
207 $this->
output(
"Done! $successCount succeeded, $skipCount skipped.\n" );
209 $this->
fatalError(
"Import failed with $failCount failed pages.\n", $exit );
215 require_once RUN_MAINTENANCE_IF_MAIN;
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.
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
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.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static notifyEdit( $timestamp, $page, $minor, $user, $comment, $oldId, $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0, $tags=[], EditResult $editResult=null)
Makes an entry in the database corresponding to an edit.
static notifyNew( $timestamp, $page, $minor, $user, $comment, $bot, $ip='', $size=0, $newId=0, $patrol=0, $tags=[])
Makes an entry in the database corresponding to page creation.
Represents a revision, log entry or upload during the import process.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.