Skip to content

Comments

Fix "Call to undefined method MediaWiki\Parser\ParserOutput::getText()"#837

Open
paladox wants to merge 5 commits intoProfessionalWiki:masterfrom
paladox:patch-2
Open

Fix "Call to undefined method MediaWiki\Parser\ParserOutput::getText()"#837
paladox wants to merge 5 commits intoProfessionalWiki:masterfrom
paladox:patch-2

Conversation

@paladox
Copy link
Contributor

@paladox paladox commented Feb 17, 2026

Copies something similar to https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/src/Utils/ParserOutputHelper.php#L29

Summary by CodeRabbit

  • Bug Fixes

    • Improved robustness of wikitext-to-HTML rendering to handle multiple parser outputs and missing body content.
    • Added safer error handling to avoid failures when body content is absent, improving page reliability.
    • Result: more consistent rendering across varied inputs.
  • Tests

    • Updated tests to reflect multi-stage parser output handling and defensive content extraction.

@paladox
Copy link
Contributor Author

paladox commented Feb 17, 2026

Fixes MW 1.45 support (well one issue)

@coderabbitai
Copy link

coderabbitai bot commented Feb 17, 2026

📝 Walkthrough

Walkthrough

Reworks extraction in parser result handling: captures parser output into a variable, returns getContentHolderText() when available (catching LogicException to return '' if no body), else falls back to getText() or ''. Adds LogicException import and preserves early empty-input return.

Changes

Cohort / File(s) Summary
Parser output extraction — source
src/Presentation/WikitextParser.php
Capture parser output in $parserOutput; prefer getContentHolderText() with LogicException handling, fallback to getText(), otherwise return ''. Added LogicException import. Preserves early empty-input return.
Parser output extraction — tests
tests/Util/TestFactory.php
Mirrors extraction changes for test parser usage: store parser output, attempt getContentHolderText() (handle LogicException), else use getText() or ''.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main issue being fixed across multiple files: handling the missing getText() method in MediaWiki's ParserOutput.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/Presentation/WikitextParser.php (1)

39-40: Adding explicit parameter to getText() is optional but harmless.

According to MediaWiki 1.39 documentation, ParserOutput::getText() has a default parameter ($options = []), so calling without arguments is safe and should not cause warnings. The codebase's defensive approach using method_exists() checks appropriately handles version differences by prioritizing getContentHolderText() and getRawText() before falling back to getText(). Other calls to getText() in the repository (e.g., tests/Util/TestFactory.php, tests/System/SemanticQueryTest.php) follow the same pattern without explicit parameters.

If you prefer to be more explicit:

Optional improvement
 } elseif ( method_exists( $parserOutput, 'getText' ) ) {
-	return $parserOutput->getText();
+	return $parserOutput->getText( [] );
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Presentation/WikitextParser.php` around lines 39 - 40, The fallback call
to ParserOutput::getText() in WikitextParser (the elseif branch that checks
method_exists($parserOutput, 'getText')) should be made explicit by passing the
default options parameter to avoid any ambiguity; change the return from
$parserOutput->getText() to $parserOutput->getText([]) so it explicitly supplies
the default $options = [] when calling ParserOutput::getText().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/Presentation/WikitextParser.php`:
- Around line 39-40: The fallback call to ParserOutput::getText() in
WikitextParser (the elseif branch that checks method_exists($parserOutput,
'getText')) should be made explicit by passing the default options parameter to
avoid any ambiguity; change the return from $parserOutput->getText() to
$parserOutput->getText([]) so it explicitly supplies the default $options = []
when calling ParserOutput::getText().

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/Presentation/WikitextParser.php (1)

30-36: Consider hasText() guard instead of exception-as-control-flow, and log unexpected LogicExceptions.

ParserOutput is documented to represent "no HTML output" with null and throw on getRawText() in that case — which is what happens for redirect pages that do not generate HTML output. The current catch silently swallows the exception without any diagnostic trace, making it hard to distinguish a legitimately empty parse from an unexpected failure. Consider the following improvements:

♻️ Suggested improvement
-		if ( method_exists( $parserOutput, 'getContentHolderText' ) ) {
-			try {
-				return $parserOutput->getContentHolderText();
-			} catch ( LogicException $e ) {
-				// Handle case where there is no body content
-				return '';
-			}
-		} elseif ( method_exists( $parserOutput, 'getText' ) ) {
+		if ( method_exists( $parserOutput, 'getContentHolderText' ) ) {
+			// hasText() avoids exception-as-control-flow for the "no HTML" case
+			if ( method_exists( $parserOutput, 'hasText' ) && !$parserOutput->hasText() ) {
+				return '';
+			}
+			try {
+				return $parserOutput->getContentHolderText();
+			} catch ( LogicException $e ) {
+				// Unexpected: log for diagnostics but degrade gracefully
+				wfLogWarning( 'WikitextParser: getContentHolderText() threw: ' . $e->getMessage() );
+				return '';
+			}
+		} elseif ( method_exists( $parserOutput, 'getText' ) ) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Presentation/WikitextParser.php` around lines 30 - 36, Replace the
exception-as-control-flow pattern by first guarding with hasText() on
$parserOutput (if method_exists($parserOutput, 'hasText') &&
!$parserOutput->hasText() return '') and only call getContentHolderText() when
text is present; keep a try/catch around getContentHolderText() but in the catch
(LogicException $e) call your logger (e.g. $this->logger->warning or similar) to
record the unexpected exception before returning an empty string so legitimate
empty parses are handled without silencing unexpected failures.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/Presentation/WikitextParser.php`:
- Around line 30-36: Replace the exception-as-control-flow pattern by first
guarding with hasText() on $parserOutput (if method_exists($parserOutput,
'hasText') && !$parserOutput->hasText() return '') and only call
getContentHolderText() when text is present; keep a try/catch around
getContentHolderText() but in the catch (LogicException $e) call your logger
(e.g. $this->logger->warning or similar) to record the unexpected exception
before returning an empty string so legitimate empty parses are handled without
silencing unexpected failures.

@malberts
Copy link
Contributor

With the updated CI, it looks like there are still some places where the missing method is used.

Refactor parse method to handle different parser output methods.
@paladox
Copy link
Contributor Author

paladox commented Feb 18, 2026

With the updated CI, it looks like there are still some places where the missing method is used.

Fixed.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/Util/TestFactory.php (1)

25-44: Extraction logic is duplicated across WikitextParser and TestFactory

The identical getContentHolderText / getText / '' fallback chain now exists verbatim in both WikitextParser::wikitextToHtml() and TestFactory::parse(). If the MW API changes again, both copies need to be updated in sync.

Consider extracting it to a shared static helper (e.g., ParserOutputHelper::getHtmlText(ParserOutput $output): string) consumable by both production and test code.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/Util/TestFactory.php` around lines 25 - 44, The parsing result
extraction logic duplicated between TestFactory::parse and
WikitextParser::wikitextToHtml should be centralized: create a shared static
helper (e.g., ParserOutputHelper::getHtmlText(ParserOutput $output): string)
that implements the existing chain (use getContentHolderText() with
LogicException handling, fall back to getText(), then ''), then replace the
duplicate code in TestFactory::parse and WikitextParser::wikitextToHtml to call
ParserOutputHelper::getHtmlText($parserOutput); ensure type hints/imports match
ParserOutput and preserve the existing behavior and exception handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/Util/TestFactory.php`:
- Around line 33-39: The catch currently references an unqualified
LogicException inside the Maps\Tests\Util namespace so thrown global
\LogicException instances won't be caught; update the file so the catch matches
the global exception by either adding a top-level import `use LogicException;`
to the file or changing the catch to `catch (\LogicException $e)` for the block
surrounding parserOutput->getContentHolderText() to ensure the empty-string
fallback runs when getContentHolderText() throws.

---

Nitpick comments:
In `@tests/Util/TestFactory.php`:
- Around line 25-44: The parsing result extraction logic duplicated between
TestFactory::parse and WikitextParser::wikitextToHtml should be centralized:
create a shared static helper (e.g.,
ParserOutputHelper::getHtmlText(ParserOutput $output): string) that implements
the existing chain (use getContentHolderText() with LogicException handling,
fall back to getText(), then ''), then replace the duplicate code in
TestFactory::parse and WikitextParser::wikitextToHtml to call
ParserOutputHelper::getHtmlText($parserOutput); ensure type hints/imports match
ParserOutput and preserve the existing behavior and exception handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants