MediaWiki master
MachineReadableRCFeedFormatter.php
Go to the documentation of this file.
1<?php
2
22namespace MediaWiki\RCFeed;
23
27use RecentChange;
28
36
44 abstract protected function formatArray( array $packet );
45
57 public function getLine( array $feed, RecentChange $rc, $actionComment ) {
58 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
59 $canonicalServer = $mainConfig->get( MainConfigNames::CanonicalServer );
60 $serverName = $mainConfig->get( MainConfigNames::ServerName );
61 $scriptPath = $mainConfig->get( MainConfigNames::ScriptPath );
62 $packet = [
63 // Usually, RC ID is exposed only for patrolling purposes,
64 // but there is no real reason not to expose it in other cases,
65 // and I can see how this may be potentially useful for clients.
66 'id' => $rc->getAttribute( 'rc_id' ),
67 'type' => RecentChange::parseFromRCType( $rc->getAttribute( 'rc_type' ) ),
68 'namespace' => $rc->getTitle()->getNamespace(),
69 'title' => $rc->getTitle()->getPrefixedText(),
70 'title_url' => $rc->getTitle()->getCanonicalURL(),
71 'comment' => $rc->getAttribute( 'rc_comment' ),
72 'timestamp' => (int)wfTimestamp( TS_UNIX, $rc->getAttribute( 'rc_timestamp' ) ),
73 'user' => $rc->getAttribute( 'rc_user_text' ),
74 'bot' => (bool)$rc->getAttribute( 'rc_bot' ),
75 'notify_url' => $rc->getNotifyUrl(),
76 ];
77
78 if ( isset( $feed['channel'] ) ) {
79 $packet['channel'] = $feed['channel'];
80 }
81
82 $type = $rc->getAttribute( 'rc_type' );
83 if ( $type == RC_EDIT || $type == RC_NEW ) {
84 $useRCPatrol = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseRCPatrol );
85 $useNPPatrol = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseNPPatrol );
86 $packet['minor'] = (bool)$rc->getAttribute( 'rc_minor' );
87 if ( $useRCPatrol || ( $type == RC_NEW && $useNPPatrol ) ) {
88 $packet['patrolled'] = (bool)$rc->getAttribute( 'rc_patrolled' );
89 }
90 }
91
92 switch ( $type ) {
93 case RC_EDIT:
94 $packet['length'] = [
95 'old' => $rc->getAttribute( 'rc_old_len' ),
96 'new' => $rc->getAttribute( 'rc_new_len' )
97 ];
98 $packet['revision'] = [
99 'old' => $rc->getAttribute( 'rc_last_oldid' ),
100 'new' => $rc->getAttribute( 'rc_this_oldid' )
101 ];
102 break;
103
104 case RC_NEW:
105 $packet['length'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_new_len' ) ];
106 $packet['revision'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_this_oldid' ) ];
107 break;
108
109 case RC_LOG:
110 $packet['log_id'] = $rc->getAttribute( 'rc_logid' );
111 $packet['log_type'] = $rc->getAttribute( 'rc_log_type' );
112 $packet['log_action'] = $rc->getAttribute( 'rc_log_action' );
113 if ( $rc->getAttribute( 'rc_params' ) ) {
114 $params = $rc->parseParams();
115 if (
116 // If it's an actual serialised false...
117 $rc->getAttribute( 'rc_params' ) == serialize( false ) ||
118 // Or if we did not get false back when trying to unserialise
119 $params !== false
120 ) {
121 // From ApiQueryLogEvents::addLogParams
122 $logParams = [];
123 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
124 foreach ( $params as $key => $value ) {
125 if ( strpos( $key, ':' ) === false ) {
126 $logParams[$key] = $value;
127 continue;
128 }
129 $logParam = explode( ':', $key, 3 );
130 $logParams[$logParam[2]] = $value;
131 }
132 $packet['log_params'] = $logParams;
133 } else {
134 $packet['log_params'] = explode( "\n", $rc->getAttribute( 'rc_params' ) );
135 }
136 }
137 $packet['log_action_comment'] = $actionComment;
138 break;
139 }
140
141 $packet['server_url'] = $canonicalServer;
142 $packet['server_name'] = $serverName;
143
144 $packet['server_script_path'] = $scriptPath ?: '/';
145 $packet['wiki'] = WikiMap::getCurrentWikiId();
146
147 return $this->formatArray( $packet );
148 }
149}
151class_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 and reading rows in the recentchanges table.
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.