PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wp-includes/pomo/po.php

https://gitlab.com/morganestes/wordpress-develop
PHP | 508 lines | 371 code | 29 blank | 108 comment | 86 complexity | 6e725fc91368894f82b9415dcfffb70a MD5 | raw file
  1. <?php
  2. /**
  3. * Class for working with PO files
  4. *
  5. * @version $Id: po.php 1158 2015-11-20 04:31:23Z dd32 $
  6. * @package pomo
  7. * @subpackage po
  8. */
  9. require_once dirname( __FILE__ ) . '/translations.php';
  10. if ( ! defined( 'PO_MAX_LINE_LEN' ) ) {
  11. define( 'PO_MAX_LINE_LEN', 79 );
  12. }
  13. ini_set( 'auto_detect_line_endings', 1 );
  14. /**
  15. * Routines for working with PO files
  16. */
  17. if ( ! class_exists( 'PO', false ) ) :
  18. class PO extends Gettext_Translations {
  19. var $comments_before_headers = '';
  20. /**
  21. * Exports headers to a PO entry
  22. *
  23. * @return string msgid/msgstr PO entry for this PO file headers, doesn't contain newline at the end
  24. */
  25. function export_headers() {
  26. $header_string = '';
  27. foreach ( $this->headers as $header => $value ) {
  28. $header_string .= "$header: $value\n";
  29. }
  30. $poified = PO::poify( $header_string );
  31. if ( $this->comments_before_headers ) {
  32. $before_headers = $this->prepend_each_line( rtrim( $this->comments_before_headers ) . "\n", '# ' );
  33. } else {
  34. $before_headers = '';
  35. }
  36. return rtrim( "{$before_headers}msgid \"\"\nmsgstr $poified" );
  37. }
  38. /**
  39. * Exports all entries to PO format
  40. *
  41. * @return string sequence of mgsgid/msgstr PO strings, doesn't containt newline at the end
  42. */
  43. function export_entries() {
  44. //TODO sorting
  45. return implode( "\n\n", array_map( array( 'PO', 'export_entry' ), $this->entries ) );
  46. }
  47. /**
  48. * Exports the whole PO file as a string
  49. *
  50. * @param bool $include_headers whether to include the headers in the export
  51. * @return string ready for inclusion in PO file string for headers and all the enrtries
  52. */
  53. function export( $include_headers = true ) {
  54. $res = '';
  55. if ( $include_headers ) {
  56. $res .= $this->export_headers();
  57. $res .= "\n\n";
  58. }
  59. $res .= $this->export_entries();
  60. return $res;
  61. }
  62. /**
  63. * Same as {@link export}, but writes the result to a file
  64. *
  65. * @param string $filename where to write the PO string
  66. * @param bool $include_headers whether to include tje headers in the export
  67. * @return bool true on success, false on error
  68. */
  69. function export_to_file( $filename, $include_headers = true ) {
  70. $fh = fopen( $filename, 'w' );
  71. if ( false === $fh ) {
  72. return false;
  73. }
  74. $export = $this->export( $include_headers );
  75. $res = fwrite( $fh, $export );
  76. if ( false === $res ) {
  77. return false;
  78. }
  79. return fclose( $fh );
  80. }
  81. /**
  82. * Text to include as a comment before the start of the PO contents
  83. *
  84. * Doesn't need to include # in the beginning of lines, these are added automatically
  85. */
  86. function set_comment_before_headers( $text ) {
  87. $this->comments_before_headers = $text;
  88. }
  89. /**
  90. * Formats a string in PO-style
  91. *
  92. * @param string $string the string to format
  93. * @return string the poified string
  94. */
  95. public static function poify( $string ) {
  96. $quote = '"';
  97. $slash = '\\';
  98. $newline = "\n";
  99. $replaces = array(
  100. "$slash" => "$slash$slash",
  101. "$quote" => "$slash$quote",
  102. "\t" => '\t',
  103. );
  104. $string = str_replace( array_keys( $replaces ), array_values( $replaces ), $string );
  105. $po = $quote . implode( "${slash}n$quote$newline$quote", explode( $newline, $string ) ) . $quote;
  106. // add empty string on first line for readbility
  107. if ( false !== strpos( $string, $newline ) &&
  108. ( substr_count( $string, $newline ) > 1 || ! ( $newline === substr( $string, -strlen( $newline ) ) ) ) ) {
  109. $po = "$quote$quote$newline$po";
  110. }
  111. // remove empty strings
  112. $po = str_replace( "$newline$quote$quote", '', $po );
  113. return $po;
  114. }
  115. /**
  116. * Gives back the original string from a PO-formatted string
  117. *
  118. * @param string $string PO-formatted string
  119. * @return string enascaped string
  120. */
  121. public static function unpoify( $string ) {
  122. $escapes = array(
  123. 't' => "\t",
  124. 'n' => "\n",
  125. 'r' => "\r",
  126. '\\' => '\\',
  127. );
  128. $lines = array_map( 'trim', explode( "\n", $string ) );
  129. $lines = array_map( array( 'PO', 'trim_quotes' ), $lines );
  130. $unpoified = '';
  131. $previous_is_backslash = false;
  132. foreach ( $lines as $line ) {
  133. preg_match_all( '/./u', $line, $chars );
  134. $chars = $chars[0];
  135. foreach ( $chars as $char ) {
  136. if ( ! $previous_is_backslash ) {
  137. if ( '\\' == $char ) {
  138. $previous_is_backslash = true;
  139. } else {
  140. $unpoified .= $char;
  141. }
  142. } else {
  143. $previous_is_backslash = false;
  144. $unpoified .= isset( $escapes[ $char ] ) ? $escapes[ $char ] : $char;
  145. }
  146. }
  147. }
  148. // Standardise the line endings on imported content, technically PO files shouldn't contain \r
  149. $unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified );
  150. return $unpoified;
  151. }
  152. /**
  153. * Inserts $with in the beginning of every new line of $string and
  154. * returns the modified string
  155. *
  156. * @param string $string prepend lines in this string
  157. * @param string $with prepend lines with this string
  158. */
  159. public static function prepend_each_line( $string, $with ) {
  160. $lines = explode( "\n", $string );
  161. $append = '';
  162. if ( "\n" === substr( $string, -1 ) && '' === end( $lines ) ) {
  163. // Last line might be empty because $string was terminated
  164. // with a newline, remove it from the $lines array,
  165. // we'll restore state by re-terminating the string at the end
  166. array_pop( $lines );
  167. $append = "\n";
  168. }
  169. foreach ( $lines as &$line ) {
  170. $line = $with . $line;
  171. }
  172. unset( $line );
  173. return implode( "\n", $lines ) . $append;
  174. }
  175. /**
  176. * Prepare a text as a comment -- wraps the lines and prepends #
  177. * and a special character to each line
  178. *
  179. * @access private
  180. * @param string $text the comment text
  181. * @param string $char character to denote a special PO comment,
  182. * like :, default is a space
  183. */
  184. public static function comment_block( $text, $char = ' ' ) {
  185. $text = wordwrap( $text, PO_MAX_LINE_LEN - 3 );
  186. return PO::prepend_each_line( $text, "#$char " );
  187. }
  188. /**
  189. * Builds a string from the entry for inclusion in PO file
  190. *
  191. * @param Translation_Entry $entry the entry to convert to po string (passed by reference).
  192. * @return false|string PO-style formatted string for the entry or
  193. * false if the entry is empty
  194. */
  195. public static function export_entry( &$entry ) {
  196. if ( null === $entry->singular || '' === $entry->singular ) {
  197. return false;
  198. }
  199. $po = array();
  200. if ( ! empty( $entry->translator_comments ) ) {
  201. $po[] = PO::comment_block( $entry->translator_comments );
  202. }
  203. if ( ! empty( $entry->extracted_comments ) ) {
  204. $po[] = PO::comment_block( $entry->extracted_comments, '.' );
  205. }
  206. if ( ! empty( $entry->references ) ) {
  207. $po[] = PO::comment_block( implode( ' ', $entry->references ), ':' );
  208. }
  209. if ( ! empty( $entry->flags ) ) {
  210. $po[] = PO::comment_block( implode( ', ', $entry->flags ), ',' );
  211. }
  212. if ( $entry->context ) {
  213. $po[] = 'msgctxt ' . PO::poify( $entry->context );
  214. }
  215. $po[] = 'msgid ' . PO::poify( $entry->singular );
  216. if ( ! $entry->is_plural ) {
  217. $translation = empty( $entry->translations ) ? '' : $entry->translations[0];
  218. $translation = PO::match_begin_and_end_newlines( $translation, $entry->singular );
  219. $po[] = 'msgstr ' . PO::poify( $translation );
  220. } else {
  221. $po[] = 'msgid_plural ' . PO::poify( $entry->plural );
  222. $translations = empty( $entry->translations ) ? array( '', '' ) : $entry->translations;
  223. foreach ( $translations as $i => $translation ) {
  224. $translation = PO::match_begin_and_end_newlines( $translation, $entry->plural );
  225. $po[] = "msgstr[$i] " . PO::poify( $translation );
  226. }
  227. }
  228. return implode( "\n", $po );
  229. }
  230. public static function match_begin_and_end_newlines( $translation, $original ) {
  231. if ( '' === $translation ) {
  232. return $translation;
  233. }
  234. $original_begin = "\n" === substr( $original, 0, 1 );
  235. $original_end = "\n" === substr( $original, -1 );
  236. $translation_begin = "\n" === substr( $translation, 0, 1 );
  237. $translation_end = "\n" === substr( $translation, -1 );
  238. if ( $original_begin ) {
  239. if ( ! $translation_begin ) {
  240. $translation = "\n" . $translation;
  241. }
  242. } elseif ( $translation_begin ) {
  243. $translation = ltrim( $translation, "\n" );
  244. }
  245. if ( $original_end ) {
  246. if ( ! $translation_end ) {
  247. $translation .= "\n";
  248. }
  249. } elseif ( $translation_end ) {
  250. $translation = rtrim( $translation, "\n" );
  251. }
  252. return $translation;
  253. }
  254. /**
  255. * @param string $filename
  256. * @return boolean
  257. */
  258. function import_from_file( $filename ) {
  259. $f = fopen( $filename, 'r' );
  260. if ( ! $f ) {
  261. return false;
  262. }
  263. $lineno = 0;
  264. while ( true ) {
  265. $res = $this->read_entry( $f, $lineno );
  266. if ( ! $res ) {
  267. break;
  268. }
  269. if ( $res['entry']->singular == '' ) {
  270. $this->set_headers( $this->make_headers( $res['entry']->translations[0] ) );
  271. } else {
  272. $this->add_entry( $res['entry'] );
  273. }
  274. }
  275. PO::read_line( $f, 'clear' );
  276. if ( false === $res ) {
  277. return false;
  278. }
  279. if ( ! $this->headers && ! $this->entries ) {
  280. return false;
  281. }
  282. return true;
  283. }
  284. /**
  285. * Helper function for read_entry
  286. *
  287. * @param string $context
  288. * @return bool
  289. */
  290. protected static function is_final( $context ) {
  291. return ( $context === 'msgstr' ) || ( $context === 'msgstr_plural' );
  292. }
  293. /**
  294. * @param resource $f
  295. * @param int $lineno
  296. * @return null|false|array
  297. */
  298. function read_entry( $f, $lineno = 0 ) {
  299. $entry = new Translation_Entry();
  300. // where were we in the last step
  301. // can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
  302. $context = '';
  303. $msgstr_index = 0;
  304. while ( true ) {
  305. $lineno++;
  306. $line = PO::read_line( $f );
  307. if ( ! $line ) {
  308. if ( feof( $f ) ) {
  309. if ( self::is_final( $context ) ) {
  310. break;
  311. } elseif ( ! $context ) { // we haven't read a line and eof came
  312. return null;
  313. } else {
  314. return false;
  315. }
  316. } else {
  317. return false;
  318. }
  319. }
  320. if ( $line == "\n" ) {
  321. continue;
  322. }
  323. $line = trim( $line );
  324. if ( preg_match( '/^#/', $line, $m ) ) {
  325. // the comment is the start of a new entry
  326. if ( self::is_final( $context ) ) {
  327. PO::read_line( $f, 'put-back' );
  328. $lineno--;
  329. break;
  330. }
  331. // comments have to be at the beginning
  332. if ( $context && $context != 'comment' ) {
  333. return false;
  334. }
  335. // add comment
  336. $this->add_comment_to_entry( $entry, $line );
  337. } elseif ( preg_match( '/^msgctxt\s+(".*")/', $line, $m ) ) {
  338. if ( self::is_final( $context ) ) {
  339. PO::read_line( $f, 'put-back' );
  340. $lineno--;
  341. break;
  342. }
  343. if ( $context && $context != 'comment' ) {
  344. return false;
  345. }
  346. $context = 'msgctxt';
  347. $entry->context .= PO::unpoify( $m[1] );
  348. } elseif ( preg_match( '/^msgid\s+(".*")/', $line, $m ) ) {
  349. if ( self::is_final( $context ) ) {
  350. PO::read_line( $f, 'put-back' );
  351. $lineno--;
  352. break;
  353. }
  354. if ( $context && $context != 'msgctxt' && $context != 'comment' ) {
  355. return false;
  356. }
  357. $context = 'msgid';
  358. $entry->singular .= PO::unpoify( $m[1] );
  359. } elseif ( preg_match( '/^msgid_plural\s+(".*")/', $line, $m ) ) {
  360. if ( $context != 'msgid' ) {
  361. return false;
  362. }
  363. $context = 'msgid_plural';
  364. $entry->is_plural = true;
  365. $entry->plural .= PO::unpoify( $m[1] );
  366. } elseif ( preg_match( '/^msgstr\s+(".*")/', $line, $m ) ) {
  367. if ( $context != 'msgid' ) {
  368. return false;
  369. }
  370. $context = 'msgstr';
  371. $entry->translations = array( PO::unpoify( $m[1] ) );
  372. } elseif ( preg_match( '/^msgstr\[(\d+)\]\s+(".*")/', $line, $m ) ) {
  373. if ( $context != 'msgid_plural' && $context != 'msgstr_plural' ) {
  374. return false;
  375. }
  376. $context = 'msgstr_plural';
  377. $msgstr_index = $m[1];
  378. $entry->translations[ $m[1] ] = PO::unpoify( $m[2] );
  379. } elseif ( preg_match( '/^".*"$/', $line ) ) {
  380. $unpoified = PO::unpoify( $line );
  381. switch ( $context ) {
  382. case 'msgid':
  383. $entry->singular .= $unpoified;
  384. break;
  385. case 'msgctxt':
  386. $entry->context .= $unpoified;
  387. break;
  388. case 'msgid_plural':
  389. $entry->plural .= $unpoified;
  390. break;
  391. case 'msgstr':
  392. $entry->translations[0] .= $unpoified;
  393. break;
  394. case 'msgstr_plural':
  395. $entry->translations[ $msgstr_index ] .= $unpoified;
  396. break;
  397. default:
  398. return false;
  399. }
  400. } else {
  401. return false;
  402. }
  403. }
  404. $have_translations = false;
  405. foreach ( $entry->translations as $t ) {
  406. if ( $t || ( '0' === $t ) ) {
  407. $have_translations = true;
  408. break;
  409. }
  410. }
  411. if ( false === $have_translations ) {
  412. $entry->translations = array();
  413. }
  414. return array(
  415. 'entry' => $entry,
  416. 'lineno' => $lineno,
  417. );
  418. }
  419. /**
  420. * @staticvar string $last_line
  421. * @staticvar boolean $use_last_line
  422. *
  423. * @param resource $f
  424. * @param string $action
  425. * @return boolean
  426. */
  427. function read_line( $f, $action = 'read' ) {
  428. static $last_line = '';
  429. static $use_last_line = false;
  430. if ( 'clear' == $action ) {
  431. $last_line = '';
  432. return true;
  433. }
  434. if ( 'put-back' == $action ) {
  435. $use_last_line = true;
  436. return true;
  437. }
  438. $line = $use_last_line ? $last_line : fgets( $f );
  439. $line = ( "\r\n" == substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line;
  440. $last_line = $line;
  441. $use_last_line = false;
  442. return $line;
  443. }
  444. /**
  445. * @param Translation_Entry $entry
  446. * @param string $po_comment_line
  447. */
  448. function add_comment_to_entry( &$entry, $po_comment_line ) {
  449. $first_two = substr( $po_comment_line, 0, 2 );
  450. $comment = trim( substr( $po_comment_line, 2 ) );
  451. if ( '#:' == $first_two ) {
  452. $entry->references = array_merge( $entry->references, preg_split( '/\s+/', $comment ) );
  453. } elseif ( '#.' == $first_two ) {
  454. $entry->extracted_comments = trim( $entry->extracted_comments . "\n" . $comment );
  455. } elseif ( '#,' == $first_two ) {
  456. $entry->flags = array_merge( $entry->flags, preg_split( '/,\s*/', $comment ) );
  457. } else {
  458. $entry->translator_comments = trim( $entry->translator_comments . "\n" . $comment );
  459. }
  460. }
  461. /**
  462. * @param string $s
  463. * @return sring
  464. */
  465. public static function trim_quotes( $s ) {
  466. if ( substr( $s, 0, 1 ) == '"' ) {
  467. $s = substr( $s, 1 );
  468. }
  469. if ( substr( $s, -1, 1 ) == '"' ) {
  470. $s = substr( $s, 0, -1 );
  471. }
  472. return $s;
  473. }
  474. }
  475. endif;