<!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>[3373] sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory: Plugin Directory: Add a SVN watching script to process & import plugins to the plugin directory after each commit.</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/3373">3373</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/3373","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>2016-06-15 04:47:04 +0000 (Wed, 15 Jun 2016)</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: Add a SVN watching script to process & import plugins to the plugin directory after each commit.

This doesn't act as a post-commit hook, but instead is designed to be run in an endless loop.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinimportpluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/import-plugin.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>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinsvnwatcherphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/svn-watcher.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorycliclasssvnwatcherphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-svn-watcher.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinimportpluginphp"></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/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/import-plugin.php   2016-06-15 04:18:23 UTC (rev 3372)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/import-plugin.php     2016-06-15 04:47:04 UTC (rev 3373)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8,7 +8,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> ob_start();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-$opts = getopt( '', array( 'url:', 'abspath:', 'plugin:' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+$opts = getopt( '', array( 'url:', 'abspath:', 'plugin:', 'changed-tags:' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> // Guess the default parameters:
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $opts ) && $argc == 2 ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,6 +22,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $opts['abspath'] = substr( __DIR__, 0, strpos( __DIR__, 'wp-content' ) );
</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">+if ( empty( $opts['changed-tags'] ) ) {
+       $opts['changed-tags'] = array( 'trunk' );
+} else {
+       $opts['changed-tags'] = explode( ',', $opts['changed-tags'] );
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( array( 'url', 'abspath', 'plugin' ) as $opt ) {
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $opts[ $opt ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                fwrite( STDERR, "Missing Parameter: $opt\n" );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -46,15 +52,18 @@
</span><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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-$plugin_slug = $opts['plugin'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+$plugin_slug  = $opts['plugin'];
+$changed_tags = $opts['changed-tags'];
+$start_time   = microtime(1);
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> echo "Processing Import for $plugin_slug... ";
</span><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 );
-       echo "OK\n";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $importer->import_from_svn( $plugin_slug, $changed_tags );
+       echo "OK. Took " . round( microtime(1) - $start_time, 2 )  . "s\n";
</ins><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">-        echo "Failed.\n";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ echo "Failed. Took " . round( microtime(1) - $start_time, 2 )  . "s\n";
+
</ins><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">        exit(1);
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinsvnwatcherphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/svn-watcher.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/svn-watcher.php                             (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/svn-watcher.php       2016-06-15 04:47:04 UTC (rev 3373)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,20 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Plugin_Directory;
+/**
+ * This is a stand alone script which is regularly run to import plugins into the WordPress Plugin Directory after svn commits.
+ */
+
+// This script should only be called in a CLI environment.
+if ( 'cli' != php_sapi_name() ) {
+       die();
+}
+
+$_SERVER['HTTPS'] = 'on';
+$_SERVER['HTTP_HOST'] = 'wordpress.org';
+$_SERVER['REQUEST_URI'] = '/plugins/';
+
+// Assume a standard WordPress wp-content/plugins file structure.
+include dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/wp-load.php';
+
+$watcher = new CLI\SVN_Watcher();
+$watcher->watch();
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/svn-watcher.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><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    2016-06-15 04:18:23 UTC (rev 3372)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php      2016-06-15 04:47:04 UTC (rev 3373)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,7 +22,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        var $readme_fields = array(
</span><span class="cx" style="display: block; padding: 0 10px">                'tested',
</span><span class="cx" style="display: block; padding: 0 10px">                'requires',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'stable_tag',
</del><span class="cx" style="display: block; padding: 0 10px">                 'donate_link',
</span><span class="cx" style="display: block; padding: 0 10px">                'upgrade_notice',
</span><span class="cx" style="display: block; padding: 0 10px">                'contributors',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -45,9 +44,10 @@
</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.
</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.
</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 ) {
</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' ) ) {
</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">@@ -151,14 +151,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                continue;
</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">-                        if ( 'stable_tag' == $readme_field ) {
-                               // The stable_tag needs to come from the trunk readme at all times.
-                               $value = $stable_tag;
-                       } else {
-                               $value = $readme->$readme_field;
-                       }
-
-                       update_post_meta( $plugin->ID, $readme_field, wp_slash( $value ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 update_post_meta( $plugin->ID, $readme_field, wp_slash( $readme->$readme_field ) );
</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">                foreach ( $this->plugin_headers as $plugin_header => $meta_field ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -189,9 +182,86 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $user->add_role( 'plugin_committer' );
</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">+
+               $current_stable_tag = get_post_meta( $plugin->ID, 'stable_tag', true );
+
+               $this->rebuild_invalidate_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags );
+
+               // Finally, set the new version live.
+               update_post_meta( $plugin->ID, 'stable_tag', $stable_tag );
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Rebuild and Invalidate plugin ZIPs on all web nodes using the REST API Endpoints.
+        *
+        * @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.
+        */
+       protected function rebuild_invalidate_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags ) {
+               global $wporg_webs;
+               $invalidate_zips = $rebuild_zips = array();
+
+               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";
+                       }
+               }
+               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' == $tag ? '' : ".{$stable_tag}" ) . '.zip';
+                       }
+               }
+
+               if ( empty( $wporg_webs ) || ( empty( $invalidate_zips ) && empty( $rebuild_zips ) ) ) {
+                       return;
+               }
+
+               $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.
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Export a plugin and determine all the information about the current state of the plugin.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * - Creates a /trunk/ export of the plugin.
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorycliclasssvnwatcherphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-svn-watcher.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-svn-watcher.php                               (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-svn-watcher.php 2016-06-15 04:47:04 UTC (rev 3373)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,207 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Plugin_Directory\CLI;
+use WordPressdotorg\Plugin_Directory\Plugin_Directory;
+use WordPressdotorg\Plugin_Directory\Tools\SVN;
+use Exception;
+
+/**
+ * A class that watches SVN and triggers plugin imports into WordPress and GlotPress
+ *
+ * @package WordPressdotorg\Plugin_Directory\CLI
+ */
+class SVN_Watcher {
+
+       const SVN_URL = 'https://plugins.svn.wordpress.org/';
+       const PHP     = '/usr/local/bin/php';
+
+       public function __construct() {
+               $this->watch();
+       }
+
+       /**
+        * This method is responsible for running a loop over all SVN revisions to fetch updated details.
+        */
+       public function watch() {
+               $svn_rev_option_name = 'svn_rev_' . php_uname( 'n' );
+
+               $last_rev_processed = $this->get_option( $svn_rev_option_name );
+               if ( ! $last_rev_processed ) {
+                       throw new Exception( "Unknown Revision to parse from, please check the value of {$svn_rev_option_name} in the options table." );
+               }
+
+               $head_rev = $this->get_head_rev();
+               // Don't attempt to process more than 100 revs at a time, both SimpleXML and our SVN server doesn't like it.
+               if ( $head_rev > $last_rev_processed + 100 ) {
+                       $head_rev = $last_rev_processed + 100;
+               }
+
+               if ( $last_rev_processed == $head_rev ) {
+                       // Nothing to do!
+                       return;
+               }
+
+               echo "Processing changes from $last_rev_processed to $head_rev..\n";
+
+               $plugins_to_process = $this->get_plugin_changes_between( $last_rev_processed, $head_rev );
+
+               foreach ( $plugins_to_process as $plugin_slug => $plugin_data ) {
+                       if ( $plugin_data['assets_touched'] && ! in_array( 'trunk', $plugin_data['tags_touched'] ) ) {
+                               $plugin_data['tags_touched'][] = 'trunk';
+                       }
+
+                       $esc_plugin_slug  = escapeshellarg( $plugin_slug );
+                       $esc_changed_tags = escapeshellarg( implode( ',', $plugin_data['tags_touched'] ) );
+                       $esc_revision     = escapeshellarg( $plugin_data['revision'] );
+
+                       $cmd = self::PHP . ' ' . dirname( __DIR__ ) . "/bin/import-plugin.php --plugin {$esc_plugin_slug} --changed-tags {$esc_changed_tags} --revision {$esc_revision}";
+
+                       echo "\$$cmd\n";
+                       echo shell_exec( $cmd ) . "\n";
+
+                       if ( $this->process_i18n_for_plugin( $plugin_slug) && $plugin_data['readme_touched'] ) {
+// TODO                                echo "import GlotPress readme\n";
+                       }
+                       if ( $this->process_i18n_for_plugin( $plugin_slug) && $plugin_data['code_touched'] ) {
+// TODO                                echo "import GlotPress code\n";
+                       }
+
+                       $this->update_option( $svn_rev_option_name, $plugin_data['revision'] );
+               }
+
+               // Update it to HEAD again. We do this as $plugin_data['revision'] may be set to PREVHEAD in the event the latest 2 (or more) commits are to a single plugin.
+               $this->update_option( $svn_rev_option_name, $head_rev );
+       }
+
+       /**
+        * Determine Whether i18n processing is enabled for a plugin.
+        */
+       protected function process_i18n_for_plugin( $plugin_slug ) {
+               return false;
+       }
+
+       /**
+        * Determines what plugin changes have happened between two revisions.
+        *
+        * @param string $rev      The revision to search from.
+        * @param string $head_rev The revision to search to. Default HEAD.
+        * @return array A list of plugin changes to process.
+        */
+       protected function get_plugin_changes_between( $rev, $head_rev = 'HEAD' ) {
+
+               $logs = SVN::log( self::SVN_URL, array( $rev, $head_rev ) );
+               if ( $logs['error'] ) {
+                       throw new Exception( "Could not fetch plugins.svn logs: " . implode( ', ', $logs['error'] ) );
+               }
+
+               // If no changes (either no log entries, or HEAD was the same as $rev)
+               if ( ! count( $logs['log'] ) || ( 1 == count( $logs['log'] ) && $rev == array_keys( $logs['log'] )[0] ) ) {
+                       return array();
+               }
+
+               // Summarize the plugin changes down into something more useful
+               $plugins = array();
+
+               foreach ( $logs['log'] as $log ) {
+                       $plugin_slug = explode( '/', $log['paths'][0] )[1];
+
+                       if ( ! isset( $plugins[ $plugin_slug ] ) ) {
+                               $plugins[ $plugin_slug ] = array(
+                                       'tags_touched' => array(), // trunk is a tag too!
+                                       'readme_touched' => false, // minor optimization, only parse readme i18n on readme-related commits
+                                       'code_touched' => false,
+                                       'assets_touched' => false,
+                                       'revision' => PHP_INT_MAX,
+                               );
+                       }
+                       $plugin =& $plugins[ $plugin_slug ];
+
+                       // Keep track of the lowest revision number we've seen for this plugin
+                       $plugin['revision'] = min( $plugin['revision'], $log['revision'] );
+                       foreach ( $log['paths'] as $path ) {
+                               $path_parts = explode('/', trim( $path, '/' ) );
+
+                               if ( ! isset( $path_parts[1] ) ) {
+                                       continue;
+                               }
+
+                               if ( 'trunk' == $path_parts[1] ) {
+                                       $plugin['tags_touched'][] = 'trunk';
+
+                               } elseif ( 'tags' == $path_parts[1] && isset( $path_parts[2] ) ) {
+                                       $plugin['tags_touched'][] = $path_parts[2];
+
+                               } elseif ( 'assets' == $path_parts[1] ) {
+                                       $plugin['assets_touched'] = true;
+
+                               }
+
+                               // This will have false-positives for when a readme in a subdirectory is hit, but this is only for optimizations.
+                               if ( in_array( strtolower( basename( $path ) ), array( 'readme.txt', 'readme.md' ) ) ) {
+                                       $plugin['readme_touched'] = true;
+                               }
+                               if ( ! $plugin['code_touched'] && ( '/' == substr( $path, -1 ) || '.php' == substr( $path, -4 ) ) ) {
+                                       $plugin['code_touched'] = true;
+                               }
+
+                       }
+                       $plugin['tags_touched'] = array_unique( $plugin['tags_touched'] );
+               }
+
+               // Sort plugins by minimum revision, it should already be in this order, but double check.
+               uasort( $plugins, function( $a, $b ) {
+                       if ( $a['revision'] == $b['revision'] ) {
+                               return 0;
+                       }
+
+                       return ( $a['revision'] < $b['revision'] ) ? -1 : 1;
+               } );
+
+               return $plugins;
+       }
+
+       /**
+        * Determines the HEAD revision of a repository.
+        *
+        * @return int The revision ID of HEAD.
+        */
+       protected function get_head_rev() {
+               $log = SVN::log( self::SVN_URL, 'HEAD' );
+               if ( $log['error'] || ! $log['log'] ) {
+                       throw new Exception( "Unable to determine HEAD revision" );
+               }
+               return array_keys( $log['log'] )[0];
+       }
+
+       /**
+        * An implementation of `get_option()~ which doesn't utilise the cache.
+        * As this is a long-running script, we don't want to hit an alloptions race condition bug.
+        *
+        * @param string $option_name The Option to retrieve.
+        * @return mixed The option value.
+        */
+       protected function get_option( $option_name ) {
+               global $wpdb;
+               return maybe_unserialize( $wpdb->get_var( $wpdb->prepare(
+                       "SELECT option_value FROM $wpdb->options WHERE option_name = %s",
+                       $option_name
+               ) ) );
+       }
+
+       /**
+        * An implementation of `update_option()` which doesn't utilise the cache.
+        * As this is a long-running script, we don't want to hit an alloptions race condition bug.
+        *
+        * @param string $option_name  The Option to update.
+        * @param mixed  $option_value The option value to update to.
+        * @return bool Whether the operation succeeeded.
+        */
+       protected function update_option( $option_name, $option_value ) {
+               global $wpdb;
+               return (bool) $wpdb->query( $wpdb->prepare(
+                       "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s LIMIT 1",
+                       maybe_serialize( $option_value ),
+                       $option_name
+               ) );
+       }
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-svn-watcher.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>