Making WordPress.org


Ignore:
Timestamp:
10/04/2018 09:11:05 PM (8 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Add WP-CLI command for parsing WP source into the Code Reference.

Ex: wp devhub parse /path/to/wp/code/to/parse 3606

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/cli-commands.php

    r7716 r7717  
    44 */
    55class DevHub_Command extends WP_CLI_Command {
     6
     7        /**
     8         * Parses WP code.
     9         *
     10         * The source code for the version of WordPress to be parsed needs to be
     11         * obtained and unpackaged locally. You should not be code used in an
     12         * active install.
     13         *
     14         * ## OPTIONS
     15         *
     16         * <path>
     17         * : The path to a copy of WordPress to be parsed. Should not be code used in
     18         * an active install.
     19         *
     20         * <user-id>
     21         * : ID of user to attribute all parsed posts to.
     22         *
     23         * ## EXAMPLES
     24         *
     25         *     # Parse WP 4.9.5 into DevHub.
     26         *     $ wp devhub /home/wporgdev/developer/wp495 3606
     27         *
     28         * @when after_wp_load
     29         */
     30        public function parse( $args, $assoc_args ) {
     31                list( $path, $user_id ) = $args;
     32
     33                if ( ! is_numeric( $user_id ) ) {
     34                        WP_CLI::error( 'Invalid user_id provided.' );
     35                } else {
     36                        $user_id = (int) $user_id;
     37                }
     38                WP_CLI::log( "Importing as user ID $user_id" );
     39
     40                $plugins = [
     41                        'phpdoc-parser'  => 'phpdoc-parser/plugin.php',
     42                        'posts-to-posts' => 'posts-to-posts/posts-to-posts.php',
     43                ];
     44
     45                // Verify path is a file or directory.
     46                if ( ! file_exists( $path ) ) {
     47                        WP_CLI::error( 'Path for WordPress source to parse does not exist.' );
     48                }
     49
     50                // Verify path is not a file.
     51                if ( is_file( $path ) ) {
     52                        WP_CLI::error( 'Path provided for WordPress source to parse does not appear to be a directory.' );
     53                }
     54
     55                // Verify path looks like WP.
     56                if ( ! file_exists ( $path . '/wp-includes/version.php' ) ) {
     57                        WP_CLI::error( 'Path provided for WordPress source to parse does not contain WordPress files.' );
     58                }
     59
     60                // Get WP version of files to be parsed.
     61                $version_file = file_get_contents( $path . '/wp-includes/version.php' );
     62                preg_match( '/\$wp_version = \'([^\']+)\'/', $version_file, $matches );
     63                $version = $matches[1];
     64
     65                // Get WP version last parsed (if any) and confirm if reparsing that version.
     66                $last_parsed_wp_ver = get_option( 'wp_parser_imported_wp_version' );
     67                if ( $last_parsed_wp_ver && $last_parsed_wp_ver == $version ) {
     68                        $last_parsed_date = get_option( 'wp_parser_last_import' );
     69                        WP_CLI::confirm( "Looks like WP $version was already parsed on " . date_i18n( 'Y-m-d H:i', $last_parsed_date ) . ". Proceed anyway?" );
     70                }
     71
     72                // Verify that the PHP-Parser plugin is available locally.
     73                $all_plugins = get_plugins();
     74                if ( ! in_array( $plugins['phpdoc-parser'], array_keys( $all_plugins ) ) ) {
     75                        // TODO: Attempt to install the plugin automatically.
     76                        WP_CLI::error( 'The PHP-Parser plugin (from https://github.com/WordPress/phpdoc-parser) is not installed locally in ' . WP_PLUGIN_DIR . '/.' );
     77                }
     78
     79                // Confirm the parsing.
     80                WP_CLI::confirm( "Are you sure you want to parse the source code for WP {$version} (and that you've run a backup of the existing data)?" );
     81
     82                // 1. Deactivate posts-to-posts plugin.
     83                if ( is_plugin_active( $plugins['posts-to-posts'] ) ) {
     84                        WP_CLI::log( 'Deactivating posts-to-posts plugin...' );
     85                        WP_CLI::runcommand( 'plugin deactivate ' . $plugins['posts-to-posts'] );
     86                } else {
     87                        WP_CLI::log( 'Warning: plugin posts-to-posts already deactivated.' );
     88                }
     89
     90                // 2. Activate phpdoc-parser plugin.
     91                if ( is_plugin_active( $plugins['phpdoc-parser'] ) ) {
     92                        WP_CLI::log( 'Warning: plugin phpdoc-parser already activated.' );
     93                } else {
     94                        WP_CLI::log( 'Activating phpdoc-parser plugin...' );
     95                        WP_CLI::runcommand( 'plugin activate ' . $plugins['phpdoc-parser'] );
     96                }
     97
     98                // 3. Run the parser.
     99                WP_CLI::log( 'Running the parser (this will take awhile)...' );
     100                WP_CLI::runcommand( "parser create {$path} --user={$user_id}" );
     101
     102                // 4. Deactivate phpdoc-parser plugin.
     103                WP_CLI::log( 'Deactivating phpdoc-parser plugin...' );
     104                WP_CLI::runcommand( 'plugin deactivate ' . $plugins['phpdoc-parser'] );
     105
     106                // 5. Activate posts-to-posts plugin.
     107                WP_CLI::log( 'Activating posts-to-posts plugin...' );
     108                WP_CLI::runcommand( 'plugin activate ' . $plugins['posts-to-posts'] );
     109
     110                // 6. Pre-cache source code.
     111                WP_CLI::runcommand( 'devhub pre-cache-source' );
     112
     113                // Done.
     114                WP_CLI::success( "Parsing of WP $version is complete." );
     115        }
    6116
    7117        /**
Note: See TracChangeset for help on using the changeset viewer.