MediaWiki  REL1_22
JSONRCFeedFormatter.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class JSONRCFeedFormatter implements RCFeedFormatter {
00008     public function getLine( array $feed, RecentChange $rc, $actionComment ) {
00009         global $wgCanonicalServer, $wgScriptPath, $wgDBname;
00010         $attrib = $rc->getAttributes();
00011 
00012         $packet = array(
00013             // Usually, RC ID is exposed only for patrolling purposes,
00014             // but there is no real reason not to expose it in other cases,
00015             // and I can see how this may be potentially useful for clients.
00016             'id' => $attrib['rc_id'],
00017             'type' => $attrib['rc_type'],
00018             'namespace' => $rc->getTitle()->getNamespace(),
00019             'title' => $rc->getTitle()->getPrefixedText(),
00020             'comment' => $attrib['rc_comment'],
00021             'timestamp' => (int)wfTimestamp( TS_UNIX, $attrib['rc_timestamp'] ),
00022             'user' => $attrib['rc_user_text'],
00023             'bot' => (bool)$attrib['rc_bot'],
00024         );
00025 
00026         if ( isset( $feed['channel'] ) ) {
00027             $packet['channel'] = $feed['channel'];
00028         }
00029 
00030         $type = $attrib['rc_type'];
00031         if ( $type == RC_EDIT || $type == RC_NEW ) {
00032             global $wgUseRCPatrol, $wgUseNPPatrol;
00033 
00034             $packet['minor'] = $attrib['rc_minor'];
00035             if ( $wgUseRCPatrol || ( $type == RC_NEW && $wgUseNPPatrol ) ) {
00036                 $packet['patrolled'] = $attrib['rc_patrolled'];
00037             }
00038         }
00039 
00040         switch ( $type ) {
00041             case RC_EDIT:
00042                 $packet['length'] = array( 'old' => $attrib['rc_old_len'], 'new' => $attrib['rc_new_len'] );
00043                 $packet['revision'] = array( 'old' => $attrib['rc_last_oldid'], 'new' => $attrib['rc_this_oldid'] );
00044                 break;
00045 
00046             case RC_NEW:
00047                 $packet['length'] = array( 'old' => NULL, 'new' => $attrib['rc_new_len'] );
00048                 $packet['revision'] = array( 'old' => NULL, 'new' => $attrib['rc_this_oldid'] );
00049                 break;
00050 
00051             case RC_LOG:
00052                 $packet['log_type'] = $attrib['rc_log_type'];
00053                 $packet['log_action'] = $attrib['rc_log_action'];
00054                 if ( $attrib['rc_params'] ) {
00055                     wfSuppressWarnings();
00056                     $params = unserialize( $attrib['rc_params'] );
00057                     wfRestoreWarnings();
00058                     if (
00059                         // If it's an actual serialised false...
00060                         $attrib['rc_params'] == serialize( false ) ||
00061                         // Or if we did not get false back when trying to unserialise
00062                         $params !== false
00063                     ) {
00064                         // From ApiQueryLogEvents::addLogParams
00065                         $logParams = array();
00066                         // Keys like "4::paramname" can't be used for output so we change them to "paramname"
00067                         foreach ( $params as $key => $value ) {
00068                             if ( strpos( $key, ':' ) === false ) {
00069                                 $logParams[$key] = $value;
00070                                 continue;
00071                             }
00072                             $logParam = explode( ':', $key, 3 );
00073                             $logParams[$logParam[2]] = $value;
00074                         }
00075                         $packet['log_params'] = $logParams;
00076                     } else {
00077                         $packet['log_params'] = explode( "\n", $attrib['rc_params'] );
00078                     }
00079                 }
00080                 $packet['log_action_comment'] = $actionComment;
00081                 break;
00082         }
00083 
00084         $packet['server_url'] = $wgCanonicalServer;
00085         $packet['server_script_path'] = $wgScriptPath ?: '/';
00086         $packet['wiki'] = $wgDBname;
00087 
00088         return FormatJson::encode( $packet );
00089     }
00090 }