<!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>[5147] sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory: Plugin Directory: ZIPs: Do not build zips on demand, instead store them within a SVN repository.</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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/5147">5147</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/5147","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-03-13 05:56:27 +0000 (Mon, 13 Mar 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Plugin Directory: ZIPs: Do not build zips on demand, instead store them within a SVN repository.

See <a href="http://meta.trac.wordpress.org/ticket/1578">#1578</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiclassbasephp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclassplugindirectoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorycliclassimportphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryjobsclasspluginimportphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-plugin-import.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorytoolsclasssvnphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/tools/class-svn.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryzipclassbuilderphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-builder.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryzipclassservephp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinrebuildzipphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/rebuild-zip.php</a></li>
</ul>

<h3>Removed Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiroutesclasszipmanagementphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-zip-management.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiclassbasephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php      2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php        2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -19,7 +19,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                new Routes\Popular_Tags();
</span><span class="cx" style="display: block; padding: 0 10px">                new Routes\Query_Plugins();
</span><span class="cx" style="display: block; padding: 0 10px">                new Routes\SVN_Access();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                new Routes\Zip_Management();
</del><span class="cx" style="display: block; padding: 0 10px">                 new Routes\Plugin_Committers();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiroutesclasszipmanagementphp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-zip-management.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-zip-management.php     2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-zip-management.php       2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,148 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<?php
-namespace WordPressdotorg\Plugin_Directory\API\Routes;
-use WordPressdotorg\Plugin_Directory\Plugin_Directory;
-use WordPressdotorg\Plugin_Directory\API\Base;
-use WordPressdotorg\Plugin_Directory\ZIP;
-use WP_REST_Server;
-use Exception;
-
-/**
- * An internal API endpoint used to manage the plugin ZIP files on the server.
- *
- * Each WordPress.org server manages it's own set of plugin ZIP files, this API
- * endpoint allows a script to remotely perform commands on plugin zip files.
- *
- * @package WordPressdotorg_Plugin_Directory
- */
-class Zip_Management extends Base {
-
-       const ZIP_DIR = Zip\Serve::ZIP_DIR;
-
-       function __construct() {
-               register_rest_route( 'plugins/v1', '/zip-management', array(
-                       'methods'  => WP_REST_Server::CREATABLE,
-                       'callback' => array( $this, 'callback' ),
-                       'permission_callback' => array( $this, 'permission_check_internal_api_bearer' ),
-               ) );
-       }
-
-       /**
-        * Endpoint to manage plugin zip files.
-        *
-        * @param \WP_REST_Request $request The Rest API Request.
-        * @return array A formatted array of all the data for the plugin.
-        */
-       function callback( $request ) {
-
-               $results = array();
-
-               foreach ( $request['plugins'] as $plugin_slug => $actions ) {
-                       $plugin_slug = preg_replace( '![^a-z0-9%+-]!i', '', $plugin_slug );
-
-                       $to_invalidate = $to_rebuild = array();
-
-                       if ( !empty( $actions['invalidate'] ) ) {
-                               if ( 'all' == $actions['invalidate'] ) {
-                                       $to_invalidate = $this->get_plugin_zips( $plugin_slug );
-                               } elseif ( is_array( $actions['invalidate'] ) ) {
-                                       $to_invalidate = $actions['invalidate'];
-                               }
-                       }
-
-                       if ( !empty( $actions['rebuild'] ) ) {
-                               if ( 'all' == $actions['rebuild'] ) {
-                                       $to_rebuild = $this->get_plugin_zips( $plugin_slug );
-                               } elseif ( is_array( $actions['rebuild'] ) ) {
-                                       $to_rebuild = $actions['rebuild'];
-                               }
-
-                               foreach ( $to_rebuild as $zip ) {
-                                       if ( false !== ( $pos = array_search( $zip, $to_invalidate ) ) ) {
-                                               unset( $to_invalidate[ $pos ] );
-                                       }
-                               }
-                       }
-
-                       foreach ( $to_rebuild as $zip ) {
-                               try {
-                                       $this->rebuild( $plugin_slug, $zip );
-                                       $results['rebuild'][ $zip ] = file_get_contents( self::ZIP_DIR . '/' . $plugin_slug . '/' . $zip . '.md5' );
-                               } catch( Exception $e ) {
-                                       $results['rebuild'][ $zip ] = array(
-                                               false,
-                                               $e->getMessage(),
-                                       );
-                               }
-                       }
-
-                       foreach ( $to_invalidate as $zip ) {
-                               $results['invalidate'][ $zip ] = $this->invalidate( $plugin_slug, $zip );
-                       }
-               }
-
-               return $results;
-       }
-
-       /**
-        * Get a listing of ZIP files for a given plugin slug.
-        *
-        * @param string $plugin_slug The plugin slug to search for.
-        * @return array The list of ZIP files for the specified plugin slug.
-        */
-       protected function get_plugin_zips( $plugin_slug ) {
-               $files = glob( self::ZIP_DIR . '/' . $plugin_slug . '/*.zip' );
-               $files = array_map( 'basename', $files );
-
-               return $files;
-       }
-
-       /**
-        * Invalidates a specified ZIP file for a given plugin.
-        *
-        * @param string $plugin_slug The slug of the plugin to act upon.
-        * @param string $zip         The name of the zip file to act upon.
-        * @return bool Whether the operation suceeded.
-        */
-       protected function invalidate( $plugin_slug, $zip ) {
-               $file = self::ZIP_DIR . '/' . $plugin_slug . '/' . $zip;
-
-               if ( file_exists( $file ) ) {
-                       if ( ! unlink( $file ) ) {
-                               return false;
-                       }
-
-                       if ( file_exists( "{$file}.md5" ) ) {
-                               if ( ! unlink( "{$file}.md5" ) ) {
-                                       return false;
-                               }
-                       }
-               }
-
-               return true;
-       }
-
-       /**
-        * Rebuilds a specified plugin zip.
-        *
-        * @param string $plugin_slug The slug of the plugin to act upon.
-        * @param string $zip         The name of the zip file to rebuild.
-        * @return bool Whether the operation suceeded. Exceptions thrown on failure.
-        */
-       public function rebuild( $slug, $zip ) {
-               $version = preg_replace( '!^' . preg_quote( $slug, '!' ) . '(?:\.(.+))?\.zip$!i', '$1', $zip );
-               if ( $version == $slug ) {
-                       throw new Exception( __METHOD__ . ': Invalid ZIP file format' );
-               }
-               if ( '' == $version ) {
-                       $version = 'trunk';
-               }
-
-               $builder = new Zip\Builder( $slug, $version );
-               $builder->build();
-
-               clearstatcache();
-
-               return true;
-       }
-
-}
</del></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinrebuildzipphpfromrev5139sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinimportpluginphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/rebuild-zip.php (from rev 5139, sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/import-plugin.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/rebuild-zip.php                             (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/rebuild-zip.php       2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,97 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Plugin_Directory;
+use WordPressdotorg\Plugin_Directory\Tools\SVN;
+
+// This script should only be called in a CLI environment.
+if ( 'cli' != php_sapi_name() ) {
+       die();
+}
+
+//ob_start();
+
+$opts = getopt( '', array( 'url:', 'abspath:', 'plugin:', 'versions:', 'async' ) );
+
+// Guess the default parameters:
+if ( empty( $opts ) && $argc == 2 ) {
+       $opts['plugin'] = $argv[1];
+       $argv[1] = '--plugin ' . $argv[1];
+}
+if ( empty( $opts ) && $argc == 3 ) {
+       $opts['plugin'] = $argv[1];
+       $argv[1] = '--plugin ' . $argv[1];
+
+       $opts['versions'] = $argv[2];
+       $argv[2] = '--versions ' . $argv[2];
+}
+if ( empty( $opts['url'] ) ) {
+       $opts['url'] = 'https://wordpress.org/plugins/';
+}
+if ( empty( $opts['abspath'] ) && false !== strpos( __DIR__, 'wp-content' ) ) {
+       $opts['abspath'] = substr( __DIR__, 0, strpos( __DIR__, 'wp-content' ) );
+}
+if ( empty( $opts['versions'] ) ) {
+       $opts['versions'] = '';
+}
+
+foreach ( array( 'url', 'abspath', 'plugin' ) as $opt ) {
+       if ( empty( $opts[ $opt ] ) ) {
+               fwrite( STDERR, "Missing Parameter: $opt\n" );
+               fwrite( STDERR, "Usage: php {$argv[0]} --plugin hello-dolly --abspath /home/example/public_html --url https://wordpress.org/plugins/\n" );
+               fwrite( STDERR, "--url and --abspath will be guessed if possible.\n" );
+               exit(1);
+       }
+}
+
+// Bootstrap WordPress
+$_SERVER['HTTP_HOST']   = parse_url( $opts['url'], PHP_URL_HOST );
+$_SERVER['REQUEST_URI'] = parse_url( $opts['url'], PHP_URL_PATH );
+
+include rtrim( $opts['abspath'], '/' ) . '/wp-load.php';
+
+if ( ! class_exists( '\WordPressdotorg\Plugin_Directory\Plugin_Directory' ) ) {
+       fwrite( STDERR, "Error! This site doesn't have the Plugin Directory plugin enabled.\n" );
+       if ( defined( 'WPORG_PLUGIN_DIRECTORY_BLOGID' ) ) {
+               fwrite( STDERR, "Run the following command instead:\n" );
+               fwrite( STDERR, "\tphp " . implode( ' ', $argv ) . " --url " . get_site_url( WPORG_PLUGIN_DIRECTORY_BLOGID, '/' ) . "\n" );
+       }
+       exit(1);
+}
+
+$plugin_slug = $opts['plugin'];
+$versions    = array_filter( array_unique( array_map( 'trim', (array) explode( ',', $opts['versions'] ) ) ), 'strlen' );
+$start_time  = microtime(1);
+
+if ( empty( $versions ) ) {
+       // Rebuild them all!
+       $svn_tags = SVN::ls( "http://plugins.svn.wordpress.org/{$plugin_slug}/tags/" );
+       if ( false === $svn_tags ) {
+               fwrite( STDERR, "Error! Failed to retrieve SVN tag listing." );
+               exit(1);
+       }
+
+       $versions = array_map(
+               function( $dir ) { return trim( $dir, '/' ); },
+               $svn_tags
+       );
+       $versions[] = 'trunk';
+}
+
+if ( ! $versions ) {
+       fwrite( STDERR, "Error! No versions specified (or we couldn't find any)" );
+       exit(1);
+}
+
+echo "Rebuilding ZIPs for $plugin_slug... ";
+try {
+       $zip_builder = new ZIP\Builder();
+       $zip_builder->build(
+               $plugin_slug,
+               $versions,
+               "{$plugin_slug}: Rebuild triggered by " . php_uname('n' )
+       );
+
+       echo "OK. Took " . round( microtime(1) - $start_time, 2 )  . "s\n";
+} catch( \Exception $e ) {
+       fwrite( STDERR, "{$plugin_slug}: Zip Rebuild failed: " . $e->getMessage() . "\n" );
+       exit(1);
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclassplugindirectoryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php      2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php        2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,7 +120,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'edit_others_posts'  => 'plugin_edit_others',
</span><span class="cx" style="display: block; padding: 0 10px">                                'publish_posts'      => 'plugin_approve',
</span><span class="cx" style="display: block; padding: 0 10px">                                'read_private_posts' => 'do_not_allow',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'delete_posts'       => 'do_not_allow',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'delete_posts'       => is_super_admin() ? 'manage_options' : 'do_not_allow',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'create_posts'       => 'do_not_allow',
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1005,7 +1005,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Handles all the custom redirects needed in the Plugin Directory.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function custom_redirects() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Handle a redirect for /$plugin/$tab_name/ to /$plugin/#$tab_name.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( get_query_var( 'redirect_plugin_tab' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_safe_redirect( site_url( get_query_var( 'redirect_plugin_tab' ) ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1030,7 +1029,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // The about page is now over at /developers/.
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( 'about' === $path[2] ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                wp_safe_redirect( home_url( '/developers/' . ( ( isset( $path[3] ) && 'add' == $path[3] ) ? 'add/' : '' ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( isset( $path[3] ) && 'add' == $path[3] ) {
+                                       wp_safe_redirect( home_url( '/developers/add/' ) );
+                               } elseif ( isset( $path[3] ) && 'validator' == $path[3] ) {
+                                       wp_safe_redirect( home_url( '/developers/readme-validator/' ) );
+                               } else {
+                                       wp_safe_redirect( home_url( '/developers/' ) );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                                 die();
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorycliclassimportphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php    2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php      2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,6 +7,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> use WordPressdotorg\Plugin_Directory\Tools;
</span><span class="cx" style="display: block; padding: 0 10px"> use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
</span><span class="cx" style="display: block; padding: 0 10px"> use WordPressdotorg\Plugin_Directory\Tools\SVN;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+use WordPressdotorg\Plugin_Directory\Zip\Builder;
</ins><span class="cx" style="display: block; padding: 0 10px"> use Exception;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -46,10 +47,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @throws \Exception
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $plugin_slug      The slug of the plugin to import.
-        * @param array  $svn_changed_tags A list of tags/trunk which the SVN change touched. Optional.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $plugin_slug            The slug of the plugin to import.
+        * @param array  $svn_changed_tags       A list of tags/trunk which the SVN change touched. Optional.
+        * @param array  $svn_revision_triggered The SVN revision which this import has been triggered by.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function import_from_svn( $plugin_slug, $svn_changed_tags = array( 'trunk' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function import_from_svn( $plugin_slug, $svn_changed_tags = array( 'trunk' ), $svn_revision_triggered = 0 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $plugin = Plugin_Directory::get_plugin_post( $plugin_slug );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -189,7 +191,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $current_stable_tag = get_post_meta( $plugin->ID, 'stable_tag', true ) ?: 'trunk';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->rebuild_invalidate_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->rebuild_affected_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags, $svn_revision_triggered );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Finally, set the new version live.
</span><span class="cx" style="display: block; padding: 0 10px">                update_post_meta( $plugin->ID, 'stable_tag', $stable_tag );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -201,73 +203,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Rebuild and Invalidate plugin ZIPs on all web nodes using the REST API Endpoints.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * (Re)build plugin ZIPs affected by this commit.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $plugin_slug        The plugin slug.
-        * @param string $stable_tag         The new stable tag.
-        * @param string $current_stable_tag The new stable tag.
-        * @param array  $svn_changed_tags   The list of SVN tags modified since last import.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $plugin_slug            The plugin slug.
+        * @param string $stable_tag             The new stable tag.
+        * @param string $current_stable_tag     The new stable tag.
+        * @param array  $svn_changed_tags       The list of SVN tags modified since last import.
+        * @param string $svn_revision_triggered The SVN revision which triggered the rebuild.
+        *
+        * @return bool
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function rebuild_invalidate_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags ) {
-               global $wporg_webs;
-               $invalidate_zips = $rebuild_zips = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function rebuild_affected_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags, $svn_revision_triggered = 0 ) {
+               $versions_to_build = $svn_changed_tags;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $svn_changed_tags as $tag ) {
-                       if ( 'trunk' == $tag ) {
-                               if ( 'trunk' == $stable_tag ) {
-                                       // Trunk is stable, so we'll need to rebuild the zip
-                                       $rebuild_zips[] = "{$plugin_slug}.zip";
-                               } else {
-                                       // Trunk isn't stable, so we'll just remove it so it's rebuilt on demand
-                                       $invalidate_zips[] = "{$plugin_slug}.zip";
-                               }
-                               continue;
-                       }
-                       if ( $tag == $stable_tag || $tag == $current_stable_tag ) {
-                               $rebuild_zips[] = "{$plugin_slug}.{$tag}.zip";
-                       } else {
-                               $invalidate_zips[] = "{$plugin_slug}.{$tag}.zip";
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Ensure that the stable zip is built/rebuilt if need be.
+               if ( $stable_tag != $current_stable_tag && ! in_array( $stable_tag, $versions_to_build ) ) {
+                       $versions_to_build[] = $stable_tag;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $stable_tag != $current_stable_tag ) {
-                       // plugin is updated, ensure that everything is rebuilt.
-                       if ( ! in_array( $stable_tag, $svn_changed_tags ) ) {
-                               $rebuild_zips[] = "{$plugin_slug}" . ( 'trunk' == $stable_tag ? '' : ".{$stable_tag}" ) . '.zip';
-                       }
-               }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( empty( $wporg_webs ) || ( empty( $invalidate_zips ) && empty( $rebuild_zips ) ) ) {
-                       return;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Rebuild/Build $build_zips
+               try {
+                       // This will rebuild the ZIP.
+                       $zip_builder = new Builder();
+                       $zip_builder->build(
+                               $plugin_slug,
+                               array_unique( $versions_to_build ),
+                               $svn_revision_triggered ?
+                                       "{$plugin_slug}: ZIP build triggered by https://plugins.trac.wordpress.org/changeset/{$svn_revision_triggered}" :
+                                       "{$plugin_slug}: ZIP build triggered by " . php_uname('n')
+                       );
+               } catch( Exception $e ) {
+                       return false;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $urls = array();
-               foreach ( $wporg_webs as $node ) {
-                       $urls[] = preg_replace( '!^https?://wordpress.org/!', "http://$node/", site_url( '/wp-json/plugins/v1/zip-management' ) );
-               }
-               $headers = array(
-                       'User-Agent' => 'WordPress.org Plugin Directory',
-                       'Host' => 'WordPress.org',
-                       'Authorization' => 'BEARER ' . PLUGIN_API_INTERNAL_BEARER_TOKEN,
-               );
-               $body = array(
-                       'plugins' => array(
-                               $plugin_slug => array(
-                                       'invalidate' => $invalidate_zips,
-                                       'rebuild' => $rebuild_zips,
-                               )
-                       )
-               );
-
-               $results = array();
-               foreach ( $urls as $url ) {
-                       $results[ $url ] = wp_remote_post( $url, array(
-                               'body' => $body,
-                               'headers' => $headers,
-                               'sslverify' => false
-                       ) );
-               }
-
-               // TODO Do something with $results to verify all servers said the rebuilt zip was correct or something.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return true;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryjobsclasspluginimportphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-plugin-import.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-plugin-import.php    2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-plugin-import.php      2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -33,9 +33,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $plugin_slug  = $plugin_data['plugin'];
</span><span class="cx" style="display: block; padding: 0 10px">                $changed_tags = isset( $plugin_data['tags_touched'] ) ? $plugin_data['tags_touched'] : array( 'trunk' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $revision = isset( $plugin_data['revisions'] ) ? max( (array)$plugin_data['revisions'] ) : false;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 try {
</span><span class="cx" style="display: block; padding: 0 10px">                        $importer = new CLI\Import;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $importer->import_from_svn( $plugin_slug, $changed_tags );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $importer->import_from_svn( $plugin_slug, $changed_tags, $revision );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } catch( Exception $e ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        fwrite( STDERR, "[{$plugin_slug}] Plugin Import Failed: " . $e->getMessage() . "\n" );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorytoolsclasssvnphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/tools/class-svn.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/tools/class-svn.php     2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/tools/class-svn.php       2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21,13 +21,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int  $revision The revision imported.
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public static function import( $path, $url, $message ) {
-               $options     = array(
-                       'non-interactive',
-                       'm'    => $message,
-                       'user' => PLUGIN_SVN_MANAGEMENT_USER,
-                       'pass' => PLUGIN_SVN_MANAGEMENT_PASS,
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function import( $path, $url, $message, $options = array() ) {
+               $options[] = 'non-interactive';
+               $options['m'] = $message;
+               if ( empty( $options['username'] ) ) {
+                       $options['username'] = PLUGIN_SVN_MANAGEMENT_USER;
+                       $options['password'] = PLUGIN_SVN_MANAGEMENT_PASS;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $esc_options = self::parse_esc_parameters( $options );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $esc_path = escapeshellarg( $path );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,6 +79,132 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Create an SVN Checkout of a URL to a local directory.
+        *
+        * @static
+        *
+        * @param string $url         The URL to export.
+        * @param string $destination The local folder to checkout into.
+        * @param array  $options     Optional. A list of options to pass to SVN. Default: empty array.
+        * @return array {
+        *     @type bool $result   The result of the operation.
+        *     @type int  $revision The revision exported.
+        * }
+        */
+       public static function checkout( $url, $destination, $options = array() ) {
+               $options[]   = 'non-interactive';
+               $esc_options = self::parse_esc_parameters( $options );
+
+               $esc_url         = escapeshellarg( $url );
+               $esc_destination = escapeshellarg( $destination );
+
+               $output = self::shell_exec( "svn checkout $esc_options $esc_url $esc_destination 2>&1" );
+               if ( preg_match( '/Checked out revision (?P<revision>\d+)[.]/i', $output, $m ) ) {
+                       $revision = (int) $m['revision'];
+                       $result   = true;
+               } else {
+                       $result = false;
+                       $errors = self::parse_svn_errors( $output );
+               }
+
+               return compact( 'result', 'revision', 'errors' );
+       }
+
+       /**
+        * Update a SVN checkout.
+        *
+        * @static
+        *
+        * @param string $checkout The path of the SVN checkout to update.
+        * @param array  $options  Optional. A list of options to pass to SVN. Default: empty array.
+        * @return array {
+        *     @type bool $result   The result of the operation.
+        *     @type int  $revision The revision exported.
+        * }
+        */
+       public static function up( $checkout, $options = array() ) {
+               $options[]   = 'non-interactive';
+               $esc_options = self::parse_esc_parameters( $options );
+
+               $esc_checkout = escapeshellarg( $checkout );
+
+               $output = self::shell_exec( "svn up $esc_options $esc_checkout 2>&1" );
+               if ( preg_match( '/Updated to revision (?P<revision>\d+)[.]/i', $output, $m ) ) {
+                       $revision = (int) $m['revision'];
+                       $result   = true;
+               } else {
+                       $result = false;
+                       $errors = self::parse_svn_errors( $output );
+               }
+
+               return compact( 'result', 'revision', 'errors' );
+       }
+
+       /**
+        * Add a file in a SVN checkout to be revisioned.
+        *
+        * @static
+        *
+        * @param string $checkout The path of the file to add to SVN.
+        * @return array {
+        *     @type bool $result   The result of the operation.
+        * }
+        */
+       public static function add( $file ) {
+               $options[]   = 'non-interactive';
+               $esc_options = self::parse_esc_parameters( $options );
+
+               $esc_file     = escapeshellarg( $file );
+
+               $output = self::shell_exec( "svn add $esc_options $esc_file 2>&1" );
+               if ( preg_match( "/^A/i", $output ) ) {;
+                       $result   = true;
+               } else {
+                       $result = false;
+                       $errors = self::parse_svn_errors( $output );
+               }
+
+               return compact( 'result', 'errors' );
+       }
+
+       /**
+        * Commit changes in a SVN checkout.
+        *
+        * @static
+        *
+        * @param string $checkout The local folder to import into SVN.
+        * @param string $message  The commit message.
+        * @param array  $options  Any specific options to pass to SVN.
+        * @return array {
+        *     @type bool $result   The result of the operation.
+        *     @type int  $revision The revision imported.
+        * }
+        */
+       public static function commit( $checkout, $message, $options = array() ) {
+               $options[] = 'non-interactive';
+               $options['m'] = $message;
+               if ( empty( $options['username'] ) ) {
+                       $options['username'] = PLUGIN_SVN_MANAGEMENT_USER;
+                       $options['password'] = PLUGIN_SVN_MANAGEMENT_PASS;
+               }
+
+               $esc_options = self::parse_esc_parameters( $options );
+
+               $esc_checkout = escapeshellarg( $checkout );
+
+               $output = self::shell_exec( "svn commit $esc_options $esc_checkout 2>&1" );
+               if ( preg_match( '/Committed revision (?P<revision>\d+)[.]/i', $output, $m ) ) {
+                       $revision = (int) $m['revision'];
+                       $result   = true;
+               } else {
+                       $result = false;
+                       $errors = self::parse_svn_errors( $output );
+               }
+
+               return compact( 'result', 'revision', 'errors' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * List the files in a remote SVN destination.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @static
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryzipclassbuilderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-builder.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-builder.php   2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-builder.php     2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,76 +10,138 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Builder {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * The base directory for the ZIP files.
-        * Zip files will be stored in a sub-directory, such as:
-        * /tmp/plugin-zipfiles/hello-dolly/hello-dolly.zip
-        */
-       const ZIP_DIR = '/tmp/plugin-zipfiles';
-       const SVN_URL = 'https://plugins.svn.wordpress.org';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ const TMP_DIR = '/tmp/plugin-zip-builder';
+       const SVN_URL = 'http://plugins.svn.wordpress.org';
+       const ZIP_SVN_URL = PLUGIN_ZIP_SVN_URL;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public $zip_file = '';
-       public $md5_file = '';
-       protected $tmp_build_file = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $zip_file = '';
</ins><span class="cx" style="display: block; padding: 0 10px">         protected $tmp_build_dir  = '';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        protected $tmp_dir = '';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        protected $slug    = '';
+       protected $version = '';
+       protected $context = '';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Generate a ZIP for a provided Plugin Version.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Generate a ZIP for a provided Plugin versions.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $plugin_slug The Plugin slug
-        * @param string $version     The version to build (tag, or trunk)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $slug     The plugin slug.
+        * @param array  $versions The versions of the plugin to build ZIPs for.
+        * @param string $context  The context of this Builder instance (commit #, etc)
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function __construct( $slug, $version ) {
-               if ( ! is_dir( self::ZIP_DIR ) ) {
-                       mkdir( self::ZIP_DIR, 0777, true );
-                       chmod( self::ZIP_DIR, 0777 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function build( $slug, $versions, $context = '' ) {
+               // Bail when in an unconfigured environment.
+               if ( ! defined( 'PLUGIN_ZIP_SVN_URL' ) ) {
+                       return false;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! is_dir( self::ZIP_DIR . '/' . $slug ) ) {
-                       mkdir( self::ZIP_DIR . '/' . $slug, 0777, true );
-                       chmod( self::ZIP_DIR . '/' . $slug, 0777 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $this->slug     = $slug;
+               $this->versions = $versions;
+               $this->context  = $context;
+
+               // General TMP directory
+               if ( ! is_dir( self::TMP_DIR ) ) {
+                       mkdir( self::TMP_DIR, 0777, true );
+                       chmod( self::TMP_DIR, 0777 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->slug = $slug;
-               $this->version = $version;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Temp Directory for this instance of the Builder class.
+               $this->tmp_dir = $this->generate_temporary_directory( self::TMP_DIR, $slug );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( 'trunk' == $this->version ) {
-                       $this->zip_file = self::ZIP_DIR . "/{$this->slug}/{$this->slug}.zip";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Create a checkout of the ZIP SVN
+               $res_checkout = SVN::checkout(
+                       self::ZIP_SVN_URL,
+                       $this->tmp_dir,
+                       array(
+                               'depth' => 'empty',
+                               'username' => PLUGIN_ZIP_SVN_USER,
+                               'password' => PLUGIN_ZIP_SVN_PASS,
+                       )
+               );
+
+               if ( $res_checkout['result'] ) {
+
+                       // Ensure the plugins folder exists within svn
+                       $plugin_folder = "{$this->tmp_dir}/{$this->slug}/";
+                       $res = SVN::up(
+                               $plugin_folder,
+                               array(
+                                       'depth' => 'empty'
+                               )
+                       );
+                       if ( ! is_dir( $plugin_folder ) ) {
+                               mkdir( $plugin_folder, 0777, true );
+                               $res = SVN::add( $plugin_folder );
+                       }
+                       if ( ! $res['result'] ) {
+                               throw new Exception( __METHOD__ . ": Failed to create {$plugin_folder}." );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->zip_file = self::ZIP_DIR . "/{$this->slug}/{$this->slug}.{$this->version}.zip";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 throw new Exception( __METHOD__ . ": Failed to create checkout of {$svn_url}." );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->md5_file = $this->zip_file . '.md5';
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Build the requested ZIPs
+               foreach ( $versions as $version ) {
+                       $this->version = $version;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Generate a ZIP for the plugin + version 
-        */
-       public function build() {
-               try {
-                       $this->tmp_build_file = $this->generate_temporary_filename( dirname( $this->zip_file ), "tmp-{$this->slug}.{$this->version}", '.zip' );
-                       $this->tmp_build_dir  = $this->tmp_build_file . '-files';
-                       mkdir( $this->tmp_build_dir, 0777, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( 'trunk' == $version ) {
+                               $this->zip_file = "{$this->tmp_dir}/{$this->slug}/{$this->slug}.zip";
+                       } else {
+                               $this->zip_file = "{$this->tmp_dir}/{$this->slug}/{$this->slug}.{$version}.zip";
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->export_plugin();
-                       $this->fix_directory_dates();                        
-                       $this->generate_zip();
-                       $this->move_into_place();
-                       $this->generate_md5();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Pull the ZIP file down we're going to modify, which may not already exist.
+                       SVN::up( $this->zip_file );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->cleanup();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 try {
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return true;
-               } catch( Exception $e ) {
-                       $this->cleanup();
-                       throw $e;
-               }/* finally { // PHP 5.5+, meta.svn is limited to PHP 5.4 code still.
-                       $this->cleanup();
-               }*/
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $this->tmp_build_dir  = $this->zip_file . '-files';
+                               mkdir( $this->tmp_build_dir, 0777, true );
+
+                               $this->export_plugin();
+                               $this->fix_directory_dates();                        
+                               $this->generate_zip();
+                               $this->cleanup_plugin_tmp();
+
+                       } catch( Exception $e ) {
+                               // In event of error, skip this file this time.
+                               $this->cleanup_plugin_tmp();
+
+                               // Perform an SVN up to revert any changes made.
+                               SVN::up( $this->zip_file );
+                               continue;
+                       }
+
+                       // Add the ZIP file to SVN - This is only really needed for new files which don't exist in SVN.
+                       SVN::add( $this->zip_file );                 
+               }
+
+               $res = SVN::commit(
+                       $this->tmp_dir,
+                       $this->context ? $this->context : "Updated ZIPs for {$this->slug}.",
+                       array(
+                               'username' => PLUGIN_ZIP_SVN_USER,
+                               'password' => PLUGIN_ZIP_SVN_PASS,
+                       )
+               );
+
+               $this->invalidate_zip_caches( $versions );
+
+               $this->cleanup();
+
+               if ( ! $res['result'] ) {
+                       if ( $res['errors'] ) {
+                               throw new Exception( __METHOD__ . ': Failed to commit the new ZIPs: ' . $res['errors'][0]['error_message'] );
+                       } else {
+                               throw new Exception( __METHOD__ . ': Commit failed without error, maybe there were no modified files?' );
+                       }
+               }
+
+               return true;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Generates a temporary unique file in a given directory
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Generates a temporary unique directory in a given directory
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * Performs a similar job to `tempnam()` with an added suffix and doesn't
</span><span class="cx" style="display: block; padding: 0 10px">         * cut off the $prefix at 60 characters.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -91,9 +153,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $prefix The file prefix.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $suffix The file suffix, optional.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return string Filename of unique temporary file.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return string Path of unique temporary directory.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function generate_temporary_filename( $dir, $prefix, $suffix = '' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function generate_temporary_directory( $dir, $prefix, $suffix = '' ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $i = 0;
</span><span class="cx" style="display: block; padding: 0 10px">                do {
</span><span class="cx" style="display: block; padding: 0 10px">                        $rand = uniqid();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -106,6 +168,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                fclose( $fp );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Convert file to directory.
+               unlink( $filename );
+               if ( ! mkdir( $filename, 0777, true ) ) {
+                       throw new Exception( __METHOD__ . ': Could not convert temporary filename to directory.' );
+               }
+               chmod( $filename, 0777 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $filename;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -167,7 +236,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        escapeshellarg( $this->tmp_build_dir )
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $latest_file_modified_timestamp ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        throw new Exception( _METHOD__ . ': Unable to locate the latest modified files timestamp.', 503 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 throw new Exception( __METHOD__ . ': Unable to locate the latest modified files timestamp.', 503 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->exec( sprintf(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -181,13 +250,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Generates the actual ZIP file we've painstakingly created the files for.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function generate_zip() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // We have to remove the temporary 0-byte file first as zip will complain about not being able to find the zip structures.
-               unlink( $this->tmp_build_file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // If we're building an existing zip, remove the existing file first.
+               if ( file_exists( $this->zip_file ) ) {
+                       unlink( $this->zip_file );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->exec( sprintf(
</span><span class="cx" style="display: block; padding: 0 10px">                        'cd %s && find %s -print0 | sort -z | xargs -0 zip -Xu %s 2>&1',
</span><span class="cx" style="display: block; padding: 0 10px">                        escapeshellarg( $this->tmp_build_dir ),
</span><span class="cx" style="display: block; padding: 0 10px">                        escapeshellarg( $this->slug ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        escapeshellarg( $this->tmp_build_file )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 escapeshellarg( $this->zip_file )
</ins><span class="cx" style="display: block; padding: 0 10px">                 ), $zip_build_output, $return_value );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $return_value ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -195,45 +266,52 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Moves the completed ZIP into it's real-life location.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Purge ZIP caches after ZIP building.
+        *
+        * @param array $versions The list of plugin versions of modified zips.
+        * @return bool
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function move_into_place() {
-               $this->exec( sprintf(
-                       'mv -f %s %s',
-                       escapeshellarg( $this->tmp_build_file ),
-                       escapeshellarg( $this->zip_file )
-               ), $output, $return_value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function invalidate_zip_caches( $versions ) {
+               // TODO: Implement PURGE 
+               return true;
+               if ( ! defined( 'PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION' ) ) {
+                       return true;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $return_value ) {
-                       throw new Exception( __METHOD__ . ': Could not move ZIP into place.', 503 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $versions as $version ) {
+                       if ( 'trunk' == $version ) {
+                               $zip = "{$this->slug}/{$this->slug}.zip";
+                       } else {
+                               $zip = "{$this->slug}/{$this->slug}.{$version}.zip";
+                       }
+
+                       foreach ( $plugins_downloads_load_balancer /* TODO */ as $lb ) {
+                               $url = 'http://' . $lb . PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION . $zip;
+                               wp_remote_request(
+                                       $url,
+                                       array(
+                                               'method' => 'PURGE',
+                                       )
+                               );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Generates the MD5 for the ZIP file used for serving.
-        *
-        * This can also be used for generating a package signature in the future.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Cleans up any temporary directories created by the ZIP Builder.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function generate_md5() {
-               $this->exec( sprintf(
-                       "md5sum %s | head -c 32 > %s",
-                       escapeshellarg( $this->zip_file ),
-                       escapeshellarg( $this->md5_file )
-               ), $output, $return_code );
-
-               if ( $return_code ) {
-                       throw new Exception( __METHOD__ . ': Failed to create file checksum.', 503 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function cleanup() {
+               if ( $this->tmp_dir ) {
+                       $this->exec( sprintf( 'rm -rf %s', escapeshellarg( $this->tmp_dir ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Cleans up any temporary directories created by the ZIP Builder.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Cleans up any temporary directories created by the ZIP builder for a specific build.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function cleanup() {
-               if ( $this->tmp_build_file && file_exists( $this->tmp_build_file ) ) {
-                       unlink( $this->tmp_build_file );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function cleanup_plugin_tmp() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( $this->tmp_build_dir ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->exec( sprintf( 'rm -rf %s', escapeshellarg( $this->tmp_build_dir ) ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryzipclassservephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php     2017-03-10 20:00:51 UTC (rev 5146)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php       2017-03-13 05:56:27 UTC (rev 5147)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,28 +12,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Serve {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        const ZIP_DIR = '/tmp/plugin-zipfiles';
-
</del><span class="cx" style="display: block; padding: 0 10px">         public function __construct() {
</span><span class="cx" style="display: block; padding: 0 10px">                try {
</span><span class="cx" style="display: block; padding: 0 10px">                        $request = $this->determine_request();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Serve & perhaps build if need be
-                       $files = $this->get_files( $request );
-                       if ( ! file_exists( $files['zip'] ) ) {
-                               $builder = new Builder( $request['slug'], $request['version'] );
-                               $builder->build();
-                               clearstatcache();
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->serve_zip( $request );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->serve_zip( $files, $request );
-
</del><span class="cx" style="display: block; padding: 0 10px">                         if ( $request['args']['stats'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->record_stats( $request );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                } catch ( Exception $e )  {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->error( $e->getCode() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->error();
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -50,7 +40,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $version = 'trunk';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! preg_match( "!^(?P<slug>[a-z0-9-]+)(.(?P<version>.+))?.zip$!i", $zip, $m ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        throw new Exception( __METHOD__ . ": Invalid URL" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 throw new Exception( __METHOD__ . ": Invalid URL." );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $slug = strtolower( $m['slug'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -95,7 +85,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $version = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'stable_tag' LIMIT 1", $post_id ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $version ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        throw new Exception( __METHOD__ . ": A version for $plugin_slug cannot be determined.", 404 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 throw new Exception( __METHOD__ . ": A version for $plugin_slug cannot be determined." );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $version;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,7 +110,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $post_id ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        throw new Exception( __METHOD__ . ": A post_id for $plugin_slug cannot be determined.", 404 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 throw new Exception( __METHOD__ . ": A post_id for $plugin_slug cannot be determined." );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $post_id;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -132,40 +122,37 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $request The request object for the request.
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array An array containing the files to use for the request, 'zip' and 'md5'.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function get_files( $request ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function get_file( $request ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( empty( $request['version'] ) || 'trunk' == $request['version'] ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $zip = self::ZIP_DIR . "/{$request['slug']}/{$request['slug']}.zip";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return "{$request['slug']}/{$request['slug']}.zip";
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $zip = self::ZIP_DIR . "/{$request['slug']}/{$request['slug']}.{$request['version']}.zip";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return "{$request['slug']}/{$request['slug']}.{$request['version']}.zip";
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $md5 = $zip . '.md5';
-
-               return compact( 'zip', 'md5' );
</del><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Output a ZIP file with all headers.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array $files {
-        *   Array of files for the request.
-        *
-        *   @type string $zip The Zip file to serve.
-        *   @type string $md5 The MD5 file to use for the Content-MD5 header. Optional.
-        * }
</del><span class="cx" style="display: block; padding: 0 10px">          * @param array $request The request array for the request.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function serve_zip( $files, $request ) {
-               header( 'Content-Type: application/zip' );
-               header( 'Content-Disposition: attachment; filename=' . basename( $files['zip'] ) );
-               if ( !empty( $files['md5'] ) && ( $md5 = file_get_contents( $files['md5'] ) )  ) {
-                       header( 'Content-MD5: ' . $md5 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function serve_zip( $request ) {
+               $zip = $this->get_file( $request );
+
+               if ( defined( 'PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION' ) ) {
+                       $zip_url = PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION . $zip;
+
+                       header( 'Content-Type: application/zip' );
+                       header( 'Content-Disposition: attachment; filename=' . basename( $zip ) );
+                       header( "X-Accel-Redirect: $zip_url" );
+               } else {
+                       header( 'Content-Type: text/plain' );
+                       echo "This is a request for $zip, this server isn't currently configured to serve zip files.\n";
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // TODO: Accel Redirect allows for ZIP files to be cached on the LB's
-               // header('X-Accel-Redirect: ' . $accel_redirect );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( function_exists( 'fastcgi_finish_request' ) ) {
+                       fastcgi_finish_request();
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                header( 'Content-Length: ' . filesize( $files['zip'] ) );
-               readfile( $files['zip'] );
</del><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -224,23 +211,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Quit with an Error code.
-        *
-        * @param int $code The HTTP Error code, 404 or 503.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Bail with a 404.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function error( $code = 404 ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function error() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
</span><span class="cx" style="display: block; padding: 0 10px">                $protocol .= ' ';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                switch ( $code ) {
-                       case 503:
-                               header( $protocol . '503 Service Unavailable' );
-                               die( '503 Service Unavailable' );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        default:
-                       case 404:
-                               header( $protocol . '404 File not found' );
-                               die( '404 File not found' );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         header( $protocol . '404 File not found' );
+               die( '404 file not found' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>