MediaWiki REL1_28
XMPValidate.php
Go to the documentation of this file.
1<?php
24use Psr\Log\LoggerInterface;
25use Psr\Log\LoggerAwareInterface;
26
46class XMPValidate implements LoggerAwareInterface {
47
51 private $logger;
52
53 public function __construct( LoggerInterface $logger ) {
54 $this->setLogger( $logger );
55 }
56
57 public function setLogger( LoggerInterface $logger ) {
58 $this->logger = $logger;
59 }
67 public function validateBoolean( $info, &$val, $standalone ) {
68 if ( !$standalone ) {
69 // this only validates standalone properties, not arrays, etc
70 return;
71 }
72 if ( $val !== 'True' && $val !== 'False' ) {
73 $this->logger->info( __METHOD__ . " Expected True or False but got $val" );
74 $val = null;
75 }
76 }
77
85 public function validateRational( $info, &$val, $standalone ) {
86 if ( !$standalone ) {
87 // this only validates standalone properties, not arrays, etc
88 return;
89 }
90 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
91 $this->logger->info( __METHOD__ . " Expected rational but got $val" );
92 $val = null;
93 }
94 }
95
106 public function validateRating( $info, &$val, $standalone ) {
107 if ( !$standalone ) {
108 // this only validates standalone properties, not arrays, etc
109 return;
110 }
111 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
112 || !is_numeric( $val )
113 ) {
114 $this->logger->info( __METHOD__ . " Expected rating but got $val" );
115 $val = null;
116
117 return;
118 } else {
119 $nVal = (float)$val;
120 if ( $nVal < 0 ) {
121 // We do < 0 here instead of < -1 here, since
122 // the values between 0 and -1 are also illegal
123 // as -1 is meant as a special reject rating.
124 $this->logger->info( __METHOD__ . " Rating too low, setting to -1 (Rejected)" );
125 $val = '-1';
126
127 return;
128 }
129 if ( $nVal > 5 ) {
130 $this->logger->info( __METHOD__ . " Rating too high, setting to 5" );
131 $val = '5';
132
133 return;
134 }
135 }
136 }
137
145 public function validateInteger( $info, &$val, $standalone ) {
146 if ( !$standalone ) {
147 // this only validates standalone properties, not arrays, etc
148 return;
149 }
150 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
151 $this->logger->info( __METHOD__ . " Expected integer but got $val" );
152 $val = null;
153 }
154 }
155
164 public function validateClosed( $info, &$val, $standalone ) {
165 if ( !$standalone ) {
166 // this only validates standalone properties, not arrays, etc
167 return;
168 }
169
170 // check if its in a numeric range
171 $inRange = false;
172 if ( isset( $info['rangeLow'] )
173 && isset( $info['rangeHigh'] )
174 && is_numeric( $val )
175 && ( intval( $val ) <= $info['rangeHigh'] )
176 && ( intval( $val ) >= $info['rangeLow'] )
177 ) {
178 $inRange = true;
179 }
180
181 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
182 $this->logger->info( __METHOD__ . " Expected closed choice, but got $val" );
183 $val = null;
184 }
185 }
186
194 public function validateFlash( $info, &$val, $standalone ) {
195 if ( $standalone ) {
196 // this only validates flash structs, not individual properties
197 return;
198 }
199 if ( !( isset( $val['Fired'] )
200 && isset( $val['Function'] )
201 && isset( $val['Mode'] )
202 && isset( $val['RedEyeMode'] )
203 && isset( $val['Return'] )
204 ) ) {
205 $this->logger->info( __METHOD__ . " Flash structure did not have all the required components" );
206 $val = null;
207 } else {
208 $val = ( "\0" | ( $val['Fired'] === 'True' )
209 | ( intval( $val['Return'] ) << 1 )
210 | ( intval( $val['Mode'] ) << 3 )
211 | ( ( $val['Function'] === 'True' ) << 5 )
212 | ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
213 }
214 }
215
229 public function validateLangCode( $info, &$val, $standalone ) {
230 if ( !$standalone ) {
231 // this only validates standalone properties, not arrays, etc
232 return;
233 }
234 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
235 // this is a rather naive check.
236 $this->logger->info( __METHOD__ . " Expected Lang code but got $val" );
237 $val = null;
238 }
239 }
240
258 public function validateDate( $info, &$val, $standalone ) {
259 if ( !$standalone ) {
260 // this only validates standalone properties, not arrays, etc
261 return;
262 }
263 $res = [];
264 // @codingStandardsIgnoreStart Long line that cannot be broken
265 if ( !preg_match(
266 /* ahh! scary regex... */
267 '/^([0-3]\d{3})(?:-([01]\d)(?:-([0-3]\d)(?:T([0-2]\d):([0-6]\d)(?::([0-6]\d)(?:\.\d+)?)?([-+]\d{2}:\d{2}|Z)?)?)?)?$/D',
268 $val, $res )
269 ) {
270 // @codingStandardsIgnoreEnd
271
272 $this->logger->info( __METHOD__ . " Expected date but got $val" );
273 $val = null;
274 } else {
275 /*
276 * $res is formatted as follows:
277 * 0 -> full date.
278 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
279 * 7-> Timezone specifier (Z or something like +12:30 )
280 * many parts are optional, some aren't. For example if you specify
281 * minute, you must specify hour, day, month, and year but not second or TZ.
282 */
283
284 /*
285 * First of all, if year = 0000, Something is wrongish,
286 * so don't extract. This seems to happen when
287 * some programs convert between metadata formats.
288 */
289 if ( $res[1] === '0000' ) {
290 $this->logger->info( __METHOD__ . " Invalid date (year 0): $val" );
291 $val = null;
292
293 return;
294 }
295
296 if ( !isset( $res[4] ) ) { // hour
297 // just have the year month day (if that)
298 $val = $res[1];
299 if ( isset( $res[2] ) ) {
300 $val .= ':' . $res[2];
301 }
302 if ( isset( $res[3] ) ) {
303 $val .= ':' . $res[3];
304 }
305
306 return;
307 }
308
309 if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
310 // if hour is set, then minute must also be or regex above will fail.
311 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
312 . ' ' . $res[4] . ':' . $res[5];
313 if ( isset( $res[6] ) && $res[6] !== '' ) {
314 $val .= ':' . $res[6];
315 }
316
317 return;
318 }
319
320 // Extra check for empty string necessary due to TZ but no second case.
321 $stripSeconds = false;
322 if ( !isset( $res[6] ) || $res[6] === '' ) {
323 $res[6] = '00';
324 $stripSeconds = true;
325 }
326
327 // Do timezone processing. We've already done the case that tz = Z.
328
329 // We know that if we got to this step, year, month day hour and min must be set
330 // by virtue of regex not failing.
331
333 $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6]
334 );
335 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
336 $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
337 if ( substr( $res[7], 0, 1 ) === '-' ) {
338 $offset = -$offset;
339 }
340 $val = ConvertibleTimestamp::convert( TS_EXIF, $unix + $offset );
341
342 if ( $stripSeconds ) {
343 // If seconds weren't specified, remove the trailing ':00'.
344 $val = substr( $val, 0, -3 );
345 }
346 }
347 }
348
361 public function validateGPS( $info, &$val, $standalone ) {
362 if ( !$standalone ) {
363 return;
364 }
365
366 $m = [];
367 if ( preg_match(
368 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
369 $val, $m )
370 ) {
371 $coord = intval( $m[1] );
372 $coord += intval( $m[2] ) * ( 1 / 60 );
373 $coord += intval( $m[3] ) * ( 1 / 3600 );
374 if ( $m[4] === 'S' || $m[4] === 'W' ) {
375 $coord = -$coord;
376 }
377 $val = $coord;
378
379 return;
380 } elseif ( preg_match(
381 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
382 $val, $m )
383 ) {
384 $coord = intval( $m[1] );
385 $coord += floatval( $m[2] ) * ( 1 / 60 );
386 if ( $m[3] === 'S' || $m[3] === 'W' ) {
387 $coord = -$coord;
388 }
389 $val = $coord;
390
391 return;
392 } else {
393 $this->logger->info( __METHOD__
394 . " Expected GPSCoordinate, but got $val." );
395 $val = null;
396
397 return;
398 }
399 }
400}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static convert( $style=TS_UNIX, $ts)
Convert a timestamp string to a given format.
This contains some static methods for validating XMP properties.
validateRating( $info, &$val, $standalone)
function to validate rating properties -1, 0-5
validateInteger( $info, &$val, $standalone)
function to validate integers
validateClosed( $info, &$val, $standalone)
function to validate properties with a fixed number of allowed choices.
setLogger(LoggerInterface $logger)
validateBoolean( $info, &$val, $standalone)
Function to validate boolean properties ( True or False )
validateDate( $info, &$val, $standalone)
function to validate date properties, and convert to (partial) Exif format.
validateGPS( $info, &$val, $standalone)
function to validate, and more importantly translate the XMP DMS form of gps coords to the decimal fo...
__construct(LoggerInterface $logger)
LoggerInterface $logger
validateRational( $info, &$val, $standalone)
function to validate rational properties ( 12/10 )
validateFlash( $info, &$val, $standalone)
function to validate and modify flash structure
validateLangCode( $info, &$val, $standalone)
function to validate LangCode properties ( en-GB, etc )
$res
Definition database.txt:21
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition defines.php:6
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
Definition defines.php:37