module-wpreference.php in wpshell – WordPress Code Trac

1<?php23class wpreference {45        var $fileindexes = array();6        7        function d( $n ) {8                $indexes = $this->bigfindex();9                if ( !ctype_digit( $n ) )10                        $results = (array)preg_grep( '/^'.$n.'\|/i', $indexes['functions'] );11                else12                        $results = array( $n => $indexes['functions'][$n] );13                $found=0;14                foreach ( array_keys($results) as $i ) {15                        $found++;16                        if ( count($results) > 1 )17                                echo "// Search result #$found\n";18                        $f = explode( '|', $indexes['functions'][$i] );19                        echo rtrim( implode( '', array_slice( file( $f[1] ), $f[2]-1, 1 ) ) );20                        if ( $found < count($results) )21                                echo "\n\n";22                }2324        }2526        function f( $n ) {27                if ( ctype_digit( $n ) )28                        return $this->fprint( $n );29                $indexes = $this->bigfindex();30                $results = (array)preg_grep( '/^'.$n.'\|/i', $indexes['functions'] );31                $found=0;32                foreach ( array_keys($results) as $i ) {33                        $found++;34                        if ( count($results) > 1 )35                                echo "// Search result #$found\n";36                        $this->fprint($i);37                        if ( $found < count($results) )38                                echo "\n\n";39                }40        }4142        function fprint( $n ) {43                $indexes = $this->bigfindex();44                echo "// {$indexes['functions'][$n]}\n";45                $f = explode( '|', $indexes['functions'][$n] );46                echo rtrim( implode( '', array_slice( file( $f[1] ), $f[2]-1, ($f[3]-$f[2])+2 ) ) );47        }4849        function fsearch( $func ) {50                $indexes = $this->bigfindex();51                $results = preg_grep( '/^[^|]*'.$func.'[^|]*\|/i', $indexes['functions'] );52                $results = str_replace( getcwd().'/', '', $results );53                $results = preg_grep( '/\|\//', $results, PREG_GREP_INVERT );54                print_r( tabularize( $results, '|', array(STR_PAD_RIGHT, STR_PAD_RIGHT, STR_PAD_LEFT) ) );55        }5657        function cprint( $n ) {58                $indexes = $this->bigfindex();59                $c = explode( '|', $indexes['classes'][$n] );60                echo trim( implode( '', array_slice( file( $c[1] ), $c[2]-1, ($c[3]-$c[2])+2 ) ) );61        }6263        function csearch( $class ) {64                $indexes = $this->bigfindex();65                $results = preg_grep( '/^[^|]*'.$class.'[^|]*\|/i', $indexes['classes'] );66                $results = str_replace( getcwd().'/', '', $results );67                $results = preg_grep( '/\|\//', $results, PREG_GREP_INVERT );68                print_r( tabularize( $results, '|', array(STR_PAD_RIGHT, STR_PAD_RIGHT, STR_PAD_LEFT) ) );69        }70        71        function c( $n ) {72                if ( ctype_digit( $n ) )73                        return $this->fprint( $n );74                $indexes = $this->bigfindex();75                $results = (array)preg_grep( '/^'.$n.'\|/i', $indexes['classes'] );76                $found=0;77                foreach ( array_keys($results) as $i ) {78                        $found++;79                        if ( count($results) > 1 )80                                echo "// Search result #$found\n";81                        $this->cprint($i);82                        if ( $found < count($results) )83                                echo "\n\n";84                }85        }8687        function bigfindex() {88                if ( count( $this->file_indexes ) )89                        return $this->file_indexes;90                foreach ( get_included_files() as $file ) {91                        $this->findex( $file );92                }93                return $this->file_indexes;94        }9596        function findex( $file ) {97                $tokens = token_get_all( file_get_contents( $file ) );98                99                $next_is_func = false;100                $next_is_class = false;101102                $classes     = array();103                $class_stack = array();104                $current_class = '';105106                $functions   = array();107                $func_stack  = array();108                $current_function = '';109110                $curly_level = 0;111112                foreach ( $tokens as $token ) { 113                        if ( ( $next_is_func || $next_is_class ) && is_array( $token ) && token_name( $token[0] ) == 'T_WHITESPACE' )114                                continue;115                        if ( is_array( $token ) )116                                $current_line = $token[2];117                        else if ( $token == '&' )118                                continue;119                        if ( $next_is_func ) {120                                $next_is_func = false;121                                $current_function = $token[1];122                                if ( $current_class )123                                        $current_function = "$current_class::$current_function";124                                $func_stack[] = $current_function;125                                if ( !isset( $functions[$current_function] ) )126                                        $functions[$current_function] = array( 'start' => $current_line, 'stop' => null, 'depth' => 0 );127                        } else if ( $next_is_class ) {128                                $next_is_class = false;129                                $current_class = $token[1];130                                $class_stack[] = $current_class;131                                if ( !isset( $classes[$current_class] ) )132                                        $classes[$current_class] = array( 'start' => $current_line, 'stop' => null, 'depth' => 0 );133                        }134                        if ( is_array( $token ) && $token[1] == '}' ) $token = '}';135                        if ( is_array( $token ) && $token[1] == '{' ) $token = '{';136                        if ( !is_array($token) ) {137                                if ( $token == '{' ) {138                                        if ( $current_class )139                                                $classes[$current_class]['depth']++;140                                        if ( $current_function )141                                                $functions[$current_function]['depth']++;142                                        $curly_level++;143                                }144                                if ( $token == '}' ) {145                                        if ( $current_class ) {146                                                $classes[$current_class]['depth']--;147                                                $classes[$current_class]['stop'] = $current_line;148                                                if ( $classes[$current_class]['depth'] == 0 ) {149                                                        unset( $classes[$current_class]['depth'] );150                                                        array_pop( $class_stack );151                                                        if ( count( $class_stack ) )152                                                                $current_class = $class_stack[ (count($class_stack)-1) ];153                                                        else154                                                                $current_class = '';155                                                }156                                        }157                                        if ( $current_function ) {158                                                $functions[$current_function]['depth']--;159                                                $functions[$current_function]['stop'] = $current_line;160                                                if ( $functions[$current_function]['depth'] < 1 ) {161                                                        unset( $functions[$current_function]['depth'] ); 162                                                        array_pop( $func_stack );163                                                        if ( count( $func_stack ) )164                                                                $current_function = $func_stack[ (count($func_stack)-1) ];165                                                        else166                                                                $current_function = '';167                                                }168                                        }169                                        $curly_level--;170                                }171                                continue;172                        }173                        $tname = token_name( $token[0] );174                        switch( $tname ) {175                                case 'T_CLASS':176                                        $next_is_class = true;177                                        break;178                                case 'T_FUNCTION':179                                        $next_is_func = true;180                                        break;181                        }182                }183184                if ( !isset( $this->file_indexes['classes'] ) )185                        $this->file_indexes['classes'] = array();186                foreach ( $classes as $class => $info ) {187                        $new = "$class|$file|{$info['start']}|{$info['stop']}";188                        if ( !in_array( $new, $this->file_indexes['classes'] ) )189                                $this->file_indexes['classes'][] = $new;190                }191192                if ( !isset( $this->file_indexes['functions'] ) )193                        $this->file_indexes['functions'] = array();194                foreach ( $functions as $func => $info ) {195                        $new = "$func|$file|{$info['start']}|{$info['stop']}";196                        if ( !in_array( $new, $this->file_indexes['functions'] ) )197                                $this->file_indexes['functions'][] = $new;198                }199200                sort( $this->file_indexes['functions'] );201                sort( $this->file_indexes['classes'] );202203                return $this->file_indexes;             204        }205206}207208$modules[] = new wpreference();