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