-
Notifications
You must be signed in to change notification settings - Fork 3
/
HeaderCount.hooks.php
56 lines (45 loc) · 2.1 KB
/
HeaderCount.hooks.php
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
<?php
use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
class HeaderCountHooks implements ParserFirstCallInitHook {
/**
* Set as the render reason for the parser to skip #headcount processing. This is necessary to prevent infinite loops
* caused by the parsing done by the #headcount function when passed a page name that also uses the #headcount function.
* @var string
*/
const RENDER_REASON = 'HeaderCount parser';
public function onParserFirstCallInit($parser) {
$parser->setFunctionHook('headcount', 'HeaderCountHooks::renderHeadCount');
}
public static function renderHeadCount(Parser $parser, $page = '', $level = '') {
if ($parser->getOptions()->getRenderReason() == self::RENDER_REASON) {
return 'Fake parse by HeaderCount extension. Skipping processing.';
}
$title = empty($page) ? $parser->getPage()->getText() : $page;
$page = MediaWikiServices::getInstance()->getPageStore()->getExistingPageByText($title);
if ($page === null) {
return "'''$title does not exist.'''";
}
$rev = MediaWikiServices::getInstance()->getRevisionStore()->getRevisionByTitle($page);
if ($rev === null) {
return "'''Could not retrieve revision from $title.'''";
}
$content = $rev->getContent(SlotRecord::MAIN);
if ($content === null) {
return "'''Could not extract text from $title.'''";
}
$parserOpts = ParserOptions::newFromUser($parser->getUserIdentity());
$parserOpts->setRenderReason(self::RENDER_REASON);
$parserOutput = MediaWikiServices::getInstance()->getContentRenderer()->getParserOutput($content, $page, null, $parserOpts);
$level = empty($level) ? 2 : intval($level);
$count = 0;
foreach ($parserOutput->getTOCData()->getSections() as $section) {
if ($section->hLevel == $level) {
$count++;
}
}
return $count;
}
}