MediaWiki master
MachineReadableRCFeedFormatter.php
Go to the documentation of this file.
1<?php
2
22namespace MediaWiki\RCFeed;
23
27use RecentChange;
28
35
43 abstract protected function formatArray( array $packet );
44
56 public function getLine( array $feed, RecentChange $rc, $actionComment ) {
57 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
58 $canonicalServer = $mainConfig->get( MainConfigNames::CanonicalServer );
59 $serverName = $mainConfig->get( MainConfigNames::ServerName );
60 $scriptPath = $mainConfig->get( MainConfigNames::ScriptPath );
61 $packet = [
62 // Usually, RC ID is exposed only for patrolling purposes,
63 // but there is no real reason not to expose it in other cases,
64 // and I can see how this may be potentially useful for clients.
65 'id' => $rc->getAttribute( 'rc_id' ),
66 'type' => RecentChange::parseFromRCType( $rc->getAttribute( 'rc_type' ) ),
67 'namespace' => $rc->getTitle()->getNamespace(),
68 'title' => $rc->getTitle()->getPrefixedText(),
69 'title_url' => $rc->getTitle()->getCanonicalURL(),
70 'comment' => $rc->getAttribute( 'rc_comment' ),
71 'timestamp' => (int)wfTimestamp( TS_UNIX, $rc->getAttribute( 'rc_timestamp' ) ),
72 'user' => $rc->getAttribute( 'rc_user_text' ),
73 'bot' => (bool)$rc->getAttribute( 'rc_bot' ),
74 'notify_url' => $rc->getNotifyUrl(),
75 ];
76
77 if ( isset( $feed['channel'] ) ) {
78 $packet['channel'] = $feed['channel'];
79 }
80
81 $type = $rc->getAttribute( 'rc_type' );
82 if ( $type == RC_EDIT || $type == RC_NEW ) {
83 $useRCPatrol = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseRCPatrol );
84 $useNPPatrol = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseNPPatrol );
85 $packet['minor'] = (bool)$rc->getAttribute( 'rc_minor' );
86 if ( $useRCPatrol || ( $type == RC_NEW && $useNPPatrol ) ) {
87 $packet['patrolled'] = (bool)$rc->getAttribute( 'rc_patrolled' );
88 }
89 }
90
91 switch ( $type ) {
92 case RC_EDIT:
93 $packet['length'] = [
94 'old' => $rc->getAttribute( 'rc_old_len' ),
95 'new' => $rc->getAttribute( 'rc_new_len' )
96 ];
97 $packet['revision'] = [
98 'old' => $rc->getAttribute( 'rc_last_oldid' ),
99 'new' => $rc->getAttribute( 'rc_this_oldid' )
100 ];
101 break;
102
103 case RC_NEW:
104 $packet['length'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_new_len' ) ];
105 $packet['revision'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_this_oldid' ) ];
106 break;
107
108 case RC_LOG:
109 $packet['log_id'] = $rc->getAttribute( 'rc_logid' );
110 $packet['log_type'] = $rc->getAttribute( 'rc_log_type' );
111 $packet['log_action'] = $rc->getAttribute( 'rc_log_action' );
112 if ( $rc->getAttribute( 'rc_params' ) ) {
113 $params = $rc->parseParams();
114 if (
115 // If it's an actual serialised false...
116 $rc->getAttribute( 'rc_params' ) == serialize( false ) ||
117 // Or if we did not get false back when trying to unserialise
118 $params !== false
119 ) {
120 // From ApiQueryLogEvents::addLogParams
121 $logParams = [];
122 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
123 foreach ( $params as $key => $value ) {
124 if ( strpos( $key, ':' ) === false ) {
125 $logParams[$key] = $value;
126 continue;
127 }
128 $logParam = explode( ':', $key, 3 );
129 $logParams[$logParam[2]] = $value;
130 }
131 $packet['log_params'] = $logParams;
132 } else {
133 $packet['log_params'] = explode( "\n", $rc->getAttribute( 'rc_params' ) );
134 }
135 }
136 $packet['log_action_comment'] = $actionComment;
137 break;
138 }
139
140 $packet['server_url'] = $canonicalServer;
141 $packet['server_name'] = $serverName;
142
143 $packet['server_script_path'] = $scriptPath ?: '/';
144 $packet['wiki'] = WikiMap::getCurrentWikiId();
145
146 return $this->formatArray( $packet );
147 }
148}
150class_alias( MachineReadableRCFeedFormatter::class, 'MachineReadableRCFeedFormatter' );
const RC_NEW
Definition Defines.php:118
const RC_LOG
Definition Defines.php:119
const RC_EDIT
Definition Defines.php:117
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
A class containing constants representing the names of configuration variables.
const ServerName
Name constant for the ServerName setting, for use with Config::get()
const UseRCPatrol
Name constant for the UseRCPatrol setting, for use with Config::get()
const CanonicalServer
Name constant for the CanonicalServer setting, for use with Config::get()
const ScriptPath
Name constant for the ScriptPath setting, for use with Config::get()
const UseNPPatrol
Name constant for the UseNPPatrol setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Abstract class so there can be multiple formatters outputting the same data.
getLine(array $feed, RecentChange $rc, $actionComment)
Generates a notification that can be easily interpreted by a machine.
formatArray(array $packet)
Take the packet and return the formatted string.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Utility class for creating new RC entries.
parseParams()
Parses and returns the rc_params attribute.
getAttribute( $name)
Get an attribute value.
getNotifyUrl()
Get the extra URL that is given as part of the notification to RCFeed consumers.
Interface for RC feed formatters.