Projet

Général

Profil

Paste
Télécharger (1,71 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds_xpathparser / FeedsXPathParserHTML.inc @ a2baadd1

1
<?php
2

    
3
/**
4
 * @files
5
 * Provides the FeedsXPathParserHTML class.
6
 */
7
class FeedsXPathParserHTML extends FeedsXPathParserBase {
8

    
9
  protected $hasSaveHTML = FALSE;
10

    
11
  /**
12
   * Overrides parent::__construct().
13
   */
14
  public function __construct($id) {
15
    parent::__construct($id);
16

    
17
    // DOMDocument::saveHTML() cannot take $node as an argument prior to 5.3.6.
18
    if (version_compare(phpversion(), '5.3.6', '>=')) {
19
      $this->hasSaveHTML = TRUE;
20
    }
21
  }
22

    
23
  /**
24
   * Implements FeedsXPathParserBase::setup().
25
   */
26
  protected function setup($source_config, FeedsFetcherResult $fetcher_result) {
27

    
28
    if (!empty($source_config['exp']['tidy'])) {
29
      $config = array(
30
        'merge-divs'       => FALSE,
31
        'merge-spans'      => FALSE,
32
        'join-styles'      => FALSE,
33
        'drop-empty-paras' => FALSE,
34
        'wrap'             => 0,
35
        'tidy-mark'        => FALSE,
36
        'escape-cdata'     => TRUE,
37
        'word-2000'        => TRUE,
38
      );
39
      // Default tidy encoding is UTF8.
40
      $encoding = $source_config['exp']['tidy_encoding'];
41
      $raw = tidy_repair_string(trim($fetcher_result->getRaw()), $config, $encoding);
42
    }
43
    else {
44
      $raw = $fetcher_result->getRaw();
45
    }
46
    $doc = new DOMDocument();
47
    // Use our own error handling.
48
    $use = $this->errorStart();
49
    $success = $doc->loadHTML($raw);
50
    unset($raw);
51
    $this->errorStop($use, $source_config['exp']['errors']);
52
    if (!$success) {
53
      throw new Exception(t('There was an error parsing the HTML document.'));
54
    }
55
    return $doc;
56
  }
57

    
58
  protected function getRaw(DOMNode $node) {
59
    if ($this->hasSaveHTML) {
60
      return $this->doc->saveHTML($node);
61
    }
62

    
63
    return $this->doc->saveXML($node, LIBXML_NOEMPTYTAG);
64
  }
65
}