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