Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.62% |
58 / 64 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RecentChangesListener | |
90.62% |
58 / 64 |
|
25.00% |
1 / 4 |
11.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| onAfterInsert | |
92.45% |
49 / 53 |
|
0.00% |
0 / 1 |
6.02 | |||
| getRcTitle | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| isAllowed | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Data\Listener; |
| 4 | |
| 5 | use Closure; |
| 6 | use Flow\Data\Utils\RecentChangeFactory; |
| 7 | use Flow\FlowActions; |
| 8 | use Flow\Formatter\IRCLineUrlFormatter; |
| 9 | use Flow\Model\AbstractRevision; |
| 10 | use Flow\Model\Workflow; |
| 11 | use Flow\Repository\UserNameBatch; |
| 12 | use MediaWiki\MediaWikiServices; |
| 13 | use MediaWiki\RecentChanges\RecentChange; |
| 14 | use MediaWiki\Title\Title; |
| 15 | use MediaWiki\WikiMap\WikiMap; |
| 16 | |
| 17 | /** |
| 18 | * Inserts mw recentchange rows for flow AbstractRevision instances. |
| 19 | */ |
| 20 | class RecentChangesListener extends AbstractListener { |
| 21 | |
| 22 | // Value used in rc_source field of recentchanges to identify flow specific changes |
| 23 | public const SRC_FLOW = "flow"; |
| 24 | |
| 25 | /** |
| 26 | * @var FlowActions |
| 27 | */ |
| 28 | protected $actions; |
| 29 | |
| 30 | /** |
| 31 | * @var UserNameBatch |
| 32 | */ |
| 33 | protected $usernames; |
| 34 | |
| 35 | /** |
| 36 | * @var RecentChangeFactory |
| 37 | */ |
| 38 | protected $rcFactory; |
| 39 | |
| 40 | /** |
| 41 | * @var IRCLineUrlFormatter |
| 42 | */ |
| 43 | protected $ircFormatter; |
| 44 | |
| 45 | /** |
| 46 | * @param FlowActions $actions |
| 47 | * @param UserNameBatch $usernames |
| 48 | * @param RecentChangeFactory $rcFactory |
| 49 | * @param IRCLineUrlFormatter $ircFormatter |
| 50 | */ |
| 51 | public function __construct( |
| 52 | FlowActions $actions, |
| 53 | UserNameBatch $usernames, |
| 54 | RecentChangeFactory $rcFactory, |
| 55 | IRCLineUrlFormatter $ircFormatter |
| 56 | ) { |
| 57 | $this->actions = $actions; |
| 58 | $this->usernames = $usernames; |
| 59 | $this->rcFactory = $rcFactory; |
| 60 | $this->ircFormatter = $ircFormatter; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param AbstractRevision $revision |
| 65 | * @param array $row Revision row |
| 66 | * @param array $metadata |
| 67 | */ |
| 68 | public function onAfterInsert( $revision, array $row, array $metadata ) { |
| 69 | global $wgRCFeeds; |
| 70 | |
| 71 | // No action on imported revisions |
| 72 | if ( isset( $metadata['imported'] ) && $metadata['imported'] ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $action = $revision->getChangeType(); |
| 77 | $revisionId = $revision->getRevisionId()->getAlphadecimal(); |
| 78 | $timestamp = $revision->getRevisionId()->getTimestamp(); |
| 79 | /** @var Workflow $workflow */ |
| 80 | $workflow = $metadata['workflow']; |
| 81 | $user = $revision->getUser(); |
| 82 | |
| 83 | if ( !$this->isAllowed( $revision, $action ) ) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | $title = $this->getRcTitle( $workflow, $revision->getChangeType() ); |
| 88 | $autopatrolAllowed = MediaWikiServices::getInstance()->getPermissionManager() |
| 89 | ->userHasRight( $user, 'autopatrol' ); |
| 90 | $attribs = [ |
| 91 | 'rc_namespace' => $title->getNamespace(), |
| 92 | 'rc_title' => $title->getDBkey(), |
| 93 | 'rc_user' => $row['rev_user_id'], |
| 94 | 'rc_user_text' => $this->usernames->get( WikiMap::getCurrentWikiId(), $row['rev_user_id'], $row['rev_user_ip'] ), |
| 95 | 'rc_type' => RC_FLOW, |
| 96 | 'rc_source' => self::SRC_FLOW, |
| 97 | 'rc_minor' => 0, |
| 98 | 'rc_bot' => 0, // TODO: is revision by bot |
| 99 | 'rc_patrolled' => $autopatrolAllowed ? RecentChange::PRC_AUTOPATROLLED : RecentChange::PRC_UNPATROLLED, |
| 100 | 'rc_old_len' => $revision->getPreviousContentLength(), |
| 101 | 'rc_new_len' => $revision->getContentLength(), |
| 102 | 'rc_this_oldid' => 0, |
| 103 | 'rc_last_oldid' => 0, |
| 104 | 'rc_logid' => 0, |
| 105 | 'rc_log_type' => null, |
| 106 | 'rc_params' => serialize( [ |
| 107 | 'flow-workflow-change' => [ |
| 108 | 'action' => $action, |
| 109 | 'revision_type' => get_class( $revision ), |
| 110 | 'revision' => $revisionId, |
| 111 | 'workflow' => $workflow->getId()->getAlphadecimal(), |
| 112 | ], |
| 113 | ] ), |
| 114 | 'rc_cur_id' => 0, |
| 115 | 'rc_comment' => '', |
| 116 | 'rc_comment_text' => '', |
| 117 | 'rc_comment_data' => null, |
| 118 | 'rc_timestamp' => $timestamp, |
| 119 | 'rc_deleted' => 0, |
| 120 | ]; |
| 121 | |
| 122 | $rc = $this->rcFactory->newFromRow( (object)$attribs ); |
| 123 | // Insert into db only, don't send to any RC feeds (yet) |
| 124 | $rc->save( RecentChange::SEND_NONE ); |
| 125 | $feeds = $wgRCFeeds; |
| 126 | // Override the IRC formatter with our own formatter |
| 127 | foreach ( $feeds as $name => &$feed ) { |
| 128 | $feed['original_formatter'] = $feed['formatter']; |
| 129 | $feed['formatter'] = $this->ircFormatter; |
| 130 | } |
| 131 | // pre-load the irc formatter which will be triggered via hook |
| 132 | $this->ircFormatter->associate( $rc, [ |
| 133 | 'revision' => $revision |
| 134 | ] + $metadata ); |
| 135 | // run the feeds/irc/etc external notifications |
| 136 | $rc->notifyRCFeeds( $feeds ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param Workflow $workflow |
| 141 | * @param string $action |
| 142 | * @return Title |
| 143 | */ |
| 144 | public function getRcTitle( Workflow $workflow, $action ) { |
| 145 | if ( $this->actions->getValue( $action, 'rc_title' ) === 'owner' ) { |
| 146 | return $workflow->getOwnerTitle(); |
| 147 | } else { |
| 148 | return $workflow->getArticleTitle(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param AbstractRevision $revision |
| 154 | * @param string $action |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function isAllowed( AbstractRevision $revision, $action ) { |
| 158 | $allowed = $this->actions->getValue( $action, 'rc_insert' ); |
| 159 | if ( $allowed instanceof Closure ) { |
| 160 | $allowed = $allowed( $revision, $this ); |
| 161 | } |
| 162 | |
| 163 | return (bool)$allowed; |
| 164 | } |
| 165 | } |