Add support for backdating creation times using <meta name="date" />
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / Metadata.php
blobd97d8e17e5927c636df29e8962cbd9e103bf5cad
1 <?php
3 /**
4 * Extracts metadata from XHTMLCompiler_Page (which comes the version
5 * control system) and inserts it in the HTML page.
6 */
7 class XHTMLCompiler_DOMFilter_Metadata extends XHTMLCompiler_DOMFilter {
9 protected $name = 'Metadata';
11 public function process(DOMDocument $dom, $page, $manager) {
13 $head = $dom->getElementsByTagName('head')->item(0);
15 $date = $page->getCreatedTime();
16 // check if the meta element already exists
17 $meta = $dom->createElement('meta');
18 $append = true;
19 foreach ($head->getElementsByTagName('meta') as $node) {
20 if ($node->getAttribute('name') == 'Date') {
21 // it exists: use this date as the "real" date.
22 $meta = $node;
23 if ($t = $meta->getAttribute('content')) {
24 // this code is slightly redundant since getCreatedTime()
25 // will have already accounted for this. Still, can't hurt.
26 $date = new DateTime($t);
28 $append = false;
29 break;
32 // only write if we have a valid date
33 if ($date) {
34 $meta->setAttribute('name', 'Date');
35 $meta->setAttribute('content', $date->format('c'));
36 if ($append) $head->appendChild($meta);