<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[709] sites/trunk/api.wordpress.org/public_html: New api.wordpress.org/translations API.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://meta.trac.wordpress.org/changeset/709">709</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-06-18 17:05:45 +0000 (Wed, 18 Jun 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>New api.wordpress.org/translations API.</pre>

<h3>Added Paths</h3>
<ul>
<li>sites/trunk/api.wordpress.org/public_html/translations/</li>
<li>sites/trunk/api.wordpress.org/public_html/translations/core/</li>
<li>sites/trunk/api.wordpress.org/public_html/translations/core/1.0/</li>
<li><a href="#sitestrunkapiwordpressorgpublic_htmltranslationscore10indexphp">sites/trunk/api.wordpress.org/public_html/translations/core/1.0/index.php</a></li>
<li><a href="#sitestrunkapiwordpressorgpublic_htmltranslationslibphp">sites/trunk/api.wordpress.org/public_html/translations/lib.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkapiwordpressorgpublic_htmltranslationscore10indexphp"></a>
<div class="addfile"><h4>Added: sites/trunk/api.wordpress.org/public_html/translations/core/1.0/index.php (0 => 709)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/api.wordpress.org/public_html/translations/core/1.0/index.php                                (rev 0)
+++ sites/trunk/api.wordpress.org/public_html/translations/core/1.0/index.php   2014-06-18 17:05:45 UTC (rev 709)
</span><span class="lines">@@ -0,0 +1,18 @@
</span><ins>+<?php
+
+$base_dir = dirname( dirname( dirname( __DIR__ ) ) );
+require( $base_dir . '/translations/lib.php' );
+require( $base_dir . '/init.php' );
+require( $base_dir . '/includes/hyperdb/bb-10-hyper-db.php' );
+require( $base_dir . '/includes/object-cache.php' );
+wp_cache_init();
+
+$version = isset( $_GET['version'] ) ? $_GET['version'] : WP_CORE_LATEST_RELEASE;
+
+$translations = find_all_translations_for_core( $version );
+
+call_headers( 'application/json' );
+
+echo json_encode( $translations );
+exit;
+
</ins><span class="cx">Property changes on: sites/trunk/api.wordpress.org/public_html/translations/core/1.0/index.php
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4>Added: svn:eol-style</h4></div>
<ins>+native
</ins><span class="cx">\ No newline at end of property
</span><a id="sitestrunkapiwordpressorgpublic_htmltranslationslibphp"></a>
<div class="addfile"><h4>Added: sites/trunk/api.wordpress.org/public_html/translations/lib.php (0 => 709)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/api.wordpress.org/public_html/translations/lib.php                           (rev 0)
+++ sites/trunk/api.wordpress.org/public_html/translations/lib.php      2014-06-18 17:05:45 UTC (rev 709)
</span><span class="lines">@@ -0,0 +1,205 @@
</span><ins>+<?php
+
+function version_compare_version_key_desc( $a, $b ) {
+    return version_compare( $b['version'], $a['version'] );
+}
+
+function version_compare_version_prop_desc( $a, $b ) {
+       return version_compare( $b->version, $a->version );
+}
+
+function find_all_translations_for_core( $version = null ) {
+       return find_all_translations_for_type_and_domain( 'core', 'default', $version );
+}
+
+function find_all_translations_for_type_and_domain( $type, $domain = 'default', $version = null ) {
+       global $wpdb;
+
+       if ( $type === 'core' && null === $version ) {
+               $version = WP_CORE_LATEST_RELEASE;
+       }
+
+       $cache_group = 'translations-query';
+       $cache_time = 900; // 15 min
+       $cache_key = "$type:$domain:$version";
+       
+       $translations = wp_cache_get( $cache_key, $cache_group );
+       if ( '_empty_' === $translations ) {
+               return array();
+       } elseif ( ! $translations ) {
+               $translations = $wpdb->get_results( $wpdb->prepare(
+                       "SELECT language, version, updated FROM language_packs WHERE type = %s AND domain = %s AND active = 1",
+                       $type, $domain ) );
+
+               if ( ! $translations ) {
+                       wp_cache_add( $cache_key, '_empty_', $cache_group, $cache_time );
+                       return array();
+               }
+
+               usort( $translations, 'version_compare_version_prop_desc' );
+
+               $_translations = array();
+               foreach ( $translations as $translation ) {
+                       if ( isset( $_translations[ $translation->language ] ) ) {
+                               continue;
+                       }
+                       if ( ! $version || $version === $translation->version || version_compare( $version, $translation->version, '>=' ) ) {
+                               $_translations[ $translation->language ] = $translation;
+                       }
+               }
+               $translations = array_values( $_translations );
+       }
+
+       require_once WPORGPATH . 'translate/glotpress/locales/locales.php';
+       $base_url = is_ssl() ? 'https' : 'http';
+       $base_url .= '://downloads.wordpress.org/translation/';
+       $base_url .= ( $type == 'core' ) ? 'core' : "$type/$domain";
+
+       $_translations = array();
+       foreach ( $translations as $translation ) {
+               $locale = GP_Locales::by_field( 'wp_locale', $translation->language );
+
+               // We'll use ISO codes for sorting.
+               if ( $locale->lang_code_iso_639_3 ) {
+                       $iso = $locale->lang_code_iso_639_3;
+               } elseif ( $locale->lang_code_iso_639_2 ) {
+                       $iso = $locale->lang_code_iso_639_2;
+               } elseif ( $locale->lang_code_iso_639_1 ) {
+                       $iso = $locale->lang_code_iso_639_1;
+               } else {
+                       continue; // uhhh
+               }
+
+               $_translations[ $iso ] = $translation;
+               $_translations[ $iso ]->english_name = $locale->english_name;
+               $_translations[ $iso ]->native_name = $locale->native_name;
+               $_translations[ $iso ]->download_url = sprintf( "$base_url/%s/%s.zip", $translation->version, $translation->language );
+       }
+       ksort( $_translations );
+       $translations = array_values( $_translations );
+
+       wp_cache_add( $cache_key, $translations, $cache_group, $cache_time );
+
+       return $translations;
+}
+
+function find_latest_translations( $args ) {
+       global $wpdb;
+       extract( $args, EXTR_SKIP );
+
+       $translations_cache_group = 'update-check-translations';
+       $translations_cache_time = 900; // 15 minutes
+
+       $return = array();
+       foreach ( $languages as $language ) {
+
+               // Disable LP's for en_US for now, can re-enable later if we have non-en_US native items
+               if ( 'en_US' == $language )
+                       continue;
+
+               $cache_key = "{$type}:{$language}:{$domain}";
+
+               $results = wp_cache_get( $cache_key, $translations_cache_group );
+
+               // No language packs were found
+               if ( '_empty_' == $results )
+                       continue;
+
+               if ( ! $results ) {
+                       $query = $wpdb->prepare(
+                               "SELECT `version`, `updated` FROM `language_packs` WHERE `type` = %s AND `domain` = %s AND `language` = %s",
+                               $type, $domain, $language );
+                       $results = $wpdb->get_results( $query, ARRAY_A );
+
+                       wp_cache_set( $cache_key, ( $results ? $results : '_empty_' ), $translations_cache_group, $translations_cache_time );
+
+                       if ( ! $results )
+                               continue;
+               }
+
+               usort( $results, 'version_compare_version_key_desc' );
+               if ( $version ) {
+                       $found = false;             
+                       foreach ( $results as $result ) {
+                               if ( version_compare( $result['version'], $version, '<=' ) ) {
+                                       $found = true;
+                                       break;
+                               }
+                       }
+               } else {
+                       $found = true;
+                       $result = $results[0];
+               }
+
+               if ( 'core' === $type ) {
+                       $path = "core/{$result['version']}/$language.zip";
+               } else {
+                       $path = "{$type}s/$domain/{$result['version']}/$language.zip";
+               }
+
+               if ( $found && $result && file_exists( ROSETTA_BUILDS . $path ) ) {
+                       $return[] = array(
+                               'type'     => $type,
+                               'slug'     => $domain,
+                               'language' => $language,
+                               'version'  => $result['version'],
+                               'updated'  => $result['updated'],
+                               'package'  => maybe_ssl_url( "http://global.wordpress.org/builds/$path" ),
+                               'autoupdate' => true,
+                       );
+               }
+       }
+
+       return $return;
+}
+
+function check_for_translations_paired_with_update( $args ) {
+       extract( $args );
+       $translations_for_update = array();
+       $translations_found_for_update = find_latest_translations( array( 'type' => $type, 'domain' => $domain, 'version' => $version, 'languages' => $languages ) );
+
+       foreach ( $translations_found_for_update as $language_pack ) {
+               $update = true;
+               if ( isset( $language_data[ $language_pack['language'] ] ) ) {
+                       $wporg_updated = strtotime( $language_pack['updated'] );
+                       $site_updated  = strtotime( $language_data[ $language_pack['language'] ]['PO-Revision-Date'] );
+
+                       // This update has a language update if: a) the PO file on WP.org is newer,
+                       // or b) the language pack's minimum version is higher than the currently installed version.
+                       // Example: version 3.7 is ready to go, but someone releases an update for 3.6.1.
+                       // The 3.6.1 strings are "newer" than the 3.7 strings, but the 3.7 language pack is
+                       // the new minimum, so it needs to be served.
+                       if ( $wporg_updated <= $site_updated && version_compare( $current_version, $language_pack['version'], '>=' ) )
+                               $update = false;
+               }
+               if ( ! $update )
+                       continue;
+
+               $translations_for_update[] = $language_pack;
+       }
+       return $translations_for_update;
+}
+
+function check_for_translations_of_installed_items( $args ) {
+       extract( $args );
+       $translations_for_current = find_latest_translations( array( 'type' => $type, 'domain' => $domain, 'version' => $version, 'languages' => $languages ) );
+
+       $translations = array();
+       foreach ( $translations_for_current as $language_pack ) {
+               $update = true;
+               if ( isset( $language_data[ $language_pack['language'] ] ) ) {
+                       $wporg_updated = strtotime( $language_pack['updated'] );
+                       $site_updated  = strtotime( $language_data[ $language_pack['language'] ]['PO-Revision-Date'] );
+
+                       if ( $wporg_updated <= $site_updated )
+                               $update = false;
+               }
+               if ( ! $update )
+                       continue;
+
+               $translations[] = $language_pack;
+       }
+
+       return $translations;
+}
+
</ins><span class="cx">Property changes on: sites/trunk/api.wordpress.org/public_html/translations/lib.php
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4>Added: svn:eol-style</h4></div>
<ins>+native
</ins><span class="cx">\ No newline at end of property
</span></div>

</body>
</html>