gb_maint::sync_site_state PHP Method

sync_site_state() static public method

static public sync_site_state ( )
    static function sync_site_state()
    {
        ignore_user_abort(true);
        # verify repo setup, which also makes sure the repo setup (hooks, config,
        # etc) is up to date:
        self::repair_repo_setup();
        # assure gitblog submodule is set up
        $dotgitmodules = gb::$site_dir . '/.gitmodules';
        if (!is_file($dotgitmodules) || !preg_match('/\\[submodule[\\s\\t ]+"gitblog"\\]/', file_get_contents($dotgitmodules))) {
            self::add_gitblog_submodule();
        }
        # read id of HEAD and current branch
        $gb_branch = 'master';
        $gb_head = '0000000000000000000000000000000000000000';
        try {
            $branches = trim(git::exec('branch --no-abbrev --verbose --no-color', null, gb::$dir . '/.git', gb::$dir));
            foreach (explode("\n", $branches) as $line) {
                if (!$line) {
                    continue;
                }
                if ($line[0] === '*') {
                    if (strpos($line, '(no branch)') !== false) {
                        $line = preg_split('/[ \\t]+/', $line, 5);
                        $gb_branch = null;
                        $gb_head_id = $line[3];
                    } else {
                        $line = preg_split('/[ \\t]+/', $line, 4);
                        $gb_branch = $line[1];
                        $gb_head_id = $line[2];
                    }
                    break;
                }
            }
        } catch (GitError $e) {
            gb::log(LOG_WARNING, 'failed to read branch info for gitblog -- git: %s', $e->getMessage());
        }
        # no previous state?
        if (!gb::$site_state) {
            gb::$site_state = array();
        }
        # Set current values
        gb::$site_state['url'] = gb::$site_url;
        gb::$site_state['version'] = gb::$version;
        gb::$site_state['posts_pagesize'] = gb::$posts_pagesize;
        # appeard in 0.1.3:
        gb::$site_state['gitblog'] = array('branch' => $gb_branch, 'head' => $gb_head_id);
        # Write site url for hooks
        $bytes_written = file_put_contents(gb::$site_dir . '/.git/info/gitblog-site-url', gb::$site_url, LOCK_EX);
        # Encode site.json
        $json = json::pretty(gb::$site_state) . "\n";
        $path = gb::$site_dir . '/data/site.json';
        # create data/ ?
        if (!is_dir(gb::$site_dir . '/data')) {
            mkdir(gb::$site_dir . '/data', 0775);
            chmod(gb::$site_dir . '/data', 0775);
        }
        # Write site.json
        $bytes_written += file_put_contents($path, $json, LOCK_EX);
        chmod($path, 0664);
        gb::log(LOG_NOTICE, 'wrote site state to %s (%d bytes)', $path, $bytes_written);
        return $bytes_written;
    }

Usage Example

コード例 #1
0
ファイル: gitblog.php プロジェクト: rsms/gitblog
 /**
  * Verify integrity of the site, automatically taking any actions to restore
  * it if broken.
  * 
  * Return values:
  *   0  Nothing done (everything is probably OK).
  *   -1 Error (the error has been logged through trigger_error).
  *   1  gitblog cache was updated.
  *   2  gitdir is missing and need to be created (git init).
  *   3  upgrade performed
  */
 static function verify_integrity()
 {
     $r = 0;
     if (!is_dir(gb::$site_dir . '/.git/info/gitblog')) {
         if (!is_dir(gb::$site_dir . '/.git')) {
             # 2: no repo/not initialized
             return 2;
         }
         # 1: gitblog cache updated
         gb_maint::sync_site_state();
         GBRebuilder::rebuild(true);
         return 1;
     }
     # load site.json
     $r = self::load_site_state();
     # check site state
     if ($r === false || !isset(gb::$site_state['url']) || !gb::$site_state['url'] || gb::$site_state['url'] !== gb::$site_url && strpos(gb::$site_url, '://localhost') === false && strpos(gb::$site_url, '://127.0.0.1') === false) {
         return gb_maint::sync_site_state() === false ? -1 : 0;
     } elseif (gb::$site_state['version'] !== gb::$version) {
         return gb_maint::upgrade(gb::$site_state['version']) ? 0 : -1;
     } elseif (gb::$site_state['posts_pagesize'] !== gb::$posts_pagesize) {
         gb_maint::sync_site_state();
         GBRebuilder::rebuild(true);
         return 1;
     }
     return 0;
 }
All Usage Examples Of gb_maint::sync_site_state