Skip to content

Commit

Permalink
Merge branch 'MOODLE_24_STABLE' into install_24_STABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
AMOS bot committed Aug 31, 2013
2 parents afd76d0 + 6cef890 commit 85ed2ec
Show file tree
Hide file tree
Showing 28 changed files with 496 additions and 207 deletions.
10 changes: 8 additions & 2 deletions backup/util/loggers/database_logger.class.php
Expand Up @@ -59,7 +59,13 @@ protected function action($message, $level, $options = null) {
if ($this->levelcol) {
$columns[$this->levelcol] = $level;
}
$columns[$this->messagecol] = $message; //TODO: should this be cleaned?
$message = clean_param($message, PARAM_NOTAGS);
// Check if the message exceeds the 255 character limit in the database,
// if it does, shorten it so that it can be inserted successfully.
if (textlib::strlen($message) > 255) {
$message = textlib::substr($message, 0, 252) . '...';
}
$columns[$this->messagecol] = $message;
return $this->insert_log_record($this->logtable, $columns);
}

Expand All @@ -69,6 +75,6 @@ protected function insert_log_record($table, $columns) {
// to preserve DB logs if the whole backup/restore transaction is
// rollback
global $DB;
return $DB->insert_record($this->logtable, $columns, false); // Don't return inserted id
return $DB->insert_record($table, $columns, false); // Don't return inserted id
}
}
2 changes: 1 addition & 1 deletion backup/util/structure/tests/baseoptiogroup_test.php
Expand Up @@ -69,7 +69,7 @@ function test_creation() {
/**
* Incorrect creation tests (attributes and final elements)
*/
function itest_wrong_creation() {
function test_wrong_creation() {

// Create instance with invalid name
try {
Expand Down
2 changes: 1 addition & 1 deletion backup/util/ui/renderer.php
Expand Up @@ -140,7 +140,7 @@ public function backup_details($details, $nextstageurl) {
}
if (empty($table)) {
$table = new html_table();
$table->head = array('Module', 'Title', 'Userinfo');
$table->head = array(get_string('module','backup'), get_string('title','backup'), get_string('userinfo','backup'));
$table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
$table->align = array('left','left', 'center');
$table->attributes = array('class'=>'activitytable generaltable');
Expand Down
333 changes: 189 additions & 144 deletions cache/classes/loaders.php

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions cache/stores/file/lib.php
Expand Up @@ -352,23 +352,19 @@ public function get($key) {
if (!$readfile) {
return false;
}
// Check the filesize first, likely not needed but important none the less.
$filesize = filesize($file);
if (!$filesize) {
return false;
}
// Open ensuring the file for writing, truncating it and setting the pointer to the start.
// Open ensuring the file for reading in binary format.
if (!$handle = fopen($file, 'rb')) {
return false;
}
// Lock it up!
// We don't care if this succeeds or not, on some systems it will, on some it won't, meah either way.
flock($handle, LOCK_SH);
// HACK ALERT
// There is a problem when reading from the file during PHPUNIT tests. For one reason or another the filesize is not correct
// Doesn't happen during normal operation, just during unit tests.
// Read it.
$data = fread($handle, $filesize+128);
$data = '';
// Read the data in 1Mb chunks. Small caches will not loop more than once. We don't use filesize as it may
// be cached with a different value than what we need to read from the file.
do {
$data .= fread($handle, 1048576);
} while (!feof($handle));
// Unlock it.
flock($handle, LOCK_UN);
// Return it unserialised.
Expand Down
2 changes: 1 addition & 1 deletion cache/stores/memcache/lib.php
Expand Up @@ -132,8 +132,8 @@ public function __construct($name, array $configuration = array()) {
$this->connection = new Memcache;
foreach ($this->servers as $server) {
$this->connection->addServer($server[0], $server[1], true, $server[2]);
// Test the connection to this server.
}
// Test the connection to the pool of servers.
$this->isready = @$this->connection->set($this->parse_key('ping'), 'ping', MEMCACHE_COMPRESSED, 1);
}

Expand Down
1 change: 1 addition & 0 deletions cache/stores/memcached/lib.php
Expand Up @@ -141,6 +141,7 @@ public function __construct($name, array $configuration = array()) {
}
$this->connection->addServers($this->servers);
}
// Test the connection to the pool of servers.
$this->isready = @$this->connection->set("ping", 'ping', 1);
}

Expand Down
68 changes: 63 additions & 5 deletions cache/stores/session/lib.php
Expand Up @@ -190,8 +190,9 @@ public static function is_supported_mode($mode) {
*/
public function initialise(cache_definition $definition) {
$this->storeid = $definition->generate_definition_hash();
$this->store = &self::register_store_id($definition->get_id());
$this->store = &self::register_store_id($this->name.'-'.$definition->get_id());
$this->ttl = $definition->get_ttl();
$this->check_ttl();
}

/**
Expand Down Expand Up @@ -219,10 +220,13 @@ public function is_ready() {
*/
public function get($key) {
if (isset($this->store[$key])) {
if ($this->ttl == 0) {
if ($this->ttl === 0) {
return $this->store[$key][0];
} else if ($this->store[$key][1] >= (cache::now() - $this->ttl)) {
return $this->store[$key][0];
} else {
// Element is present but has expired.
$this->check_ttl();
}
}
return false;
Expand All @@ -239,20 +243,28 @@ public function get($key) {
*/
public function get_many($keys) {
$return = array();
$maxtime = 0;
if ($this->ttl != 0) {
$maxtime = cache::now() - $this->ttl;
}

$hasexpiredelements = false;
foreach ($keys as $key) {
$return[$key] = false;
if (isset($this->store[$key])) {
if ($this->ttl == 0) {
$return[$key] = $this->store[$key][0];
} else if ($this->store[$key][1] >= $maxtime) {
$return[$key] = $this->store[$key][0];
} else {
$hasexpiredelements = true;
}
}
}
if ($hasexpiredelements) {
// There are some elements that are present but have expired.
$this->check_ttl();
}
return $return;
}

Expand All @@ -264,8 +276,8 @@ public function get_many($keys) {
* @return bool True if the operation was a success false otherwise.
*/
public function set($key, $data) {
if ($this->ttl == 0) {
$this->store[$key][0] = $data;
if ($this->ttl === 0) {
$this->store[$key] = array($data, 0);
} else {
$this->store[$key] = array($data, cache::now());
}
Expand All @@ -283,8 +295,14 @@ public function set($key, $data) {
public function set_many(array $keyvaluearray) {
$count = 0;
foreach ($keyvaluearray as $pair) {
$this->set($pair['key'], $pair['value']);
$key = $pair['key'];
$data = $pair['value'];
$count++;
if ($this->ttl === 0) {
$this->store[$key] = array($data, 0);
} else {
$this->store[$key] = array($data, cache::now());
}
}
return $count;
}
Expand Down Expand Up @@ -313,6 +331,7 @@ public function has($key) {
* @return bool
*/
public function has_all(array $keys) {
$maxtime = 0;
if ($this->ttl != 0) {
$maxtime = cache::now() - $this->ttl;
}
Expand All @@ -335,6 +354,7 @@ public function has_all(array $keys) {
* @return bool
*/
public function has_any(array $keys) {
$maxtime = 0;
if ($this->ttl != 0) {
$maxtime = cache::now() - $this->ttl;
}
Expand All @@ -354,6 +374,9 @@ public function has_any(array $keys) {
* @return bool Returns true if the operation was a success, false otherwise.
*/
public function delete($key) {
if (!isset($this->store[$key])) {
return false;
}
unset($this->store[$key]);
return true;
}
Expand All @@ -365,6 +388,7 @@ public function delete($key) {
* @return int The number of items successfully deleted.
*/
public function delete_many(array $keys) {
// The number of items that have been successfully deleted.
$count = 0;
foreach ($keys as $key) {
unset($this->store[$key]);
Expand Down Expand Up @@ -420,19 +444,45 @@ public function my_name() {
return $this->name;
}

/**
* Removes expired elements.
* @return int number of removed elements
*/
protected function check_ttl() {
if ($this->ttl === 0) {
return 0;
}
$maxtime = cache::now() - $this->ttl;
$count = 0;
for ($value = reset($this->store); $value !== false; $value = next($this->store)) {
if ($value[1] >= $maxtime) {
// We know that elements are sorted by ttl so no need to continue.
break;
}
$count++;
}
if ($count) {
// Remove first $count elements as they are expired.
$this->store = array_slice($this->store, $count, null, true);
}
return $count;
}

/**
* Finds all of the keys being stored in the cache store instance.
*
* @return array
*/
public function find_all() {
$this->check_ttl();
return array_keys($this->store);
}

/**
* Finds all of the keys whose keys start with the given prefix.
*
* @param string $prefix
* @return array An array of keys.
*/
public function find_by_prefix($prefix) {
$return = array();
Expand All @@ -443,4 +493,12 @@ public function find_by_prefix($prefix) {
}
return $return;
}

/**
* This store supports native TTL handling.
* @return bool
*/
public function store_supports_native_ttl() {
return true;
}
}
91 changes: 89 additions & 2 deletions cache/tests/cache_test.php
Expand Up @@ -1109,9 +1109,9 @@ public function test_disable() {
}

/**
* Test that multiple loaders work ok.
* Test that multiple application loaders work ok.
*/
public function test_multiple_loaders() {
public function test_multiple_application_loaders() {
$instance = cache_config_phpunittest::instance(true);
$instance->phpunit_add_file_store('phpunittest1');
$instance->phpunit_add_file_store('phpunittest2');
Expand Down Expand Up @@ -1156,6 +1156,93 @@ public function test_multiple_loaders() {
$this->assertFalse($result['a']);
$this->assertEquals('B', $result['b']);
$this->assertFalse($result['c']);

// Test non-recursive deletes.
$this->assertTrue($cache->set('test', 'test'));
$this->assertSame('test', $cache->get('test'));
$this->assertTrue($cache->delete('test', false));
// We should still have it on a deeper loader.
$this->assertSame('test', $cache->get('test'));
// Test non-recusive with many functions.
$this->assertSame(3, $cache->set_many(array(
'one' => 'one',
'two' => 'two',
'three' => 'three'
)));
$this->assertSame('one', $cache->get('one'));
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
$this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
$this->assertSame('one', $cache->get('one'));
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
}

/**
* Test that multiple application loaders work ok.
*/
public function test_multiple_session_loaders() {
/* @var cache_config_phpunittest $instance */
$instance = cache_config_phpunittest::instance(true);
$instance->phpunit_add_session_store('phpunittest1');
$instance->phpunit_add_session_store('phpunittest2');
$instance->phpunit_add_definition('phpunit/multi_loader', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
'area' => 'multi_loader'
));
$instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
$instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);

$cache = cache::make('phpunit', 'multi_loader');
$this->assertInstanceOf('cache_session', $cache);
$this->assertFalse($cache->get('test'));
$this->assertTrue($cache->set('test', 'test'));
$this->assertEquals('test', $cache->get('test'));
$this->assertTrue($cache->delete('test'));
$this->assertFalse($cache->get('test'));
$this->assertTrue($cache->set('test', 'test'));
$this->assertTrue($cache->purge());
$this->assertFalse($cache->get('test'));

// Test the many commands.
$this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
$result = $cache->get_many(array('a', 'b', 'c'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('b', $result);
$this->assertArrayHasKey('c', $result);
$this->assertEquals('A', $result['a']);
$this->assertEquals('B', $result['b']);
$this->assertEquals('C', $result['c']);
$this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
$this->assertEquals(2, $cache->delete_many(array('a', 'c')));
$result = $cache->get_many(array('a', 'b', 'c'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('b', $result);
$this->assertArrayHasKey('c', $result);
$this->assertFalse($result['a']);
$this->assertEquals('B', $result['b']);
$this->assertFalse($result['c']);

// Test non-recursive deletes.
$this->assertTrue($cache->set('test', 'test'));
$this->assertSame('test', $cache->get('test'));
$this->assertTrue($cache->delete('test', false));
// We should still have it on a deeper loader.
$this->assertSame('test', $cache->get('test'));
// Test non-recusive with many functions.
$this->assertSame(3, $cache->set_many(array(
'one' => 'one',
'two' => 'two',
'three' => 'three'
)));
$this->assertSame('one', $cache->get('one'));
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
$this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
$this->assertSame('one', $cache->get('one'));
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions cache/tests/fixtures/lib.php
Expand Up @@ -86,6 +86,24 @@ public function phpunit_add_file_store($name) {
);
}

/**
* Forcefully adds a session store.
*
* @param string $name
*/
public function phpunit_add_session_store($name) {
$this->configstores[$name] = array(
'name' => $name,
'plugin' => 'session',
'configuration' => array(),
'features' => 14,
'modes' => 2,
'default' => true,
'class' => 'cachestore_session',
'lock' => 'cachelock_file_default',
);
}

/**
* Forcefully injects a definition => store mapping.
*
Expand Down

0 comments on commit 85ed2ec

Please sign in to comment.