Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 178 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| Importer | |
0.00% |
0 / 178 |
|
0.00% |
0 / 13 |
1806 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| setStorage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| put | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| handleBoard | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
12 | |||
| handleHeader | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| handleTopic | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
12 | |||
| handlePost | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| handleSummary | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| getRevisions | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| getRevision | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
110 | |||
| mapId | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| checkTransWikiMode | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| createLocalUser | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Dump; |
| 4 | |
| 5 | use Flow\Container; |
| 6 | use Flow\Data\ManagerGroup; |
| 7 | use Flow\DbFactory; |
| 8 | use Flow\Import\HistoricalUIDGenerator; |
| 9 | use Flow\Import\ImportException; |
| 10 | use Flow\Model\AbstractRevision; |
| 11 | use Flow\Model\Header; |
| 12 | use Flow\Model\PostRevision; |
| 13 | use Flow\Model\PostSummary; |
| 14 | use Flow\Model\TopicListEntry; |
| 15 | use Flow\Model\UUID; |
| 16 | use Flow\Model\Workflow; |
| 17 | use Flow\OccupationController; |
| 18 | use MediaWiki\Deferred\SiteStatsUpdate; |
| 19 | use MediaWiki\Extension\CentralAuth\CentralAuthServices; |
| 20 | use MediaWiki\Extension\CentralAuth\User\CentralAuthUser; |
| 21 | use MediaWiki\MediaWikiServices; |
| 22 | use MediaWiki\Title\Title; |
| 23 | use MediaWiki\User\CentralId\CentralIdLookup; |
| 24 | use MediaWiki\User\User; |
| 25 | use MediaWiki\WikiMap\WikiMap; |
| 26 | use RuntimeException; |
| 27 | use WikiImporter; |
| 28 | use XMLReader; |
| 29 | |
| 30 | class Importer { |
| 31 | /** |
| 32 | * @var WikiImporter |
| 33 | */ |
| 34 | protected $importer; |
| 35 | |
| 36 | /** |
| 37 | * @var ManagerGroup|null |
| 38 | */ |
| 39 | protected $storage; |
| 40 | |
| 41 | /** |
| 42 | * The most recently imported board workflow (if any). |
| 43 | * |
| 44 | * @var Workflow|null |
| 45 | */ |
| 46 | protected $boardWorkflow; |
| 47 | |
| 48 | /** |
| 49 | * The most recently imported topic workflow (if any). |
| 50 | * |
| 51 | * @var Workflow|null |
| 52 | */ |
| 53 | protected $topicWorkflow; |
| 54 | |
| 55 | /** |
| 56 | * @var array Map of old to new IDs |
| 57 | */ |
| 58 | protected $idMap = []; |
| 59 | |
| 60 | /** |
| 61 | * To convert between global and local user ids |
| 62 | * |
| 63 | * @var CentralIdLookup|null |
| 64 | */ |
| 65 | protected $lookup; |
| 66 | |
| 67 | /** |
| 68 | * Whether the current board is being imported in trans-wiki mode |
| 69 | * |
| 70 | * @var bool |
| 71 | */ |
| 72 | protected $transWikiMode = false; |
| 73 | |
| 74 | public function __construct( WikiImporter $importer ) { |
| 75 | $this->importer = $importer; |
| 76 | try { |
| 77 | $this->lookup = MediaWikiServices::getInstance() |
| 78 | ->getCentralIdLookupFactory() |
| 79 | ->getLookup( 'CentralAuth' ); |
| 80 | } catch ( \Throwable $unused ) { |
| 81 | $this->lookup = null; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public function setStorage( ManagerGroup $storage ) { |
| 86 | $this->storage = $storage; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param object $object |
| 91 | * @param array $metadata |
| 92 | */ |
| 93 | protected function put( $object, array $metadata = [] ) { |
| 94 | if ( $this->storage ) { |
| 95 | $this->storage->put( $object, [ 'imported' => true ] + $metadata ); |
| 96 | |
| 97 | // prevent memory from being filled up |
| 98 | $this->storage->clear(); |
| 99 | |
| 100 | // keep workflow objects around, so follow-up `put`s (e.g. to update |
| 101 | // last_update_timestamp) don't confuse it for a new object |
| 102 | foreach ( [ $this->boardWorkflow, $this->topicWorkflow ] as $object ) { |
| 103 | if ( $object ) { |
| 104 | $this->storage->getStorage( get_class( $object ) )->merge( $object ); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public function handleBoard() { |
| 111 | $this->checkTransWikiMode( |
| 112 | $this->importer->nodeAttribute( 'id' ), |
| 113 | $this->importer->nodeAttribute( 'title' ) |
| 114 | ); |
| 115 | |
| 116 | $id = $this->mapId( $this->importer->nodeAttribute( 'id' ) ); |
| 117 | $this->importer->debug( 'Enter board handler for ' . $id ); |
| 118 | |
| 119 | $uuid = UUID::create( $id ); |
| 120 | $title = Title::newFromDBkey( $this->importer->nodeAttribute( 'title' ) ); |
| 121 | |
| 122 | $this->boardWorkflow = Workflow::fromStorageRow( [ |
| 123 | 'workflow_id' => $uuid->getAlphadecimal(), |
| 124 | 'workflow_type' => 'discussion', |
| 125 | 'workflow_wiki' => WikiMap::getCurrentWikiId(), |
| 126 | 'workflow_page_id' => $title->getArticleID(), |
| 127 | 'workflow_namespace' => $title->getNamespace(), |
| 128 | 'workflow_title_text' => $title->getDBkey(), |
| 129 | 'workflow_last_update_timestamp' => $uuid->getTimestamp( TS_MW ), |
| 130 | ] ); |
| 131 | |
| 132 | // create page if it does not yet exist |
| 133 | /** @var OccupationController $occupationController */ |
| 134 | $occupationController = Container::get( 'occupation_controller' ); |
| 135 | $creationStatus = $occupationController->safeAllowCreation( $title, $occupationController->getTalkpageManager() ); |
| 136 | if ( !$creationStatus->isOK() ) { |
| 137 | throw new RuntimeException( $creationStatus->__toString() ); |
| 138 | } |
| 139 | |
| 140 | $ensureStatus = $occupationController->ensureFlowRevision( |
| 141 | MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title ), |
| 142 | $this->boardWorkflow |
| 143 | ); |
| 144 | if ( !$ensureStatus->isOK() ) { |
| 145 | throw new RuntimeException( $ensureStatus->__toString() ); |
| 146 | } |
| 147 | |
| 148 | $this->put( $this->boardWorkflow, [] ); |
| 149 | } |
| 150 | |
| 151 | public function handleHeader() { |
| 152 | $id = $this->mapId( $this->importer->nodeAttribute( 'id' ) ); |
| 153 | $this->importer->debug( 'Enter description handler for ' . $id ); |
| 154 | |
| 155 | $metadata = [ 'workflow' => $this->boardWorkflow ]; |
| 156 | |
| 157 | $revisions = $this->getRevisions( [ Header::class, 'fromStorageRow' ] ); |
| 158 | foreach ( $revisions as $revision ) { |
| 159 | $this->put( $revision, $metadata ); |
| 160 | } |
| 161 | |
| 162 | /** @var Header $revision */ |
| 163 | $revision = end( $revisions ); |
| 164 | $this->boardWorkflow->updateLastUpdated( $revision->getRevisionId() ); |
| 165 | $this->put( $this->boardWorkflow, [] ); |
| 166 | } |
| 167 | |
| 168 | public function handleTopic() { |
| 169 | $id = $this->mapId( $this->importer->nodeAttribute( 'id' ) ); |
| 170 | $this->importer->debug( 'Enter topic handler for ' . $id ); |
| 171 | |
| 172 | $uuid = UUID::create( $id ); |
| 173 | $title = $this->boardWorkflow->getArticleTitle(); |
| 174 | |
| 175 | $this->topicWorkflow = Workflow::fromStorageRow( [ |
| 176 | 'workflow_id' => $uuid->getAlphadecimal(), |
| 177 | 'workflow_type' => 'topic', |
| 178 | 'workflow_wiki' => WikiMap::getCurrentWikiId(), |
| 179 | 'workflow_page_id' => $title->getArticleID(), |
| 180 | 'workflow_namespace' => $title->getNamespace(), |
| 181 | 'workflow_title_text' => $title->getDBkey(), |
| 182 | 'workflow_last_update_timestamp' => $uuid->getTimestamp( TS_MW ), |
| 183 | ] ); |
| 184 | $topicListEntry = TopicListEntry::create( $this->boardWorkflow, $this->topicWorkflow ); |
| 185 | |
| 186 | $metadata = [ |
| 187 | 'board-workflow' => $this->boardWorkflow, |
| 188 | 'workflow' => $this->topicWorkflow, |
| 189 | // @todo: topic-title & first-post? (used only in NotificationListener) |
| 190 | ]; |
| 191 | |
| 192 | // create page if it does not yet exist |
| 193 | /** @var OccupationController $occupationController */ |
| 194 | $occupationController = Container::get( 'occupation_controller' ); |
| 195 | $creationStatus = $occupationController->safeAllowCreation( |
| 196 | $this->topicWorkflow->getArticleTitle(), |
| 197 | $occupationController->getTalkpageManager() |
| 198 | ); |
| 199 | if ( !$creationStatus->isOK() ) { |
| 200 | throw new RuntimeException( $creationStatus->__toString() ); |
| 201 | } |
| 202 | |
| 203 | $ensureStatus = $occupationController->ensureFlowRevision( |
| 204 | MediaWikiServices::getInstance()->getWikiPageFactory() |
| 205 | ->newFromTitle( $this->topicWorkflow->getArticleTitle() ), |
| 206 | $this->topicWorkflow |
| 207 | ); |
| 208 | if ( !$ensureStatus->isOK() ) { |
| 209 | throw new RuntimeException( $ensureStatus->__toString() ); |
| 210 | } |
| 211 | |
| 212 | $this->put( $this->topicWorkflow, $metadata ); |
| 213 | $this->put( $topicListEntry, $metadata ); |
| 214 | } |
| 215 | |
| 216 | public function handlePost() { |
| 217 | $id = $this->mapId( $this->importer->nodeAttribute( 'id' ) ); |
| 218 | $this->importer->debug( 'Enter post handler for ' . $id ); |
| 219 | |
| 220 | $metadata = [ |
| 221 | 'workflow' => $this->topicWorkflow |
| 222 | // @todo: topic-title? (used only in NotificationListener) |
| 223 | ]; |
| 224 | |
| 225 | $revisions = $this->getRevisions( [ PostRevision::class, 'fromStorageRow' ] ); |
| 226 | foreach ( $revisions as $revision ) { |
| 227 | $this->put( $revision, $metadata ); |
| 228 | } |
| 229 | |
| 230 | /** @var PostRevision $revision */ |
| 231 | $revision = end( $revisions ); |
| 232 | $this->topicWorkflow->updateLastUpdated( $revision->getRevisionId() ); |
| 233 | $this->put( $this->topicWorkflow, $metadata ); |
| 234 | } |
| 235 | |
| 236 | public function handleSummary() { |
| 237 | $id = $this->mapId( $this->importer->nodeAttribute( 'id' ) ); |
| 238 | $this->importer->debug( 'Enter summary handler for ' . $id ); |
| 239 | |
| 240 | $metadata = [ 'workflow' => $this->topicWorkflow ]; |
| 241 | |
| 242 | $revisions = $this->getRevisions( [ PostSummary::class, 'fromStorageRow' ] ); |
| 243 | foreach ( $revisions as $revision ) { |
| 244 | $this->put( $revision, $metadata ); |
| 245 | } |
| 246 | |
| 247 | /** @var PostSummary $revision */ |
| 248 | $revision = end( $revisions ); |
| 249 | $this->topicWorkflow->updateLastUpdated( $revision->getRevisionId() ); |
| 250 | $this->put( $this->topicWorkflow, $metadata ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * @param callable $callback The relevant fromStorageRow callback |
| 255 | * @return AbstractRevision[] |
| 256 | */ |
| 257 | protected function getRevisions( $callback ) { |
| 258 | $revisions = []; |
| 259 | |
| 260 | // keep processing <revision> nodes until </revisions> |
| 261 | while ( $this->importer->getReader()->localName !== 'revisions' || |
| 262 | $this->importer->getReader()->nodeType !== XMLReader::END_ELEMENT |
| 263 | ) { |
| 264 | if ( $this->importer->getReader()->localName === 'revision' ) { |
| 265 | $revisions[] = $this->getRevision( $callback ); |
| 266 | } |
| 267 | $this->importer->getReader()->read(); |
| 268 | } |
| 269 | |
| 270 | return $revisions; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @param callable $callback The relevant fromStorageRow callback |
| 275 | * @return AbstractRevision |
| 276 | */ |
| 277 | protected function getRevision( $callback ) { |
| 278 | $id = $this->mapId( $this->importer->nodeAttribute( 'id' ) ); |
| 279 | $this->importer->debug( 'Enter revision handler for ' . $id ); |
| 280 | |
| 281 | // isEmptyElement will no longer be valid after we've started iterating |
| 282 | // the attributes |
| 283 | $empty = $this->importer->getReader()->isEmptyElement; |
| 284 | |
| 285 | $attribs = []; |
| 286 | |
| 287 | $this->importer->getReader()->moveToFirstAttribute(); |
| 288 | do { |
| 289 | $attribs[$this->importer->getReader()->name] = $this->importer->getReader()->value; |
| 290 | } while ( $this->importer->getReader()->moveToNextAttribute() ); |
| 291 | |
| 292 | $idFields = [ 'id', 'typeid', 'treedescendantid', 'treerevid', 'parentid', 'treeparentid', 'lasteditid' ]; |
| 293 | foreach ( $idFields as $idField ) { |
| 294 | if ( isset( $attribs[ $idField ] ) ) { |
| 295 | $attribs[ $idField ] = $this->mapId( $attribs[ $idField ] ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if ( $this->transWikiMode && $this->lookup ) { |
| 300 | $userFields = [ 'user', 'treeoriguser', 'moduser', 'edituser' ]; |
| 301 | foreach ( $userFields as $userField ) { |
| 302 | $globalUserIdField = 'global' . $userField . 'id'; |
| 303 | if ( isset( $attribs[ $globalUserIdField ] ) ) { |
| 304 | $localUser = $this->lookup->localUserFromCentralId( |
| 305 | (int)$attribs[ $globalUserIdField ], |
| 306 | CentralIdLookup::AUDIENCE_RAW |
| 307 | ); |
| 308 | if ( !$localUser ) { |
| 309 | $localUser = $this->createLocalUser( (int)$attribs[ $globalUserIdField ] ); |
| 310 | } |
| 311 | $attribs[ $userField . 'id' ] = $localUser->getId(); |
| 312 | $attribs[ $userField . 'wiki' ] = WikiMap::getCurrentWikiId(); |
| 313 | } elseif ( isset( $attribs[ $userField . 'ip' ] ) ) { |
| 314 | // make anons local users |
| 315 | $attribs[ $userField . 'wiki' ] = WikiMap::getCurrentWikiId(); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // now that we've moved inside the node (to fetch attributes), |
| 321 | // nodeContents() is no longer reliable: is uses isEmptyContent (which |
| 322 | // will now no longer respond with 'true') to see if the node should be |
| 323 | // skipped - use the value we've fetched earlier! |
| 324 | $attribs['content'] = $empty ? '' : $this->importer->nodeContents(); |
| 325 | |
| 326 | // make sure there are no leftover key columns (unknown to $attribs) |
| 327 | $keys = array_intersect_key( array_flip( Exporter::$map ), $attribs ); |
| 328 | // now make sure $values columns are in the same order as $keys are |
| 329 | // (array_merge) and there are no leftover columns (array_intersect_key) |
| 330 | $values = array_intersect_key( array_merge( $keys, $attribs ), $keys ); |
| 331 | // combine them |
| 332 | $attribs = array_combine( $keys, $values ); |
| 333 | |
| 334 | // now fill in missing attributes |
| 335 | $keys = array_fill_keys( array_keys( Exporter::$map ), null ); |
| 336 | $attribs += $keys; |
| 337 | |
| 338 | return $callback( $attribs ); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * When in trans-wiki mode, return a new id based on the same timestamp |
| 343 | * |
| 344 | * @param string $id |
| 345 | * @return string |
| 346 | */ |
| 347 | private function mapId( $id ) { |
| 348 | if ( !$this->transWikiMode ) { |
| 349 | return $id; |
| 350 | } |
| 351 | |
| 352 | if ( !isset( $this->idMap[ $id ] ) ) { |
| 353 | $this->idMap[ $id ] = UUID::create( HistoricalUIDGenerator::historicalTimestampedUID88( |
| 354 | UUID::hex2timestamp( UUID::create( $id )->getHex() ) |
| 355 | ) )->getAlphadecimal(); |
| 356 | } |
| 357 | return $this->idMap[ $id ]; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Check if a board already exist and should be imported in trans-wiki mode |
| 362 | * |
| 363 | * @param string $boardWorkflowId |
| 364 | * @param string $title |
| 365 | */ |
| 366 | private function checkTransWikiMode( $boardWorkflowId, $title ) { |
| 367 | /** @var DbFactory $dbFactory */ |
| 368 | $dbFactory = Container::get( 'db.factory' ); |
| 369 | $workflowExist = (bool)$dbFactory->getDB( DB_PRIMARY )->newSelectQueryBuilder() |
| 370 | ->select( 'workflow_id' ) |
| 371 | ->from( 'flow_workflow' ) |
| 372 | ->where( [ 'workflow_id' => UUID::create( $boardWorkflowId )->getBinary() ] ) |
| 373 | ->caller( __METHOD__ ) |
| 374 | ->fetchField(); |
| 375 | |
| 376 | if ( $workflowExist ) { |
| 377 | $this->importer->debug( "$title will be imported in trans-wiki mode" ); |
| 378 | } |
| 379 | $this->transWikiMode = $workflowExist; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Create a local user corresponding to a global id |
| 384 | * |
| 385 | * @param int $globalUserId |
| 386 | * @return User Local user |
| 387 | * @throws ImportException |
| 388 | */ |
| 389 | private function createLocalUser( $globalUserId ) { |
| 390 | if ( !$this->lookup ) { |
| 391 | throw new ImportException( 'Creating local users is not supported without central id provider' ); |
| 392 | } |
| 393 | |
| 394 | $globalUser = CentralAuthUser::newFromId( $globalUserId ); |
| 395 | $localUser = User::newFromName( $globalUser->getName() ); |
| 396 | |
| 397 | if ( $localUser->getId() ) { |
| 398 | throw new ImportException( "User '{$localUser->getName()}' already exists" ); |
| 399 | } |
| 400 | |
| 401 | $status = CentralAuthServices::getUtilityService()->autoCreateUser( $localUser, true, $localUser ); |
| 402 | if ( !$status->isGood() ) { |
| 403 | throw new ImportException( |
| 404 | "autoCreateUser failed for {$localUser->getName()}: " . print_r( $status->getErrors(), true ) |
| 405 | ); |
| 406 | } |
| 407 | |
| 408 | # Update user count |
| 409 | $ssUpdate = SiteStatsUpdate::factory( [ 'users' => 1 ] ); |
| 410 | $ssUpdate->doUpdate(); |
| 411 | |
| 412 | return $localUser; |
| 413 | } |
| 414 | } |