MDL-44630: Review changes

* Fixing coding style violations.
* Fixing DOMDocument warning.
This commit is contained in:
Mark Nielsen
2014-04-03 10:10:10 -07:00
parent c3d245cff3
commit cb4b7e55a0
22 changed files with 793 additions and 828 deletions
+70 -64
View File
@@ -12,38 +12,35 @@
*
*/
require_once 'gral_lib/cssparser.php';
require_once('gral_lib/cssparser.php');
/**
* Base XML class
*
*/
class XMLGenericDocument {
private $charset;
private $charset;
/**
*
* Document
* @var DOMDocument
*/
public $doc = null;
public $doc = null;
/**
*
* Xpath
* @var DOMXPath
*/
protected $dxpath = null;
protected $filename;
private $filepath;
private $isloaded = false;
private $arrayPrefixNS = array();
private $is_html = false;
protected $filename;
private $filepath;
private $isloaded = false;
private $arrayPrefixNS = array();
private $is_html = false;
/**
* @param string $value
* @return string
*/
* @param string $value
* @return string
*/
public static function safexml($value) {
$result = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'),
ENT_NOQUOTES,
@@ -52,51 +49,51 @@ class XMLGenericDocument {
return $result;
}
function __construct($ch = 'UTF-8',$validatenow=true){
function __construct($ch = 'UTF-8', $validatenow = true) {
$this->charset = $ch;
$this->documentInit();
$this->doc->validateOnParse = $validatenow;
}
function __destruct(){
function __destruct() {
$this->dxpath = null;
$this->doc = null;
$this->doc = null;
}
private function documentInit($withonCreate=true) {
private function documentInit($withonCreate = true) {
$hg = false;
if ($this->isloaded){
if ($this->isloaded) {
$guardstate = $this->doc->validateOnParse;
$hg = true;
unset($this->dxpath);
unset($this->doc);
$this->isloaded = false;
}
$this->doc = new DOMDocument("1.0",$this->charset);
$this->doc = new DOMDocument("1.0", $this->charset);
$this->doc->strictErrorChecking = true;
if ($hg) {
$this->doc->validateOnParse = $guardstate;
}
$this->doc->formatOutput = true;
$this->doc->preserveWhiteSpace = true;
if($withonCreate) {
if ($withonCreate) {
$this->on_create();
}
}
public function viewXML (){
public function viewXML() {
return $this->doc->saveXML();
}
public function registerNS($prefix, $nsuri) {
$this->arrayPrefixNS[$prefix]=$nsuri;
$this->arrayPrefixNS[$prefix] = $nsuri;
}
public function load($fname) {
// Sine xml will remain loaded should the repeated load fail we should recreate document to be empty.
// Sine xml will remain loaded should the repeated load fail we should recreate document to be empty.
$this->documentInit(false);
$this->isloaded = $this->doc->load($fname);
if ($this->isloaded){
if ($this->isloaded) {
$this->filename = $fname;
$this->processPath();
$this->is_html = false;
@@ -104,7 +101,7 @@ class XMLGenericDocument {
return $this->on_load();
}
public function loadUrl($url){
public function loadUrl($url) {
$this->documentInit();
$this->isloaded = true;
$this->doc->loadXML( file_get_contents($url) );
@@ -112,7 +109,7 @@ class XMLGenericDocument {
return $this->on_load();
}
public function loadHTML ($content){
public function loadHTML($content) {
$this->documentInit();
$this->doc->validateOnParse = false;
$this->isloaded = true;
@@ -121,7 +118,7 @@ class XMLGenericDocument {
return $this->on_load();
}
public function loadXML ($content){
public function loadXML($content) {
$this->documentInit();
$this->doc->validateOnParse = false;
$this->isloaded = true;
@@ -130,9 +127,9 @@ class XMLGenericDocument {
return $this->on_load();
}
public function loadHTMLFile($fname){
//Sine xml will remain loaded should the repeated load fail
//we shoudl recreate document to be empty
public function loadHTMLFile($fname) {
// Sine xml will remain loaded should the repeated load fail
// we should recreate document to be empty.
$this->documentInit();
$this->doc->validateOnParse = false;
$this->isloaded = $this->doc->loadHTMLFile($fname);
@@ -144,29 +141,28 @@ class XMLGenericDocument {
return $this->on_load();
}
public function loadXMLFile($fname){
//Sine xml will remain loaded should the repeated load fail
//we shoudl recreate document to be empty
public function loadXMLFile($fname) {
// Sine xml will remain loaded should the repeated load fail
// we should recreate document to be empty.
$this->documentInit();
$this->doc->validateOnParse = false;
$this->isloaded = $this->doc->load($fname);
if ($this->isloaded) {
$this->filename = $fname;
$this->processPath();
$this->is_html=true;
$this->is_html = true;
}
return $this->on_load();
}
public function loadString($content){
public function loadString($content) {
$this->doc = new DOMDocument("1.0",$this->charset);
$this->doc = new DOMDocument("1.0", $this->charset);
$content = '<virtualtag>'.$content.'</virtualtag>';
$this->doc->loadXML($content);
return true;
}
public function save() {
@@ -192,18 +188,16 @@ class XMLGenericDocument {
return $this->doc->validate();
}
public function attributeValue($path,$attrname,$node=null) {
public function attributeValue($path, $attrname, $node = null) {
$this->chkxpath();
$result = null;
$resultlist = null;
if (is_null($node)) {
$resultlist = $this->dxpath->query($path);
} else {
$resultlist = $this->dxpath->query($path,$node);
$resultlist = $this->dxpath->query($path, $node);
}
if ( is_object($resultlist) &&
($resultlist->length > 0) &&
$resultlist->item(0)->hasAttribute($attrname)){
if (is_object($resultlist) && ($resultlist->length > 0) && $resultlist->item(0)->hasAttribute($attrname)) {
$result = $resultlist->item(0)->getAttribute($attrname);
}
return $result;
@@ -217,7 +211,7 @@ class XMLGenericDocument {
* @param int $count
* @return string
*/
public function nodeValue($path, $node = null, $count = 1){
public function nodeValue($path, $node = null, $count = 1) {
$nd = $this->node($path, $node, $count);
return $this->nodeTextValue($nd);
}
@@ -228,17 +222,17 @@ class XMLGenericDocument {
* @param DOMNode $node
* @return string
*/
public function nodeTextValue($node){
public function nodeTextValue($node) {
$result = '';
if (is_object($node)) {
if ($node->hasChildNodes()) {
$chnodesList = $node->childNodes;
$types = array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE);
foreach ($chnodesList as $chnode) {
if (in_array($chnode->nodeType, $types)){
$result .= $chnode->wholeText;
}
}
if (in_array($chnode->nodeType, $types)) {
$result .= $chnode->wholeText;
}
}
}
}
return $result;
@@ -255,9 +249,8 @@ class XMLGenericDocument {
public function node($path, $nd = null, $count = 1) {
$result = null;
$resultlist = $this->nodeList($path,$nd);
if ( is_object($resultlist) &&
($resultlist->length > 0) ){
$result = $resultlist->item($count -1);
if (is_object($resultlist) && ($resultlist->length > 0)) {
$result = $resultlist->item($count - 1);
}
return $result;
}
@@ -269,7 +262,7 @@ class XMLGenericDocument {
* @param DOMNode $node
* @return DOMNodeList
*/
public function nodeList($path,$node=null){
public function nodeList($path, $node = null) {
$this->chkxpath();
@@ -277,7 +270,7 @@ class XMLGenericDocument {
if (is_null($node)) {
$resultlist = $this->dxpath->query($path);
} else {
$resultlist = $this->dxpath->query($path,$node);
$resultlist = $this->dxpath->query($path, $node);
}
return $resultlist;
}
@@ -392,12 +385,25 @@ class XMLGenericDocument {
return $node->appendChild($this->create_attribute_ns($namespace, $name, $value));
}
public function fileName() {return $this->filename;}
public function filePath() {return $this->filepath;}
public function fileName() {
return $this->filename;
}
protected function on_load() {return $this->isloaded;}
protected function on_save() {return true;}
protected function on_create() {return true;}
public function filePath() {
return $this->filepath;
}
protected function on_load() {
return $this->isloaded;
}
protected function on_save() {
return true;
}
protected function on_create() {
return true;
}
public function resetXpath() {
$this->dxpath = null;
@@ -405,16 +411,16 @@ class XMLGenericDocument {
}
private function chkxpath() {
if (!isset($this->dxpath) || is_null($this->dxpath)){
if (!isset($this->dxpath) || is_null($this->dxpath)) {
$this->dxpath = new DOMXPath($this->doc);
foreach ($this->arrayPrefixNS as $nskey => $nsuri) {
$this->dxpath->registerNamespace($nskey,$nsuri);
$this->dxpath->registerNamespace($nskey, $nsuri);
}
}
}
protected function processPath(){
$path_parts = pathinfo($this->filename);
$this->filepath = array_key_exists('dirname',$path_parts) ? $path_parts['dirname']."/" : '';
protected function processPath() {
$path_parts = pathinfo($this->filename);
$this->filepath = array_key_exists('dirname', $path_parts) ? $path_parts['dirname']."/" : '';
}
}