<!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>[11232] sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory: Theme Directory: Add a cron task to monitor themes.svn.wordpress.org for new commits.</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 { white-space: pre-line; 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/11232">11232</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/11232","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>2021-09-14 07:19:00 +0000 (Tue, 14 Sep 2021)</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'>Theme Directory: Add a cron task to monitor themes.svn.wordpress.org for new commits.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryjobsclassmanagerphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-manager.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectorylibclassexecwithloggingphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/lib/class-exec-with-logging.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectorythemedirectoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/theme-directory.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryjobsclasssvnimportphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-svn-import.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryjobsclassmanagerphp"></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/theme-directory/jobs/class-manager.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/theme-directory/jobs/class-manager.php   2021-09-14 06:39:47 UTC (rev 11231)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-manager.php     2021-09-14 07:19:00 UTC (rev 11232)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,6 +27,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // A cronjob to check cronjobs.
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'theme_directory_check_cronjobs', [ $this, 'register_cron_tasks' ] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Import from SVN tasks.
+               add_action( 'theme_directory_svn_import_watcher', [ __NAMESPACE__ . '\SVN_Import', 'watcher_trigger' ] );
+               add_action( 'theme_directory_svn_import', [ __NAMESPACE__ . '\SVN_Import', 'import_trigger' ] );
</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">@@ -43,6 +47,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'display'  => 'Every 15 minutes',
</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">+                $schedules['every_5m']  = [
+                       'interval' => 5 * MINUTE_IN_SECONDS,
+                       'display'  => 'Every 5 minutes',
+               ];
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $schedules;
</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">@@ -59,5 +68,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! wp_next_scheduled( 'theme_directory_check_cronjobs' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_schedule_event( time() + 60, 'every_15m', 'theme_directory_check_cronjobs' );
</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 ( ! wp_next_scheduled( 'theme_directory_svn_import_watcher' ) ) {
+                       wp_schedule_event( time() + 60, 'every_5m', 'theme_directory_svn_import_watcher' );
+               }
</ins><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_htmlwpcontentpluginsthemedirectoryjobsclasssvnimportphp"></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/theme-directory/jobs/class-svn-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/theme-directory/jobs/class-svn-import.php                                (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-svn-import.php  2021-09-14 07:19:00 UTC (rev 11232)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,128 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Watch SVN for new revisions and import those into the directory.
+ *
+ * @package WordPressdotorg\Theme_Directory\Jobs
+ */
+
+namespace WordPressdotorg\Theme_Directory\Jobs;
+use WordPressdotorg\Theme_Directory\Lib\Exec_With_Logging;
+use Exception;
+use SimpleXMLElement;
+use WPORG_Themes_Upload;
+
+/**
+ * Class SVN_Import
+ *
+ * @package WordPressdotorg\Theme_Directory\Jobs
+ */
+class SVN_Import {
+       use Exec_With_Logging;
+
+       /**
+        * Check for new SVN revisions on the target repo, and queue an import job for each matching.
+        */
+       public static function watcher_trigger() {
+               $last_revision    = (int) get_option( 'svn_import_last_revision', 0 ) - 4;
+               $current_revision = (int) trim( self::exec(
+                       'svn info --show-item=revision https://themes.svn.wordpress.org/'
+               ) );
+
+               if ( ! $last_revision ) {
+                       trigger_error( "Theme Importing aborting, no starting revision known. Set 'svn_import_last_revision' option. Latest Revision: [{$latest_revision}]", E_USER_WARNING );
+                       return;
+               }
+
+               // Don't need to parse anything if the revisions match.
+               if ( $last_revision >= $current_revision ) {
+                       return;
+               }
+
+               // We don't need to include the details of the last revision next time we check for changes.
+               $last_revision++;
+
+               // Get the changes since then..
+               $svn_output = self::shell_exec( sprintf(
+                       'svn log https://themes.svn.wordpress.org/ -v --xml -r %d:%d',
+                       $last_revision,
+                       $current_revision
+               ) );
+
+               $xml = simplexml_load_string( $svn_output );
+               if ( ! $xml ) {
+                       return false;
+               }
+
+               $theme_changes = array_filter( array_map( function( $element ) { 
+
+                       // Get slug/version.
+                       $slug = '';
+                       $version = '';
+                       foreach ( $element->xpath( 'paths/path[@kind="dir"]') as $path ) {
+                               if ( preg_match( '!^/(?P<slug>[^/]+)/(?P<version>[^/]+)(/.+)?$!', (string) $path, $m ) ) {
+                                       $slug    = $m['slug'];
+                                       $version = $m['version'];
+                                       break;
+                               }
+                       }
+                       if ( ! $slug || ! $version ) {
+                               return false;
+                       }
+
+                       $changeset = (int) $element->xpath( '@revision' )[0];
+                       $author    = (string) $element->xpath( 'author' )[0];
+                       $msg       = (string) $element->xpath( 'msg' )[0];
+
+                       $info = compact( 'slug', 'version', 'changeset', 'author', 'msg' );
+
+                       // Allow for including/skipping revisions based on external conditionals.
+                       $should_import = 'themedropbox' !== $author;
+                       $should_import = apply_filters( 'themes_svn_should_import', $should_import, $info );
+
+                       // DEBUG visible in cron log.
+                       echo "[{$changeset}] $slug: $version by $author " . ( $should_import ? 'Importing from SVN.' : 'Skipping.' ) . "\n";
+
+                       if ( ! $should_import ) {
+                               return false;
+                       }
+
+                       return $info;
+               }, $xml->xpath('/log/logentry') ) );
+
+               $theme_changes = array_unique( $theme_changes, SORT_REGULAR );
+
+               foreach ( $theme_changes as $change ) {
+                       wp_schedule_single_event( time(), 'theme_directory_svn_import', array( $change ) );
+               }
+
+               update_option( 'svn_import_last_revision', $current_revision );
+       }
+
+       /**
+        * Import a Theme from SVN into the directory on a cron task.
+        */
+       public static function import_trigger( $args ) {
+               include_once dirname( __DIR__ ) . '/class-wporg-themes-upload.php';
+
+               if ( empty( $args['slug'] ) || empty( $args['version'] ) ) {
+                       trigger_error( 'Theme Import aborted, invalid input provided: ' . json_Encode( $args ), E_USER_WARNING );
+                       return;
+               }
+
+               $uploader = new WPORG_Themes_Upload;
+
+               $return = $uploader->process_update_from_svn(
+                       $args['slug'],
+                       $args['version'],
+                       $args['changeset'] ?? false,
+                       $args['author'] ?? false
+               );
+
+               // TODO: Look at error result code, maybq re-queue in event of system issue, else email author with concerns (Theme Check, etc)
+               //       CC emails to themes team in event of having to contact the author?
+
+               if ( is_wp_error( $return ) ) {
+                       throw new Exception( "Theme Import Failure: " . $return->get_error_code . ' ' . $return->get_error_message() );
+               }
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-svn-import.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_htmlwpcontentpluginsthemedirectorylibclassexecwithloggingphp"></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/theme-directory/lib/class-exec-with-logging.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/theme-directory/lib/class-exec-with-logging.php  2021-09-14 06:39:47 UTC (rev 11231)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/lib/class-exec-with-logging.php    2021-09-14 07:19:00 UTC (rev 11232)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2,9 +2,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> namespace WordPressdotorg\Theme_Directory\Lib;
</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">- * Simple `exec()` wrapper which adds error reporting to WordPress.org.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Simple `exec()` wrappers which add error reporting to WordPress.org.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @package WordPressdotorg\Theme_Directory
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @package WordPressdotorg\Theme_Directory\Lib
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> trait Exec_With_Logging {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,7 +17,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * 
</span><span class="cx" style="display: block; padding: 0 10px">         * @return false|string False on failure, last line of output on success, as per exec().
</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 function exec( $command, &$output = null, &$return_var = null ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function exec( $command, &$output = null, &$return_var = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $proc = proc_open(
</span><span class="cx" style="display: block; padding: 0 10px">                        $command,
</span><span class="cx" style="display: block; padding: 0 10px">                        [
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,6 +27,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $pipes
</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 ( ! $proc ) {
+                       return false;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $stdout = stream_get_contents( $pipes[1] );
</span><span class="cx" style="display: block; padding: 0 10px">                $stderr = stream_get_contents( $pipes[2] );
</span><span class="cx" style="display: block; padding: 0 10px">                fclose( $pipes[1] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -76,4 +80,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $stdout ? end( $output ) : '';
</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">-}
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Execute a shell process, same behaviour as `shell_exec()` but with PHP Warnings/Notices generated on errors.
+        * 
+        * @param string $command Command to execute. Escape it.
+        * 
+        * @return false|string False on failure, output on success, mostly per shell_exec().
+        */
+       public static function shell_exec( $command ) {
+               $output     = [];
+               $return_var = 0;
+               $return = self::exec( $command, $output, $return_var );
+
+               // Diverge from shell_exec(): return false for any error.
+               if ( $return_var > 0 || false === $return || ! $output ) {
+                       return false;
+               }
+
+               return implode( "\n", $output );
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectorythemedirectoryphp"></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/theme-directory/theme-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/theme-directory/theme-directory.php      2021-09-14 06:39:47 UTC (rev 11231)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/theme-directory.php        2021-09-14 07:19:00 UTC (rev 11232)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -26,15 +26,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Load the query modifications needed for the directory.
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/query-modifications.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Load the GitHub API client.
+include __DIR__ . '/lib/class-github.php';
+include __DIR__ . '/lib/class-exec-with-logging.php';
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Load repo jobs.
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/jobs/class-manager.php';
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/jobs/class-trac-sync.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+include __DIR__ . '/jobs/class-svn-import.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> new WordPressdotorg\Theme_Directory\Jobs\Manager();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-// Load the GitHub API client.
-include __DIR__ . '/lib/class-github.php';
-include __DIR__ . '/lib/class-exec-with-logging.php';
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Load the Rest API Endpoints.
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/rest-api.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>