MediaWiki  REL1_20
IPTC.php
Go to the documentation of this file.
00001 <?php
00029 class IPTC {
00030 
00041         static function parse( $rawData ) {
00042                 $parsed = iptcparse( $rawData );
00043                 $data = Array();
00044                 if (!is_array($parsed)) {
00045                                 return $data;
00046                 }
00047 
00048                 $c = '';
00049                 //charset info contained in tag 1:90.
00050                 if (isset($parsed['1#090']) && isset($parsed['1#090'][0])) {
00051                         $c = self::getCharset($parsed['1#090'][0]);
00052                         if ($c === false) {
00053                                 //Unknown charset. refuse to parse.
00054                                 //note: There is a different between
00055                                 //unknown and no charset specified.
00056                                 return array();
00057                         }
00058                         unset( $parsed['1#090'] );
00059                 }
00060 
00061                 foreach ( $parsed as $tag => $val ) {
00062                         if ( isset( $val[0] ) && trim($val[0]) == '' ) {
00063                                 wfDebugLog('iptc', "IPTC tag $tag had only whitespace as its value.");
00064                                 continue;
00065                         }
00066                         switch( $tag ) {
00067                                 case '2#120': /*IPTC caption. mapped with exif ImageDescription*/
00068                                         $data['ImageDescription'] = self::convIPTC( $val, $c );
00069                                         break;
00070                                 case '2#116': /* copyright. Mapped with exif copyright */
00071                                         $data['Copyright'] = self::convIPTC( $val, $c );
00072                                         break;
00073                                 case '2#080': /* byline. Mapped with exif Artist */
00074                                         /* merge with byline title (2:85)
00075                                          * like how exif does it with
00076                                          * Title, person. Not sure if this is best
00077                                          * approach since we no longer have the two fields
00078                                          * separate. each byline title entry corresponds to a
00079                                          * specific byline.                          */
00080 
00081                                         $bylines = self::convIPTC( $val, $c );
00082                                         if ( isset( $parsed['2#085'] ) ) {
00083                                                 $titles = self::convIPTC( $parsed['2#085'], $c );
00084                                         } else {
00085                                                 $titles = array();
00086                                         }
00087 
00088                                         for ( $i = 0; $i < count( $titles ); $i++ ) {
00089                                                 if ( isset( $bylines[$i] ) ) {
00090                                                         // theoretically this should always be set
00091                                                         // but doesn't hurt to be careful.
00092                                                         $bylines[$i] = $titles[$i] . ', ' . $bylines[$i];
00093                                                 }
00094                                         }
00095                                         $data['Artist'] = $bylines;
00096                                         break;
00097                                 case '2#025': /* keywords */
00098                                         $data['Keywords'] = self::convIPTC( $val, $c );
00099                                         break;
00100                                 case '2#101': /* Country (shown)*/
00101                                         $data['CountryDest'] = self::convIPTC( $val, $c );
00102                                         break;
00103                                 case '2#095': /* state/province (shown) */
00104                                         $data['ProvinceOrStateDest'] = self::convIPTC( $val, $c );
00105                                         break;
00106                                 case '2#090': /* city (Shown) */
00107                                         $data['CityDest'] = self::convIPTC( $val, $c );
00108                                         break;
00109                                 case '2#092': /* sublocation (shown) */
00110                                         $data['SublocationDest'] = self::convIPTC( $val, $c );
00111                                         break;
00112                                 case '2#005': /* object name/title */
00113                                         $data['ObjectName'] = self::convIPTC( $val, $c );
00114                                         break;
00115                                 case '2#040': /* special instructions */
00116                                         $data['SpecialInstructions'] = self::convIPTC( $val, $c );
00117                                         break;
00118                                 case '2#105': /* headline*/
00119                                         $data['Headline'] = self::convIPTC( $val, $c );
00120                                         break;
00121                                 case '2#110': /* credit */
00122                                         /*"Identifies the provider of the objectdata,
00123                                          * not necessarily the owner/creator". */
00124                                         $data['Credit'] = self::convIPTC( $val, $c );
00125                                         break;
00126                                 case '2#115': /* source */
00127                                         /* "Identifies the original owner of the intellectual content of the
00128                                          *objectdata. This could be an agency, a member of an agency or
00129                                          *an individual." */
00130                                         $data['Source'] = self::convIPTC( $val, $c );
00131                                         break;
00132 
00133                                 case '2#007': /* edit status (lead, correction, etc) */
00134                                         $data['EditStatus'] = self::convIPTC( $val, $c );
00135                                         break;
00136                                 case '2#015': /* category. deprecated. max 3 letters in theory, often more */
00137                                         $data['iimCategory'] = self::convIPTC( $val, $c );
00138                                         break;
00139                                 case '2#020': /* category. deprecated. */
00140                                         $data['iimSupplementalCategory'] = self::convIPTC( $val, $c );
00141                                         break;
00142                                 case '2#010': /*urgency (1-8. 1 most, 5 normal, 8 low priority)*/
00143                                         $data['Urgency'] = self::convIPTC( $val, $c );
00144                                         break;
00145                                 case '2#022':
00146                                         /* "Identifies objectdata that recurs often and predictably...
00147                                          * Example: Euroweather" */
00148                                         $data['FixtureIdentifier'] = self::convIPTC( $val, $c );
00149                                         break;
00150                                 case '2#026':
00151                                         /* Content location code (iso 3166 + some custom things)
00152                                          * ex: TUR (for turkey), XUN (for UN), XSP (outer space)
00153                                          * See wikipedia article on iso 3166 and appendix D of iim std. */
00154                                         $data['LocationDestCode'] = self::convIPTC( $val, $c );
00155                                         break;
00156                                 case '2#027':
00157                                         /* Content location name. Full printable name
00158                                          * of location of photo. */
00159                                         $data['LocationDest'] = self::convIPTC( $val, $c );
00160                                         break;
00161                                 case '2#065':
00162                                         /* Originating Program.
00163                                          * Combine with Program version (2:70) if present.
00164                                          */
00165                                         $software = self::convIPTC( $val, $c );
00166 
00167                                         if ( count( $software ) !== 1 ) {
00168                                                 //according to iim standard this cannot have multiple values
00169                                                 //so if there is more than one, something weird is happening,
00170                                                 //and we skip it.
00171                                                 wfDebugLog( 'iptc', 'IPTC: Wrong count on 2:65 Software field' );
00172                                                 break;
00173                                         }
00174 
00175                                         if ( isset( $parsed['2#070'] ) ) {
00176                                                 //if a version is set for the software.
00177                                                 $softwareVersion = self::convIPTC( $parsed['2#070'], $c );
00178                                                 unset($parsed['2#070']);
00179                                                 $data['Software'] = array( array( $software[0], $softwareVersion[0] ) );
00180                                         } else {
00181                                                 $data['Software'] = $software;
00182                                         }
00183                                         break;
00184                                 case '2#075':
00185                                         /* Object cycle.
00186                                          * a for morning (am), p for evening, b for both */
00187                                         $data['ObjectCycle'] = self::convIPTC( $val, $c );
00188                                         break;
00189                                 case '2#100':
00190                                         /* Country/Primary location code.
00191                                          * "Indicates the code of the country/primary location where the
00192                                          * intellectual property of the objectdata was created"
00193                                          * unclear how this differs from 2#026
00194                                          */
00195                                         $data['CountryCodeDest'] = self::convIPTC( $val, $c );
00196                                         break;
00197                                 case '2#103':
00198                                         /* original transmission ref.
00199                                          * "A code representing the location of original transmission ac-
00200                                          * cording to practises of the provider."
00201                                         */
00202                                         $data['OriginalTransmissionRef'] = self::convIPTC( $val, $c );
00203                                         break;
00204                                 case '2#118': /*contact*/
00205                                         $data['Contact'] = self::convIPTC( $val, $c );
00206                                         break;
00207                                 case '2#122':
00208                                         /* Writer/Editor
00209                                          * "Identification of the name of the person involved in the writing,
00210                                          * editing or correcting the objectdata or caption/abstract."
00211                                          */
00212                                         $data['Writer'] = self::convIPTC( $val, $c );
00213                                         break;
00214                                 case '2#135': /* lang code */
00215                                         $data['LanguageCode'] = self::convIPTC( $val, $c );
00216                                         break;
00217 
00218                                 // Start date stuff.
00219                                 // It doesn't accept incomplete dates even though they are valid
00220                                 // according to spec.
00221                                 // Should potentially store timezone as well.
00222                                 case '2#055':
00223                                         //Date created (not date digitized).
00224                                         //Maps to exif DateTimeOriginal
00225                                         if ( isset( $parsed['2#060'] ) ) {
00226                                                 $time = $parsed['2#060'];
00227                                         } else {
00228                                                 $time = Array();
00229                                         }
00230                                         $timestamp =  self::timeHelper( $val, $time, $c );
00231                                         if ($timestamp) {
00232                                                 $data['DateTimeOriginal'] = $timestamp;
00233                                         }
00234                                         break;
00235 
00236                                 case '2#062':
00237                                         //Date converted to digital representation.
00238                                         //Maps to exif DateTimeDigitized
00239                                         if ( isset( $parsed['2#063'] ) ) {
00240                                                 $time = $parsed['2#063'];
00241                                         } else {
00242                                                 $time = Array();
00243                                         }
00244                                         $timestamp =  self::timeHelper( $val, $time, $c );
00245                                         if ($timestamp) {
00246                                                 $data['DateTimeDigitized'] = $timestamp;
00247                                         }
00248                                         break;
00249 
00250                                 case '2#030':
00251                                         //Date released.
00252                                         if ( isset( $parsed['2#035'] ) ) {
00253                                                 $time = $parsed['2#035'];
00254                                         } else {
00255                                                 $time = Array();
00256                                         }
00257                                         $timestamp =  self::timeHelper( $val, $time, $c );
00258                                         if ($timestamp) {
00259                                                 $data['DateTimeReleased'] = $timestamp;
00260                                         }
00261                                         break;
00262 
00263                                 case '2#037':
00264                                         //Date expires.
00265                                         if ( isset( $parsed['2#038'] ) ) {
00266                                                 $time = $parsed['2#038'];
00267                                         } else {
00268                                                 $time = Array();
00269                                         }
00270                                         $timestamp =  self::timeHelper( $val, $time, $c );
00271                                         if ($timestamp) {
00272                                                 $data['DateTimeExpires'] = $timestamp;
00273                                         }
00274                                         break;
00275 
00276                                 case '2#000': /* iim version */
00277                                         // unlike other tags, this is a 2-byte binary number.
00278                                         //technically this is required if there is iptc data
00279                                         //but in practise it isn't always there.
00280                                         if ( strlen( $val[0] ) == 2 ) {
00281                                                 //if is just to be paranoid.
00282                                                 $versionValue = ord( substr( $val[0], 0, 1 ) ) * 256;
00283                                                 $versionValue += ord( substr( $val[0], 1, 1 ) );
00284                                                 $data['iimVersion'] = $versionValue;
00285                                         }
00286                                         break;
00287 
00288                                 case '2#004':
00289                                         // IntellectualGenere.
00290                                         // first 4 characters are an id code
00291                                         // That we're not really interested in.
00292 
00293                                         // This prop is weird, since it's
00294                                         // allowed to have multiple values
00295                                         // in iim 4.1, but not in the XMP
00296                                         // stuff. We're going to just
00297                                         // extract the first value.
00298                                         $con = self::ConvIPTC( $val, $c );
00299                                         if ( strlen( $con[0] ) < 5 ) {
00300                                                 wfDebugLog( 'iptc', 'IPTC: '
00301                                                         . '2:04 too short. '
00302                                                         . 'Ignoring.' );
00303                                                         break;
00304                                         }
00305                                         $extracted = substr( $con[0], 4 );
00306                                         $data['IntellectualGenre'] = $extracted;
00307                                         break;
00308 
00309                                 case '2#012':
00310                                         // Subject News code - this is a compound field
00311                                         // at the moment we only extract the subject news
00312                                         // code, which is an 8 digit (ascii) number
00313                                         // describing the subject matter of the content.
00314                                         $codes = self::convIPTC( $val, $c );
00315                                         foreach ( $codes as $ic ) {
00316                                                 $fields = explode(':', $ic, 3 );
00317 
00318                                                 if ( count( $fields ) < 2 ||
00319                                                         $fields[0] !== 'IPTC' )
00320                                                 {
00321                                                         wfDebugLog( 'IPTC', 'IPTC: '
00322                                                                 . 'Invalid 2:12 - ' . $ic );
00323                                                         break;
00324                                                 }
00325                                                 $data['SubjectNewsCode'] = $fields[1];
00326                                         }
00327                                         break;
00328 
00329                                 // purposely does not do 2:125, 2:130, 2:131,
00330                                 // 2:47, 2:50, 2:45, 2:42, 2:8, 2:3
00331                                 // 2:200, 2:201, 2:202
00332                                 // or the audio stuff (2:150 to 2:154)
00333 
00334                                 case '2#070':
00335                                 case '2#060':
00336                                 case '2#063':
00337                                 case '2#085':
00338                                 case '2#038':
00339                                 case '2#035':
00340                                         //ignore. Handled elsewhere.
00341                                         break;
00342 
00343                                 default:
00344                                         wfDebugLog( 'iptc', "Unsupported iptc tag: $tag. Value: " . implode( ',', $val ));
00345                                         break;
00346                         }
00347 
00348                 }
00349                 return $data;
00350         }
00351 
00361         private static function timeHelper( $date, $time, $c ) {
00362                 if ( count( $date ) === 1 ) {
00363                         //the standard says this should always be 1
00364                         //just double checking.
00365                         list($date) = self::convIPTC( $date, $c );
00366                 } else {
00367                         return null;
00368                 }
00369 
00370                 if ( count( $time ) === 1 ) {
00371                         list($time) = self::convIPTC( $time, $c );
00372                         $dateOnly = false;
00373                 } else {
00374                         $time = '000000+0000'; //placeholder
00375                         $dateOnly = true;
00376                 }
00377 
00378                 if ( ! ( preg_match('/\d\d\d\d\d\d[-+]\d\d\d\d/', $time)
00379                         && preg_match('/\d\d\d\d\d\d\d\d/', $date)
00380                         && substr($date, 0, 4) !== '0000'
00381                         && substr($date, 4, 2) !== '00'
00382                         && substr($date, 6, 2) !== '00'
00383                  ) ) {
00384                         //something wrong.
00385                         // Note, this rejects some valid dates according to iptc spec
00386                         // for example: the date 00000400 means the photo was taken in
00387                         // April, but the year and day is unknown. We don't process these
00388                         // types of incomplete dates atm.
00389                         wfDebugLog( 'iptc', "IPTC: invalid time ( $time ) or date ( $date )");
00390                         return null;
00391                 }
00392 
00393                 $unixTS = wfTimestamp( TS_UNIX, $date . substr( $time, 0, 6 ));
00394                 if ( $unixTS === false ) {
00395                         wfDebugLog( 'iptc', "IPTC: can't convert date to TS_UNIX: $date $time." );
00396                         return null;
00397                 }
00398 
00399                 $tz = ( intval( substr( $time, 7, 2 ) ) *60*60 )
00400                         + ( intval( substr( $time, 9, 2 ) ) * 60 );
00401 
00402                 if ( substr( $time, 6, 1 ) === '-' ) {
00403                         $tz = - $tz;
00404                 }
00405 
00406                 $finalTimestamp = wfTimestamp( TS_EXIF, $unixTS + $tz );
00407                 if ( $finalTimestamp === false ) {
00408                         wfDebugLog( 'iptc', "IPTC: can't make final timestamp. Date: " . ( $unixTS + $tz ) );
00409                         return null;
00410                 }
00411                 if ( $dateOnly ) {
00412                         //return the date only
00413                         return substr( $finalTimestamp, 0, 10 );
00414                 } else {
00415                         return $finalTimestamp;
00416                 }
00417         }
00418 
00426         private static function convIPTC ( $data, $charset ) {
00427                 if ( is_array( $data ) ) {
00428                         foreach ($data as &$val) {
00429                                 $val = self::convIPTCHelper( $val, $charset );
00430                         }
00431                 } else {
00432                         $data = self::convIPTCHelper( $data, $charset );
00433                 }
00434 
00435                 return $data;
00436         }
00444         private static function convIPTCHelper ( $data, $charset ) {
00445                 if ( $charset ) {
00446                         wfSuppressWarnings();
00447                         $data = iconv($charset, "UTF-8//IGNORE", $data);
00448                         wfRestoreWarnings();
00449                         if ($data === false) {
00450                                 $data = "";
00451                                 wfDebugLog('iptc', __METHOD__ . " Error converting iptc data charset $charset to utf-8");
00452                         }
00453                 } else {
00454                         //treat as utf-8 if is valid utf-8. otherwise pretend its windows-1252
00455                         // most of the time if there is no 1:90 tag, it is either ascii, latin1, or utf-8
00456                         $oldData = $data;
00457                         UtfNormal::quickIsNFCVerify( $data ); //make $data valid utf-8
00458                         if ($data === $oldData) {
00459                                 return $data; //if validation didn't change $data
00460                         } else {
00461                                 return self::convIPTCHelper( $oldData, 'Windows-1252' );
00462                         }
00463                 }
00464                 return trim( $data );
00465         }
00466 
00475         static function getCharset($tag) {
00476 
00477                 //According to iim standard, charset is defined by the tag 1:90.
00478                 //in which there are iso 2022 escape sequences to specify the character set.
00479                 //the iim standard seems to encourage that all necessary escape sequences are
00480                 //in the 1:90 tag, but says it doesn't have to be.
00481 
00482                 //This is in need of more testing probably. This is definitely not complete.
00483                 //however reading the docs of some other iptc software, it appears that most iptc software
00484                 //only recognizes utf-8. If 1:90 tag is not present content is
00485                 // usually ascii or iso-8859-1 (and sometimes utf-8), but no guarantee.
00486 
00487                 //This also won't work if there are more than one escape sequence in the 1:90 tag
00488                 //or if something is put in the G2, or G3 charsets, etc. It will only reliably recognize utf-8.
00489 
00490                 // This is just going through the charsets mentioned in appendix C of the iim standard.
00491 
00492                 //  \x1b = ESC.
00493                 switch ( $tag ) {
00494                         case "\x1b%G": //utf-8
00495                         //Also call things that are compatible with utf-8, utf-8 (e.g. ascii)
00496                         case "\x1b(B": // ascii
00497                         case "\x1b(@": // iso-646-IRV (ascii in latest version, $ different in older version)
00498                                 $c = 'UTF-8';
00499                                 break;
00500                         case "\x1b(A": //like ascii, but british.
00501                                 $c = 'ISO646-GB';
00502                                 break;
00503                         case "\x1b(C": //some obscure sweedish/finland encoding
00504                                 $c = 'ISO-IR-8-1';
00505                                 break;
00506                         case "\x1b(D":
00507                                 $c = 'ISO-IR-8-2';
00508                                 break;
00509                         case "\x1b(E": //some obscure danish/norway encoding
00510                                 $c = 'ISO-IR-9-1';
00511                                 break;
00512                         case "\x1b(F":
00513                                 $c = 'ISO-IR-9-2';
00514                                 break;
00515                         case "\x1b(G":
00516                                 $c = 'SEN_850200_B'; // aka iso 646-SE; ascii-like
00517                                 break;
00518                         case "\x1b(I":
00519                                 $c = "ISO646-IT";
00520                                 break;
00521                         case "\x1b(L":
00522                                 $c = "ISO646-PT";
00523                                 break;
00524                         case "\x1b(Z":
00525                                 $c = "ISO646-ES";
00526                                 break;
00527                         case "\x1b([":
00528                                 $c = "GREEK7-OLD";
00529                                 break;
00530                         case "\x1b(K":
00531                                 $c = "ISO646-DE";
00532                                 break;
00533                         case "\x1b(N":  //crylic
00534                                 $c = "ISO_5427";
00535                                 break;
00536                         case "\x1b(`": //iso646-NO
00537                                 $c = "NS_4551-1";
00538                                 break;
00539                         case "\x1b(f": //iso646-FR
00540                                 $c = "NF_Z_62-010";
00541                                 break;
00542                         case "\x1b(g":
00543                                 $c = "PT2"; //iso646-PT2
00544                                 break;
00545                         case "\x1b(h":
00546                                 $c = "ES2";
00547                                 break;
00548                         case "\x1b(i": //iso646-HU
00549                                 $c = "MSZ_7795.3";
00550                                 break;
00551                         case "\x1b(w":
00552                                 $c = "CSA_Z243.4-1985-1";
00553                                 break;
00554                         case "\x1b(x":
00555                                 $c = "CSA_Z243.4-1985-2";
00556                                 break;
00557                         case "\x1b\$(B":
00558                         case "\x1b\$B":
00559                         case "\x1b&@\x1b\$B":
00560                         case "\x1b&@\x1b\$(B":
00561                                 $c = "JIS_C6226-1983";
00562                                 break;
00563                         case "\x1b-A": // iso-8859-1. at least for the high code characters.
00564                         case "\x1b(@\x1b-A":
00565                         case "\x1b(B\x1b-A":
00566                                 $c = 'ISO-8859-1';
00567                                 break;
00568                         case "\x1b-B": // iso-8859-2. at least for the high code characters.
00569                                 $c = 'ISO-8859-2';
00570                                 break;
00571                         case "\x1b-C": // iso-8859-3. at least for the high code characters.
00572                                 $c = 'ISO-8859-3';
00573                                 break;
00574                         case "\x1b-D": // iso-8859-4. at least for the high code characters.
00575                                 $c = 'ISO-8859-4';
00576                                 break;
00577                         case "\x1b-E": // iso-8859-5. at least for the high code characters.
00578                                 $c = 'ISO-8859-5';
00579                                 break;
00580                         case "\x1b-F": // iso-8859-6. at least for the high code characters.
00581                                 $c = 'ISO-8859-6';
00582                                 break;
00583                         case "\x1b-G": // iso-8859-7. at least for the high code characters.
00584                                 $c = 'ISO-8859-7';
00585                                 break;
00586                         case "\x1b-H": // iso-8859-8. at least for the high code characters.
00587                                 $c = 'ISO-8859-8';
00588                                 break;
00589                         case "\x1b-I": // CSN_369103. at least for the high code characters.
00590                                 $c = 'CSN_369103';
00591                                 break;
00592                         default:
00593                                 wfDebugLog('iptc', __METHOD__ . 'Unknown charset in iptc 1:90: ' . bin2hex( $tag ) );
00594                                 //at this point just give up and refuse to parse iptc?
00595                                 $c = false;
00596                 }
00597                 return $c;
00598         }
00599 }