Merge "Actually use $ignoreInvalidDB in UserRightProxy::getDB()"
[lhc/web/wiklou.git] / includes / rcfeed / JSONRCFeedFormatter.php
1 <?php
2 class JSONRCFeedFormatter implements RCFeedFormatter {
3 /**
4 * Generates a notification that can be easily interpreted by a machine.
5 * @see RCFeedFormatter::getLine
6 */
7 public function getLine( array $feed, RecentChange $rc, $actionComment ) {
8 global $wgCanonicalServer, $wgScriptPath, $wgArticlePath, $wgDBname;
9 $attrib = $rc->getAttributes();
10
11 $packet = array(
12 // Usually, RC ID is exposed only for patrolling purposes,
13 // but there is no real reason not to expose it in other cases,
14 // and I can see how this may be potentially useful for clients.
15 'id' => $attrib['rc_id'],
16 'type' => $attrib['rc_type'],
17 'namespace' => $rc->getTitle()->getNamespace(),
18 'title' => $rc->getTitle()->getPrefixedText(),
19 'comment' => $attrib['rc_comment'],
20 'timestamp' => (int)wfTimestamp( TS_UNIX, $attrib['rc_timestamp'] ),
21 'user' => $attrib['rc_user_text'],
22 'bot' => (bool)$attrib['rc_bot'],
23 );
24
25 if ( isset( $feed['channel'] ) ) {
26 $packet['channel'] = $feed['channel'];
27 }
28
29 $type = $attrib['rc_type'];
30 if ( $type == RC_EDIT || $type == RC_NEW ) {
31 global $wgUseRCPatrol, $wgUseNPPatrol;
32
33 $packet['minor'] = $attrib['rc_minor'];
34 if ( $wgUseRCPatrol || ( $type == RC_NEW && $wgUseNPPatrol ) ) {
35 $packet['patrolled'] = $attrib['rc_patrolled'];
36 }
37 }
38
39 switch ( $type ) {
40 case RC_EDIT:
41 $packet['length'] = array( 'old' => $attrib['rc_old_len'], 'new' => $attrib['rc_new_len'] );
42 $packet['revision'] = array( 'old' => $attrib['rc_last_oldid'], 'new' => $attrib['rc_this_oldid'] );
43 break;
44
45 case RC_NEW:
46 $packet['length'] = array( 'old' => NULL, 'new' => $attrib['rc_new_len'] );
47 $packet['revision'] = array( 'old' => NULL, 'new' => $attrib['rc_this_oldid'] );
48 break;
49
50 case RC_LOG:
51 $packet['log_type'] = $attrib['rc_log_type'];
52 $packet['log_action'] = $attrib['rc_log_action'];
53 if ( $attrib['rc_params'] ) {
54 wfSuppressWarnings();
55 $params = unserialize( $attrib['rc_params'] );
56 wfRestoreWarnings();
57 if (
58 // If it's an actual serialised false...
59 $attrib['rc_params'] == serialize( false ) ||
60 // Or if we did not get false back when trying to unserialise
61 $params !== false
62 ) {
63 // From ApiQueryLogEvents::addLogParams
64 $logParams = array();
65 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
66 foreach ( $params as $key => $value ) {
67 if ( strpos( $key, ':' ) === false ) {
68 $logParams[$key] = $value;
69 continue;
70 }
71 $logParam = explode( ':', $key, 3 );
72 $logParams[$logParam[2]] = $value;
73 }
74 $packet['log_params'] = $logParams;
75 } else {
76 $packet['log_params'] = explode( "\n", $attrib['rc_params'] );
77 }
78 }
79 $packet['log_action_comment'] = $actionComment;
80 break;
81 }
82
83 $packet['server_url'] = $wgCanonicalServer;
84 $packet['server_script_path'] = $wgScriptPath ?: '/';
85 $packet['wiki'] = $wgDBname;
86
87 return FormatJson::encode( $packet );
88 }
89 }