MediaWiki  REL1_23
MachineReadableRCFeedFormatter.php
Go to the documentation of this file.
00001 <?php
00002 
00027 abstract class MachineReadableRCFeedFormatter implements RCFeedFormatter {
00028 
00034     abstract protected function formatArray( array $packet );
00035 
00040     public function getLine( array $feed, RecentChange $rc, $actionComment ) {
00041         global $wgCanonicalServer, $wgScriptPath;
00042         $attrib = $rc->getAttributes();
00043 
00044         $packet = array(
00045             // Usually, RC ID is exposed only for patrolling purposes,
00046             // but there is no real reason not to expose it in other cases,
00047             // and I can see how this may be potentially useful for clients.
00048             'id' => $attrib['rc_id'],
00049             'type' => $attrib['rc_type'],
00050             'namespace' => $rc->getTitle()->getNamespace(),
00051             'title' => $rc->getTitle()->getPrefixedText(),
00052             'comment' => $attrib['rc_comment'],
00053             'timestamp' => (int)wfTimestamp( TS_UNIX, $attrib['rc_timestamp'] ),
00054             'user' => $attrib['rc_user_text'],
00055             'bot' => (bool)$attrib['rc_bot'],
00056         );
00057 
00058         if ( isset( $feed['channel'] ) ) {
00059             $packet['channel'] = $feed['channel'];
00060         }
00061 
00062         $type = $attrib['rc_type'];
00063         if ( $type == RC_EDIT || $type == RC_NEW ) {
00064             global $wgUseRCPatrol, $wgUseNPPatrol;
00065 
00066             $packet['minor'] = $attrib['rc_minor'];
00067             if ( $wgUseRCPatrol || ( $type == RC_NEW && $wgUseNPPatrol ) ) {
00068                 $packet['patrolled'] = $attrib['rc_patrolled'];
00069             }
00070         }
00071 
00072         switch ( $type ) {
00073             case RC_EDIT:
00074                 $packet['length'] = array(
00075                     'old' => $attrib['rc_old_len'],
00076                     'new' => $attrib['rc_new_len']
00077                 );
00078                 $packet['revision'] = array(
00079                     'old' => $attrib['rc_last_oldid'],
00080                     'new' => $attrib['rc_this_oldid']
00081                 );
00082                 break;
00083 
00084             case RC_NEW:
00085                 $packet['length'] = array( 'old' => null, 'new' => $attrib['rc_new_len'] );
00086                 $packet['revision'] = array( 'old' => null, 'new' => $attrib['rc_this_oldid'] );
00087                 break;
00088 
00089             case RC_LOG:
00090                 $packet['log_type'] = $attrib['rc_log_type'];
00091                 $packet['log_action'] = $attrib['rc_log_action'];
00092                 if ( $attrib['rc_params'] ) {
00093                     wfSuppressWarnings();
00094                     $params = unserialize( $attrib['rc_params'] );
00095                     wfRestoreWarnings();
00096                     if (
00097                         // If it's an actual serialised false...
00098                         $attrib['rc_params'] == serialize( false ) ||
00099                         // Or if we did not get false back when trying to unserialise
00100                         $params !== false
00101                     ) {
00102                         // From ApiQueryLogEvents::addLogParams
00103                         $logParams = array();
00104                         // Keys like "4::paramname" can't be used for output so we change them to "paramname"
00105                         foreach ( $params as $key => $value ) {
00106                             if ( strpos( $key, ':' ) === false ) {
00107                                 $logParams[$key] = $value;
00108                                 continue;
00109                             }
00110                             $logParam = explode( ':', $key, 3 );
00111                             $logParams[$logParam[2]] = $value;
00112                         }
00113                         $packet['log_params'] = $logParams;
00114                     } else {
00115                         $packet['log_params'] = explode( "\n", $attrib['rc_params'] );
00116                     }
00117                 }
00118                 $packet['log_action_comment'] = $actionComment;
00119                 break;
00120         }
00121 
00122         $packet['server_url'] = $wgCanonicalServer;
00123         $packet['server_script_path'] = $wgScriptPath ?: '/';
00124         $packet['wiki'] = wfWikiID();
00125 
00126         return $this->formatArray( $packet );
00127     }
00128 }