diff --git a/lib/classes/output/progress_trace.php b/lib/classes/output/progress_trace.php new file mode 100644 index 00000000000..8259ddb7d71 --- /dev/null +++ b/lib/classes/output/progress_trace.php @@ -0,0 +1,41 @@ +. + +/** + * Progress trace class. + * + * Use this class from long operations where you want to output occasional information about + * what is going on, but don't know if, or in what format, the output should be. + * + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + */ +abstract class progress_trace { + /** + * Output an progress message in whatever format. + * + * @param string $message the message to output. + * @param integer $depth indent depth for this message. + */ + abstract public function output($message, $depth = 0); + + /** + * Called when the processing is finished. + */ + public function finished() { + } +} diff --git a/lib/classes/output/progress_trace/combined_progress_trace.php b/lib/classes/output/progress_trace/combined_progress_trace.php new file mode 100644 index 00000000000..084114de521 --- /dev/null +++ b/lib/classes/output/progress_trace/combined_progress_trace.php @@ -0,0 +1,61 @@ +. + +/** + * Special type of trace that can be used for redirecting to multiple other traces. + * + * @copyright Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + */ +class combined_progress_trace extends progress_trace { + + /** + * An array of traces. + * @var array + */ + protected $traces; + + /** + * Constructs a new instance. + * + * @param array $traces multiple traces + */ + public function __construct(array $traces) { + $this->traces = $traces; + } + + /** + * Output an progress message in whatever format. + * + * @param string $message the message to output. + * @param integer $depth indent depth for this message. + */ + public function output($message, $depth = 0) { + foreach ($this->traces as $trace) { + $trace->output($message, $depth); + } + } + + /** + * Called when the processing is finished. + */ + public function finished() { + foreach ($this->traces as $trace) { + $trace->finished(); + } + } +} diff --git a/lib/classes/output/progress_trace/error_log_progress_trace.php b/lib/classes/output/progress_trace/error_log_progress_trace.php new file mode 100644 index 00000000000..f27d5e791a1 --- /dev/null +++ b/lib/classes/output/progress_trace/error_log_progress_trace.php @@ -0,0 +1,46 @@ +. + +/** + * This subclass of progress_trace outputs to error log. + * + * @copyright Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + */ +class error_log_progress_trace extends progress_trace { + /** @var string log prefix */ + protected $prefix; + + /** + * Constructor. + * @param string $prefix optional log prefix + */ + public function __construct($prefix = '') { + $this->prefix = $prefix; + } + + /** + * Output the trace message. + * + * @param string $message + * @param int $depth + * @return void Output is sent to error log. + */ + public function output($message, $depth = 0) { + error_log($this->prefix . str_repeat(' ', $depth) . $message); + } +} diff --git a/lib/classes/output/progress_trace/html_list_progress_trace.php b/lib/classes/output/progress_trace/html_list_progress_trace.php new file mode 100644 index 00000000000..ef1629fb04b --- /dev/null +++ b/lib/classes/output/progress_trace/html_list_progress_trace.php @@ -0,0 +1,66 @@ +. + +/** + * HTML List Progress Tree + * + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + */ +class html_list_progress_trace extends progress_trace { + /** @var int */ + protected $currentdepth = -1; + + /** + * Echo out the list + * + * @param string $message The message to display + * @param int $depth + * @return void Output is echoed + */ + public function output($message, $depth = 0) { + $samedepth = true; + while ($this->currentdepth > $depth) { + echo "\n\n"; + $this->currentdepth -= 1; + if ($this->currentdepth == $depth) { + echo '
', str_repeat(' ', $depth), htmlspecialchars($message, ENT_COMPAT), "
\n"; + flush(); + } +} diff --git a/lib/classes/output/progress_trace/null_progress_trace.php b/lib/classes/output/progress_trace/null_progress_trace.php new file mode 100644 index 00000000000..c0b1def2680 --- /dev/null +++ b/lib/classes/output/progress_trace/null_progress_trace.php @@ -0,0 +1,34 @@ +. + +/** + * This subclass of progress_trace does not ouput anything. + * + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + */ +class null_progress_trace extends progress_trace { + /** + * Does Nothing + * + * @param string $message + * @param int $depth + * @return void Does Nothing + */ + public function output($message, $depth = 0) { + } +} diff --git a/lib/classes/output/progress_trace/progress_trace_buffer.php b/lib/classes/output/progress_trace/progress_trace_buffer.php new file mode 100644 index 00000000000..68788c46721 --- /dev/null +++ b/lib/classes/output/progress_trace/progress_trace_buffer.php @@ -0,0 +1,90 @@ +. + +/** + * Special type of trace that can be used for catching of output of other traces. + * + * @copyright Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + */ +class progress_trace_buffer extends progress_trace { + /** @var progress_trace */ + protected $trace; + /** @var bool do we pass output out */ + protected $passthrough; + /** @var string output buffer */ + protected $buffer; + + /** + * Constructor. + * + * @param progress_trace $trace + * @param bool $passthrough true means output and buffer, false means just buffer and no output + */ + public function __construct(progress_trace $trace, $passthrough = true) { + $this->trace = $trace; + $this->passthrough = $passthrough; + $this->buffer = ''; + } + + /** + * Output the trace message. + * + * @param string $message the message to output. + * @param int $depth indent depth for this message. + * @return void output stored in buffer + */ + public function output($message, $depth = 0) { + ob_start(); + $this->trace->output($message, $depth); + $this->buffer .= ob_get_contents(); + if ($this->passthrough) { + ob_end_flush(); + } else { + ob_end_clean(); + } + } + + /** + * Called when the processing is finished. + */ + public function finished() { + ob_start(); + $this->trace->finished(); + $this->buffer .= ob_get_contents(); + if ($this->passthrough) { + ob_end_flush(); + } else { + ob_end_clean(); + } + } + + /** + * Reset internal text buffer. + */ + public function reset_buffer() { + $this->buffer = ''; + } + + /** + * Return internal text buffer. + * @return string buffered plain text + */ + public function get_buffer() { + return $this->buffer; + } +} diff --git a/lib/classes/output/progress_trace/text_progress_trace.php b/lib/classes/output/progress_trace/text_progress_trace.php new file mode 100644 index 00000000000..21ee17f449b --- /dev/null +++ b/lib/classes/output/progress_trace/text_progress_trace.php @@ -0,0 +1,35 @@ +. + +/** + * This subclass of progress_trace outputs to plain text. + * + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + */ +class text_progress_trace extends progress_trace { + /** + * Output the trace message. + * + * @param string $message + * @param int $depth + * @return void Output is echo'd + */ + public function output($message, $depth = 0) { + mtrace(str_repeat(' ', $depth) . $message); + } +} diff --git a/lib/db/legacyclasses.php b/lib/db/legacyclasses.php index 0cc09322714..bc77cabdbd6 100644 --- a/lib/db/legacyclasses.php +++ b/lib/db/legacyclasses.php @@ -43,4 +43,14 @@ $legacyclasses = [ \require_login_session_timeout_exception::class => 'exception/require_login_session_timeout_exception.php', \required_capability_exception::class => 'exception/required_capability_exception.php', \webservice_parameter_exception::class => 'exception/webservice_parameter_exception.php', + + // The progress_trace classes. + \combined_progress_trace::class => 'output/progress_trace/combined_progress_trace.php', + \error_log_progress_trace::class => 'output/progress_trace/error_log_progress_trace.php', + \html_list_progress_trace::class => 'output/progress_trace/html_list_progress_trace.php', + \html_progress_trace::class => 'output/progress_trace/html_progress_trace.php', + \null_progress_trace::class => 'output/progress_trace/null_progress_trace.php', + \progress_trace::class => 'output/progress_trace.php', + \progress_trace_buffer::class => 'output/progress_trace/progress_trace_buffer.php', + \text_progress_trace::class => 'output/progress_trace/text_progress_trace.php', ]; diff --git a/lib/weblib.php b/lib/weblib.php index 7e9634c890c..e1a0bd345af 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -79,9 +79,6 @@ define('URL_MATCH_PARAMS', 1); */ define('URL_MATCH_EXACT', 2); -// TODO MDL-81933 Remove after Moodle 4.5 release. -require_once($CFG->libdir . '/classes/url.php'); - // Functions. /** @@ -2591,295 +2588,6 @@ function is_in_popup() { return ($inpopup); } -/** - * Progress trace class. - * - * Use this class from long operations where you want to output occasional information about - * what is going on, but don't know if, or in what format, the output should be. - * - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package core - */ -abstract class progress_trace { - /** - * Output an progress message in whatever format. - * - * @param string $message the message to output. - * @param integer $depth indent depth for this message. - */ - abstract public function output($message, $depth = 0); - - /** - * Called when the processing is finished. - */ - public function finished() { - } -} - -/** - * This subclass of progress_trace does not ouput anything. - * - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package core - */ -class null_progress_trace extends progress_trace { - /** - * Does Nothing - * - * @param string $message - * @param int $depth - * @return void Does Nothing - */ - public function output($message, $depth = 0) { - } -} - -/** - * This subclass of progress_trace outputs to plain text. - * - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package core - */ -class text_progress_trace extends progress_trace { - /** - * Output the trace message. - * - * @param string $message - * @param int $depth - * @return void Output is echo'd - */ - public function output($message, $depth = 0) { - mtrace(str_repeat(' ', $depth) . $message); - } -} - -/** - * This subclass of progress_trace outputs as HTML. - * - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package core - */ -class html_progress_trace extends progress_trace { - /** - * Output the trace message. - * - * @param string $message - * @param int $depth - * @return void Output is echo'd - */ - public function output($message, $depth = 0) { - echo '', str_repeat(' ', $depth), htmlspecialchars($message, ENT_COMPAT), "
\n"; - flush(); - } -} - -/** - * HTML List Progress Tree - * - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package core - */ -class html_list_progress_trace extends progress_trace { - /** @var int */ - protected $currentdepth = -1; - - /** - * Echo out the list - * - * @param string $message The message to display - * @param int $depth - * @return void Output is echoed - */ - public function output($message, $depth = 0) { - $samedepth = true; - while ($this->currentdepth > $depth) { - echo "