VE-1975: Added support for mobile (ArticleAsJson) and usage of XML parsing

Special handling for mobile is needed because article for mobile is delivered within ArticleAsJson which makes a lot of transformation during the parsing process (instead of for instance postprocessing).
Parsing HTML with XML parser seems to be giving better results than using just regexes and also it gives as ability to handle nested tabbers in a future in a better way.
This commit is contained in:
Inez Korczynski 2015-10-19 23:56:20 +00:00
parent a692e008b3
commit 6c4359a9d4

View file

@ -44,34 +44,59 @@ class NodeImage extends Node {
return false;
}
private function getImagesData( $value ) {
$html = $this->getExternalParser()->parseRecursive( $value );
$items = $this->getTabberItems( $html );
// TODO: Add support for <gallery>
$data = array();
for( $i = 0; $i < count( $items ); $i++ ) {
$data[] = $this->getImageData( $items[$i]['title'], $items[$i]['caption'], $items[$i]['caption'] );
private function getTabberMarkers( $value ) {
if ( preg_match_all('/\x7fUNIQ[A-Z0-9]*-TABBER-[0-9]{8}-QINU\x7f/is', $value, $out) ) {
return $out[0];
} else {
return [];
}
}
return $data;
private function getTabberHtml( $marker ) {
return $this->getExternalParser()->parseRecursive( $marker );
}
private function getTabberItems( $html ) {
global $wgArticleAsJson;
$doc = new \DOMDocument();
$doc->loadHTML($html);
$sxml = simplexml_import_dom($doc);
$divs = $sxml->xpath('//div[@class=\'tabbertab\']');
$items = array();
if ( preg_match_all('/class="tabbertab" title="([^"]+)".*?\sdata-image-key="([^"]+)"/is', $html, $tabberOut) ) {
for( $i = 0; $i < count( $tabberOut[0] ); $i++ ) {
$items[] = array(
'title' => $tabberOut[2][$i],
'caption' => $tabberOut[1][$i]
);
foreach ($divs as $div) {
$tabTitle = (string) $div['title'];
if ( $wgArticleAsJson ) {
if ( preg_match( '/data-ref="([^"]+)"/', $div->p->asXML(), $out ) ) {
$items[] = array( 'label' => $tabTitle, 'title' => \ArticleAsJson::$media[$out[1]]['title'] );
}
} else {
if ( preg_match( '/data-image-key="([^"]+)"/', $div->p->asXML(), $out ) ) {
$items[] = array( 'label' => $tabTitle, 'title' => $out[1] );
}
}
}
return $items;
}
private function getImagesData( $value ) {
$tabberItems = array();
$tabberMarkers = $this->getTabberMarkers( $value );
for ( $i = 0; $i < count ( $tabberMarkers ); $i++ ) {
$tabberHtml = $this->getTabberHtml( $tabberMarkers[$i] );
$tabberItems = array_merge($tabberItems, $this->getTabberItems($tabberHtml));
}
$data = array();
for( $i = 0; $i < count( $tabberItems ); $i++ ) {
$data[] = $this->getImageData( $tabberItems[$i]['title'], $tabberItems[$i]['label'], $tabberItems[$i]['label'] );
}
return $data;
}
private function getImageData( $title, $alt, $caption ) {
$titleObj = $this->getImageAsTitleObject( $title );
$fileObj = $this->getFilefromTitle( $titleObj );