<!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>[3507] sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory: Plugin Directory: Initial commit for a code POT generator/importer.</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/3507">3507</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/3507","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-20 16:07:11 +0000 (Mon, 20 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: Initial commit for a code POT generator/importer.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinimportplugintoglotpressphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/import-plugin-to-glotpress.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclii18nclassreadmeimportphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/i18n/class-readme-import.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclii18nclasscodeimportphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/i18n/class-code-import.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclii18nclassi18nimportphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/i18n/class-i18n-import.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectorybinimportplugintoglotpressphp"></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-to-glotpress.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-to-glotpress.php      2016-06-20 14:41:39 UTC (rev 3506)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/import-plugin-to-glotpress.php        2016-06-20 16:07:11 UTC (rev 3507)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,7 +49,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $type        = $opts['type'];
</span><span class="cx" style="display: block; padding: 0 10px"> $start_time  = microtime( 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-echo "Processing I18N Import for $plugin_slug... ";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+echo "Processing I18N Import for $plugin_slug...\n";
</ins><span class="cx" style="display: block; padding: 0 10px"> try {
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 'readme' === $type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $importer = new CLI\I18N\Readme_Import( $plugin_slug );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclii18nclasscodeimportphp"></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/i18n/class-code-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/i18n/class-code-import.php                          (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/i18n/class-code-import.php    2016-06-20 16:07:11 UTC (rev 3507)
</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\CLI\I18N;
+
+use WordPressdotorg\Plugin_Directory\Tools\SVN;
+use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
+use Exception;
+
+/**
+ * Class to handle plugin code imports GlotPress.
+ *
+ * @package WordPressdotorg\Plugin_Directory\CLI\I18N
+ */
+class Code_Import extends I18n_Import {
+       const PLUGIN_SVN_BASE = 'https://plugins.svn.wordpress.org';
+
+       /**
+        * Slug of the plugin.
+        *
+        * @var string
+        */
+       private $plugin;
+
+       /**
+        * Constructor.
+        *
+        * @param string $plugin The plugin slug.
+        */
+       public function __construct( $plugin ) {
+               $this->plugin = $plugin;
+       }
+
+       /**
+        * Imports the readme of a specific tag to GlotPress.
+        *
+        * @param string $tag SVN tag of the readme.
+        *
+        * @throws Exception
+        */
+       public function import_from_tag( $tag ) {
+               $plugin_url = self::PLUGIN_SVN_BASE . "/{$this->plugin}/{$tag}";
+
+               $files = SVN::ls( $plugin_url );
+               if ( ! $files ) {
+                       throw new Exception( "Plugin has no files in {$tag}." );
+               }
+
+               $tmp_directory = Filesystem::temp_directory( $this->plugin . '-code-' . $tag );
+               $export_directory = $tmp_directory . '/export';
+
+               $res = SVN::export( $plugin_url, $export_directory, [ 'ignore-externals' ] );
+               if ( ! $res['result'] ) {
+                       throw new Exception( 'Plugin export failed.' );
+               }
+
+               $esc_export_directory = escapeshellarg( $export_directory );
+
+               // Check for a plugin text domain declaration and loading, grep recursively, not necessarily in plugin.php.
+               $has_textdomain_header = shell_exec( 'grep -r -i --include "*.php" "Text Domain:" ' . $esc_export_directory );
+               $has_load_plugin_textdomain = shell_exec( 'grep -r --include "*.php" "\bload_plugin_textdomain\b" ' . $esc_export_directory );
+
+               $has_slug_as_textdomain_header = false;
+               if ( $has_textdomain_header ) {
+                       $has_slug_as_textdomain_header = shell_exec( 'grep -r -i --include "*.php" "Text Domain:[[:blank:]]*' . trim( escapeshellarg( $this->plugin ), '\'' ) . '" ' . $esc_export_directory );
+                       if ( ! $has_slug_as_textdomain_header ) {
+                               echo 'Wrong text domain in header';
+                       }
+               } else {
+                       echo 'Missing text domain in header';
+               }
+
+               if ( ! $has_load_plugin_textdomain ) {
+                       echo 'Missing load_plugin_textdomain()';
+               }
+
+               if ( ! $has_textdomain_header || ! $has_slug_as_textdomain_header || ! $has_load_plugin_textdomain ) {
+                       throw new Exception( 'Plugin is not compatible with language packs.' );
+               }
+
+               if ( ! class_exists( '\PotExtMeta' ) ) {
+                       require_once plugin_dir_path( \WordPressdotorg\Plugin_Directory\PLUGIN_FILE ) . 'libs/i18n-tools/makepot.php';
+               }
+
+               $pot_file = "{$tmp_directory}/{$this->plugin}-code.pot";
+               $makepot  = new \MakePOT;
+
+               if ( ! $makepot->wp_plugin( $export_directory, $pot_file, $this->plugin ) || ! file_exists( $pot_file ) ) {
+                       throw new Exception( "POT file couldn't be created." );
+               }
+
+               parent::set_glotpress_for_plugin( $this->plugin, 'code' );
+
+               $branch = ( 'trunk' === $tag ) ? 'dev' : 'stable';
+               parent::import_pot_to_glotpress_project( $this->plugin, $branch, $pot_file );
+
+               // @todo: Import translations only once.
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclii18nclassi18nimportphp"></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/i18n/class-i18n-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/i18n/class-i18n-import.php                          (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/i18n/class-i18n-import.php    2016-06-20 16:07:11 UTC (rev 3507)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,104 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Plugin_Directory\CLI\I18N;
+use WordPressdotorg\Plugin_Directory\Plugin_I18n;
+use WP_Error;
+
+
+/**
+ * Class to handle plugin readme imports GlotPress.
+ *
+ * @package WordPressdotorg\Plugin_Directory\CLI\I18N
+ */
+abstract class I18n_Import {
+
+       /**
+        * Sets the required GlotPress environment for a plugin
+        * See translate/bin/projects/set-wp-plugin-project.php
+        *
+        * @param string $plugin_slug
+        * @param string $process_type code|readme
+        * @return string|WP_Error Status of the project setup:
+        *  - 'created' if project was initially created
+        *  - 'updated' if an existing project was updated
+        *  - WP_Error on failures.
+        */
+       public function set_glotpress_for_plugin( $plugin_slug, $process_type ) {
+               if ( empty( $plugin_slug ) ) {
+                       return;
+               }
+
+               if ( 'code' !== $process_type ) {
+                       $process_type = 'readme';
+               }
+
+               $cmd = WPORGTRANSLATE_WPCLI . ' wporg-translate set-plugin-project ' . escapeshellarg( $plugin_slug ) . ' ' . escapeshellarg( $process_type );
+
+               $last_line = system( $cmd, $return_code );
+
+               if ( 0 === $return_code ) {
+                       // Get the first word of the last line and return it as the status.
+                       $return = strtok( $last_line, ' ' );
+                       $return = strtolower( $return );
+                       if ( ! in_array( $return, array( 'created', 'updated' ) ) ) {
+                               $return = new WP_Error( 'undefined', 'An undefined error occurred while setting the GlotPress projects.' );
+                       }
+               } else {
+                       switch ( $return_code ) {
+                               case 1:
+                                       $return = new WP_Error( 'wrong-usage', 'The script for setting GlotPress projects was called incorrectly.' );
+                                       break;
+                               case 2:
+                                       $return = new WP_Error( 'master-project', 'The master project for the GlotPress projects couldn\'t be found.' );
+                                       break;
+                               case 3:
+                                       $return = new WP_Error( 'api-failure', 'The plugin API couldn\'t be reached while setting GlotPress projects.' );
+                                       break;
+                               case 4:
+                                       $return = new WP_Error( 'no-readme', 'The readme of the plugin couldn\'t be found while setting GlotPress projects.' );
+                                       break;
+                               case 5:
+                                       $return = new WP_Error( 'create-failure', 'An undefined error occurred while creating the main GlotPress project.' );
+                                       break;
+                               default :
+                                       $return = new WP_Error( 'undefined', 'An undefined error occurred while setting the GlotPress projects.' );
+                       }
+               }
+
+               return $return;
+       }
+
+       /**
+        * Import generated POT file to GlotPress
+        *
+        * @param string $project GP project slug to import to
+        * @param string $branch GP project branch to import to (dev|stable)
+        * @param string $file Path to POT file
+        * @param array $str_priorities GP string priorities
+        */
+       function import_pot_to_glotpress_project( $project, $branch, $file, $str_priorities = array() ) {
+               global $wpdb;
+
+               // Note: this will only work if the GlotPress project/sub-projects exist.
+               $cmd = WPORGTRANSLATE_WPCLI . ' glotpress import-originals ' . escapeshellarg( "wp-plugins/{$project}/{$branch}" ) . ' ' . escapeshellarg( $file );
+               echo shell_exec( $cmd ) . "\n";
+
+               if ( empty( $str_priorities ) ) {
+                       return;
+               }
+
+               // @todo: Fix this.
+               $gp_branch_id = Plugin_I18n::instance()->get_gp_branch_id( $project, "{$branch}-readme" );
+               if ( $gp_branch_id ) {
+                       foreach ( $str_priorities as $str => $prio ) {
+                               if ( 1 !== $prio && -1 !== $prio ) {
+                                       $prio = 0;
+                               }
+
+                               $wpdb->query( $wpdb->prepare(
+                                       'UPDATE ' . GLOTPRESS_TABLE_PREFIX . 'originals SET priority = %d WHERE project_id = %d AND status = %s AND singular = %s',
+                                       $prio, $gp_branch_id, '+active', $str
+                               ) );
+                       }
+               }
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclii18nclassreadmeimportphp"></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/i18n/class-readme-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/i18n/class-readme-import.php        2016-06-20 14:41:39 UTC (rev 3506)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/i18n/class-readme-import.php  2016-06-20 16:07:11 UTC (rev 3507)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,7 +1,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> namespace WordPressdotorg\Plugin_Directory\CLI\I18N;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-use WordPressdotorg\Plugin_Directory\Plugin_I18n;
</del><span class="cx" style="display: block; padding: 0 10px"> use WordPressdotorg\Plugin_Directory\Tools\SVN;
</span><span class="cx" style="display: block; padding: 0 10px"> use WordPressdotorg\Plugin_Directory\Readme\Parser;
</span><span class="cx" style="display: block; padding: 0 10px"> use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,7 +11,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @package WordPressdotorg\Plugin_Directory\CLI\I18N
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class Readme_Import {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Readme_Import extends I18n_Import {
</ins><span class="cx" style="display: block; padding: 0 10px">         const PLUGIN_SVN_BASE = 'https://plugins.svn.wordpress.org';
</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">@@ -151,28 +150,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        throw new Exception( 'POT file not extracted.' );
</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">-                // @todo: Create/update GP projects.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         parent::set_glotpress_for_plugin( $this->plugin, 'readme' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $branch = ( 'trunk' === $tag ) ? 'dev' : 'stable';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $cmd = WPORGTRANSLATE_WPCLI . ' glotpress import-originals ' . escapeshellarg( "wp-plugins/{$this->plugin}/{$branch}-readme" ) . ' ' . escapeshellarg( $pot_file );
-
-               echo "\n\$$cmd\n";
-               echo shell_exec( $cmd ) . "\n";
-
-               // @todo: Fix this.
-               $gp_branch_id = Plugin_I18n::instance()->get_gp_branch_id( $this->plugin, "{$branch}-readme" );
-               if ( $gp_branch_id ) {
-                       foreach ( $str_priorities as $str => $prio ) {
-                               if ( 1 !== $prio && -1 !== $prio ) {
-                                       $prio = 0;
-                               }
-
-                               $wpdb->query( $wpdb->prepare(
-                                       'UPDATE ' . GLOTPRESS_TABLE_PREFIX . 'originals SET priority = %d WHERE project_id = %d AND status = %s AND singular = %s',
-                                       $prio, $gp_branch_id, '+active', $str
-                               ) );
-                       }
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         parent::import_pot_to_glotpress_project( $this->plugin, $branch, $pot_file, $str_priorities );
</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>