Skip to content

Commit

Permalink
Merge branch 'MDL-59150-master-3' of git://github.com/ryanwyllie/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonllao committed Jul 18, 2017
2 parents 7af4771 + 0f268f5 commit f510788
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
6 changes: 6 additions & 0 deletions admin/cli/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,12 @@

if (!$options['skip-database']) {
install_cli_database($options, $interactive);
// This needs to happen at the end to ensure it occurs after all caches
// have been purged for the last time.
// This will build a cached version of the current theme for the user
// to immediately start browsing the site.
require_once($CFG->libdir.'/upgradelib.php');
upgrade_themes();
} else {
echo get_string('cliskipdatabase', 'install')."\n";
}
Expand Down
7 changes: 7 additions & 0 deletions admin/cli/install_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,12 @@

install_cli_database($options, true);

// This needs to happen at the end to ensure it occurs after all caches
// have been purged for the last time.
// This will build a cached version of the current theme for the user
// to immediately start browsing the site.
require_once($CFG->libdir.'/upgradelib.php');
upgrade_themes();

echo get_string('cliinstallfinished', 'install')."\n";
exit(0); // 0 means success
6 changes: 6 additions & 0 deletions admin/cli/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,11 @@
admin_apply_default_settings(NULL, false);
admin_apply_default_settings(NULL, false);

// This needs to happen at the end to ensure it occurs after all caches
// have been purged for the last time.
// This will build a cached version of the current theme for the user
// to immediately start browsing the site.
upgrade_themes();

echo get_string('cliupgradefinished', 'admin')."\n";
exit(0); // 0 means success
54 changes: 54 additions & 0 deletions lib/classes/task/build_installed_themes_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Adhoc task that builds and caches all of the site's installed themes.
*
* @package core
* @copyright 2017 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\task;

defined('MOODLE_INTERNAL') || die();

/**
* Class that builds and caches all of the site's installed themes.
*
* @package core
* @copyright 2017 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class build_installed_themes_task extends adhoc_task {

/**
* Run the task.
*/
public function execute() {
global $CFG;
require_once("{$CFG->libdir}/outputlib.php");

$themenames = array_keys(\core_component::get_plugin_list('theme'));
// Load the theme configs.
$themeconfigs = array_map(function($themename) {
return \theme_config::load($themename);
}, $themenames);

// Build the list of themes and cache them in local cache.
theme_build_css_for_themes($themeconfigs);
}
}
20 changes: 20 additions & 0 deletions lib/classes/task/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ public static function get_scheduled_task($classname) {
return self::scheduled_task_from_record($record);
}

/**
* This function load the adhoc tasks for a given classname.
*
* @param string $classname
* @return \core\task\adhoc_task[]
*/
public static function get_adhoc_tasks($classname) {
global $DB;

if (strpos($classname, '\\') !== 0) {
$classname = '\\' . $classname;
}
// We are just reading - so no locks required.
$records = $DB->get_records('task_adhoc', array('classname' => $classname));

return array_map(function($record) {
return self::adhoc_task_from_record($record);
}, $records);
}

/**
* This function load the default scheduled task details for a given classname.
*
Expand Down
40 changes: 40 additions & 0 deletions lib/tests/adhoc_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,44 @@ public function test_get_next_adhoc_task_future() {
$task->execute();
\core\task\manager::adhoc_task_complete($task);
}

/**
* Test empty set of adhoc tasks
*/
public function test_get_adhoc_tasks_empty_set() {
$this->resetAfterTest(true);

$this->assertEquals([], \core\task\manager::get_adhoc_tasks('\\core\\task\\adhoc_test_task'));
}

/**
* Test correct set of adhoc tasks is returned for class.
*/
public function test_get_adhoc_tasks_result_set() {
$this->resetAfterTest(true);

for ($i = 0; $i < 3; $i++) {
$task = new \core\task\adhoc_test_task();
\core\task\manager::queue_adhoc_task($task);
}

for ($i = 0; $i < 3; $i++) {
$task = new \core\task\adhoc_test2_task();
\core\task\manager::queue_adhoc_task($task);
}

$adhoctests = \core\task\manager::get_adhoc_tasks('\\core\\task\\adhoc_test_task');
$adhoctest2s = \core\task\manager::get_adhoc_tasks('\\core\\task\\adhoc_test2_task');

$this->assertCount(3, $adhoctests);
$this->assertCount(3, $adhoctest2s);

foreach ($adhoctests as $task) {
$this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
}

foreach ($adhoctest2s as $task) {
$this->assertInstanceOf('\\core\\task\\adhoc_test2_task', $task);
}
}
}
5 changes: 5 additions & 0 deletions lib/tests/fixtures/task_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function execute() {
}
}

class adhoc_test2_task extends \core\task\adhoc_task {
public function execute() {
}
}

class scheduled_test_task extends \core\task\scheduled_task {
public function get_name() {
return "Test task";
Expand Down
25 changes: 25 additions & 0 deletions lib/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,31 @@ function upgrade_language_pack($lang = null) {
print_upgrade_separator();
}

/**
* Build the current theme so that the user doesn't have to wait for it
* to build on the first page load after the install / upgrade.
*/
function upgrade_themes() {
global $CFG;

require_once("{$CFG->libdir}/outputlib.php");

// Build the current theme so that the user can immediately
// browse the site without having to wait for the theme to build.
$themeconfig = theme_config::load($CFG->theme);
$direction = right_to_left() ? 'rtl' : 'ltr';
theme_build_css_for_themes([$themeconfig], [$direction]);

// Only queue the task if there isn't already one queued.
if (empty(\core\task\manager::get_adhoc_tasks('\\core\\task\\build_installed_themes_task'))) {
// Queue a task to build all of the site themes at some point
// later. These can happen offline because it doesn't block the
// user unless they quickly change theme.
$adhoctask = new \core\task\build_installed_themes_task();
\core\task\manager::queue_adhoc_task($adhoctask);
}
}

/**
* Install core moodle tables and initialize
* @param float $version target version
Expand Down

0 comments on commit f510788

Please sign in to comment.