Plugin Directory

Changeset 19556


Ignore:
Timestamp:
09/22/2007 12:27:16 PM (19 years ago)
Author:
arnee
Message:

Changed mysql queries to unbuffered queries
Uses MUCH less memory
Fixed really stupid bug with search engine pings
Option to set how many posts will be included

File:
1 edited

Legend:

Unmodified
Added
Removed
  • google-sitemap-generator/trunk/sitemap.php

    r17826 r19556  
    3333 Plugin URI: http://www.arnebrachhold.de/redir/sitemap-home/
    3434 Description: This plugin will generate a sitemaps.org compatible sitemap of your WordPress blog which is supported by Ask.com, Google, MSN Search and YAHOO. <a href="options-general.php?page=sitemap.php">Configuration Page</a>
    35  Version: 3.0b10
     35 Version: 3.0b11
    3636 Author: Arne Brachhold
    3737 Author URI: http://www.arnebrachhold.de/
    … …  
    159159                        Repaired GET building
    160160                        Added more info on debug mode
     161 2007-09-04     3.0b11  Changed mysql queries to unbuffered queries
     162                        Uses MUCH less memory
     163                        Fixed really stupid bug with search engine pings
     164                        Option to set how many posts will be included
    161165
    162166
    … …  
    10621066         * @var Version of the generator
    10631067        */
    1064         var $_version = "3.0b10";
     1068        var $_version = "3.0b11";
    10651069       
    10661070        /**
    … …  
    11181122         */             
    11191123        var $_isActive = false;
     1124
     1125        /**
     1126         * @var object The file handle which is used to write the sitemap file
     1127         */     
     1128        var $_fileHandle = null;
     1129       
     1130        /**
     1131         * @var object The file handle which is used to write the zipped sitemap file
     1132         */     
     1133        var $_fileZipHandle = null;
    11201134       
    11211135        /**
    … …  
    12441258                $this->_options["sm_b_memory"] = '';                            //Set Memory Limit (e.g. 16M)
    12451259                $this->_options["sm_b_time"] = -1;                                      //Set time limit in seconds, 0 for unlimited, -1 for disabled
     1260                $this->_options["sm_b_max_posts"] = -1;                         //Maximum number of posts, <= 0 for all
    12461261                $this->_options["sm_b_style"] = $this->GetDefaultStyle(); //Include a stylesheet in the XML
    12471262                $this->_options["sm_b_robots"] = false;                         //Modify or create robots.txt file in blog root which contains the sitemap location
    … …  
    14421457         */
    14431458        function IsGzipEnabled() {
    1444                 return ($this->GetOption("b_gzip")===true && function_exists("gzencode"));
     1459                return ($this->GetOption("b_gzip")===true && function_exists("gzwrite"));
    14451460        }
    14461461
    … …  
    18091824                if(empty($page)) return;
    18101825               
    1811                 $this->_content[] = $page;
     1826                $s = $page->Render();
     1827               
     1828                if($this->_fileZipHandle && $this->IsGzipEnabled()) {
     1829                        gzwrite($this->_fileZipHandle,$s);
     1830                }
     1831               
     1832                if($this->_fileHandle && $this->GetOption("b_xml")) {
     1833                        fwrite($this->_fileHandle,$s);
     1834                }
    18121835        }
    18131836       
    … …  
    19061929                $debug=$this->GetOption("b_debug");
    19071930               
     1931               
     1932                if($this->GetOption("b_xml")) {
     1933                        $fileName = $this->GetXmlPath();
     1934                        $status->StartXml($this->GetXmlPath(),$this->GetXmlUrl());
     1935                       
     1936                        if($this->IsFileWritable($fileName)) {
     1937                               
     1938                                $this->_fileHandle = fopen($fileName,"w");
     1939                                if(!$this->_fileHandle) $status->EndXml(false,"Not openable");
     1940                               
     1941                        } else $status->EndXml(false,"not writable");
     1942                }
     1943               
     1944                //Write gzipped sitemap file
     1945                if($this->IsGzipEnabled()) {
     1946                        $fileName = $this->GetZipPath();
     1947                        $status->StartZip($this->GetZipPath(),$this->GetZipUrl());
     1948                       
     1949                        if($this->IsFileWritable($fileName)) {
     1950                               
     1951                                $this->_fileZipHandle = gzopen($fileName,"w1");
     1952                                if(!$this->_fileZipHandle) $status->EndZip(false,"Not openable");
     1953                               
     1954                        } else $status->EndZip(false,"not writable");   
     1955                }       
     1956               
     1957                if(!$this->_fileHandle && !$this->_fileZipHandle) {
     1958                        $status->End();
     1959                        return;
     1960                }       
     1961               
     1962               
    19081963                //Content of the XML file
    19091964                $this->AddElement(new GoogleSitemapGeneratorXmlEntry('<?xml version="1.0" encoding="UTF-8"' . '?' . '>'));
    … …  
    19472002                        $wpCompat = (floatval($wp_version) < 2.1);
    19482003                       
    1949                         $sql="SELECT `ID`, `post_author`, `post_date`, `post_date_gmt`, `post_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `post_type` FROM `" . $wpdb->posts . "` WHERE (";
     2004                        $sql="SELECT `ID`, `post_author`, `post_date`, `post_date_gmt`, `post_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `post_type` FROM `" . $wpdb->posts . "` WHERE ";
     2005                       
     2006                        $where = '(';
    19502007                       
    19512008                        if($this->GetOption('in_posts')) {
    19522009                                //WP < 2.1: posts are post_status = publish
    19532010                                //WP >= 2.1: post_type must be 'post', no date check required because future posts are post_status='future'
    1954                                 if($wpCompat) $sql.="(post_status = 'publish' AND post_date_gmt <= '" . gmdate('Y-m-d H:i:59') . "')";
    1955                                 else $sql.=" (post_status = 'publish' AND (post_type = 'post' OR post_type = '')) ";
     2011                                if($wpCompat) $where.="(post_status = 'publish' AND post_date_gmt <= '" . gmdate('Y-m-d H:i:59') . "')";
     2012                                else $where.=" (post_status = 'publish' AND (post_type = 'post' OR post_type = '')) ";
    19562013                        }
    19572014                       
    19582015                        if($this->GetOption('in_pages')) {
    19592016                                if($this->GetOption('in_posts')) {
    1960                                         $sql.=" OR ";   
     2017                                        $where.=" OR ";
    19612018                                }
    19622019                                if($wpCompat) {
    19632020                                        //WP < 2.1: posts have post_status = published, pages have post_status = static
    1964                                         $sql.=" post_status='static' ";
     2021                                        $where.=" post_status='static' ";
    19652022                                } else {
    19662023                                        //WP >= 2.1: posts have post_type = 'post' and pages have post_type = 'page'. Both must be published.
    1967                                         $sql.=" (post_status = 'publish' AND post_type = 'page') ";
     2024                                        $where.=" (post_status = 'publish' AND post_type = 'page') ";
    19682025                                }
    19692026                        }
    19702027                       
    1971                         $sql.=") ";
    1972                        
    1973                         $sql.=" AND post_password='' ORDER BY post_modified DESC";
     2028                        $where.=") ";
     2029                       
     2030                        $where.=" AND post_password='' ORDER BY post_modified DESC";
     2031                       
     2032                        $sql .= $where;
     2033                       
     2034                        if($this->GetOption("sm_b_max_posts")>0) {
     2035                                $sql.=" LIMIT 0," . $this->GetOption("sm_b_max_posts");                 
     2036                        }       
     2037                       
     2038                        $postCount = intval($wpdb->get_var("SELECT COUNT(*) AS cnt FROM `" . $wpdb->posts . "` WHERE ". $where,0,0));
     2039                                                                               
    19742040                       
    19752041                        //Retrieve all posts and static pages (if enabled)
    1976                         $postRes=$wpdb->get_results($sql);
     2042                       
     2043                        $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,true);
     2044                        mysql_select_db(DB_NAME,$con);
     2045                       
     2046                        $postRes = mysql_unbuffered_query($sql,$con);
    19772047                       
    19782048                        if($postRes) {
    1979                                
    1980                                 //Count of all posts
    1981                                 $postCount=count($postRes);
    19822049                               
    19832050                                //#type $prioProvider GoogleSitemapGeneratorPrioProviderBase
    … …  
    19902057                               
    19912058                                //$posts is used by Alex King's Popularity Contest plugin
    1992                                 if($posts == null || !is_array($posts)) {
    1993                                         $posts = &$postRes;     
    1994                                 }
     2059                                //if($posts == null || !is_array($posts)) {
     2060                                //      $posts = &$postRes;     
     2061                                //}
    19952062                               
    19962063                                $z = 1;
    … …  
    20072074                                $minPrio=$this->GetOption('pr_posts_min');
    20082075                               
    2009                                 //Fill the cache with our DB result. Since it's incomplete (no text-content for example), we will clean it later.
    2010                                 update_post_cache($postRes);
    20112076
    20122077                                //Cycle through all posts and add them
    2013                                 foreach($postRes as $post) {
     2078                                while($post = mysql_fetch_object($postRes)) {
     2079                               
     2080                                        //Fill the cache with our DB result. Since it's incomplete (no text-content for example), we will clean it later.
     2081                                        $cache = array(&$post);
     2082                                        update_post_cache($cache);
    20142083                               
    20152084                                        $permalink = get_permalink($post->ID);
    … …  
    20712140                                unset($prioProvider);
    20722141                               
     2142                                mysql_close($con);
    20732143                        }
    20742144                        if($debug) $this->AddElement(new GoogleSitemapGeneratorDebugEntry("Debug: End Postings"));
    … …  
    22412311                $this->AddElement(new GoogleSitemapGeneratorXmlEntry("</urlset>"));
    22422312               
    2243                 $s='';
    2244                 $c = count($this->_content);
    2245                 for($i =0; $i<$c; $i++) {
    2246                         $s.=$this->_content[$i]->Render() . "\n";       
    2247                 }
    2248                
    2249                 $hasChanged = true;
    2250                
    2251                 $xmlHasChanged = true;
    2252                 $zipHasChanged = true;
    2253                
    2254                 if($this->GetOption("b_xml") && is_readable($this->GetXmlPath())) {
    2255                         $current = file_get_contents($this->GetXmlPath());
    2256                         $current = $this->RemoveSitemapComment($current);
    2257                         $new = $this->RemoveSitemapComment($s);
    2258                         if($new == $current) $xmlHasChanged = false;   
    2259                 }
    2260                
    2261                 if($this->IsGzipEnabled() && is_readable($this->GetZipPath())) {
    2262                         $current = file_get_contents("compress.zlib://" . $this->GetZipPath());
    2263                         $current = $this->RemoveSitemapComment($current);
    2264                         $new = $this->RemoveSitemapComment($s);
    2265                         if($new == $current) $zipHasChanged = false;   
    2266                 }
    2267                
    2268                 $hasChanged = ($zipHasChanged || $xmlHasChanged);
    2269                
    2270                 if(!$hasChanged) {
    2271                         $status->End(false);
    2272                 } else {
    2273                         $pingUrl='';
    2274                                                
    2275                         //Write normal sitemap file
    2276                         if($xmlHasChanged && $this->GetOption("b_xml")) {
    2277                                 $fileName = $this->GetXmlPath();
    2278                                 $status->StartXml($this->GetXmlPath(),$this->GetXmlUrl());
    2279                                 if($this->IsFileWritable($fileName)) {
    2280                                         if(file_put_contents($fileName,$s)) {
    2281                                                 $pingUrl=$this->GetXmlUrl();
    2282                                                 $status->EndXml(true);
    2283                                         }  else {
    2284                                                 $status->EndXml(false,$this->_lastError);       
    2285                                         }                       
    2286                                 } else $status->EndXml(false,"nt writable");
     2313
     2314                $pingUrl='';
     2315               
     2316                if($this->GetOption("b_xml")) {
     2317                        if($this->_fileHandle && fclose($this->_fileHandle)) {
     2318                                $this->_fileHandle = null;
     2319                                $status->EndXml(true);
     2320                                $pingUrl=$this->GetXmlUrl();
     2321                        } else $status->EndXml(false,"Could not close the sitemap file.");
     2322                }
     2323               
     2324                if($this->IsGzipEnabled()) {
     2325                        if($this->_fileZipHandle && fclose($this->_fileZipHandle)) {
     2326                                $this->_fileZipHandle = null;
     2327                                $status->EndZip(true);
     2328                                $pingUrl=$this->GetZipUrl();
     2329                        } else $status->EndZip(false,"Could not close the zipped sitemap file");
     2330                }
     2331               
     2332                //Ping Google
     2333                if($this->GetOption("b_ping") && !empty($pingUrl)) {
     2334                        $status->StartGooglePing();
     2335                        $sPingUrl="http://www.google.com/webmasters/sitemaps/ping?sitemap=" . urlencode($pingUrl);
     2336                        $pingres=wp_remote_fopen($sPingUrl);
     2337                                                                         
     2338                        if($pingres==NULL || $pingres===false) {
     2339                                $status->EndGooglePing(false,$this->_lastError);
     2340                        } else {
     2341                                $status->EndGooglePing(true);
    22872342                        }
    2288                        
    2289                         //Write gzipped sitemap file
    2290                         if($zipHasChanged && $this->IsGzipEnabled()) {
    2291                                 $fileName = $this->GetZipPath();
    2292                                 $status->StartZip($this->GetZipPath(),$this->GetZipUrl());
    2293                                 if($this->IsFileWritable($fileName)) {
    2294                                         //Gzip Level 1 is OK and very fast.
    2295                                         if(file_put_contents($fileName,gzencode($s,1))) {
    2296                                                 $pingUrl=$this->GetZipUrl();
    2297                                                 $status->EndZip(true);
    2298                                         }  else {
    2299                                                 $status->EndZip(false,$this->_lastError);       
    2300                                         }                       
    2301                                 } else $status->EndZip(false,"nt writable");   
     2343                }
     2344                               
     2345                //Ping Ask.com
     2346                if($this->GetOption("b_pingask") && !empty($pingUrl)) {
     2347                        $status->StartAskPing();
     2348                        $sPingUrl="http://submissions.ask.com/ping?sitemap=" . urlencode($pingUrl);
     2349                        $pingres=wp_remote_fopen($sPingUrl);
     2350                                                                         
     2351                        if($pingres==NULL || $pingres===false || strpos($pingres,"successfully received and added")===false) { //Ask.com returns 200 OK even if there was an error, so we need to check the content.
     2352                                $status->EndAskPing(false,$this->_lastError);
     2353                        } else {
     2354                                $status->EndAskPing(true);
    23022355                        }
    2303                        
    2304                         //Ping Google
    2305                         if($this->GetOption("b_ping") && !empty($pingUrl)) {
    2306                                 $status->StartGooglePing();
    2307                                 $pingUrl="http://www.google.com/webmasters/sitemaps/ping?sitemap=" . urlencode($pingUrl);
    2308                                 $pingres=@wp_remote_fopen($pingUrl);
    2309                                                                                  
    2310                                 if($pingres==NULL || $pingres===false) {
    2311                                         $status->EndGooglePing(false,$this->_lastError);
    2312                                 } else {
    2313                                         $status->EndGooglePing(true);
    2314                                 }
    2315                         }
    2316                                        
    2317                         //Ping Ask.com
    2318                         if($this->GetOption("b_pingask") && !empty($pingUrl)) {
    2319                                 $status->StartAskPing();
    2320                                 $pingUrl="http://submissions.ask.com/ping?sitemap=" . urlencode($pingUrl);
    2321                                 $pingres=@wp_remote_fopen($pingUrl);
    2322                                                                                  
    2323                                 if($pingres==NULL || $pingres===false || strpos($pingres,"successfully received and added")===false) { //Ask.com returns 200 OK even if there was an error, so we need to check the content.
    2324                                         $status->EndAskPing(false,$this->_lastError);
    2325                                 } else {
    2326                                         $status->EndAskPing(true);
    2327                                 }
    2328                         }
    2329                        
    2330                         //Ping YAHOO
    2331                         if($this->GetOption("sm_b_pingyahoo")===true && !empty($pingUrl)) {
    2332                                 $status->StartYahooPing();
    2333                                 $pingUrl="http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=" . urlencode($pingUrl);
    2334                                 $pingres=@wp_remote_fopen($pingUrl);
    2335 
    2336                                 if($pingres==NULL || $pingres===false || strpos(strtolower($pingres),"success")===false) {
    2337                                         $status->EndYahooPing(false,$this->_lastError);
    2338                                 } else {
    2339                                         $status->EndYahooPing(true);
    2340                                 }       
     2356                }
     2357               
     2358                //Ping YAHOO
     2359                if($this->GetOption("sm_b_pingyahoo")===true && !empty($pingUrl)) {
     2360                        $status->StartYahooPing();
     2361                        $sPingUrl="http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=" . urlencode($pingUrl);
     2362                        $pingres=wp_remote_fopen($sPingUrl);
     2363
     2364                        if($pingres==NULL || $pingres===false || strpos(strtolower($pingres),"success")===false) {
     2365                                $status->EndYahooPing(false,$this->_lastError);
     2366                        } else {
     2367                                $status->EndYahooPing(true);
    23412368                        }       
    2342                
    2343                         $status->End();
    2344                 }
     2369                }       
     2370       
     2371                $status->End();
     2372
    23452373               
    23462374                $this->_isActive = false;       
    … …  
    25692597                                }
    25702598                               
    2571                                 $oldErr = @error_reporting(E_ALL);
    2572                                 $oldIni = @ini_set("display_errors",1);
     2599                                $oldErr = error_reporting(E_ALL);
     2600                                $oldIni = ini_set("display_errors",1);
    25732601
    25742602                                echo '<div class="wrap">';
    … …  
    25792607                                echo '<p>WordPress ' . $GLOBALS['wp_version'] . ' with ' . ' DB ' . $GLOBALS['wp_db_version'] . ' on PHP ' . phpversion() . '</p>';
    25802608                                echo '<p>Plugin version: ' . $this->_version . ' (' . $this->_svnVersion . ')';
    2581                                 echo '<h4>Extensions</h4>';
    2582                                 echo "<pre>";
    2583                                 print_r(get_loaded_extensions());
    2584                                 echo "</pre>";
    25852609                                echo '<h4>Environment</h4>';
    25862610                                echo "<pre>";
    … …  
    26082632                                $status = $this->BuildSitemap();
    26092633                                echo '</div>';
     2634                                echo '<h3>MySQL Queries</h3>';
     2635                                if(defined('SAVEQUERIES') && SAVEQUERIES) {
     2636                                        echo '<pre>';
     2637                                        var_dump($GLOBALS['wpdb']->queries);
     2638                                        echo '</pre>';
     2639                                       
     2640                                        $total = 0;
     2641                                        foreach($GLOBALS['wpdb']->queries as $q) {
     2642                                                $total+=$q[1]; 
     2643                                        }
     2644                                        echo '<h4>Total Query Time</h4>';
     2645                                        echo '<pre>' . count($GLOBALS['wpdb']->queries) . ' queries in ' . round($total,2) . ' seconds.</pre>';
     2646                                } else {
     2647                                        echo '<p>Please edit wp-db.inc.php in wp-includes and set SAVEQUERIES to true if you want to see the queries.</p>';     
     2648                                }
    26102649                                echo "<h3>Build Process Results</h3>";
    26112650                                echo "<pre>";
    … …  
    26442683                                                if($tmp=="auto" || $tmp="manual") $this->_options[$k]=$tmp;
    26452684                                                else $this->_options[$k]="auto";                                                               
    2646                                         } else if($k == "sm_b_time") {
     2685                                        } else if($k == "sm_b_time" || $k=="sm_b_max_posts") {
    26472686                                                if($_POST[$k]=='') $_POST[$k] = -1;
    26482687                                                $this->_options[$k] = intval($_POST[$k]);                       
    … …  
    30483087                                                                                <b><?php _e('Advanced options:','sitemap'); ?></b> <a href="<?php echo $this->GetRedirectLink('sitemap-help-options-adv'); ?>"><?php _e('Learn more','sitemap'); ?></a>
    30493088                                                                                <ul>
     3089                                                                                        <li>
     3090                                                                                                <label for="sm_b_max_posts"><?php _e('Limit the number of posts in the sitemap:', 'sitemap') ?> <input type="text" name="sm_b_max_posts" id="sm_b_max_posts" style="width:40px;" value="<?php echo ($this->GetOption("sm_b_max_posts")<=0?"":$this->GetOption("sm_b_max_posts")); ?>" /></label> (<?php echo __('Newer posts will be included first', 'sitemap'); ?>)
     3091                                                                                        </li>
    30503092                                                                                        <li>
    30513093                                                                                                <label for="sm_b_memory"><?php _e('Try to increase the memory limit to:', 'sitemap') ?> <input type="text" name="sm_b_memory" id="sm_b_memory" style="width:40px;" value="<?php echo $this->GetOption("sm_b_memory"); ?>" /></label> (<?php echo htmlspecialchars(__('e.g. "4M", "16M"', 'sitemap')); ?>)
    … …  
    35383580
    35393581
     3582
     3583
    35403584//Check if ABSPATH and WPINC is defined, this is done in wp-settings.php
    35413585//If not defined, we can't guarante that all required functions are available.
Note: See TracChangeset for help on using the changeset viewer.