1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
<?php
namespace dokuwiki\Feed;
use Diff;
use dokuwiki\ChangeLog\PageChangeLog;
use TableDiffFormatter;
use UnifiedDiffFormatter;
/**
* Accept more or less arbitrary data to represent a page and provide lazy loading accessors
* to all the data we need for feed generation.
*/
class FeedPageProcessor extends FeedItemProcessor
{
/** @var array[] metadata */
protected $meta;
// region data processors
/** @inheritdoc */
public function getURL($linkto)
{
switch ($linkto) {
case 'page':
$opt = ['rev' => $this->getRev()];
break;
case 'rev':
$opt = ['rev' => $this->getRev(), 'do' => 'revisions'];
break;
case 'current':
$opt = [];
break;
case 'diff':
default:
$opt = ['rev' => $this->getRev(), 'do' => 'diff'];
}
return wl($this->getId(), $opt, true, '&');
}
/** @inheritdoc */
public function getBody($content)
{
global $lang;
switch ($content) {
case 'diff':
$diff = $this->getDiff();
// note: diff output must be escaped, UnifiedDiffFormatter provides plain text
$udf = new UnifiedDiffFormatter();
return "<pre>\n" . hsc($udf->format($diff)) . "\n</pre>";
case 'htmldiff':
$diff = $this->getDiff();
// note: no need to escape diff output, TableDiffFormatter provides 'safe' html
$tdf = new TableDiffFormatter();
$content = '<table>';
$content .= '<tr><th colspan="2" width="50%">' . dformat($this->getPrev()) . '</th>';
$content .= '<th colspan="2" width="50%">' . $lang['current'] . '</th></tr>';
$content .= $tdf->format($diff);
$content .= '</table>';
return $content;
case 'html':
if ($this->isExisting()) {
$html = p_wiki_xhtml($this->getId(), '', false);
} else {
$html = p_wiki_xhtml($this->getId(), $this->getRev(), false);
}
return $this->cleanHTML($html);
case 'abstract':
default:
return $this->getAbstract();
}
}
/** @inheritdoc */
public function getCategory()
{
$meta = $this->getMetaData();
return (array)($meta['subject'] ?? (string)getNS($this->getId()));
}
// endregion
// region data accessors
/**
* Get the page abstract
*
* @return string
*/
public function getAbstract()
{
if (!isset($this->data['abstract'])) {
$meta = $this->getMetaData();
if (isset($meta['description']['abstract'])) {
$this->data['abstract'] = (string)$meta['description']['abstract'];
} else {
$this->data['abstract'] = '';
}
}
return $this->data['abstract'];
}
/** @inheritdoc */
public function getRev()
{
$rev = parent::getRev();
if ($rev) return $rev;
if (page_exists($this->id)) {
$this->data['rev'] = filemtime(wikiFN($this->id));
$this->data['exists'] = true;
} else {
$this->loadRevisions();
}
return $this->data['rev'];
}
/**
* Get the previous revision timestamp of this page
*
* @return int|null The previous revision or null if there is none
*/
public function getPrev()
{
if ($this->data['prev'] ?? 0) return $this->data['prev'];
$this->loadRevisions();
return $this->data['prev'];
}
/**
* Does this page exist?
*
* @return bool
*/
public function isExisting()
{
if (!isset($this->data['exists'])) {
$this->data['exists'] = page_exists($this->id);
}
return $this->data['exists'];
}
/**
* Get the title of this page
*
* @return string
*/
public function getTitle()
{
global $conf;
if (!isset($this->data['title'])) {
if ($conf['useheading']) {
$this->data['title'] = p_get_first_heading($this->id);
} else {
$this->data['title'] = noNS($this->id);
}
}
return $this->data['title'];
}
// endregion
/**
* Get the metadata of this page
*
* @return array[]
*/
protected function getMetaData()
{
if (!isset($this->meta)) {
$this->meta = (array)p_get_metadata($this->id);
}
return $this->meta;
}
/**
* Load the current and previous revision from the changelog
* @return void
*/
protected function loadRevisions()
{
$changelog = new PageChangeLog($this->id);
$revs = $changelog->getRevisions(0, 2); // FIXME check that this returns the current one correctly
if (!isset($this->data['rev'])) {
// prefer an already set date, only set if missing
// it should usally not happen that neither is available
$this->data['rev'] = $revs[0] ?? 0;
}
// a previous revision might not exist
$this->data['prev'] = $revs[1] ?? null;
}
/**
* Get a diff between this and the previous revision
*
* @return Diff
*/
protected function getDiff()
{
$prev = $this->getPrev();
if ($prev) {
return new Diff(
explode("\n", rawWiki($this->getId(), $prev)),
explode("\n", rawWiki($this->getId(), ''))
);
}
return new Diff([''], explode("\n", rawWiki($this->getId(), '')));
}
}
|