+
+ /**
+ * Render the page for viewing, Parsing and performing features such as page transclusion.
+ * @param Page $page
+ * @param bool $ignorePermissions
+ * @return mixed|string
+ */
+ public function renderPage(Page $page, $ignorePermissions = false)
+ {
+ $content = $page->html;
+ $matches = [];
+ preg_match_all("/{{@\s?([0-9].*?)}}/", $content, $matches);
+ if (count($matches[0]) === 0) {
+ return $content;
+ }
+
+ $topLevelTags = ['table', 'ul', 'ol'];
+ foreach ($matches[1] as $index => $includeId) {
+ $splitInclude = explode('#', $includeId, 2);
+ $pageId = intval($splitInclude[0]);
+ if (is_nan($pageId)) {
+ continue;
+ }
+
+ $matchedPage = $this->getById('page', $pageId, false, $ignorePermissions);
+ if ($matchedPage === null) {
+ $content = str_replace($matches[0][$index], '', $content);
+ continue;
+ }
+
+ if (count($splitInclude) === 1) {
+ $content = str_replace($matches[0][$index], $matchedPage->html, $content);
+ continue;
+ }
+
+ $doc = new DOMDocument();
+ $doc->loadHTML(mb_convert_encoding('<body>'.$matchedPage->html.'</body>', 'HTML-ENTITIES', 'UTF-8'));
+ $matchingElem = $doc->getElementById($splitInclude[1]);
+ if ($matchingElem === null) {
+ $content = str_replace($matches[0][$index], '', $content);
+ continue;
+ }
+ $innerContent = '';
+ $isTopLevel = in_array(strtolower($matchingElem->nodeName), $topLevelTags);
+ if ($isTopLevel) {
+ $innerContent .= $doc->saveHTML($matchingElem);
+ } else {
+ foreach ($matchingElem->childNodes as $childNode) {
+ $innerContent .= $doc->saveHTML($childNode);
+ }
+ }
+ $content = str_replace($matches[0][$index], trim($innerContent), $content);
+ }
+
+ return $content;
+ }
+
+ /**
+ * Get the plain text version of a page's content.
+ * @param Page $page
+ * @return string
+ */
+ public function pageToPlainText(Page $page)
+ {
+ $html = $this->renderPage($page);
+ return strip_tags($html);
+ }
+