mediawiki-extensions-Echo/includes/formatters/PresentationModelSectionTrait.php
Roan Kattouw 6eea6d93a1 Used parsed section titles for anchors
This allows us to link to the right section when the section
title contains templates or magic words.

* Add getParsedSectionTitle()
* Use it in getTitleWithSection()
** Stop using Parser::guessSectionNameFromWikiText() because it
   wants preprocessed wikitext, not fully parsed and stripped text
* Move getTruncatedSectionTitle() from EventPresentationModel to
  PresentationModelSectionTrait and make it use getParsedSectionTitle()

Bug: T134216
Change-Id: I877ff6b0ce4e64400f6e5f6284ae47a11cd4335b
2016-09-27 16:17:59 -07:00

97 lines
2.4 KiB
PHP

<?php
/**
* Trait that adds section title handling to an EchoEventPresentationModel subclass.
*/
trait EchoPresentationModelSectionTrait {
private $rawSectionTitle = null;
private $parsedSectionTitle = null;
/**
* Get the raw (unparsed) section title
* @return string Section title
*/
protected function getRawSectionTitle() {
if ( $this->rawSectionTitle !== null ) {
return $this->rawSectionTitle;
}
$sectionTitle = $this->event->getExtraParam( 'section-title' );
if ( !$sectionTitle ) {
$this->rawSectionTitle = false;
return false;
}
// Check permissions
if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
$this->rawSectionTitle = false;
return false;
}
$this->rawSectionTitle = $sectionTitle;
return $this->rawSectionTitle;
}
/**
* Get the section title parsed to plain text
* @return string Section title (plain text)
*/
protected function getParsedSectionTitle() {
if ( $this->parsedSectionTitle !== null ) {
return $this->parsedSectionTitle;
}
$rawSectionTitle = $this->getRawSectionTitle();
if ( !$rawSectionTitle ) {
$this->parsedSectionTitle = false;
return false;
}
$this->parsedSectionTitle = EchoDiscussionParser::getTextSnippet(
$rawSectionTitle,
$this->language,
150,
$this->event->getTitle()
);
return $this->parsedSectionTitle;
}
/**
* Check if there is a section.
*
* This also returns false if the revision is deleted,
* even if there is a section, because the section can't
* be viewed in that case.
* @return boolean Whether there is a section
*/
protected function hasSection() {
return (bool)$this->getRawSectionTitle();
}
/**
* Get a Title pointing to the section, if available.
* @return Title
*/
protected function getTitleWithSection() {
$title = $this->event->getTitle();
$section = $this->getParsedSectionTitle();
// Like Parser::guessSectionNameFromWikiText() but without the link stripping
$fragment = Sanitizer::escapeId(
Sanitizer::normalizeSectionNameWhitespace( $section ),
'noninitial'
);
if ( $section ) {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$fragment
);
}
return $title;
}
protected function getTruncatedSectionTitle() {
return $this->language->embedBidi( $this->language->truncate(
$this->getParsedSectionTitle(),
self::SECTION_TITLE_RECOMMENDED_LENGTH,
'...',
false
) );
}
}