| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Content\ContentHandler; |
| 25 |
use MediaWiki\Revision\SlotRecord; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\User\User; |
| 28 |
|
| 29 |
|
| 30 |
require_once __DIR__ . '/Maintenance.php'; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ImportTextFiles extends Maintenance { |
| 40 |
public function __construct() { |
| 41 |
parent::__construct(); |
| 42 |
$this->addDescription( 'Reads in text files and imports their content to pages of the wiki' ); |
| 43 |
$this->addOption( 'user', 'Username to which edits should be attributed. ' . |
| 44 |
'Default: "Maintenance script"', false, true, 'u' ); |
| 45 |
$this->addOption( 'summary', 'Specify edit summary for the edits', false, true, 's' ); |
| 46 |
$this->addOption( 'use-timestamp', 'Use the modification date of the text file ' . |
| 47 |
'as the timestamp for the edit' ); |
| 48 |
$this->addOption( 'overwrite', 'Overwrite existing pages. If --use-timestamp is passed, this ' . |
| 49 |
'will only overwrite pages if the file has been modified since the page was last modified.' ); |
| 50 |
$this->addOption( 'prefix', 'A string to place in front of the file name', false, true, 'p' ); |
| 51 |
$this->addOption( 'bot', 'Mark edits as bot edits in the recent changes list.' ); |
| 52 |
$this->addOption( 'rc', 'Place revisions in RecentChanges.' ); |
| 53 |
$this->addArg( 'files', 'Files to import' ); |
| 54 |
} |
| 55 |
|
| 56 |
public function execute() { |
| 57 |
$userName = $this->getOption( 'user', false ); |
| 58 |
$summary = $this->getOption( 'summary', 'Imported from text file' ); |
| 59 |
$useTimestamp = $this->hasOption( 'use-timestamp' ); |
| 60 |
$rc = $this->hasOption( 'rc' ); |
| 61 |
$bot = $this->hasOption( 'bot' ); |
| 62 |
$overwrite = $this->hasOption( 'overwrite' ); |
| 63 |
$prefix = $this->getOption( 'prefix', '' ); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
$files = []; |
| 68 |
$i = 0; |
| 69 |
|
| 70 |
while ( $arg = $this->getArg( $i++ ) ) { |
| 71 |
if ( file_exists( $arg ) ) { |
| 72 |
$files[$arg] = file_get_contents( $arg ); |
| 73 |
} else { |
| 74 |
|
| 75 |
|
| 76 |
$found = false; |
| 77 |
foreach ( glob( $arg ) as $filename ) { |
| 78 |
$found = true; |
| 79 |
$files[$filename] = file_get_contents( $filename ); |
| 80 |
} |
| 81 |
if ( !$found ) { |
| 82 |
$this->fatalError( "Fatal error: The file '$arg' does not exist!" ); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
$count = count( $files ); |
| 88 |
$this->output( "Importing $count pages...\n" ); |
| 89 |
|
| 90 |
if ( $userName === false ) { |
| 91 |
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 92 |
} else { |
| 93 |
$user = User::newFromName( $userName ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( !$user ) { |
| 97 |
$this->fatalError( "Invalid username\n" ); |
| 98 |
} |
| 99 |
if ( $user->isAnon() ) { |
| 100 |
$user->addToDatabase(); |
| 101 |
} |
| 102 |
|
| 103 |
$exit = 0; |
| 104 |
|
| 105 |
$successCount = 0; |
| 106 |
$failCount = 0; |
| 107 |
$skipCount = 0; |
| 108 |
|
| 109 |
$revLookup = $this->getServiceContainer()->getRevisionLookup(); |
| 110 |
foreach ( $files as $file => $text ) { |
| 111 |
$pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME ); |
| 112 |
$timestamp = $useTimestamp ? wfTimestamp( TS_UNIX, filemtime( $file ) ) : wfTimestampNow(); |
| 113 |
|
| 114 |
$title = Title::newFromText( $pageName ); |
| 115 |
|
| 116 |
if ( !$title || $title->hasFragment() ) { |
| 117 |
$this->error( "Invalid title $pageName. Skipping.\n" ); |
| 118 |
$skipCount++; |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
$exists = $title->exists(); |
| 123 |
$oldRevID = $title->getLatestRevID(); |
| 124 |
$oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) : null; |
| 125 |
$actualTitle = $title->getPrefixedText(); |
| 126 |
|
| 127 |
if ( $exists ) { |
| 128 |
$touched = wfTimestamp( TS_UNIX, $title->getTouched() ); |
| 129 |
if ( !$overwrite ) { |
| 130 |
$this->output( "Title $actualTitle already exists. Skipping.\n" ); |
| 131 |
$skipCount++; |
| 132 |
continue; |
| 133 |
} elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) { |
| 134 |
$this->output( "File for title $actualTitle has not been modified since the " . |
| 135 |
"destination page was touched. Skipping.\n" ); |
| 136 |
$skipCount++; |
| 137 |
continue; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$content = ContentHandler::makeContent( rtrim( $text ), $title ); |
| 142 |
$rev = new WikiRevision(); |
| 143 |
$rev->setContent( SlotRecord::MAIN, $content ); |
| 144 |
$rev->setTitle( $title ); |
| 145 |
$rev->setUserObj( $user ); |
| 146 |
$rev->setComment( $summary ); |
| 147 |
$rev->setTimestamp( $timestamp ); |
| 148 |
|
| 149 |
if ( $exists && |
| 150 |
$overwrite && |
| 151 |
$rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) ) |
| 152 |
) { |
| 153 |
$this->output( "File for title $actualTitle contains no changes from the current " . |
| 154 |
"revision. Skipping.\n" ); |
| 155 |
$skipCount++; |
| 156 |
continue; |
| 157 |
} |
| 158 |
|
| 159 |
$status = $rev->importOldRevision(); |
| 160 |
$newId = $title->getLatestRevID(); |
| 161 |
|
| 162 |
if ( $status ) { |
| 163 |
$action = $exists ? 'updated' : 'created'; |
| 164 |
$this->output( "Successfully $action $actualTitle\n" ); |
| 165 |
$successCount++; |
| 166 |
} else { |
| 167 |
$action = $exists ? 'update' : 'create'; |
| 168 |
$this->output( "Failed to $action $actualTitle\n" ); |
| 169 |
$failCount++; |
| 170 |
$exit = 1; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
if ( $rc && $status ) { |
| 175 |
if ( $exists ) { |
| 176 |
if ( is_object( $oldRevRecord ) ) { |
| 177 |
RecentChange::notifyEdit( |
| 178 |
$timestamp, |
| 179 |
$title, |
| 180 |
$rev->getMinor(), |
| 181 |
$user, |
| 182 |
$summary, |
| 183 |
$oldRevID, |
| 184 |
$oldRevRecord->getTimestamp(), |
| 185 |
$bot, |
| 186 |
'', |
| 187 |
$oldRevRecord->getSize(), |
| 188 |
$rev->getSize(), |
| 189 |
$newId, |
| 190 |
|
| 191 |
1 |
| 192 |
); |
| 193 |
} |
| 194 |
} else { |
| 195 |
RecentChange::notifyNew( |
| 196 |
$timestamp, |
| 197 |
$title, |
| 198 |
$rev->getMinor(), |
| 199 |
$user, |
| 200 |
$summary, |
| 201 |
$bot, |
| 202 |
'', |
| 203 |
$rev->getSize(), |
| 204 |
$newId, |
| 205 |
1 |
| 206 |
); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
$this->output( "Done! $successCount succeeded, $skipCount skipped.\n" ); |
| 212 |
if ( $exit ) { |
| 213 |
$this->fatalError( "Import failed with $failCount failed pages.\n", $exit ); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
$maintClass = ImportTextFiles::class; |
| 220 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 221 |
|