Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / debug / logger / monolog / AvroFormatter.php
blobce0cda12d51171f56c5b95998dd76a8c8664e8fa
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.
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.
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
18 * @file
21 namespace MediaWiki\Logger\Monolog;
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;
32 /**
33 * Log message formatter that uses the apache Avro format.
35 * @since 1.26
36 * @author Erik Bernhardson <ebernhardson@wikimedia.org>
37 * @copyright © 2015 Erik Bernhardson and Wikimedia Foundation.
39 class AvroFormatter implements FormatterInterface {
40 /**
41 * @var Magic byte to encode schema revision id.
43 const MAGIC = 0x0;
44 /**
45 * @var array Map from schema name to schema definition
47 protected $schemas;
49 /**
50 * @var AvroStringIO
52 protected $io;
54 /**
55 * @var AvroIOBinaryEncoder
57 protected $encoder;
59 /**
60 * @var AvroIODatumWriter
62 protected $writer;
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.
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();
76 /**
77 * Formats the record context into a binary string per the
78 * schema configured for the records channel.
80 * @param array $record
81 * @return string|null The serialized record, or null if
82 * the record is not valid for the selected schema.
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;
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;
100 return chr( self::MAGIC ) . $this->encodeLong( $revId ) . $this->io->string();
104 * Format a set of records into a list of binary strings
105 * conforming to the configured schema.
107 * @param array $records
108 * @return string[]
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;
118 return $result;
122 * Get the writer for the named channel
124 * @var string $channel Name of the schema to fetch
125 * @return \AvroSchema|null
127 protected function getSchema( $channel ) {
128 if ( !isset( $this->schemas[$channel] ) ) {
129 return null;
131 if ( !isset( $this->schemas[$channel]['revision'], $this->schemas[$channel]['schema'] ) ) {
132 return null;
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()
147 return $this->schemas[$channel]['schema'];
151 * Get the writer for the named channel
153 * @var string $channel Name of the schema
154 * @return int|null
156 public function getSchemaRevisionId( $channel ) {
157 if ( isset( $this->schemas[$channel]['revision'] ) ) {
158 return (int)$this->schemas[$channel]['revision'];
160 return null;
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
169 private function encodeLong( $id ) {
170 $high = ( $id & 0xffffffff00000000 ) >> 32;
171 $low = $id & 0x00000000ffffffff;
172 return pack( 'NN', $high, $low );