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