Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LinksTableUpdater | |
0.00% |
0 / 56 |
|
0.00% |
0 / 4 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doUpdate | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| mutateParserOutput | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
182 | |||
| getReferencesForTitle | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow; |
| 4 | |
| 5 | use Flow\Data\ManagerGroup; |
| 6 | use Flow\Model\Reference; |
| 7 | use Flow\Model\URLReference; |
| 8 | use Flow\Model\WikiReference; |
| 9 | use Flow\Model\Workflow; |
| 10 | use MediaWiki\Deferred\DeferredUpdates; |
| 11 | use MediaWiki\MediaWikiServices; |
| 12 | use MediaWiki\Parser\ParserOutput; |
| 13 | use MediaWiki\Title\Title; |
| 14 | use MediaWiki\WikiMap\WikiMap; |
| 15 | |
| 16 | class LinksTableUpdater { |
| 17 | |
| 18 | /** @var ManagerGroup */ |
| 19 | protected $storage; |
| 20 | |
| 21 | /** |
| 22 | * @param ManagerGroup $storage A ManagerGroup |
| 23 | */ |
| 24 | public function __construct( ManagerGroup $storage ) { |
| 25 | $this->storage = $storage; |
| 26 | } |
| 27 | |
| 28 | public function doUpdate( Workflow $workflow ) { |
| 29 | $title = $workflow->getArticleTitle(); |
| 30 | $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title ); |
| 31 | |
| 32 | $page->doSecondaryDataUpdates( [ 'defer' => DeferredUpdates::PRESEND, 'causeAction' => 'flow' ] ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param Title $title |
| 37 | * @param ParserOutput $parserOutput |
| 38 | * @param Reference[]|null $references |
| 39 | */ |
| 40 | public function mutateParserOutput( Title $title, ParserOutput $parserOutput, ?array $references = null ) { |
| 41 | $references ??= $this->getReferencesForTitle( $title ); |
| 42 | |
| 43 | $linkBatch = MediaWikiServices::getInstance()->getLinkBatchFactory()->newLinkBatch(); |
| 44 | /** @var Title[] $internalLinks */ |
| 45 | $internalLinks = []; |
| 46 | /** @var Title[] $templates */ |
| 47 | $templates = []; |
| 48 | |
| 49 | foreach ( $references as $reference ) { |
| 50 | if ( $reference->getType() === 'link' ) { |
| 51 | if ( $reference instanceof URLReference ) { |
| 52 | $parserOutput->addExternalLink( $reference->getUrl() ); |
| 53 | } elseif ( $reference instanceof WikiReference ) { |
| 54 | $internalLinks[$reference->getTitle()->getPrefixedDBkey()] = $reference->getTitle(); |
| 55 | $linkBatch->addObj( $reference->getTitle() ); |
| 56 | } |
| 57 | } elseif ( $reference->getType() === WikiReference::TYPE_CATEGORY ) { |
| 58 | if ( $reference instanceof WikiReference ) { |
| 59 | $title = $reference->getTitle(); |
| 60 | $parserOutput->addCategory( |
| 61 | $title->getDBkey(), |
| 62 | // parsoid moves the sort key into the fragment |
| 63 | $title->getFragment() |
| 64 | ); |
| 65 | } |
| 66 | } elseif ( $reference->getType() === 'file' ) { |
| 67 | if ( $reference instanceof WikiReference ) { |
| 68 | // Only local images supported |
| 69 | $parserOutput->addImage( $reference->getTitle()->getDBkey() ); |
| 70 | } |
| 71 | } elseif ( $reference->getType() === 'template' ) { |
| 72 | if ( $reference instanceof WikiReference ) { |
| 73 | $templates[$reference->getTitle()->getPrefixedDBkey()] = $reference->getTitle(); |
| 74 | $linkBatch->addObj( $reference->getTitle() ); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $linkBatch->execute(); |
| 80 | $linkCache = MediaWikiServices::getInstance()->getLinkCache(); |
| 81 | |
| 82 | foreach ( $internalLinks as $title ) { |
| 83 | $id = $linkCache->getGoodLinkID( $title->getPrefixedDBkey() ); |
| 84 | $parserOutput->addLink( $title, $id ); |
| 85 | } |
| 86 | |
| 87 | foreach ( $templates as $title ) { |
| 88 | $id = $linkCache->getGoodLinkID( $title->getPrefixedDBkey() ); |
| 89 | $parserOutput->addTemplate( $title, $id, $title->getLatestRevID() ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public function getReferencesForTitle( Title $title ) { |
| 94 | $wikiReferences = $this->storage->find( |
| 95 | 'WikiReference', |
| 96 | [ |
| 97 | 'ref_src_wiki' => WikiMap::getCurrentWikiId(), |
| 98 | 'ref_src_namespace' => $title->getNamespace(), |
| 99 | 'ref_src_title' => $title->getDBkey(), |
| 100 | ] |
| 101 | ); |
| 102 | |
| 103 | $urlReferences = $this->storage->find( |
| 104 | 'URLReference', |
| 105 | [ |
| 106 | 'ref_src_wiki' => WikiMap::getCurrentWikiId(), |
| 107 | 'ref_src_namespace' => $title->getNamespace(), |
| 108 | 'ref_src_title' => $title->getDBkey(), |
| 109 | ] |
| 110 | ); |
| 111 | |
| 112 | // let's make sure the merge doesn't fail when nothing was found |
| 113 | $wikiReferences = $wikiReferences ?: []; |
| 114 | $urlReferences = $urlReferences ?: []; |
| 115 | |
| 116 | return array_merge( $wikiReferences, $urlReferences ); |
| 117 | } |
| 118 | } |