r92865 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92864‎ | r92865 | r92866 >
Date:16:49, 22 July 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
attempt to fix some exhibit issues
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Exhibit/SRF_Exhibit.js (modified) (history)
  • /trunk/extensions/SemanticResultFormats/Exhibit/SRF_Exhibit.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/Timeline/SRF_Timeline.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/Exhibit/SRF_Exhibit.js
@@ -119,6 +119,53 @@
120120 window.exhibit.configureFromDOM();
121121 }
122122
123 -addOnloadHook(createExhibit);
 123+// add any onload functions in this hook (please don't hard-code any events in the xhtml source)
 124+var doneExhibitOnloadHook;
124125
 126+if (!window.exhibitOnloadFuncts) {
 127+ var exhibitOnloadFuncts = [];
 128+}
125129
 130+function addExhibitOnloadHook(hookFunct) {
 131+ // Allows add-on scripts to add onload functions
 132+ if(!doneExhibitOnloadHook) {
 133+ exhibitOnloadFuncts[exhibitOnloadFuncts.length] = hookFunct;
 134+ } else {
 135+ hookFunct(); // bug in MSIE script loading
 136+ }
 137+}
 138+
 139+addExhibitOnloadHandler(window, "load",
 140+ function () {
 141+ // don't run anything below this for non-dom browsers
 142+ if (doneExhibitOnloadHook || !(document.getElementById && document.getElementsByTagName)) {
 143+ return;
 144+ }
 145+
 146+ // set this before running any hooks, since any errors below
 147+ // might cause the function to terminate prematurely
 148+ doneExhibitOnloadHook = true;
 149+
 150+ // Run any added-on functions
 151+ for (var i = 0; i < exhibitOnloadFuncts.length; i++) {
 152+ exhibitOnloadFuncts[i]();
 153+ }
 154+ }
 155+);
 156+
 157+/**
 158+ * Add an event handler to an element
 159+ *
 160+ * @param Element element Element to add handler to
 161+ * @param String attach Event to attach to
 162+ * @param callable handler Event handler callback
 163+ */
 164+function addExhibitOnloadHandler( element, attach, handler ) {
 165+ if( window.addEventListener ) {
 166+ element.addEventListener( attach, handler, false );
 167+ } else if( window.attachEvent ) {
 168+ element.attachEvent( 'on' + attach, handler );
 169+ }
 170+}
 171+
 172+addExhibitOnloadHook(createExhibit);
Index: trunk/extensions/SemanticResultFormats/Exhibit/SRF_Exhibit.php
@@ -12,26 +12,33 @@
1313 * @ingroup SMWQuery
1414 */
1515 class SRFExhibit extends SMWResultPrinter {
16 - // /mapping between SMW's and Exhibit's the data types
17 - protected $m_types = array( "_wpg" => "text", "_num" => "number", "_dat" => "date", "_geo" => "text", "_uri" => "url" );
 16+
 17+ // mapping between SMW's and Exhibit's the data types
 18+ protected $m_types = array(
 19+ '_wpg' => 'text',
 20+ '_num' => 'number',
 21+ '_dat' => 'date',
 22+ '_geo' => 'text',
 23+ '_uri' => 'url'
 24+ );
1825
1926 protected static $exhibitRunningNumber = 0; // not sufficient since there might be multiple pages rendered within one PHP run; but good enough now
2027
21 - // /overwrite function to allow execution of result printer even if no results are available (in case remote query yields no results in local wiki)
22 - public function getResult( $results, $params, $outputmode ) {
 28+ // Overwrite function to allow execution of result printer even if no results are available (in case remote query yields no results in local wiki)
 29+ public function getResult( SMWQueryResult $results, array $params, $outputmode ) {
2330 $this->readParameters( $params, $outputmode );
24 - $result = $this->getResultText( $results, $outputmode );
25 - return $result;
 31+ return $this->getResultText( $results, $outputmode );
2632 }
2733
28 - // /function aligns the format of SMW's property names to Exhibit's format
 34+ // Function aligns the format of SMW's property names to Exhibit's format
2935 protected function encodePropertyName( $property ) {
30 - return strtolower( str_replace( " ", "_", trim( $property ) ) );
 36+ return strtolower( str_replace( ' ', '_', trim( $property ) ) );
3137 }
3238
33 - // /Tries to determine the namespace in the event it got lost
 39+ // Tries to determine the namespace in the event it got lost
3440 protected function determineNamespace( $res ) {
3541 $row = $res->getNext();
 42+
3643 if ( $row != null ) {
3744 $tmp = clone $row[0];
3845 $object = efSRFGetNextDV( $tmp );
@@ -43,13 +50,14 @@
4451 return $value[0] . ':';
4552 }
4653 }
47 - return "";
 54+
 55+ return '';
4856 }
4957 }
5058
5159 protected function getResultText( SMWQueryResult $res, $outputmode ) {
5260
53 - global $smwgIQRunningNumber, $wgScriptPath, $wgGoogleMapsKey, $srfgScriptPath;
 61+ global $smwgIQRunningNumber, $wgScriptPath, $srfgScriptPath;
5462
5563 // //////////////////////////////
5664 // ///////REMOTE STUFF///////////
@@ -210,24 +218,30 @@
211219 $viewstack[] = 'ex:role=\'view\' ex:viewClass=\'Timeline\' ex:label=\'Timeline\' ex:showSummary=\'false\' ' . implode( " ", $tlparams );
212220 break;
213221 case 'map':// map view
214 - if ( isset( $wgGoogleMapsKey ) ) {
215 - $map = true;
216 - $exparams = array( 'latlng', 'colorkey' );
217 - $usparams = array( 'type', 'center', 'zoom', 'size', 'scalecontrol', 'overviewcontrol', 'mapheight' );
218 - $mapparams = array();
219 - foreach ( $exparams as $param ) {
220 - if ( array_key_exists( $param, $this->m_params ) ) $mapparams[] = 'ex:' . $param . '=\'.' . $this->encodePropertyName( $this->m_params[$param] ) . '\' ';
221 - }
222 - foreach ( $usparams as $param ) {
223 - if ( array_key_exists( $param, $this->m_params ) ) $mapparams[] = 'ex:' . $param . '=\'' . $this->encodePropertyName( $this->m_params[$param] ) . '\' ';
224 - }
225 - if ( !array_key_exists( 'start', $this->m_params ) && !array_key_exists( 'end', $this->m_params ) ) { // find out if a geographic coordinate is available
226 - foreach ( $res->getPrintRequests() as $pr ) {
227 - if ( $pr->getTypeID() == '_geo' ) {
228 - $mapparams[] = 'ex:latlng=\'.' . $this->encodePropertyName( $pr->getLabel() ) . '\' ';
229 - break;
 222+ global $egGoogleMapsKey;
 223+
 224+ if ( isset( $egGoogleMapsKey ) ) {
 225+
 226+ $map = true;
 227+ $exparams = array( 'latlng', 'colorkey' );
 228+ $usparams = array( 'type', 'center', 'zoom', 'size', 'scalecontrol', 'overviewcontrol', 'mapheight' );
 229+ $mapparams = array();
 230+
 231+ foreach ( $exparams as $param ) {
 232+ if ( array_key_exists( $param, $this->m_params ) ) $mapparams[] = 'ex:' . $param . '=\'.' . $this->encodePropertyName( $this->m_params[$param] ) . '\' ';
 233+ }
 234+
 235+ foreach ( $usparams as $param ) {
 236+ if ( array_key_exists( $param, $this->m_params ) ) $mapparams[] = 'ex:' . $param . '=\'' . $this->encodePropertyName( $this->m_params[$param] ) . '\' ';
 237+ }
 238+
 239+ if ( !array_key_exists( 'start', $this->m_params ) && !array_key_exists( 'end', $this->m_params ) ) { // find out if a geographic coordinate is available
 240+ foreach ( $res->getPrintRequests() as $pr ) {
 241+ if ( $pr->getTypeID() == '_geo' ) {
 242+ $mapparams[] = 'ex:latlng=\'.' . $this->encodePropertyName( $pr->getLabel() ) . '\' ';
 243+ break;
 244+ }
230245 }
231 - }
232246 }
233247 $viewstack[] .= 'ex:role=\'view\' ex:viewClass=\'Map\' ex:showSummary=\'false\' ex:label=\'Map\' ' . implode( " ", $mapparams );
234248 }
@@ -341,23 +355,10 @@
342356 $stringtoedit = substr( $stringtoedit[0], 3 );
343357 $JSONlinksrc = "var JSONlink = '" . $stringtoedit . "';";
344358
 359+ $this->includeJS( $timeline, $map );
 360+
345361 // create script header with variables containing the Exhibit markup
346362 $headervars = "<script type='text/javascript'>\n\t\t\t" . $facetsrc . "\n\t\t\t" . $viewsrc . "\n\t\t\t" . $lenssrc . "\n\t\t\t" . $stylesrc . "\n\t\t\t" . $formatssrc . "\n\t\t\t" . $JSONlinksrc . "\n\t\t\t var remote=" . $varremote . ";</script>";
347 -
348 -
349 - // To run Exhibit some links to the scripts of the API need to be included in the header
350 -
351 - $ExhibitScriptSrc1 = '<script type="text/javascript" src="' . $srfgScriptPath . '/Exhibit/exhibit/exhibit-api.js?autoCreate=false&safe=true&bundle=false';
352 - if ( $timeline ) $ExhibitScriptSrc1 .= '&views=timeline';
353 - if ( $map ) $ExhibitScriptSrc1 .= '&gmapkey=' . $wgGoogleMapsKey;
354 - $ExhibitScriptSrc1 .= '"></script>';
355 - $ExhibitScriptSrc2 = '<script type="text/javascript" src="' . $srfgScriptPath . '/Exhibit/SRF_Exhibit.js"></script>';
356 - $CSSSrc = '<link rel="stylesheet" type="text/css" href="' . $srfgScriptPath . '/Exhibit/SRF_Exhibit.css"></link>';
357 -
358 - SMWOutputs::requireHeadItem( 'CSS', $CSSSrc ); // include CSS
359 - SMWOutputs::requireHeadItem( 'EXHIBIT1', $ExhibitScriptSrc1 ); // include Exhibit API
360 - SMWOutputs::requireHeadItem( 'EXHIBIT2', $ExhibitScriptSrc2 ); // includes javascript overwriting the Exhibit start-up functions
361 - SMWOutputs::requireHeadItem( 'SOURCES' . $smwgIQRunningNumber, $sourcesrc );// include sources variable
362363 SMWOutputs::requireHeadItem( 'VIEWSFACETS', $headervars );// include views and facets variable
363364
364365
@@ -435,18 +436,75 @@
436437 }
437438 $result .= "</table>\n"; }
438439
439 - if ( SRFExhibit::$exhibitRunningNumber == 0 ) $result .= "<div id=\"exhibitLocation\"></div>"; // print placeholder (just print it one time)
 440+ if ( SRFExhibit::$exhibitRunningNumber == 0 ) {
 441+ $result .= "<div id=\"exhibitLocation\"></div>"; // print placeholder (just print it one time)
 442+ }
 443+
440444 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed
441445 SRFExhibit::$exhibitRunningNumber++;
 446+
442447 return $result;
443448 }
444449
445450 public function getParameters() {
446451 $params = parent::getParameters();
 452+
447453 $params[] = array( 'name' => 'views', 'type' => 'enum-list', 'description' => wfMsg( 'srf_paramdesc_views' ), 'values' => array( 'tiles', 'tabular', 'timeline', 'maps' ) );
448454 $params[] = array( 'name' => 'facets', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_facets' ) );
449455 $params[] = array( 'name' => 'lens', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_lens' ) );
 456+
450457 return $params;
451458 }
452459
 460+ /**
 461+ * Includes the JavaScript required for the timeline and eventline formats.
 462+ *
 463+ * @since 1.6
 464+ */
 465+ protected function includeJS( $timeline, $map ) {
 466+ global $srfgScriptPath, $egGoogleMapsKey;
 467+
 468+ $ExhibitScriptSrc1 = '<script type="text/javascript" src="' . $srfgScriptPath . '/Exhibit/exhibit/exhibit-api.js?autoCreate=false&safe=true&bundle=false';
 469+ if ( $timeline ) $ExhibitScriptSrc1 .= '&views=timeline';
 470+ if ( $map ) $ExhibitScriptSrc1 .= '&gmapkey=' . $egGoogleMapsKey;
 471+ $ExhibitScriptSrc1 .= '"></script>';
 472+
 473+ SMWOutputs::requireHeadItem( 'EXHIBIT1', $ExhibitScriptSrc1 ); // include Exhibit API
 474+
 475+ // MediaWiki 1.17 introduces the Resource Loader.
 476+ $realFunction = array( 'SMWOutputs', 'requireResource' );
 477+ if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) && is_callable( $realFunction ) ) {
 478+ SMWOutputs::requireResource( 'ext.srf.exhibit' );
 479+ }
 480+ else {
 481+ $ExhibitScriptSrc2 = '<script type="text/javascript" src="' . $srfgScriptPath . '/Exhibit/SRF_Exhibit.js"></script>';
 482+ $CSSSrc = '<link rel="stylesheet" type="text/css" href="' . $srfgScriptPath . '/Exhibit/SRF_Exhibit.css"></link>';
 483+
 484+ SMWOutputs::requireHeadItem( 'CSS', $CSSSrc ); // include CSS
 485+ SMWOutputs::requireHeadItem( 'EXHIBIT2', $ExhibitScriptSrc2 ); // includes javascript overwriting the Exhibit start-up functions
 486+ SMWOutputs::requireHeadItem( 'SOURCES' . $smwgIQRunningNumber, $sourcesrc );// include sources variable
 487+ }
 488+ }
 489+
 490+ /**
 491+ * Register the resource modules used by this result printer.
 492+ *
 493+ * @since 1.6
 494+ */
 495+ public static function registerResourceModules() {
 496+ global $wgResourceModules, $srfgScriptPath;
 497+
 498+ $moduleTemplate = array(
 499+ 'localBasePath' => dirname( __FILE__ ),
 500+ 'remoteBasePath' => $srfgScriptPath . '/Exhibit',
 501+ 'group' => 'ext.srf'
 502+ );
 503+
 504+ $wgResourceModules['ext.srf.exhibit'] = $moduleTemplate + array(
 505+ 'scripts' => array( 'SRF_Exhibit.js' ),
 506+ 'styles' => array( 'SRF_Exhibit.css' ),
 507+ 'dependencies' => array( 'mediawiki.legacy.wikibits' )
 508+ );
 509+ }
 510+
453511 }
Index: trunk/extensions/SemanticResultFormats/Timeline/SRF_Timeline.php
@@ -124,7 +124,7 @@
125125
126126 // MediaWiki 1.17 introduces the Resource Loader.
127127 $realFunction = array( 'SMWOutputs', 'requireResource' );
128 - if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) && is_callable( $realFunction ) ) {
 128+ if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) && is_callable( $realFunction ) ) {
129129 SMWOutputs::requireResource( 'ext.srf.timeline' );
130130 }
131131 else {