MaxSem has uploaded a new change for review.
https://gerrit.wikimedia.org/r/271437
Change subject: WIP: tag split
......................................................................
WIP: tag split
Change-Id: I68614aa620fbbf166c00977f98f720387b9047de
---
M includes/Hooks.php
A includes/Tag/MapData.php
A includes/Tag/MapFrame.php
A includes/Tag/MapLink.php
R includes/Tag/TagHandler.php
5 files changed, 127 insertions(+), 55 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Kartographer
refs/changes/37/271437/1
diff --git a/includes/Hooks.php b/includes/Hooks.php
index 0815d8f..4dd9ff7 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -20,7 +20,7 @@
* @return bool
*/
public static function onParserFirstCallInit( Parser $parser ) {
- $parser->setHook( 'maps',
'Kartographer\TagHandler::handleMapsTag' );
+ $parser->setHook( 'mapframe',
'Kartographer\TagHandler::handleMapsTag' );
return true;
}
diff --git a/includes/Tag/MapData.php b/includes/Tag/MapData.php
new file mode 100644
index 0000000..469352b
--- /dev/null
+++ b/includes/Tag/MapData.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Kartographer\Tag;
+
+
+class MapData extends TagHandler {
+
+}
diff --git a/includes/Tag/MapFrame.php b/includes/Tag/MapFrame.php
new file mode 100644
index 0000000..118abe0
--- /dev/null
+++ b/includes/Tag/MapFrame.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Kartographer\Tag;
+
+
+class MapFrame extends TagHandler {
+ private $width;
+ private $height;
+
+ protected function parseArgs() {
+ $this->parseMapArgs();
+ // @todo: should these have defaults?
+ $this->width = $this->getInt( 'width' );
+ $this->height = $this->getInt( 'height' );
+ }
+}
diff --git a/includes/Tag/MapLink.php b/includes/Tag/MapLink.php
new file mode 100644
index 0000000..97fba78
--- /dev/null
+++ b/includes/Tag/MapLink.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Kartographer\Tag;
+
+
+class MapLink extends TagHandler {
+
+}
diff --git a/includes/TagHandler.php b/includes/Tag/TagHandler.php
similarity index 81%
rename from includes/TagHandler.php
rename to includes/Tag/TagHandler.php
index 8282080..23cb4d4 100644
--- a/includes/TagHandler.php
+++ b/includes/Tag/TagHandler.php
@@ -7,17 +7,29 @@
* @author Yuri Astrakhan
*/
-namespace Kartographer;
+namespace Kartographer\Tag;
use FormatJson;
use Html;
+use Kartographer\SimpleStyleSanitizer;
use Parser;
use ParserOutput;
use PPFrame;
use Status;
use stdClass;
-class TagHandler {
+abstract class TagHandler {
+ /** @var Status */
+ protected $status;
+
+ /** @var stdClass[] */
+ protected $geometries;
+
+ /** @var Parser */
+ protected $parser;
+
+ protected $args;
+
/**
* @param string $input
* @param array $args
@@ -25,35 +37,19 @@
* @param PPFrame $frame
* @return string
*/
- public static function handleMapsTag(
- /** @noinspection PhpUnusedParameterInspection */
+ public function handleMapsTag(
$input, array $args, Parser $parser, PPFrame $frame
) {
- global $wgKartographerStyles, $wgKartographerDfltStyle;
$output = $parser->getOutput();
$output->addModuleStyles( 'ext.kartographer' );
- if ( $input !== '' && $input !== null ) {
- $status = FormatJson::parse( $input,
FormatJson::TRY_FIXING | FormatJson::STRIP_COMMENTS );
- $value = false;
- if ( $status->isOK() ) {
- $value = self::validateContent( $status );
- if ( $value && !is_array( $value ) ) {
- $value = array( $value );
- }
- $sanitizer = new SimpleStyleSanitizer( $parser,
$frame );
- $sanitizer->sanitize( $value );
- }
- } else {
- $status = Status::newGood();
- $value = array();
- }
+ $this->status = Status::newGood();
+ $this->args = $args;
- $mode = self::validateEnum( $status, $args, 'mode', false,
'static' );
- if ( !in_array( $mode, array( 'interactive', 'static', 'data',
'link' ) ) ) {
- $status->fatal( 'kartographer-error-bad_attr', 'mode' );
- return self::reportError( $output, $status );
- }
+ $this->parseGeometries( $input, $parser, $frame );
+ $this->parseArgs();
+
+
$width = $height = $groups = $liveId = null;
$group = isset( $args['group'] ) ? $args['group'] : '*';
@@ -195,6 +191,74 @@
return $html;
}
+ protected function parseGeometries( $input, Parser $parser, PPFrame
$frame ) {
+ if ( $input !== '' && $input !== null ) {
+ $status = FormatJson::parse( $input,
FormatJson::TRY_FIXING | FormatJson::STRIP_COMMENTS );
+ if ( $status->isOK() ) {
+ $json = $status->getValue();
+ if ( $json && !is_array( $json ) ) {
+ $json = array( $json );
+ }
+ $status = $this->validateContent( $json );
+ $sanitizer = new SimpleStyleSanitizer( $parser,
$frame );
+ $sanitizer->sanitize( $json );
+ $this->geometries = $json;
+ }
+ } else {
+ $status = Status::newGood();
+ }
+ $this->status->merge( $status );
+
+ return $status->isOK();
+ }
+
+ protected abstract function parseArgs();
+
+ protected function parseMapArgs() {
+ global $wgKartographerStyles, $wgKartographerDfltStyle;
+
+ $this->lat = $this->getFloat( 'latitude' );
+ $this->lon = $this->getFloat( 'longitude' );
+ $this->zoom = $this->getInt( 'zoom' );
+ $regexp = '/^(' . implode( '|', $wgKartographerStyles ) . ')$/';
+ $this->style = $this->getAndValidate( 'style',
$wgKartographerDfltStyle, $regexp );
+ }
+
+ protected function getInt( $name, $default = false ) {
+ $value = $this->getAndValidate( $name, $default, '/^-?[0-9]+$/'
);
+ if ( $value !== false ) {
+ $value = intval( $value );
+ }
+
+ return $value;
+ }
+
+ protected function getFloat( $name, $default = false ) {
+ $value = $this->getAndValidate( $name, $default,
'/^-?[0-9]*\.?[0-9]+$/' );
+ if ( $value !== false ) {
+ $value = floatval( $value );
+ }
+
+ return $value;
+ }
+
+
+ protected function getAndValidate( $name, $default, $regexp ) {
+ if ( !isset( $this->args[$name] ) ) {
+ if ( $default === false ) {
+ $this->status->fatal(
'kartographer-error-bad_attr', $name );
+ }
+ return $default;
+ }
+ $value = $this->args[$name];
+ if ( !preg_match( $regexp, $value ) ) {
+ $value = false;
+ $this->status->fatal( 'kartographer-error-bad_attr',
$name );
+ }
+
+ return $value;
+ }
+
/**
* Handles the last step of parse process
* @param Parser $parser
@@ -218,25 +282,6 @@
* @param Status $status
* @param array $args
* @param string $value
- * @param bool $isInt
- * @return float|int|false
- */
- private static function validateNumber( $status, $args, $value, $isInt
) {
- if ( isset( $args[$value] ) ) {
- $v = $args[$value];
- $pattern = $isInt ? '/^[0-9]+$/' :
'/^[-+]?[0-9]*\.?[0-9]+$/';
- if ( preg_match( $pattern, $v ) ) {
- return $isInt ? intval( $v ) : floatval( $v );
- }
- }
- $status->fatal( 'kartographer-error-bad_attr', $value );
- return false;
- }
-
- /**
- * @param Status $status
- * @param array $args
- * @param string $value
* @param array|bool|false $set
* @param string|bool|false $default
* @return string|false
@@ -254,21 +299,16 @@
}
/**
- * @param Status $status
+ * @param mixed $json
* @return mixed
*/
- private static function validateContent( $status ) {
- $value = $status->getValue();
-
+ private function validateContent( $json ) {
// The content must be a non-associative array of values or an
object
- if ( $value instanceof stdClass ) {
- $value = array ( $value );
- } elseif ( !is_array( $value ) ) {
- $status->fatal( 'kartographer-error-bad_data' );
- return false;
+ if ( !is_array( $json ) ) {
+ return Status::newFatal( 'kartographer-error-bad_data'
);
}
- return $value;
+ return Status::newGood();
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/271437
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I68614aa620fbbf166c00977f98f720387b9047de
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Kartographer
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits