r74885 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74884‎ | r74885 | r74886 >
Date:09:27, 17 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.7.1 - improvements to layer validation
Modified paths:
  • /trunk/extensions/Maps/Maps.i18n.php (modified) (history)
  • /trunk/extensions/Maps/includes/Maps_Layer.php (modified) (history)
  • /trunk/extensions/Maps/includes/Maps_LayerPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/Maps.i18n.php
@@ -26,6 +26,7 @@
2727 'maps-ns-layer-talk' => 'Layer talk',
2828 'maps-layer-property' => 'Property',
2929 'maps-layer-value' => 'Value',
 30+ 'maps-layer-errors' => 'Errors',
3031 'maps-error-invalid-layerdef' => 'This layer definition is not valid.',
3132
3233 // Validation
Index: trunk/extensions/Maps/includes/Maps_LayerPage.php
@@ -44,12 +44,29 @@
4545
4646 $layer = $this->getLayer();
4747
 48+ $errorHeader = '';
 49+
4850 if ( !$layer->isValid() ) {
 51+ $messages = $layer->getErrorMessages( 'missing' );
 52+ $errorString = '';
 53+
 54+ if ( count( $messages ) > 0 ) {
 55+ $errorString = '<br />' . implode( '<br />', array_map( 'htmlspecialchars', $messages ) );
 56+ }
 57+
4958 $wgOut->addHTML(
5059 '<span class="errorbox">' .
51 - htmlspecialchars( wfMsg( 'maps-error-invalid-layerdef' ) ) .
 60+ htmlspecialchars( wfMsg( 'maps-error-invalid-layerdef' ) ) . $errorString .
5261 '</span><br />'
5362 );
 63+
 64+ if ( count( $layer->getErrorMessages() ) - count( $messages ) > 0 ) {
 65+ $errorHeader = Html::element(
 66+ 'th',
 67+ array( 'width' => '50%' ),
 68+ wfMsg( 'maps-layer-errors' )
 69+ );
 70+ }
5471 }
5572
5673 $rows = array();
@@ -66,10 +83,32 @@
6784 'th',
6885 array(),
6986 wfMsg( 'maps-layer-value' )
70 - )
 87+ ) . $errorHeader
7188 );
7289
7390 foreach ( $layer->getProperties() as $property => $value ) {
 91+ $errorTD = '';
 92+
 93+ if ( !$layer->isValid() ) {
 94+ $messages = $layer->getErrorMessages( $property );
 95+
 96+ if ( count( $messages ) > 0 ) {
 97+ $errorString = implode( '<br />', array_map( 'htmlspecialchars', $messages ) );
 98+
 99+ $errorTD = Html::rawElement(
 100+ 'td',
 101+ array(),
 102+ $errorString
 103+ );
 104+ }
 105+ }
 106+
 107+ $valueTD = Html::element(
 108+ 'td',
 109+ array( 'colspan' => $errorTD == '' && !$layer->isValid() ? 2 : 1 ),
 110+ $value
 111+ );
 112+
74113 $rows[] = Html::rawElement(
75114 'tr',
76115 array(),
@@ -78,11 +117,7 @@
79118 array(),
80119 $property
81120 ) .
82 - Html::element(
83 - 'td',
84 - array(),
85 - $value
86 - )
 121+ $valueTD . $errorTD
87122 );
88123 }
89124
@@ -110,7 +145,7 @@
111146 */
112147 public function getLayer() {
113148 if ( $this->cachedLayer === false ) {
114 - $this->cachedLayer = MapsLayer::newFromArray( $this->getProperties() );
 149+ $this->cachedLayer = new MapsLayer( $this->getProperties() );
115150 }
116151
117152 return $this->cachedLayer;
Index: trunk/extensions/Maps/includes/Maps_Layer.php
@@ -46,38 +46,41 @@
4747 protected $properties;
4848
4949 /**
50 - * Returns the default layer type.
 50+ * @since 0.7.1
5151 *
 52+ * @var array
 53+ */
 54+ protected $errors = array();
 55+
 56+ /**
 57+ * Keeps track if the layer has been validated, to prevent doing redundant work.
 58+ *
5259 * @since 0.7.1
5360 *
54 - * @return string
 61+ * @var boolean
5562 */
56 - public static function getDefaultType() {
57 - return 'image';
58 - }
 63+ protected $hasValidated = false;
5964
6065 /**
61 - * Creates and returns a new instance of an MapsLayer, based on the provided array of key value pairs.
 66+ * Returns the default layer type.
6267 *
6368 * @since 0.7.1
6469 *
65 - * @param array $properties
66 - *
67 - * @return MapsLayer
 70+ * @return string
6871 */
69 - public static function newFromArray( array $properties ) {
70 - $layer = new MapsLayer();
71 - $layer->setProperties( $properties );
72 - return $layer;
 72+ public static function getDefaultType() {
 73+ return 'image';
7374 }
7475
7576 /**
7677 * Constructor.
7778 *
7879 * @since 0.7.1
 80+ *
 81+ * @param array $properties
7982 */
80 - public function __construct() {
81 -
 83+ public function __construct( array $properties ) {
 84+ $this->properties = $properties;
8285 }
8386
8487 /**
@@ -92,6 +95,27 @@
9396 }
9497
9598 /**
 99+ * Returns the error messages, optionaly filtered by an error tag.
 100+ *
 101+ * @since 0.7.1
 102+ *
 103+ * @param mixed $tag
 104+ *
 105+ * @return array of string
 106+ */
 107+ public function getErrorMessages( $tag = false ) {
 108+ $messages = array();
 109+
 110+ foreach ( $this->errors as $error ) {
 111+ if ( $tag === false || $error->hasTag( $tag ) ) {
 112+ $messages[] = $error->getMessage();
 113+ }
 114+ }
 115+
 116+ return $messages;
 117+ }
 118+
 119+ /**
96120 * Returns the layers properties.
97121 *
98122 * @since 0.7.1
@@ -103,40 +127,64 @@
104128 }
105129
106130 /**
107 - * Gets if the properties make up a valid layer definition.
 131+ * Returns an array of parameter definitions.
108132 *
109133 * @since 0.7.1
110134 *
111 - * @return boolean
 135+ * @return array
112136 */
113 - public function isValid() {
114 - if ( array_key_exists( $this->getType(), self::$types ) ) {
115 - $typeDefinition = self::$types[$this->getType()];
116 -
117 - // Loop over the required parameters.
118 - foreach ( $typeDefinition['required'] as $paramName ) {
119 - if ( !array_key_exists( $paramName, $this->properties ) ) {
120 - return false;
121 - }
122 - }
123 -
124 - return true;
 137+ protected function getParameterDefinitions() {
 138+ $params = array();
 139+
 140+ $params[] = new Parameter( 'label' );
 141+
 142+ $params[] = new Parameter( 'lowerbound', Parameter::TYPE_INTEGER );
 143+ $params[] = new Parameter( 'upperbound', Parameter::TYPE_INTEGER );
 144+ $params[] = new Parameter( 'leftbound', Parameter::TYPE_INTEGER );
 145+ $params[] = new Parameter( 'rightbound', Parameter::TYPE_INTEGER );
 146+ $params[] = new Parameter( 'width', Parameter::TYPE_INTEGER );
 147+ $params[] = new Parameter( 'height', Parameter::TYPE_INTEGER );
 148+
 149+ $params[] = new Parameter( 'zoomlevels', Parameter::TYPE_INTEGER, false );
 150+
 151+ $params['source'] = new Parameter( 'source' );
 152+ //$params['source']->addCriteria();
 153+ //$params['source']->addManipulations();
 154+
 155+ return $params;
 156+ }
 157+
 158+ /**
 159+ * Validates the layer.
 160+ *
 161+ * @since 0.7.1
 162+ */
 163+ protected function validate() {
 164+ $validator = new Validator();
 165+
 166+ $validator->setParameters( $this->properties, $this->getParameterDefinitions() );
 167+ $validator->validateParameters();
 168+
 169+ if ( $validator->hasFatalError() !== false ) {
 170+ $this->errors = $validator->getErrors();
125171 }
126 - else {
127 - return false;
128 - }
129 - }
 172+ }
130173
131174 /**
132 - * Sets the properties.
 175+ * Gets if the properties make up a valid layer definition.
133176 *
134 - * @since 0.7.1
 177+ * @since 0.7.1
135178 *
136 - * @param array $properties
 179+ * @return boolean
137180 */
138 - public function setProperties( array $properties ) {
139 - $this->properties = $properties;
140 - }
 181+ public function isValid() {
 182+ if ( !$this->hasValidated ) {
 183+ $this->validate();
 184+ $this->hasValidated = true;
 185+ }
 186+
 187+ return count( $this->errors ) == 0;
 188+ }
141189
142190 /**
143191 * Returns a string containing the JavaScript definition of this layer.

Follow-up revisions

RevisionCommit summaryAuthorDate
r74886Changes for 0.4.1 - follow up to r74885jeroendedauw09:28, 17 October 2010
r74888Changes for 0.7.1 - follow up to r74885jeroendedauw09:54, 17 October 2010

Status & tagging log