wp/wp-includes/SimplePie/Cache/MySQL.php
changeset 16 a86126ab1dd4
parent 0 d970ebf37754
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     3  * SimplePie
     3  * SimplePie
     4  *
     4  *
     5  * A PHP-Based RSS and Atom Feed Framework.
     5  * A PHP-Based RSS and Atom Feed Framework.
     6  * Takes the hard work out of managing a complete RSS/Atom solution.
     6  * Takes the hard work out of managing a complete RSS/Atom solution.
     7  *
     7  *
     8  * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
     8  * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
     9  * All rights reserved.
     9  * All rights reserved.
    10  *
    10  *
    11  * Redistribution and use in source and binary forms, with or without modification, are
    11  * Redistribution and use in source and binary forms, with or without modification, are
    12  * permitted provided that the following conditions are met:
    12  * permitted provided that the following conditions are met:
    13  *
    13  *
    31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    33  * POSSIBILITY OF SUCH DAMAGE.
    33  * POSSIBILITY OF SUCH DAMAGE.
    34  *
    34  *
    35  * @package SimplePie
    35  * @package SimplePie
    36  * @version 1.3.1
    36  * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
    37  * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
       
    38  * @author Ryan Parman
    37  * @author Ryan Parman
    39  * @author Geoffrey Sneddon
    38  * @author Sam Sneddon
    40  * @author Ryan McCue
    39  * @author Ryan McCue
    41  * @link http://simplepie.org/ SimplePie
    40  * @link http://simplepie.org/ SimplePie
    42  * @license http://www.opensource.org/licenses/bsd-license.php BSD License
    41  * @license http://www.opensource.org/licenses/bsd-license.php BSD License
    43  */
    42  */
    44 
    43 
    92 			'host' => '127.0.0.1',
    91 			'host' => '127.0.0.1',
    93 			'port' => '3306',
    92 			'port' => '3306',
    94 			'path' => '',
    93 			'path' => '',
    95 			'extras' => array(
    94 			'extras' => array(
    96 				'prefix' => '',
    95 				'prefix' => '',
       
    96 				'cache_purge_time' => 2592000
    97 			),
    97 			),
    98 		);
    98 		);
    99 		$this->options = array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));
    99 
       
   100 		$this->options = SimplePie_Misc::array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));
   100 
   101 
   101 		// Path is prefixed with a "/"
   102 		// Path is prefixed with a "/"
   102 		$this->options['dbname'] = substr($this->options['path'], 1);
   103 		$this->options['dbname'] = substr($this->options['path'], 1);
   103 
   104 
   104 		try
   105 		try
   128 		if (!in_array($this->options['extras']['prefix'] . 'cache_data', $db))
   129 		if (!in_array($this->options['extras']['prefix'] . 'cache_data', $db))
   129 		{
   130 		{
   130 			$query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))');
   131 			$query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))');
   131 			if ($query === false)
   132 			if ($query === false)
   132 			{
   133 			{
       
   134 				trigger_error("Can't create " . $this->options['extras']['prefix'] . "cache_data table, check permissions", E_USER_WARNING);
   133 				$this->mysql = null;
   135 				$this->mysql = null;
       
   136 				return;
   134 			}
   137 			}
   135 		}
   138 		}
   136 
   139 
   137 		if (!in_array($this->options['extras']['prefix'] . 'items', $db))
   140 		if (!in_array($this->options['extras']['prefix'] . 'items', $db))
   138 		{
   141 		{
   139 			$query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` TEXT CHARACTER SET utf8 NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))');
   142 			$query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` MEDIUMBLOB NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))');
   140 			if ($query === false)
   143 			if ($query === false)
   141 			{
   144 			{
       
   145 				trigger_error("Can't create " . $this->options['extras']['prefix'] . "items table, check permissions", E_USER_WARNING);
   142 				$this->mysql = null;
   146 				$this->mysql = null;
       
   147 				return;
   143 			}
   148 			}
   144 		}
   149 		}
   145 	}
   150 	}
   146 
   151 
   147 	/**
   152 	/**
   151 	 * @return bool Successfulness
   156 	 * @return bool Successfulness
   152 	 */
   157 	 */
   153 	public function save($data)
   158 	public function save($data)
   154 	{
   159 	{
   155 		if ($this->mysql === null)
   160 		if ($this->mysql === null)
       
   161 		{
       
   162 			return false;
       
   163 		}
       
   164 
       
   165 		$query = $this->mysql->prepare('DELETE i, cd FROM `' . $this->options['extras']['prefix'] . 'cache_data` cd, ' .
       
   166 			'`' . $this->options['extras']['prefix'] . 'items` i ' .
       
   167 			'WHERE cd.id = i.feed_id ' .
       
   168 			'AND cd.mtime < (unix_timestamp() - :purge_time)');
       
   169 		$query->bindValue(':purge_time', $this->options['extras']['cache_purge_time']);
       
   170 
       
   171 		if (!$query->execute())
   156 		{
   172 		{
   157 			return false;
   173 			return false;
   158 		}
   174 		}
   159 
   175 
   160 		if ($data instanceof SimplePie)
   176 		if ($data instanceof SimplePie)
   377 		$query->bindValue(':id', $this->id);
   393 		$query->bindValue(':id', $this->id);
   378 		if ($query->execute() && ($time = $query->fetchColumn()))
   394 		if ($query->execute() && ($time = $query->fetchColumn()))
   379 		{
   395 		{
   380 			return $time;
   396 			return $time;
   381 		}
   397 		}
   382 		else
   398 
   383 		{
   399 		return false;
   384 			return false;
       
   385 		}
       
   386 	}
   400 	}
   387 
   401 
   388 	/**
   402 	/**
   389 	 * Set the last modified time to the current time
   403 	 * Set the last modified time to the current time
   390 	 *
   404 	 *
   398 		}
   412 		}
   399 
   413 
   400 		$query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id');
   414 		$query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id');
   401 		$query->bindValue(':time', time());
   415 		$query->bindValue(':time', time());
   402 		$query->bindValue(':id', $this->id);
   416 		$query->bindValue(':id', $this->id);
   403 		if ($query->execute() && $query->rowCount() > 0)
   417 
   404 		{
   418 		return $query->execute() && $query->rowCount() > 0;
   405 			return true;
       
   406 		}
       
   407 		else
       
   408 		{
       
   409 			return false;
       
   410 		}
       
   411 	}
   419 	}
   412 
   420 
   413 	/**
   421 	/**
   414 	 * Remove the cache
   422 	 * Remove the cache
   415 	 *
   423 	 *
   424 
   432 
   425 		$query = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
   433 		$query = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
   426 		$query->bindValue(':id', $this->id);
   434 		$query->bindValue(':id', $this->id);
   427 		$query2 = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :id');
   435 		$query2 = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :id');
   428 		$query2->bindValue(':id', $this->id);
   436 		$query2->bindValue(':id', $this->id);
   429 		if ($query->execute() && $query2->execute())
   437 
   430 		{
   438 		return $query->execute() && $query2->execute();
   431 			return true;
       
   432 		}
       
   433 		else
       
   434 		{
       
   435 			return false;
       
   436 		}
       
   437 	}
   439 	}
   438 }
   440 }