Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| IRCLineUrlFormatter | |
0.00% |
0 / 61 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getHistoryType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| associate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLine | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
6 | |||
| serializeRcRevision | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| formatDescription | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| format | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Formatter; |
| 4 | |
| 5 | use Flow\Container; |
| 6 | use Flow\RevisionActionPermissions; |
| 7 | use MediaWiki\Context\IContextSource; |
| 8 | use MediaWiki\Context\RequestContext; |
| 9 | use MediaWiki\Json\FormatJson; |
| 10 | use MediaWiki\Logger\LoggerFactory; |
| 11 | use MediaWiki\RCFeed\RCFeedFormatter; |
| 12 | use MediaWiki\RecentChanges\RecentChange; |
| 13 | use SplObjectStorage; |
| 14 | |
| 15 | /** |
| 16 | * Generates URL's to be inserted into the IRC |
| 17 | * recent changes feed. |
| 18 | */ |
| 19 | class IRCLineUrlFormatter extends AbstractFormatter implements RCFeedFormatter { |
| 20 | /** |
| 21 | * @var SplObjectStorage |
| 22 | */ |
| 23 | protected $data; |
| 24 | |
| 25 | public function __construct( RevisionActionPermissions $permissions, RevisionFormatter $serializer ) { |
| 26 | parent::__construct( $permissions, $serializer ); |
| 27 | $this->data = new SplObjectStorage; |
| 28 | } |
| 29 | |
| 30 | protected function getHistoryType() { |
| 31 | return 'irc'; |
| 32 | } |
| 33 | |
| 34 | public function associate( RecentChange $rc, array $metadata ) { |
| 35 | $this->data[$rc] = $metadata; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Allows us to set the rc_comment field |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * @param array $feed |
| 44 | * @param RecentChange $rc |
| 45 | * @param null|string $actionComment |
| 46 | * @return string|null Text for IRC line, or null on failure |
| 47 | */ |
| 48 | public function getLine( array $feed, RecentChange $rc, $actionComment ) { |
| 49 | $ctx = RequestContext::getMain(); |
| 50 | |
| 51 | $serialized = $this->serializeRcRevision( $rc, $ctx ); |
| 52 | if ( !$serialized ) { |
| 53 | LoggerFactory::getInstance( 'Flow' )->debug( |
| 54 | __METHOD__ . ': Failed to obtain serialized RC revision.', |
| 55 | [ |
| 56 | 'rc_attributes' => FormatJson::encode( $rc->getAttributes(), true ), |
| 57 | 'user_id' => $ctx->getUser()->getId(), |
| 58 | ] |
| 59 | ); |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | $rcAttribs = $rc->getAttributes(); |
| 64 | $rcAttribs['rc_comment'] = $this->formatDescription( $serialized, $ctx ); |
| 65 | $rcAttribs['rc_comment_text'] = $rcAttribs['rc_comment']; |
| 66 | $rcAttribs['rc_comment_data'] = null; |
| 67 | $rc->setAttribs( $rcAttribs ); |
| 68 | |
| 69 | /** @var RCFeedFormatter $formatter */ |
| 70 | $formatter = new $feed['original_formatter'](); |
| 71 | return $formatter->getLine( $feed, $rc, $actionComment ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Gets the formatted RC revision, or returns null if this revision is not to be |
| 76 | * shown in RC, or on failure. |
| 77 | * |
| 78 | * @param RecentChange $rc |
| 79 | * @param IContextSource $ctx |
| 80 | * @return array|false Array of data, or false on failure |
| 81 | * |
| 82 | * @fixme this looks slow, likely a better way |
| 83 | */ |
| 84 | protected function serializeRcRevision( RecentChange $rc, IContextSource $ctx ) { |
| 85 | /** @var ChangesListQuery $query */ |
| 86 | $query = Container::get( 'query.changeslist' ); |
| 87 | $query->loadMetadataBatch( [ (object)$rc->getAttributes() ] ); |
| 88 | $rcRow = $query->getResult( null, $rc ); |
| 89 | if ( !$rcRow ) { |
| 90 | LoggerFactory::getInstance( 'Flow' )->debug( |
| 91 | __METHOD__ . ': Failed to load result.', |
| 92 | [ |
| 93 | 'rc_attributes' => FormatJson::encode( $rc->getAttributes(), true ), |
| 94 | 'user_id' => $ctx->getUser()->getId() |
| 95 | ] |
| 96 | ); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | $this->serializer->setIncludeHistoryProperties( true ); |
| 101 | return $this->serializer->formatApi( $rcRow, $ctx, 'recentchanges' ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Generate a plaintext revision description suitable for IRC consumption |
| 106 | * |
| 107 | * @param array $data |
| 108 | * @param IContextSource $ctx not used |
| 109 | * @return string |
| 110 | */ |
| 111 | protected function formatDescription( array $data, IContextSource $ctx ) { |
| 112 | $msg = $this->getDescription( $data, $ctx ); |
| 113 | return $msg->inLanguage( 'en' )->text(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @param RecentChange $rc |
| 118 | * @return string|null |
| 119 | */ |
| 120 | public function format( RecentChange $rc ) { |
| 121 | // commit metadata provided via self::associate |
| 122 | if ( !isset( $this->data[$rc] ) ) { |
| 123 | wfDebugLog( 'Flow', __METHOD__ . ': Nothing pre-loaded about rc ' . $rc->getAttribute( 'rc_id' ) ); |
| 124 | return null; |
| 125 | } |
| 126 | $metadata = $this->data[$rc]; |
| 127 | |
| 128 | $row = new FormatterRow; |
| 129 | $row->revision = $metadata['revision']; |
| 130 | $row->currentRevision = $row->revision; |
| 131 | $row->workflow = $metadata['workflow']; |
| 132 | $links = $this->serializer->buildLinks( $row ); |
| 133 | |
| 134 | // Listed in order of preference |
| 135 | $accept = [ |
| 136 | 'diff', |
| 137 | 'post-history', 'topic-history', 'board-history', |
| 138 | 'post', 'topic', |
| 139 | 'workflow' |
| 140 | ]; |
| 141 | |
| 142 | foreach ( $accept as $key ) { |
| 143 | if ( isset( $links[$key] ) ) { |
| 144 | return $links[$key]->getCanonicalURL(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | wfDebugLog( 'Flow', __METHOD__ |
| 149 | . ': No url generated for action ' . $row->workflow->getType() |
| 150 | . ' on revision ' . $row->revision->getRevisionId() |
| 151 | ); |
| 152 | return null; |
| 153 | } |
| 154 | } |