MediaWiki  1.23.6
XMPValidate.php
Go to the documentation of this file.
1 <?php
43 class XMPValidate {
51  public static function validateBoolean( $info, &$val, $standalone ) {
52  if ( !$standalone ) {
53  // this only validates standalone properties, not arrays, etc
54  return;
55  }
56  if ( $val !== 'True' && $val !== 'False' ) {
57  wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
58  $val = null;
59  }
60  }
61 
69  public static function validateRational( $info, &$val, $standalone ) {
70  if ( !$standalone ) {
71  // this only validates standalone properties, not arrays, etc
72  return;
73  }
74  if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
75  wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" );
76  $val = null;
77  }
78  }
79 
90  public static function validateRating( $info, &$val, $standalone ) {
91  if ( !$standalone ) {
92  // this only validates standalone properties, not arrays, etc
93  return;
94  }
95  if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
96  || !is_numeric( $val )
97  ) {
98  wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" );
99  $val = null;
100 
101  return;
102  } else {
103  $nVal = (float)$val;
104  if ( $nVal < 0 ) {
105  // We do < 0 here instead of < -1 here, since
106  // the values between 0 and -1 are also illegal
107  // as -1 is meant as a special reject rating.
108  wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)" );
109  $val = '-1';
110 
111  return;
112  }
113  if ( $nVal > 5 ) {
114  wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5" );
115  $val = '5';
116 
117  return;
118  }
119  }
120  }
121 
129  public static function validateInteger( $info, &$val, $standalone ) {
130  if ( !$standalone ) {
131  // this only validates standalone properties, not arrays, etc
132  return;
133  }
134  if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
135  wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
136  $val = null;
137  }
138  }
139 
148  public static function validateClosed( $info, &$val, $standalone ) {
149  if ( !$standalone ) {
150  // this only validates standalone properties, not arrays, etc
151  return;
152  }
153 
154  //check if its in a numeric range
155  $inRange = false;
156  if ( isset( $info['rangeLow'] )
157  && isset( $info['rangeHigh'] )
158  && is_numeric( $val )
159  && ( intval( $val ) <= $info['rangeHigh'] )
160  && ( intval( $val ) >= $info['rangeLow'] )
161  ) {
162  $inRange = true;
163  }
164 
165  if ( !isset( $info['choices'][$val] ) && !$inRange ) {
166  wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
167  $val = null;
168  }
169  }
170 
178  public static function validateFlash( $info, &$val, $standalone ) {
179  if ( $standalone ) {
180  // this only validates flash structs, not individual properties
181  return;
182  }
183  if ( !( isset( $val['Fired'] )
184  && isset( $val['Function'] )
185  && isset( $val['Mode'] )
186  && isset( $val['RedEyeMode'] )
187  && isset( $val['Return'] )
188  ) ) {
189  wfDebugLog( 'XMP', __METHOD__ . " Flash structure did not have all the required components" );
190  $val = null;
191  } else {
192  $val = ( "\0" | ( $val['Fired'] === 'True' )
193  | ( intval( $val['Return'] ) << 1 )
194  | ( intval( $val['Mode'] ) << 3 )
195  | ( ( $val['Function'] === 'True' ) << 5 )
196  | ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
197  }
198  }
199 
212  public static function validateLangCode( $info, &$val, $standalone ) {
213  if ( !$standalone ) {
214  // this only validates standalone properties, not arrays, etc
215  return;
216  }
217  if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
218  //this is a rather naive check.
219  wfDebugLog( 'XMP', __METHOD__ . " Expected Lang code but got $val" );
220  $val = null;
221  }
222  }
223 
241  public static function validateDate( $info, &$val, $standalone ) {
242  if ( !$standalone ) {
243  // this only validates standalone properties, not arrays, etc
244  return;
245  }
246  $res = array();
247  // @codingStandardsIgnoreStart Long line that cannot be broken
248  if ( !preg_match(
249  /* ahh! scary regex... */
250  '/^([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',
251  $val, $res )
252  ) {
253  // @codingStandardsIgnoreEnd
254 
255  wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
256  $val = null;
257  } else {
258  /*
259  * $res is formatted as follows:
260  * 0 -> full date.
261  * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
262  * 7-> Timezone specifier (Z or something like +12:30 )
263  * many parts are optional, some aren't. For example if you specify
264  * minute, you must specify hour, day, month, and year but not second or TZ.
265  */
266 
267  /*
268  * First of all, if year = 0000, Something is wrongish,
269  * so don't extract. This seems to happen when
270  * some programs convert between metadata formats.
271  */
272  if ( $res[1] === '0000' ) {
273  wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
274  $val = null;
275 
276  return;
277  }
278 
279  if ( !isset( $res[4] ) ) { //hour
280  //just have the year month day (if that)
281  $val = $res[1];
282  if ( isset( $res[2] ) ) {
283  $val .= ':' . $res[2];
284  }
285  if ( isset( $res[3] ) ) {
286  $val .= ':' . $res[3];
287  }
288 
289  return;
290  }
291 
292  if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
293  //if hour is set, then minute must also be or regex above will fail.
294  $val = $res[1] . ':' . $res[2] . ':' . $res[3]
295  . ' ' . $res[4] . ':' . $res[5];
296  if ( isset( $res[6] ) && $res[6] !== '' ) {
297  $val .= ':' . $res[6];
298  }
299 
300  return;
301  }
302 
303  // Extra check for empty string necessary due to TZ but no second case.
304  $stripSeconds = false;
305  if ( !isset( $res[6] ) || $res[6] === '' ) {
306  $res[6] = '00';
307  $stripSeconds = true;
308  }
309 
310  // Do timezone processing. We've already done the case that tz = Z.
311 
312  // We know that if we got to this step, year, month day hour and min must be set
313  // by virtue of regex not failing.
314 
315  $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
316  $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
317  $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
318  if ( substr( $res[7], 0, 1 ) === '-' ) {
319  $offset = -$offset;
320  }
321  $val = wfTimestamp( TS_EXIF, $unix + $offset );
322 
323  if ( $stripSeconds ) {
324  // If seconds weren't specified, remove the trailing ':00'.
325  $val = substr( $val, 0, -3 );
326  }
327  }
328  }
329 
342  public static function validateGPS( $info, &$val, $standalone ) {
343  if ( !$standalone ) {
344  return;
345  }
346 
347  $m = array();
348  if ( preg_match(
349  '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
350  $val, $m )
351  ) {
352  $coord = intval( $m[1] );
353  $coord += intval( $m[2] ) * ( 1 / 60 );
354  $coord += intval( $m[3] ) * ( 1 / 3600 );
355  if ( $m[4] === 'S' || $m[4] === 'W' ) {
356  $coord = -$coord;
357  }
358  $val = $coord;
359 
360  return;
361  } elseif ( preg_match(
362  '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
363  $val, $m )
364  ) {
365  $coord = intval( $m[1] );
366  $coord += floatval( $m[2] ) * ( 1 / 60 );
367  if ( $m[3] === 'S' || $m[3] === 'W' ) {
368  $coord = -$coord;
369  }
370  $val = $coord;
371 
372  return;
373  } else {
374  wfDebugLog( 'XMP', __METHOD__
375  . " Expected GPSCoordinate, but got $val." );
376  $val = null;
377 
378  return;
379  }
380  }
381 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
XMPValidate\validateBoolean
static validateBoolean( $info, &$val, $standalone)
Function to validate boolean properties ( True or False )
Definition: XMPValidate.php:51
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1040
XMPValidate\validateFlash
static validateFlash( $info, &$val, $standalone)
function to validate and modify flash structure
Definition: XMPValidate.php:178
TS_EXIF
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
Definition: GlobalFunctions.php:2457
XMPValidate\validateRational
static validateRational( $info, &$val, $standalone)
function to validate rational properties ( 12/10 )
Definition: XMPValidate.php:69
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
XMPValidate\validateClosed
static validateClosed( $info, &$val, $standalone)
function to validate properties with a fixed number of allowed choices.
Definition: XMPValidate.php:148
XMPValidate\validateLangCode
static validateLangCode( $info, &$val, $standalone)
function to validate LangCode properties ( en-GB, etc )
Definition: XMPValidate.php:212
XMPValidate
This contains some static methods for validating XMP properties.
Definition: XMPValidate.php:43
XMPValidate\validateInteger
static validateInteger( $info, &$val, $standalone)
function to validate integers
Definition: XMPValidate.php:129
XMPValidate\validateRating
static validateRating( $info, &$val, $standalone)
function to validate rating properties -1, 0-5
Definition: XMPValidate.php:90
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2426
XMPValidate\validateDate
static validateDate( $info, &$val, $standalone)
function to validate date properties, and convert to (partial) Exif format.
Definition: XMPValidate.php:241
XMPValidate\validateGPS
static validateGPS( $info, &$val, $standalone)
function to validate, and more importantly translate the XMP DMS form of gps coords to the decimal fo...
Definition: XMPValidate.php:342
$res
$res
Definition: database.txt:21