OSDN Git Service

fake $current_site and $current_blog globals
authorscribu <mail@scribu.net>
Wed, 17 Jul 2013 00:19:22 +0000 (03:19 +0300)
committerscribu <mail@scribu.net>
Wed, 17 Jul 2013 00:29:32 +0000 (03:29 +0300)
php/WP_CLI/Runner.php
php/utils.php

index 85a0563..917f843 100644 (file)
@@ -97,7 +97,7 @@ class Runner {
                }
        }
 
-       private static function set_url( $assoc_args ) {
+       private static function guess_url( $assoc_args ) {
                if ( isset( $assoc_args['blog'] ) ) {
                        $assoc_args['url'] = $assoc_args['blog'];
                        unset( $assoc_args['blog'] );
@@ -136,8 +136,10 @@ class Runner {
                }
 
                if ( isset( $url ) ) {
-                       Utils\set_url_params( $url );
+                       return $url;
                }
+
+               return false;
        }
 
        private function cmd_starts_with( $prefix ) {
@@ -336,7 +338,11 @@ class Runner {
                self::set_wp_root( $this->config );
 
                // Handle --url and --blog parameters
-               self::set_url( $this->config );
+               $url = self::guess_url( $this->config );
+               if ( $url ) {
+                       $url_parts = self::parse_url( $url );
+                       Utils\set_url_params( $url_parts );
+               }
 
                $this->do_early_invoke( 'before_wp_load' );
 
@@ -366,8 +372,15 @@ class Runner {
                ) {
                        define( 'WP_INSTALLING', true );
 
+                       // We really need a URL here
                        if ( !isset( $_SERVER['HTTP_HOST'] ) ) {
-                               Utils\set_url_params( 'http://example.com' );
+                               $url_parts = self::parse_url( 'http://example.com' );
+                               Utils\set_url_params( $url_parts );
+                       }
+
+                       if ( 'multisite-install' == $this->arguments[1] ) {
+                               // need to fake some globals to skip the checks in wp-inclues/ms-settings.php
+                               self::fake_current_site_blog( $url_parts );
                        }
                }
 
@@ -375,7 +388,46 @@ class Runner {
                        define( 'WP_LOAD_IMPORTERS', true );
                        define( 'WP_IMPORTING', true );
                }
+       }
+
+       private static function parse_url( $url ) {
+               $url_parts = parse_url( $url );
+
+               if ( !isset( $url_parts['scheme'] ) ) {
+                       $url_parts = parse_url( 'http://' . $url );
+               }
 
+               return $url_parts;
+       }
+
+       private static function fake_current_site_blog( $url_parts ) {
+               global $current_site, $current_blog;
+
+               if ( !isset( $url_parts['path'] ) ) {
+                       $url_parts['path'] = '/';
+               }
+
+               $current_site = (object) array(
+                       'id' => 1,
+                       'blog_id' => 1,
+                       'domain' => $url_parts['host'],
+                       'path' => $url_parts['path'],
+                       'cookie_domain' => $url_parts['host'],
+                       'site_name' => 'Fake Site',
+               );
+
+               $current_blog = (object) array(
+                       'blog_id' => 1,
+                       'site_id' => 1,
+                       'domain' => $url_parts['host'],
+                       'path' => $url_parts['path'],
+                       'public' => '1',
+                       'archived' => '0',
+                       'mature' => '0',
+                       'spam' => '0',
+                       'deleted' => '0',
+                       'lang_id' => '0',
+               );
        }
 
        public function after_wp_load() {
index 8b8c463..cb21c7a 100644 (file)
@@ -140,15 +140,9 @@ function esc_cmd( $cmd ) {
 /**
  * Sets the appropriate $_SERVER keys based on a given string
  *
- * @param string $url The URL
+ * @param array $url_parts The URL, as represented by parse_url()
  */
-function set_url_params( $url ) {
-       $url_parts = parse_url( $url );
-
-       if ( !isset( $url_parts['scheme'] ) ) {
-               $url_parts = parse_url( 'http://' . $url );
-       }
-
+function set_url_params( $url_parts ) {
        $f = function( $key ) use ( $url_parts ) {
                return isset( $url_parts[ $key ] ) ? $url_parts[ $key ] : '';
        };
@@ -255,7 +249,7 @@ function recursive_unserialize_replace( $from = '', $to = '', $data = '', $seria
  * @param array|string  $fields     Named fields for each item of data. Can be array or comma-separated list
  */
 function format_items( $format, $items, $fields ) {
-       
+
        if ( 'ids' == $format ) {
                echo implode( ' ', $items );
                return;