r26277 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26276‎ | r26277 | r26278 >
Date:16:48, 1 October 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Initial code for on-demand loading of JavaScripts. Needs some testing. Thanks to Ittay Dror for getting me started on this with his patch for
SMW0.7.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Table.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Timeline.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php
@@ -314,6 +314,7 @@
315315 }
316316 // add tooltip (only if "linking" is enabled)
317317 if ( ($linked !== NULL) && ($linked !==false) && ($this->getTooltip() != '') ) {
 318+ smwfRequireHeadItem(SMW_SCRIPT_TOOLTIP);
318319 $result = '<span class="smwttinline">' . $result . '<span class="smwttcontent">' . $this->getTooltip() . '</span></span>';
319320 }
320321 return $result;
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Table.php
@@ -11,6 +11,7 @@
1212
1313 protected function getHTML($res) {
1414 global $smwgIQRunningNumber;
 15+ smwfRequireHeadItem(SMW_SCRIPT_SORTTABLE);
1516
1617 // print header
1718 if ('broadtable' == $this->mFormat)
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php
@@ -115,6 +115,7 @@
116116 if ( ($linked === NULL)||($linked === false) ) {
117117 return mb_substr($this->m_value, 0, 42) . ' <span class="smwwarning">&hellip;</span> ' . mb_substr($this->m_value, $len - 42);
118118 } else {
 119+ smwfRequireHeadItem(SMW_SCRIPT_TOOLTIP);
119120 return mb_substr($this->m_value, 0, 42) . ' <span class="smwttpersist"> &hellip; <span class="smwttcontent">' . $this->m_value . '</span></span> ' . mb_substr($this->m_value, $len - 42);
120121 }
121122 } else {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Timeline.php
@@ -46,6 +46,7 @@
4747
4848 public function getHTML($res) {
4949 global $smwgIQRunningNumber;
 50+ smwfRequireHeadItem(SMW_SCRIPT_TIMELINE); //make sure JavaScripts are available
5051
5152 $eventline = ('eventline' == $this->mFormat);
5253
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php
@@ -26,6 +26,13 @@
2727 define('SMW_FACTBOX_NONEMPTY', 3);
2828 define('SMW_FACTBOX_SHOWN', 5);
2929
 30+// constants for identifying javascripts as used in smwfRequireHeadItem
 31+define('SMW_SCRIPT_TIMELINE', 1);
 32+define('SMW_SCRIPT_TOOLTIP', 2);
 33+define('SMW_SCRIPT_SORTTABLE', 3);
 34+// HTML items to load in current page; format: "idtext => "<script ... "
 35+$smwgHeadItems = array();
 36+
3037 /**
3138 * Switch on Semantic MediaWiki. This function must be called in LocalSettings.php
3239 * after incldung this file. It is used to ensure that required parameters for SMW
@@ -119,6 +126,7 @@
120127 $wgHooks['ParserBeforeStrip'][] = 'smwfRegisterInlineQueries'; // a hook for registering the <ask> parser hook
121128 $wgHooks['ArticleFromTitle'][] = 'smwfShowListPage';
122129 $wgHooks['LoadAllMessages'][] = 'smwfLoadAllMessages'; // enable a complete display of all messages when requested by MW
 130+ $wgHooks['ParserAfterTidy'][] = 'smwfInsertHTMLHeaders';
123131
124132 ///// credits (see "Special:Version") /////
125133 $wgExtensionCredits['parserhook'][]= array('name'=>'Semantic&nbsp;MediaWiki', 'version'=>SMW_VERSION, 'author'=>"Klaus&nbsp;Lassleben, Markus&nbsp;Kr&ouml;tzsch, Denny&nbsp;Vrandecic, S&nbsp;Page, and others. Maintained by [http://www.aifb.uni-karlsruhe.de/Forschungsgruppen/WBS/english AIFB Karlsruhe].", 'url'=>'http://ontoworld.org/wiki/Semantic_MediaWiki', 'description' => 'Making your wiki more accessible&nbsp;&ndash; for machines \'\'and\'\' humans. [http://ontoworld.org/wiki/Help:Semantics View online documentation.]');
@@ -140,7 +148,7 @@
141149 /**
142150 * The <ask> parser hook processing part.
143151 */
144 -function smwfProcessInlineQuery($text, $param) {
 152+function smwfProcessInlineQuery($text, $param, &$parser) {
145153 global $smwgQEnabled, $smwgIP;
146154 if ($smwgQEnabled) {
147155 require_once($smwgIP . '/includes/SMW_QueryProcessor.php');
@@ -154,6 +162,50 @@
155163 /***** Header modifications *****/
156164 /**********************************************/
157165
 166+/**
 167+ * Hook function to insert HTML headers into parser output.
 168+ */
 169+function smwfInsertHTMLHeaders(&$parser, &$text) {
 170+ global $smwgHeadItems;
 171+ foreach ($smwgHeadItems as $item) {
 172+ $parser->mOutput->addHeadItem($item);
 173+ }
 174+ $smwgHeadItems = array(); // flush array
 175+ return true;
 176+}
 177+
 178+/**
 179+ * Add a head item (e.g. JavaScript) to the current list of things that
 180+ * SMW will add to the output page. This works only while a page is parsed.
 181+ * The $id can be one of SMW's internal numerical IDs for builtin scripts,
 182+ * or a non-numerical tag that uniquely identifies this item, followed by
 183+ * the full HTML-text $item that is to be added.
 184+ */
 185+function smwfRequireHeadItem($id, $item='') {
 186+ global $smwgHeadItems;
 187+ if (is_numeric($id)) { //builtin head items
 188+ global $smwgScriptPath;
 189+ // Note: currently all scripts need the sorttable-script to work, and some tooltips require the timeline.
 190+ // This is bad and will change.
 191+ switch ($id) {
 192+ case SMW_SCRIPT_TIMELINE:
 193+ smwfRequireHeadItem(SMW_SCRIPT_SORTTABLE);
 194+ $smwgHeadItems['smw_tl'] = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SimileTimeline/timeline-api.js"></script>';
 195+ $smwgHeadItems['smw_tlhelper'] = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_timeline.js"></script>';
 196+ return;
 197+ case SMW_SCRIPT_TOOLTIP:
 198+ smwfRequireHeadItem(SMW_SCRIPT_TIMELINE);
 199+ $smwgHeadItems['smw_tt'] = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_tooltip.js"></script>';
 200+ return;
 201+ case SMW_SCRIPT_SORTTABLE:
 202+ $smwgHeadItems['smw_st'] ='<script type="text/javascript" id="SMW_sorttable_script_inclusion" src="' . $smwgScriptPath . '/skins/SMW_sorttable.js"></script>';
 203+ return;
 204+ }
 205+ } else { // custom head items
 206+ $smwgHeadItems[$id] = $item;
 207+ }
 208+}
 209+
158210 /**
159211 * This method is in charge of inserting additional CSS, JScript, and meta tags
160212 * into the html header of each page. It is either called after initialising wgout
@@ -169,20 +221,31 @@
170222 global $smwgArticleHeadersInPlace; // record whether article name specific headers are already there
171223 global $smwgScriptPath;
172224
 225+ global $smwgHeadItems;
 226+
 227+ // Fallback: add scripts to output if not done already (should happen only if we are
 228+ // not using a parser, e.g on special pages.
 229+ $i = 0;
 230+ foreach ($smwgHeadItems as $item) {
 231+ $out->addHeadItem("smw_$i", $item);
 232+ $i++;
 233+ }
 234+ $smwgHeadItems = array(); // flush array
 235+
173236 if (!$smwgHeadersInPlace) {
174 - $sortTableScript = '<script type="text/javascript" id="SMW_sorttable_script_inclusion" src="' . $smwgScriptPath . '/skins/SMW_sorttable.js"></script>';
175 - // The above id is essential for the JavaScript to find out the $smwgScriptPath to
176 - // include images. Changes in the above must always be coordinated with the script!
177 - $out->addScript($sortTableScript);
 237+// $sortTableScript = '<script type="text/javascript" id="SMW_sorttable_script_inclusion" src="' . $smwgScriptPath . '/skins/SMW_sorttable.js"></script>';
 238+// // The above id is essential for the JavaScript to find out the $smwgScriptPath to
 239+// // include images. Changes in the above must always be coordinated with the script!
 240+// $out->addScript($sortTableScript);
178241
179 - $toolTipScript = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_tooltip.js"></script>';
180 - $out->addScript($toolTipScript);
 242+// $toolTipScript = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_tooltip.js"></script>';
 243+// $out->addScript($toolTipScript);
181244
182245 // TODO: we should rather have a script that only pulls the whole Timeline on demand, if possible
183 - $TimelineScript = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SimileTimeline/timeline-api.js"></script>';
184 - $SMWTimelineScript = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_timeline.js"></script>';
185 - $out->addScript($TimelineScript);
186 - $out->addScript($SMWTimelineScript);
 246+// $TimelineScript = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SimileTimeline/timeline-api.js"></script>';
 247+// $SMWTimelineScript = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_timeline.js"></script>';
 248+// $out->addScript($TimelineScript);
 249+// $out->addScript($SMWTimelineScript);
187250
188251 // Also we add a custom CSS file for our needs
189252 $customCssUrl = $smwgScriptPath . '/skins/SMW_custom.css';
@@ -416,6 +479,7 @@
417480 */
418481 function smwfEncodeMessages($msgarray) {
419482 if (count($msgarray) > 0) {
 483+ smwfRequireHeadItem(SMW_SCRIPT_TOOLTIP);
420484 $msgs = implode(' ', $msgarray);
421485 return '<span class="smwttpersist"><span class="smwtticon">warning.png</span><span class="smwttcontent">' . $msgs . '</span></span>';
422486 } else {

Status & tagging log