Merge "Perform a permission check on the title when changing the page language"
[lhc/web/wiklou.git] / includes / debug / logger / monolog / LegacyHandler.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 LogicException;
24 use MediaWiki\Logger\LegacyLogger;
25 use Monolog\Handler\AbstractProcessingHandler;
26 use Monolog\Logger;
27 use UnexpectedValueException;
28
29 /**
30 * Log handler that replicates the behavior of MediaWiki's wfErrorLog()
31 * logging service. Log output can be directed to a local file, a PHP stream,
32 * or a udp2log server.
33 *
34 * For udp2log output, the stream specification must have the form:
35 * "udp://HOST:PORT[/PREFIX]"
36 * where:
37 * - HOST: IPv4, IPv6 or hostname
38 * - PORT: server port
39 * - PREFIX: optional (but recommended) prefix telling udp2log how to route
40 * the log event. The special prefix "{channel}" will use the log event's
41 * channel as the prefix value.
42 *
43 * When not targeting a udp2log stream this class will act as a drop-in
44 * replacement for \Monolog\Handler\StreamHandler.
45 *
46 * @since 1.25
47 * @copyright © 2013 Wikimedia Foundation and contributors
48 */
49 class LegacyHandler extends AbstractProcessingHandler {
50
51 /**
52 * Log sink descriptor
53 * @var string $uri
54 */
55 protected $uri;
56
57 /**
58 * Filter log events using legacy rules
59 * @var bool $useLegacyFilter
60 */
61 protected $useLegacyFilter;
62
63 /**
64 * Log sink
65 * @var resource $sink
66 */
67 protected $sink;
68
69 /**
70 * @var string $error
71 */
72 protected $error;
73
74 /**
75 * @var string $host
76 */
77 protected $host;
78
79 /**
80 * @var int $port
81 */
82 protected $port;
83
84 /**
85 * @var string $prefix
86 */
87 protected $prefix;
88
89 /**
90 * @param string $stream Stream URI
91 * @param bool $useLegacyFilter Filter log events using legacy rules
92 * @param int $level Minimum logging level that will trigger handler
93 * @param bool $bubble Can handled meesages bubble up the handler stack?
94 */
95 public function __construct(
96 $stream,
97 $useLegacyFilter = false,
98 $level = Logger::DEBUG,
99 $bubble = true
100 ) {
101 parent::__construct( $level, $bubble );
102 $this->uri = $stream;
103 $this->useLegacyFilter = $useLegacyFilter;
104 }
105
106 /**
107 * Open the log sink described by our stream URI.
108 */
109 protected function openSink() {
110 if ( !$this->uri ) {
111 throw new LogicException(
112 'Missing stream uri, the stream can not be opened.' );
113 }
114 $this->error = null;
115 set_error_handler( [ $this, 'errorTrap' ] );
116
117 if ( substr( $this->uri, 0, 4 ) == 'udp:' ) {
118 $parsed = parse_url( $this->uri );
119 if ( !isset( $parsed['host'] ) ) {
120 throw new UnexpectedValueException( sprintf(
121 'Udp transport "%s" must specify a host', $this->uri
122 ) );
123 }
124 if ( !isset( $parsed['port'] ) ) {
125 throw new UnexpectedValueException( sprintf(
126 'Udp transport "%s" must specify a port', $this->uri
127 ) );
128 }
129
130 $this->host = $parsed['host'];
131 $this->port = $parsed['port'];
132 $this->prefix = '';
133
134 if ( isset( $parsed['path'] ) ) {
135 $this->prefix = ltrim( $parsed['path'], '/' );
136 }
137
138 if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
139 $domain = AF_INET6;
140
141 } else {
142 $domain = AF_INET;
143 }
144
145 $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
146
147 } else {
148 $this->sink = fopen( $this->uri, 'a' );
149 }
150 restore_error_handler();
151
152 if ( !is_resource( $this->sink ) ) {
153 $this->sink = null;
154 throw new UnexpectedValueException( sprintf(
155 'The stream or file "%s" could not be opened: %s',
156 $this->uri, $this->error
157 ) );
158 }
159 }
160
161 /**
162 * Custom error handler.
163 * @param int $code Error number
164 * @param string $msg Error message
165 */
166 protected function errorTrap( $code, $msg ) {
167 $this->error = $msg;
168 }
169
170 /**
171 * Should we use UDP to send messages to the sink?
172 * @return bool
173 */
174 protected function useUdp() {
175 return $this->host !== null;
176 }
177
178 protected function write( array $record ) {
179 if ( $this->useLegacyFilter &&
180 !LegacyLogger::shouldEmit(
181 $record['channel'], $record['message'],
182 $record['level'], $record
183 ) ) {
184 // Do not write record if we are enforcing legacy rules and they
185 // do not pass this message. This used to be done in isHandling(),
186 // but Monolog 1.12.0 made a breaking change that removed access
187 // to the needed channel and context information.
188 return;
189 }
190
191 if ( $this->sink === null ) {
192 $this->openSink();
193 }
194
195 $text = (string)$record['formatted'];
196 if ( $this->useUdp() ) {
197 // Clean it up for the multiplexer
198 if ( $this->prefix !== '' ) {
199 $leader = ( $this->prefix === '{channel}' ) ?
200 $record['channel'] : $this->prefix;
201 $text = preg_replace( '/^/m', "{$leader} ", $text );
202
203 // Limit to 64KB
204 if ( strlen( $text ) > 65506 ) {
205 $text = substr( $text, 0, 65506 );
206 }
207
208 if ( substr( $text, -1 ) != "\n" ) {
209 $text .= "\n";
210 }
211
212 } elseif ( strlen( $text ) > 65507 ) {
213 $text = substr( $text, 0, 65507 );
214 }
215
216 socket_sendto(
217 $this->sink, $text, strlen( $text ), 0, $this->host, $this->port
218 );
219
220 } else {
221 fwrite( $this->sink, $text );
222 }
223 }
224
225 public function close() {
226 if ( is_resource( $this->sink ) ) {
227 if ( $this->useUdp() ) {
228 socket_close( $this->sink );
229
230 } else {
231 fclose( $this->sink );
232 }
233 }
234 $this->sink = null;
235 }
236 }