Making WordPress.org

Ticket #1365: 1365.diff

File 1365.diff, 9.8 KB (added by obenland, 9 years ago)

First pass. Needs optimization.

  • translate/gp-plugins/wporg-projects-wp-plugins-overview.php

     
     1<?php
     2
     3class GP_WPorg_Route_WP_Plugins extends GP_Route {
     4
     5        public function get_plugin_projects( $project_slug ) {
     6                global $gpdb;
     7
     8                $project_path = 'wp-plugins/' . $project_slug;
     9                $project = GP::$project->by_path( $project_path );
     10                if ( ! $project ) {
     11                        return $this->die_with_404();
     12                }
     13
     14                $rows = $gpdb->get_results( "
     15                        SELECT
     16                                path, locale, locale_slug,
     17                                (100 * stats.current/stats.all) as percent_complete,
     18                                stats.waiting+stats.fuzzy as waiting_strings
     19                        FROM {$gpdb->prefix}project_translation_status stats
     20                                LEFT JOIN {$gpdb->prefix}projects p ON stats.project_id = p.id
     21                        WHERE
     22                                p.path = '$project_path'
     23                " );
     24
     25                // Split out into $[Locale][Project] = %
     26                $translation_locale_statuses = array();
     27                foreach ( $rows as $set ) {
     28
     29                        // Find unique locale key.
     30                        $locale_key = $set->locale;
     31                        if ( 'default' != $set->locale_slug ) {
     32                                $locale_key = $set->locale . '/' . $set->locale_slug;
     33                        }
     34
     35                        foreach ( $this->get_active_sub_projects( $project ) as $sub_project_id => $sub_project ) {
     36                                $translation_set = GP::$translation_set->by_project_id_slug_and_locale(
     37                                        $sub_project_id,
     38                                        $set->locale_slug,
     39                                        $locale_key
     40                                );
     41
     42                                if ( $translation_set ) {
     43                                        /*
     44                                         * > 50% round down, so that a project with all strings except 1 translated shows 99%, instead of 100%.
     45                                         * < 50% round up, so that a project with just a few strings shows 1%, instead of 0%.
     46                                         */
     47                                        $percent_complete = (int) $translation_set->current_count() / (int) $translation_set->all_count() * 100;
     48                                        $translation_locale_statuses[ $locale_key ][ $sub_project->slug ] = ( $percent_complete > 50 ) ? floor( $percent_complete ) : ceil( $percent_complete );
     49
     50                                        // Increment the amount of waiting strings.
     51                                        if ( ! isset( $translation_locale_statuses[ $locale_key ]['waiting'] ) ) {
     52                                                $translation_locale_statuses[ $locale_key ]['waiting'] = 0;
     53                                        }
     54                                        $translation_locale_statuses[ $locale_key ]['waiting'] += (int) $translation_set->all_count() - (int) $translation_set->current_count();
     55                                }
     56                        }
     57                        ksort( $translation_locale_statuses[ $locale_key ], SORT_NATURAL );
     58                }
     59                unset( $project_path, $locale_key, $set, $sub_project_id, $sub_project, $translation_set );
     60
     61                // Calculate a list of [Locale] = % subtotals
     62                $translation_locale_complete = array();
     63                foreach ( $rows as $set ) {
     64                        $translation_locale_complete[ $set->locale ] = (float) $set->percent_complete;
     65                }
     66                unset( $rows, $set );
     67
     68                // Sort by Percent Complete, secondly by Slug
     69                uksort( $translation_locale_complete, function ( $a, $b ) use ( $translation_locale_complete ) {
     70                        if ( $translation_locale_complete[ $a ] < $translation_locale_complete[ $b ] ) {
     71                                return 1;
     72                        } elseif ( $translation_locale_complete[ $a ] == $translation_locale_complete[ $b ] ) {
     73                                return strnatcmp( $a, $b );
     74                        } else {
     75                                return -1;
     76                        }
     77                } );
     78
     79                $project->icon = wporg_get_plugin_icon( $project->slug, 64 );
     80
     81                $this->tmpl( 'project-wp-plugin', get_defined_vars() );
     82        }
     83
     84        /**
     85         * Retrieves active sub projects.
     86         *
     87         * @param  GP_Project $project           The parent project
     88         * @param  bool       $with_sub_projects Whether sub projects should be fetched too.
     89         *                                       Default false.
     90         * @return array List of sub projects.
     91         */
     92        private function get_active_sub_projects( $project, $with_sub_projects = false ) {
     93                global $gpdb;
     94
     95                $_projects = $project->many( "
     96                        SELECT *
     97                        FROM {$gpdb->projects}
     98                        WHERE
     99                                parent_project_id = %d AND
     100                                active = 1
     101                        ORDER BY id ASC
     102                ", $project->id );
     103
     104                $projects = array();
     105                foreach ( $_projects as $project ) {
     106                        $projects[ $project->id ] = $project;
     107
     108                        if ( $with_sub_projects ) {
     109                                // e.g. wp/dev/admin/network
     110                                $sub_projects = $project->many( "
     111                                        SELECT *
     112                                        FROM {$gpdb->projects}
     113                                        WHERE
     114                                                parent_project_id = %d AND
     115                                                active = 1
     116                                        ORDER BY id ASC
     117                                ", $project->id );
     118
     119                                foreach ( $sub_projects as $sub_project ) {
     120                                        $projects[ $sub_project->id ] = $sub_project;
     121                                }
     122                                unset( $sub_projects);
     123                        }
     124                }
     125                unset( $_projects );
     126
     127                return $projects;
     128        }
     129}
  • translate/gp-plugins/wporg-routes/wporg-routes.php

    Property changes on: translate/gp-plugins/wporg-projects-wp-plugins-overview.php
    ___________________________________________________________________
    Added: svn:eol-style
    ## -0,0 +1 ##
    +native
    \ No newline at end of property
     
    4141                GP::$router->prepend( "/locale/$locale/$path", array( 'GP_WPorg_Route_Locale', 'get_locale_projects' ) );
    4242                GP::$router->prepend( "/locale/$locale/$path/$path", array( 'GP_WPorg_Route_Locale', 'get_locale_projects' ) );
    4343                GP::$router->prepend( "/locale/$locale/$path/$path/$path", array( 'GP_WPorg_Route_Locale', 'get_locale_project' ) );
     44
     45                $project = '(.+?)';
     46                GP::$router->prepend( "/projects/wp-plugins/$project", array( 'GP_WPorg_Route_WP_Plugins', 'get_plugin_projects' ) );
    4447        }
    4548}
    4649
  • translate/gp-templates/projects-wp-plugins.php

     
     1<?php
     2
     3$edit_link = gp_link_project_edit_get( $project, __('(edit)') );
     4
     5gp_title( sprintf( __( '%s &lt; GlotPress' ), esc_html( $project->name ) ) );
     6gp_breadcrumb_project( $project );
     7
     8wp_enqueue_script( 'common' );
     9wp_enqueue_script( 'tablesorter' );
     10
     11gp_tmpl_header();
     12?>
     13
     14<div class="project-header">
     15        <p class="project-description"><?php echo apply_filters( 'project_description', $project->description, $project ); ?></p>
     16
     17        <div class="project-box">
     18                <div class="project-box-header">
     19                        <div class="project-icon">
     20                                <?php echo $project->icon; ?>
     21                        </div>
     22
     23                        <ul class="project-meta">
     24                                <li class="project-name"><?php echo $project->name; ?> <?php echo $edit_link; ?></li>
     25                        </ul>
     26                </div>
     27        </div>
     28</div>
     29
     30<div class="stats-table">
     31        <table id="stats-table" class="table">
     32                <thead>
     33                        <tr>
     34                                <th class="title"><?php _e( 'Locale' ); ?></th>
     35                                <th class="title"><?php _e( 'Development' ); ?></th>
     36                                <th class="title"><?php _e( 'Development Readme' ); ?></th>
     37                                <th class="title"><?php _e( 'Stable' ); ?></th>
     38                                <th class="title"><?php _e( 'Stable Readme' ); ?></th>
     39                                <th class="title"><?php _e( 'Waiting' ); ?></th>
     40                        </tr>
     41                </thead>
     42                <tbody>
     43                        <?php
     44                        foreach ( $translation_locale_complete as $locale_slug => $completeness ) :
     45                                $gp_locale = GP_Locales::by_slug( $locale_slug );
     46                                $set_slug = 'default';
     47                                // Variants (de/formal for example) don't have GP_Locales in this context
     48                                if ( ! $gp_locale && ( list( $base_locale_slug, $set_slug ) = explode( '/', $locale_slug ) ) ) {
     49                                        $gp_locale = clone GP_Locales::by_slug( $base_locale_slug );
     50                                        // Just append it for now..
     51                                        $gp_locale->wp_locale .= '/' . $set_slug;
     52                                }
     53                                if ( ! $gp_locale || ! $gp_locale->wp_locale ) {
     54                                        continue;
     55                                }
     56                        ?>
     57                                <tr>
     58                                        <th title="<?php echo esc_attr( $gp_locale->english_name ); ?>">
     59                                                <a href="<?php echo gp_url_join( 'locale', $gp_locale->slug, $set_slug ); ?>">
     60                                                        <?php echo esc_html( $gp_locale->english_name ); ?>
     61                                                </a>
     62                                        </th>
     63                                        <?php
     64
     65                                        if ( $translation_locale_statuses[ $locale_slug ] ) {
     66                                                foreach ( $translation_locale_statuses[ $locale_slug ] as $slug => $percent ) {
     67                                                        $projecturl = gp_url_join( 'locale', $gp_locale->slug, $set_slug, $project->path );
     68
     69                                                        if ( 'waiting' === $slug ) {
     70                                                                // Color code it on -0~500 waiting strings
     71                                                                $percent_class = 100-min( (int) ( $percent / 50 ) * 10, 100 );
     72                                                                // It's only 100 if it has 0 strings.
     73                                                                if ( 100 == $percent_class && $percent ) {
     74                                                                        $percent_class = 90;
     75                                                                }
     76                                                                $percent_class = 'percent' . $percent_class;
     77                                                                echo '<td data-sort-value="'. esc_attr( $percent ) . '" class="' . $percent_class .'"><a href="' . $projecturl . '">' . number_format( $percent ) . '</a></td>';
     78                                                        } else {
     79                                                                $percent_class = 'percent' . (int) ( $percent / 10 ) * 10;
     80                                                                echo '<td data-sort-value="' . esc_attr( $percent ) . '" class="' . $percent_class .'">';
     81                                                                gp_link( gp_url_project( $project->path, gp_url_join( $locale_slug, $set_slug ), array( 'filters[translated]' => 'yes', 'filters[status]' => 'current') ), "$percent%" );
     82                                                                echo '</td>';
     83                                                        }
     84                                                }
     85                                        } else {
     86                                                echo '<td class="none" data-sort-value="-1">&mdash;</td>';
     87                                                echo '<td class="none" data-sort-value="-1">&mdash;</td>';
     88                                                echo '<td class="none" data-sort-value="-1">&mdash;</td>';
     89                                                echo '<td class="none" data-sort-value="-1">&mdash;</td>';
     90                                                echo '<td class="none" data-sort-value="-1">&mdash;</td>';
     91                                        }
     92                                        ?>
     93                                </tr>
     94                        <?php endforeach; ?>
     95                </tbody>
     96        </table>
     97</div>
     98
     99<script type="text/javascript">
     100jQuery( function( $ ) {
     101        $( '#stats-table' ).tablesorter( {
     102                textExtraction: function( node ) {
     103                        var cellValue = $( node ).text(),
     104                                sortValue = $( node ).data( 'sortValue' );
     105
     106                        return ( undefined !== sortValue ) ? sortValue : cellValue;
     107                }
     108        });
     109});
     110</script>
     111
     112<?php gp_tmpl_footer();