2015-09-22 15:48:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wikia\PortableInfobox\Helpers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper used for checking if template consist any hidden infoboxes (eg. put into <includeonly> tag)
|
|
|
|
*
|
|
|
|
* Class PortableInfoboxTemplatesHelper
|
|
|
|
*/
|
|
|
|
class PortableInfoboxTemplatesHelper {
|
|
|
|
|
|
|
|
/**
|
2015-09-24 14:25:27 +00:00
|
|
|
* @desc Try to find out if infobox got "hidden" inside includeonly tag. Parse it if that's the case.
|
2015-09-22 15:48:38 +00:00
|
|
|
*
|
|
|
|
* @param $title \Title
|
|
|
|
*
|
2015-09-23 13:51:41 +00:00
|
|
|
* @return mixed false when no infoboxes found, Array with infoboxes on success
|
2015-09-22 15:48:38 +00:00
|
|
|
*/
|
|
|
|
public function parseInfoboxes( $title ) {
|
|
|
|
// for templates we need to check for include tags
|
2015-09-24 11:48:51 +00:00
|
|
|
$templateText = $this->fetchContent( $title );
|
2015-09-22 15:48:38 +00:00
|
|
|
$includeonlyText = $this->getIncludeonlyText( $templateText );
|
|
|
|
|
|
|
|
if ( $includeonlyText ) {
|
|
|
|
$parser = new \Parser();
|
|
|
|
$parserOptions = new \ParserOptions();
|
|
|
|
$frame = $parser->getPreprocessor()->newFrame();
|
|
|
|
|
2015-09-24 11:48:51 +00:00
|
|
|
$templateTextWithoutIncludeonly = $parser->getPreloadText( $includeonlyText, $title, $parserOptions );
|
2015-09-22 15:48:38 +00:00
|
|
|
$infoboxes = $this->getInfoboxes( $templateTextWithoutIncludeonly );
|
|
|
|
|
|
|
|
if ( $infoboxes ) {
|
|
|
|
// clear up cache before parsing
|
|
|
|
foreach ( $infoboxes as $infobox ) {
|
|
|
|
try {
|
|
|
|
\PortableInfoboxParserTagController::getInstance()->render( $infobox, $parser, $frame );
|
|
|
|
} catch ( \Exception $e ) {
|
|
|
|
\Wikia\Logger\WikiaLogger::instance()->info( 'Invalid infobox syntax in includeonly tag' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 13:51:41 +00:00
|
|
|
return json_decode( $parser->getOutput()
|
|
|
|
->getProperty( \PortableInfoboxDataService::INFOBOXES_PROPERTY_NAME ), true );
|
2015-09-22 15:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-24 11:48:51 +00:00
|
|
|
* @param $title \Title
|
2015-09-22 15:48:38 +00:00
|
|
|
*
|
2015-09-24 11:48:51 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function fetchContent( $title ) {
|
|
|
|
if ( $title && $title->exists() ) {
|
|
|
|
$article = \Article::newFromTitle( $title, \RequestContext::getMain() );
|
|
|
|
if ( $article && $article->exists() ) {
|
|
|
|
$content = $article->fetchContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset( $content ) && $content ? $content : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc for given template text returns it without text in <nowiki> and <pre> tags
|
2015-09-22 15:48:38 +00:00
|
|
|
*
|
2015-09-24 11:48:51 +00:00
|
|
|
* @param $text string
|
|
|
|
*
|
|
|
|
* @return string
|
2015-09-22 15:48:38 +00:00
|
|
|
*/
|
2015-09-24 11:48:51 +00:00
|
|
|
protected function removeNowikiPre( $text ) {
|
|
|
|
$text = preg_replace( "/<nowiki>.+<\/nowiki>/sU", '', $text );
|
|
|
|
$text = preg_replace( "/<pre>.+<\/pre>/sU", '', $text );
|
2015-09-22 15:48:38 +00:00
|
|
|
|
2015-09-24 11:48:51 +00:00
|
|
|
return $text;
|
2015-09-22 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc returns the text from inside of the first <includeonly> tag and
|
|
|
|
* without the nowiki and pre tags.
|
|
|
|
*
|
|
|
|
* @param $text string template text
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getIncludeonlyText( $text ) {
|
2015-09-24 11:48:51 +00:00
|
|
|
$clean = $this->removeNowikiPre( $text );
|
|
|
|
|
|
|
|
preg_match_all( "/<includeonly>(.+)<\/includeonly>/sU", $clean, $result );
|
2015-09-22 15:48:38 +00:00
|
|
|
if ( !isset( $result[ 1 ][ 0 ] ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-09-24 11:48:51 +00:00
|
|
|
return $result[ 1 ][ 0 ];
|
2015-09-22 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-24 11:48:51 +00:00
|
|
|
* @desc From the template without <includeonly> tags, creates an array of
|
|
|
|
* strings containing only infoboxes. All template content which is not an infobox is removed.
|
2015-09-22 15:48:38 +00:00
|
|
|
*
|
2015-09-24 11:48:51 +00:00
|
|
|
* @param $text string Content of template which uses the <includeonly> tags
|
2015-09-22 15:48:38 +00:00
|
|
|
*
|
2015-09-24 11:48:51 +00:00
|
|
|
* @return array of striped infoboxes ready to parse
|
2015-09-22 15:48:38 +00:00
|
|
|
*/
|
2015-09-24 11:48:51 +00:00
|
|
|
protected function getInfoboxes( $text ) {
|
|
|
|
preg_match_all( "/<infobox.+<\/infobox>/sU", $text, $result );
|
2015-09-22 15:48:38 +00:00
|
|
|
|
2015-09-24 11:48:51 +00:00
|
|
|
return $result[ 0 ];
|
2015-09-22 15:48:38 +00:00
|
|
|
}
|
|
|
|
}
|