From a5e3c4e5f31da41a129d2b2bc24aa2c75241b68d Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sat, 30 Nov 2019 00:16:15 +0100 Subject: [PATCH] MDL-67438 wiki: Formating changes (file doc block + whitespace fixes) Also: - change 2 variable/property names, both internal. - format empty function. Towards getting it fully phpcs compliant. Note this does not fix the missing (in all methods) phpdoc blocks. --- mod/wiki/parser/parser.php | 182 +++++++++++++++++++++---------------- 1 file changed, 102 insertions(+), 80 deletions(-) diff --git a/mod/wiki/parser/parser.php b/mod/wiki/parser/parser.php index 4fefa00b442..bfb0aa44d9f 100644 --- a/mod/wiki/parser/parser.php +++ b/mod/wiki/parser/parser.php @@ -1,4 +1,39 @@ . + +/** + * Library of functions and constants for module wiki + * + * It contains the great majority of functions defined by Moodle + * that are mandatory to develop a module. + * + * @package mod_wiki + * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu + * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu + * + * @author Jordi Piguillem + * @author Marc Alier + * @author David Jimenez + * @author Josep Arus + * @author Kenneth Riba + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); /** * Generic parser implementation @@ -8,66 +43,59 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package mod_wiki */ - class wiki_parser_proxy { private static $parsers = array(); private static $basepath = ""; public static function parse(&$string, $type, $options = array()) { - if(empty(self::$basepath)) { + if (empty(self::$basepath)) { global $CFG; - self::$basepath = $CFG->dirroot.'/mod/wiki/parser/'; + self::$basepath = $CFG->dirroot . '/mod/wiki/parser/'; } $type = strtolower($type); self::$parsers[$type] = null; // Reset the current parser because it may have other options. - if(self::create_parser_instance($type)) { + if (self::create_parser_instance($type)) { return self::$parsers[$type]->parse($string, $options); - } - else { + } else { return false; } } public static function get_token($name, $type) { - if(self::create_parser_instance($type)) { + if (self::create_parser_instance($type)) { return self::$parsers[$type]->get_token($name); - } - else { + } else { return false; } } - public static function get_section(&$string, $type, $section, $all_content = false) { - if(self::create_parser_instance($type)) { + public static function get_section(&$string, $type, $section, $allcontent = false) { + if (self::create_parser_instance($type)) { $content = self::$parsers[$type]->get_section($section, $string, true); - if($all_content) { + if ($allcontent) { return $content; - } - else { + } else { return is_array($content) ? $content[1] : null; } - } - else { + } else { return false; } } private static function create_parser_instance($type) { - if(empty(self::$parsers[$type])) { - include_once(self::$basepath."markups/$type.php"); - $class = strtolower($type)."_parser"; - if(class_exists($class)) { + if (empty(self::$parsers[$type])) { + include_once(self::$basepath . "markups/$type.php"); + $class = strtolower($type) . "_parser"; + if (class_exists($class)) { self::$parsers[$type] = new $class; return true; - } - else { + } else { return false; } - } - else { + } else { return true; } } @@ -83,7 +111,7 @@ abstract class generic_parser { private $rulestack = array(); - protected $parser_status = 'Before'; + protected $parserstatus = 'Before'; /** * Dynamic return values @@ -95,14 +123,14 @@ abstract class generic_parser { protected $nowikitoken = "%!"; - public function __construct() {} + public function __construct() { + } /** * Parse function */ - public function parse(&$string, $options = array()) { - if(!is_string($string)) { + if (!is_string($string)) { return false; } @@ -112,21 +140,21 @@ abstract class generic_parser { $this->initialize_nowiki_index(); - if(method_exists($this, 'before_parsing')) { + if (method_exists($this, 'before_parsing')) { $this->before_parsing(); } - $this->parser_status = 'Parsing'; + $this->parserstatus = 'Parsing'; - foreach($this->blockrules as $name => $block) { + foreach ($this->blockrules as $name => $block) { $this->process_block_rule($name, $block); } $this->commit_nowiki_index(); - $this->parser_status = 'After'; + $this->parserstatus = 'After'; - if(method_exists($this, 'after_parsing')) { + if (method_exists($this, 'after_parsing')) { $this->after_parsing(); } @@ -136,15 +164,15 @@ abstract class generic_parser { /** * Initialize options */ - - protected function set_options($options) {} + protected function set_options($options) { + } /** * Block processing function & callbacks */ - protected function process_block_rule($name, $block) { - $this->rulestack[] = array('callback' => method_exists($this, $name."_block_rule") ? $name."_block_rule" : null, 'rule' => $block); + $this->rulestack[] = array('callback' => method_exists($this, $name . "_block_rule") ? $name . "_block_rule" : null, + 'rule' => $block); $this->string = preg_replace_callback($block['expression'], array($this, 'block_callback'), $this->string); @@ -153,24 +181,22 @@ abstract class generic_parser { private function block_callback($match) { $rule = end($this->rulestack); - if(!empty($rule['callback'])) { + if (!empty($rule['callback'])) { $stuff = $this->{$rule['callback']}($match); - } - else { + } else { $stuff = $match[1]; } - if(is_array($stuff) && $rule['rule']['tag']) { + if (is_array($stuff) && $rule['rule']['tag']) { $this->rules($stuff[0], $rule['rule']['tags']); - $stuff = "\n".parser_utils::h($rule['rule']['tag'], $stuff[0], $stuff[1])."\n"; - } - else { - if(!isset($rule['rule']['tags'])) { + $stuff = "\n" . parser_utils::h($rule['rule']['tag'], $stuff[0], $stuff[1]) . "\n"; + } else { + if (!isset($rule['rule']['tags'])) { $rule['rule']['tags'] = null; } $this->rules($stuff, $rule['rule']['tags']); - if(isset($rule['rule']['tag']) && is_string($rule['rule']['tag'])) { - $stuff = "\n".parser_utils::h($rule['rule']['tag'], $stuff)."\n"; + if (isset($rule['rule']['tag']) && is_string($rule['rule']['tag'])) { + $stuff = "\n" . parser_utils::h($rule['rule']['tag'], $stuff) . "\n"; } } @@ -182,25 +208,23 @@ abstract class generic_parser { */ protected final function rules(&$text, $rules = null) { - if($rules === null) { + if ($rules === null) { $rules = array('except' => array()); - } - else if(is_array($rules) && count($rules) > 1) { + } else if (is_array($rules) && count($rules) > 1) { $rules = array('only' => $rules); } - if(isset($rules['only']) && is_array($rules['only'])) { + if (isset($rules['only']) && is_array($rules['only'])) { $rules = $rules['only']; - foreach($rules as $r) { - if(!empty($this->tagrules[$r])) { + foreach ($rules as $r) { + if (!empty($this->tagrules[$r])) { $this->process_tag_rule($r, $this->tagrules[$r], $text); } } - } - else if(isset($rules['except']) && is_array($rules['except'])) { + } else if (isset($rules['except']) && is_array($rules['except'])) { $rules = $rules['except']; - foreach($this->tagrules as $r => $tr) { - if(!in_array($r, $rules)) { + foreach ($this->tagrules as $r => $tr) { + if (!in_array($r, $rules)) { $this->process_tag_rule($r, $tr, $text); } } @@ -208,16 +232,14 @@ abstract class generic_parser { } private function process_tag_rule($name, $rule, &$text) { - if(method_exists($this, $name."_tag_rule")) { - $this->rulestack[] = array('callback' => $name."_tag_rule", 'rule' => $rule); + if (method_exists($this, $name . "_tag_rule")) { + $this->rulestack[] = array('callback' => $name . "_tag_rule", 'rule' => $rule); $text = preg_replace_callback($rule['expression'], array($this, 'tag_callback'), $text); array_pop($this->rulestack); - } - else { - if(isset($rule['simple'])) { + } else { + if (isset($rule['simple'])) { $replace = "<{$rule['tag']} />"; - } - else { + } else { $replace = parser_utils::h($rule['tag'], "$1"); } @@ -229,10 +251,9 @@ abstract class generic_parser { $rule = end($this->rulestack); $stuff = $this->{$rule['callback']}($match); - if(is_array($stuff)) { + if (is_array($stuff)) { return parser_utils::h($rule['rule']['tag'], $stuff[0], $stuff[1]); - } - else { + } else { return $stuff; } } @@ -242,8 +263,9 @@ abstract class generic_parser { */ private function initialize_nowiki_index() { - $token = "\Q".$this->nowikitoken."\E"; - $this->string = preg_replace_callback("/".$token."\d+".$token."/", array($this, "initialize_nowiki_index_callback"), $this->string); + $token = "\Q" . $this->nowikitoken . "\E"; + $this->string = preg_replace_callback("/" . $token . "\d+" . $token . "/", + array($this, "initialize_nowiki_index_callback"), $this->string); } private function initialize_nowiki_index_callback($match) { @@ -253,12 +275,13 @@ abstract class generic_parser { protected function protect($text) { $this->nowikiindex[] = $text; - return $this->nowikitoken.(count($this->nowikiindex)-1).$this->nowikitoken; + return $this->nowikitoken . (count($this->nowikiindex) - 1) . $this->nowikitoken; } private function commit_nowiki_index() { - $token = "\Q".$this->nowikitoken."\E"; - $this->string = preg_replace_callback("/".$token."(\d+)".$token."/", array($this, "commit_nowiki_index_callback"), $this->string); + $token = "\Q" . $this->nowikitoken . "\E"; + $this->string = preg_replace_callback("/" . $token . "(\d+)" . $token . "/", + array($this, "commit_nowiki_index_callback"), $this->string); } private function commit_nowiki_index_callback($match) { @@ -268,14 +291,13 @@ abstract class generic_parser { /** * Get token of the parsable element $name. */ - - public function get_token($name) { - foreach(array_merge($this->blockrules, $this->tagrules) as $n => $v) { - if($name == $n && isset($v['token'])) { + public function get_token($name) { + foreach (array_merge($this->blockrules, $this->tagrules) as $n => $v) { + if ($name == $n && isset($v['token'])) { return $v['token'] ? $v['token'] : false; - } - } + } + } - return false; - } + return false; + } }