r74876 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74875‎ | r74876 | r74877 >
Date:04:22, 17 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.7.1 - images as layers... it only took one and a half year :D
Modified paths:
  • /trunk/extensions/Maps/includes/Maps_Layer.php (modified) (history)
  • /trunk/extensions/Maps/includes/Maps_LayerPage.php (modified) (history)
  • /trunk/extensions/Maps/includes/services/OpenLayers/CriterionOLLayer.php (modified) (history)
  • /trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayers.php (modified) (history)
  • /trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispMap.php (modified) (history)
  • /trunk/extensions/Maps/includes/services/OpenLayers/Maps_ParamOLLayers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/includes/Maps_LayerPage.php
@@ -30,8 +30,6 @@
3131 */
3232 public function view() {
3333 parent::view();
34 - //$layer = $this->getLayer();
35 - //var_dump($layer);exit;
3634 }
3735
3836 /**
@@ -55,14 +53,18 @@
5654 protected function getProperties() {
5755 $properties = array();
5856
 57+ if ( is_null( $this->mContent ) ) {
 58+ $this->loadContent();
 59+ }
 60+
5961 foreach ( explode( "\n", $this->mContent ) as $line ) {
6062 $parts = explode( '=', $line, 2 );
6163
6264 if ( count( $parts ) == 2 ) {
63 - $properties[$parts[0]] = $parts[1];
 65+ $properties[strtolower( str_replace( ' ', '', $parts[0] ) )] = $parts[1];
6466 }
6567 }
66 -
 68+
6769 return $properties;
6870 }
6971
Index: trunk/extensions/Maps/includes/Maps_Layer.php
@@ -13,13 +13,50 @@
1414 class MapsLayer {
1515
1616 /**
 17+ * List of layer type definitions.
 18+ *
1719 * @since 0.7.1
1820 *
1921 * @var array
2022 */
 23+ protected static $types = array(
 24+ 'image' => array(
 25+ 'class' => 'OpenLayers.Layer.Image',
 26+ 'required' => array(
 27+ 'label',
 28+ 'source',
 29+ 'lowerbound',
 30+ 'upperbound',
 31+ 'leftbound',
 32+ 'rightbound',
 33+ 'width',
 34+ 'height'
 35+ ),
 36+ 'optional' => array(
 37+ 'zoomlevels'
 38+ )
 39+ )
 40+ );
 41+
 42+ /**
 43+ * @since 0.7.1
 44+ *
 45+ * @var array
 46+ */
2147 protected $properties;
2248
2349 /**
 50+ * Returns the default layer type.
 51+ *
 52+ * @since 0.7.1
 53+ *
 54+ * @return string
 55+ */
 56+ public static function getDefaultType() {
 57+ return 'image';
 58+ }
 59+
 60+ /**
2461 * Creates and returns a new instance of an MapsLayer, based on the provided array of key value pairs.
2562 *
2663 * @since 0.7.1
@@ -54,4 +91,80 @@
5592 $this->properties = $properties;
5693 }
5794
58 -}
\ No newline at end of file
 95+ /**
 96+ * Returns the type of the layer.
 97+ *
 98+ * @since 0.7.1
 99+ *
 100+ * @param string
 101+ */
 102+ public function getType() {
 103+ return array_key_exists( 'type', $this->properties ) ? $this->properties['type'] : self::getDefaultType();
 104+ }
 105+
 106+ /**
 107+ * Gets if the properties make up a valid layer definition.
 108+ *
 109+ * @since 0.7.1
 110+ *
 111+ * @return boolean
 112+ */
 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;
 125+ }
 126+ else {
 127+ return false;
 128+ }
 129+ }
 130+
 131+ /**
 132+ * Returns a string containing the JavaScript definition of this layer.
 133+ * Only call this function when you are sure the layer is valid!
 134+ *
 135+ * @since 0.7.1
 136+ *
 137+ * @return string
 138+ */
 139+ public function getJavaScriptDefinition() {
 140+ // Note: this is currently hardcoded for layers of type image.
 141+ $label = Xml::encodeJsVar( $this->properties['label'] );
 142+ $source = Xml::encodeJsVar( $this->properties['source'] );
 143+ $lowerBound = Xml::encodeJsVar( (int)$this->properties['lowerbound'] );
 144+ $upperBound = Xml::encodeJsVar( (int)$this->properties['upperbound'] );
 145+ $leftBound = Xml::encodeJsVar( (int)$this->properties['leftbound'] );
 146+ $rightBound = Xml::encodeJsVar( (int)$this->properties['rightbound'] );
 147+ $width = Xml::encodeJsVar( (int)$this->properties['width'] );
 148+ $height = Xml::encodeJsVar( (int)$this->properties['height'] );
 149+
 150+ $class = self::$types[$this->getType()]['class'];
 151+
 152+ $options = array();
 153+
 154+ if ( array_key_exists( 'zoomlevels', $this->properties ) ) {
 155+ $options['numZoomLevels'] = (int)$this->properties['zoomlevels'];
 156+ }
 157+
 158+ $options = Xml::encodeJsVar( (object)$options );
 159+
 160+ return <<<EOT
 161+ new $class(
 162+ $label,
 163+ $source,
 164+ new OpenLayers.Bounds($leftBound, $lowerBound, $rightBound, $upperBound),
 165+ new OpenLayers.Size($width, $height),
 166+ {$options}
 167+ )
 168+EOT;
 169+ }
 170+
 171+}
Index: trunk/extensions/Maps/includes/services/OpenLayers/Maps_ParamOLLayers.php
@@ -31,30 +31,93 @@
3232 public function manipulate( Parameter &$parameter, array &$parameters ) {
3333 global $egMapsOLLayerGroups, $egMapsOLAvailableLayers;
3434
35 - $unpacked = array();
36 - $imageLayers = array();
 35+ $layerDefs = array();
 36+ $layerNames = array();
3737
3838 foreach ( $parameter->getValue() as $layerOrGroup ) {
3939 if ( array_key_exists( $layerOrGroup, $egMapsOLLayerGroups ) ) {
40 - $unpacked += $egMapsOLLayerGroups[$layerOrGroup];
 40+ $layerNames += $egMapsOLLayerGroups[$layerOrGroup];
 41+
 42+ foreach ( $egMapsOLLayerGroups[$layerOrGroup] as $layerName ) {
 43+ $layerDefs[] = 'new ' . $egMapsOLAvailableLayers[$layerName];
 44+ }
4145 }
42 - elseif ( in_array( $layerOrGroup, $egMapsOLAvailableLayers ) ) {
43 - $unpacked[] = $layerOrGroup;
 46+ elseif ( array_key_exists( $layerOrGroup, $egMapsOLAvailableLayers ) ) {
 47+ $layerDefs[] = 'new ' . $egMapsOLAvailableLayers[$layerOrGroup];
 48+ $layerNames[] = $layerOrGroup;
4449 }
4550 else {
4651 $title = Title::newFromText( $layerOrGroup, Maps_NS_LAYER );
4752
4853 if ( $title->getNamespace() == Maps_NS_LAYER && $title->exists() ) {
4954 $layerPage = new MapsLayerPage( $title );
50 - $imageLayers[] = $layerPage->getLayer();
 55+ $layer = $layerPage->getLayer();
 56+
 57+ if ( $layer->isValid() ) {
 58+ $layerDefs[] = $layer->getJavaScriptDefinition();
 59+ $layerNames[] = $layerOrGroup;
 60+ }
 61+ else {
 62+ wfWarn( "Invalid layer ($layerOrGroup) encountered after validation." );
 63+ }
5164 }
5265 else {
5366 wfWarn( "Invalid layer ($layerOrGroup) encountered after validation." );
5467 }
5568 }
5669 }
 70+
 71+ $parameter->setValue( array(
 72+ '[' . implode( ',', $layerDefs ) . ']',
 73+ $this->getDependencies( $layerNames )
 74+ ) );
 75+ }
 76+
 77+ /**
 78+ * Returns the depencies for the provided layers.
 79+ *
 80+ * @since 0.7.1
 81+ *
 82+ * @param array $layerNames
 83+ *
 84+ * @return array
 85+ */
 86+ protected function getDependencies( array $layerNames ) {
 87+ global $egMapsOLLayerDependencies;
 88+ static $decompressed = false;
5789
58 - $parameter->setValue( array( $unpacked, $imageLayers ) );
 90+ if ( !$decompressed ) {
 91+ $this->decompressLayerDependencies();
 92+ $decompressed = true;
 93+ }
 94+
 95+ $layerDependencies = array();
 96+
 97+ foreach ( $layerNames as $layerName ) {
 98+ if ( array_key_exists( $layerName, $egMapsOLLayerDependencies ) ) {
 99+
 100+ }
 101+ }
 102+
 103+ return $layerDependencies;
59104 }
60105
 106+ /**
 107+ * Resolves group dependencies to individual layer dependencies.
 108+ *
 109+ * @since 0.7.1
 110+ */
 111+ protected static function decompressLayerDependencies() {
 112+ global $egMapsOLLayerDependencies, $egMapsOLLayerGroups;
 113+
 114+ foreach ( $egMapsOLLayerGroups as $groupName => $groupItems ) {
 115+ if ( array_key_exists( $groupName, $egMapsOLLayerDependencies ) ) {
 116+ foreach ( $groupItems as $item ) {
 117+ $egMapsOLLayerDependencies[$item] = $egMapsOLLayerDependencies[$groupName];
 118+ }
 119+ unset($egMapsOLLayerDependencies[$groupName]);
 120+ }
 121+ }
 122+ }
 123+
61124 }
\ No newline at end of file
Index: trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispMap.php
@@ -16,7 +16,7 @@
1717 public function addSpecificMapHTML( Parser $parser ) {
1818 global $wgLang;
1919
20 - $layerItems = $this->service->createLayersStringAndLoadDependencies( $this->layers[0] );
 20+ $layerItems = $this->service->addLayerDependencies( $this->layers[1] );
2121
2222 $mapName = $this->service->getMapId();
2323
@@ -37,7 +37,7 @@
3838 $this->centreLon,
3939 $this->centreLat,
4040 $this->zoom,
41 - [$layerItems],
 41+ {$this->layers[0]},
4242 [$this->controls],
4343 [],
4444 "$langCode"
Index: trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayers.php
@@ -14,16 +14,6 @@
1515 class MapsOpenLayers extends MapsMappingService {
1616
1717 /**
18 - * List to keep track of loaded layers.
19 - * Typically this means one or more js/css files have been added.
20 - *
21 - * @since 0.7
22 - *
23 - * @var array
24 - */
25 - protected static $loadedLayers = array();
26 -
27 - /**
2818 * Constructor.
2919 *
3020 * @since 0.6.6
@@ -160,46 +150,17 @@
161151
162152 return $keys;
163153 }
164 -
165 - /**
166 - * Build up a csv string with the layers, to be outputted as a JS array
167 - *
168 - * @param array $layers
169 - *
170 - * @return csv string
171 - */
172 - public function createLayersStringAndLoadDependencies( array $layers ) {
173 - global $egMapsOLAvailableLayers;
174 -
175 - $layerStr = array();
176 -
177 - foreach ( $layers as $layer ) {
178 - $this->loadDependencyWhenNeeded( $layer );
179 - $layerStr[] = is_array( $egMapsOLAvailableLayers[$layer] ) ? $egMapsOLAvailableLayers[$layer][0] : $egMapsOLAvailableLayers[$layer];
180 - }
181 -
182 - return count( $layerStr ) == 0 ? '' : 'new ' . implode( ',new ', $layerStr );
183 - }
184154
185155 /**
186 - * Load the dependencies of a layer if they are not loaded yet.
 156+ * Adds the layer dependencies.
187157 *
188 - * Note: The check if the layer has been added is redudant with the new (>=0.6.3) dependency management.
189 - *
190 - * @param string $layer The layer to check (and load the dependencies for
 158+ * @since 0.7.1
 159+ *
 160+ * @param array $dependencies
191161 */
192 - public function loadDependencyWhenNeeded( $layer ) {
193 - global $egMapsOLAvailableLayers, $egMapsOLLayerDependencies;
194 -
195 - // Check if there is a dependency refered by the layer definition.
196 - if ( is_array( $egMapsOLAvailableLayers[$layer] )
197 - && count( $egMapsOLAvailableLayers[$layer] ) > 1
198 - && array_key_exists( $egMapsOLAvailableLayers[$layer][1], $egMapsOLLayerDependencies )
199 - && !in_array( $egMapsOLAvailableLayers[$layer][1], self::$loadedLayers ) ) {
200 - // Add the dependency to the output.
201 - $this->addDependency( $egMapsOLLayerDependencies[$egMapsOLAvailableLayers[$layer][1]] );
202 - // Register that it's added so it does not get done multiple times.
203 - self::$loadedLayers[] = $egMapsOLAvailableLayers[$layer][1];
 162+ public function addLayerDependencies( array $dependencies ) {
 163+ foreach ( $dependencies as $dependency ) {
 164+ $this->addDependency( $dependency );
204165 }
205166 }
206167
Index: trunk/extensions/Maps/includes/services/OpenLayers/CriterionOLLayer.php
@@ -36,8 +36,11 @@
3737
3838 // Image layers.
3939 $title = Title::newFromText( $value, Maps_NS_LAYER );
 40+
4041 if ( $title->getNamespace() == Maps_NS_LAYER && $title->exists() ) {
41 - return true;
 42+ $layerPage = new MapsLayerPage( $title );
 43+ $layer = $layerPage->getLayer();
 44+ return $layer->isValid();
4245 }
4346
4447 return false;

Follow-up revisions

RevisionCommit summaryAuthorDate
r74877Follow up to r74876jeroendedauw04:24, 17 October 2010
r74878Changes for 0.7.1 - Follow up to r74876 - images-as-layers!!!jeroendedauw04:24, 17 October 2010

Status & tagging log