forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
1 changed file
with
102 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,39 @@ | ||
<?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/>. | ||
|
||
/** | ||
* 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 [email protected] | ||
* @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 @@ public function parse(&$string, $options = array()) { | |
|
||
$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 @@ public function parse(&$string, $options = array()) { | |
/** | ||
* 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 @@ protected function process_block_rule($name, $block) { | |
|
||
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,42 +208,38 @@ private function block_callback($match) { | |
*/ | ||
|
||
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 @@ private function tag_callback($match) { | |
$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 @@ private function tag_callback($match) { | |
*/ | ||
|
||
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 @@ private function initialize_nowiki_index_callback($match) { | |
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 @@ private function commit_nowiki_index_callback($match) { | |
/** | ||
* 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; | ||
} | ||
} |