Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / debug / logger / monolog / AvroFormatter.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Logger\Monolog;
22
23 use AvroIODatumWriter;
24 use AvroIOBinaryEncoder;
25 use AvroIOTypeException;
26 use AvroNamedSchemata;
27 use AvroSchema;
28 use AvroStringIO;
29 use AvroValidator;
30 use Monolog\Formatter\FormatterInterface;
31
32 /**
33 * Log message formatter that uses the apache Avro format.
34 *
35 * @since 1.26
36 * @author Erik Bernhardson <ebernhardson@wikimedia.org>
37 * @copyright © 2015 Erik Bernhardson and Wikimedia Foundation.
38 */
39 class AvroFormatter implements FormatterInterface {
40 /**
41 * @var Magic byte to encode schema revision id.
42 */
43 const MAGIC = 0x0;
44 /**
45 * @var array Map from schema name to schema definition
46 */
47 protected $schemas;
48
49 /**
50 * @var AvroStringIO
51 */
52 protected $io;
53
54 /**
55 * @var AvroIOBinaryEncoder
56 */
57 protected $encoder;
58
59 /**
60 * @var AvroIODatumWriter
61 */
62 protected $writer;
63
64 /**
65 * @var array $schemas Map from Monolog channel to Avro schema.
66 * Each schema can be either the JSON string or decoded into PHP
67 * arrays.
68 */
69 public function __construct( array $schemas ) {
70 $this->schemas = $schemas;
71 $this->io = new AvroStringIO( '' );
72 $this->encoder = new AvroIOBinaryEncoder( $this->io );
73 $this->writer = new AvroIODatumWriter();
74 }
75
76 /**
77 * Formats the record context into a binary string per the
78 * schema configured for the records channel.
79 *
80 * @param array $record
81 * @return string|null The serialized record, or null if
82 * the record is not valid for the selected schema.
83 */
84 public function format( array $record ) {
85 $this->io->truncate();
86 $schema = $this->getSchema( $record['channel'] );
87 $revId = $this->getSchemaRevisionId( $record['channel'] );
88 if ( $schema === null || $revId === null ) {
89 trigger_error( "The schema for channel '{$record['channel']}' is not available" );
90 return null;
91 }
92 try {
93 $this->writer->write_data( $schema, $record['context'], $this->encoder );
94 } catch ( AvroIOTypeException $e ) {
95 $errors = AvroValidator::getErrors( $schema, $record['context'] );
96 $json = json_encode( $errors );
97 trigger_error( "Avro failed to serialize record for {$record['channel']} : {$json}" );
98 return null;
99 }
100 return chr( self::MAGIC ) . $this->encodeLong( $revId ) . $this->io->string();
101 }
102
103 /**
104 * Format a set of records into a list of binary strings
105 * conforming to the configured schema.
106 *
107 * @param array $records
108 * @return string[]
109 */
110 public function formatBatch( array $records ) {
111 $result = [];
112 foreach ( $records as $record ) {
113 $message = $this->format( $record );
114 if ( $message !== null ) {
115 $result[] = $message;
116 }
117 }
118 return $result;
119 }
120
121 /**
122 * Get the writer for the named channel
123 *
124 * @var string $channel Name of the schema to fetch
125 * @return \AvroSchema|null
126 */
127 protected function getSchema( $channel ) {
128 if ( !isset( $this->schemas[$channel] ) ) {
129 return null;
130 }
131 if ( !isset( $this->schemas[$channel]['revision'], $this->schemas[$channel]['schema'] ) ) {
132 return null;
133 }
134
135 if ( !$this->schemas[$channel]['schema'] instanceof AvroSchema ) {
136 $schema = $this->schemas[$channel]['schema'];
137 if ( is_string( $schema ) ) {
138 $this->schemas[$channel]['schema'] = AvroSchema::parse( $schema );
139 } else {
140 $this->schemas[$channel]['schema'] = AvroSchema::real_parse(
141 $schema,
142 null,
143 new AvroNamedSchemata()
144 );
145 }
146 }
147 return $this->schemas[$channel]['schema'];
148 }
149
150 /**
151 * Get the writer for the named channel
152 *
153 * @var string $channel Name of the schema
154 * @return int|null
155 */
156 public function getSchemaRevisionId( $channel ) {
157 if ( isset( $this->schemas[$channel]['revision'] ) ) {
158 return (int)$this->schemas[$channel]['revision'];
159 }
160 return null;
161 }
162
163 /**
164 * convert an integer to a 64bits big endian long (Java compatible)
165 * NOTE: certainly only compatible with PHP 64bits
166 * @param int $id
167 * @return string the binary representation of $id
168 */
169 private function encodeLong( $id ) {
170 $high = ( $id & 0xffffffff00000000 ) >> 32;
171 $low = $id & 0x00000000ffffffff;
172 return pack( 'NN', $high, $low );
173 }
174 }