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