r26303 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26302‎ | r26303 | r26304 >
Date:08:54, 2 October 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Make all head items (including CSS and RDF-link) on-demand and use parser caching for each.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Factbox.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Hooks.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)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Settings.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php
@@ -314,7 +314,7 @@
315315 }
316316 // add tooltip (only if "linking" is enabled)
317317 if ( ($linked !== NULL) && ($linked !==false) && ($this->getTooltip() != '') ) {
318 - smwfRequireHeadItem(SMW_SCRIPT_TOOLTIP);
 318+ smwfRequireHeadItem(SMW_HEADER_TOOLTIP);
319319 $result = '<span class="smwttinline">' . $result . '<span class="smwttcontent">' . $this->getTooltip() . '</span></span>';
320320 }
321321 return $result;
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Hooks.php
@@ -33,6 +33,13 @@
3434 SMWFactbox::printFactbox($text);
3535 } else SMWFactbox::clearStorage();
3636
 37+ // add link to RDF to HTML header
 38+ smwfRequireHeadItem('smw_rdf', '<link rel="alternate" type="application/rdf+xml" title="' .
 39+ $parser->getTitle()->getPrefixedText() . '" href="' .
 40+ $parser->getOptions()->getSkin()->makeSpecialUrl(
 41+ 'ExportRDF/' . $parser->getTitle()->getPrefixedText(), 'xmlmime=rdf'
 42+ ) . "\" />");
 43+
3744 return true; // always return true, in order not to stop MW's hook processing!
3845 }
3946
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Factbox.php
@@ -196,6 +196,7 @@
197197 global $wgContLang, $wgServer, $smwgShowFactbox, $smwgStoreActive, $smwgIP;
198198 if (!$smwgStoreActive) return;
199199 wfProfileIn("SMWFactbox::printFactbox (SMW)");
 200+ smwfRequireHeadItem(SMW_HEADER_STYLE);
200201 switch ($smwgShowFactbox) {
201202 case SMW_FACTBOX_HIDDEN:
202203 wfProfileOut("SMWFactbox::printFactbox (SMW)");
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Table.php
@@ -11,7 +11,7 @@
1212
1313 protected function getHTML($res) {
1414 global $smwgIQRunningNumber;
15 - smwfRequireHeadItem(SMW_SCRIPT_SORTTABLE);
 15+ smwfRequireHeadItem(SMW_HEADER_SORTTABLE);
1616
1717 // print header
1818 if ('broadtable' == $this->mFormat)
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_String.php
@@ -115,7 +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);
 119+ smwfRequireHeadItem(SMW_HEADER_TOOLTIP);
120120 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);
121121 }
122122 } else {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Timeline.php
@@ -46,7 +46,7 @@
4747
4848 public function getHTML($res) {
4949 global $smwgIQRunningNumber;
50 - smwfRequireHeadItem(SMW_SCRIPT_TIMELINE); //make sure JavaScripts are available
 50+ smwfRequireHeadItem(SMW_HEADER_TIMELINE); //make sure JavaScripts are available
5151
5252 $eventline = ('eventline' == $this->mFormat);
5353
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php
@@ -3,7 +3,7 @@
44 * Global functions and constants for Semantic MediaWiki.
55 */
66
7 -define('SMW_VERSION','1.0prealpha-4');
 7+define('SMW_VERSION','1.0alpha-1');
88
99 // constants for special properties, used for datatype assignment and storage
1010 define('SMW_SP_HAS_TYPE',1);
@@ -27,10 +27,12 @@
2828 define('SMW_FACTBOX_SHOWN', 5);
2929
3030 // 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 ... "
 31+define('SMW_HEADER_TIMELINE', 1);
 32+define('SMW_HEADER_TOOLTIP', 2);
 33+define('SMW_HEADER_SORTTABLE', 3);
 34+define('SMW_HEADER_STYLE', 4);
 35+
 36+// HTML items to load in current page, use smwfRequireHeadItem to extend
3537 $smwgHeadItems = array();
3638
3739 /**
@@ -40,14 +42,16 @@
4143 * SMW_LocalSettings.php. For readability, this is the only global function that
4244 * does not adhere to the naming conventions.
4345 */
44 -function enableSemantics($namespace = "", $complete = false) {
45 - global $smwgNamespace, $wgExtensionFunctions, $wgHooks;
46 - $smwgNamespace = $namespace;
47 -
 46+function enableSemantics($namespace = '', $complete = false) {
 47+ global $smwgNamespace, $wgExtensionFunctions;
4848 // The dot tells that the domain is not complete. It will be completed
4949 // in the Export since we do not want to create a title object here when
5050 // it is not needed in many cases.
51 - if (!$complete && !($smwgNamespace === '')) $smwgNamespace = ".$smwgNamespace";
 51+ if ( !$complete && ($smwgNamespace !== '') ) {
 52+ $smwgNamespace = '.' . $namespace;
 53+ } else {
 54+ $smwgNamespace = $namespace;
 55+ }
5256 $wgExtensionFunctions[] = 'smwfSetupExtension';
5357 return true;
5458 }
@@ -65,7 +69,7 @@
6670 * the static SMWSemanticData store, and disables printing of the
6771 * factbox, and clearing of the existing data.
6872 * This is a hack to enable parsing of included articles in a save
69 - * way without importing their annotations. Unfortunatelly, there
 73+ * way without importing their annotations. Unfortunately, there
7074 * appears to be no way for finding out whether the current parse
7175 * is the "main" parse, or whether some intro, docu, or whatever
7276 * text is parsed. Using the hook mechanism, we have to rely on
@@ -78,12 +82,12 @@
7983 smwfInitContentMessages();
8084
8185 ///// setup some autoloading /////
82 - $wgAutoloadClasses[SMWResultPrinter] = $smwgIP . '/includes/SMW_QueryPrinter.php';
83 - $wgAutoloadClasses[SMWTableResultPrinter] = $smwgIP . '/includes/SMW_QP_Table.php';
84 - $wgAutoloadClasses[SMWListResultPrinter] = $smwgIP . '/includes/SMW_QP_List.php';
85 - $wgAutoloadClasses[SMWTimelineResultPrinter] = $smwgIP . '/includes/SMW_QP_Timeline.php';
86 - $wgAutoloadClasses[SMWEmbeddedResultPrinter] = $smwgIP . '/includes/SMW_QP_Embedded.php';
87 - $wgAutoloadClasses[SMWTemplateResultPrinter] = $smwgIP . '/includes/SMW_QP_Template.php';
 86+ $wgAutoloadClasses['SMWResultPrinter'] = $smwgIP . '/includes/SMW_QueryPrinter.php';
 87+ $wgAutoloadClasses['SMWTableResultPrinter'] = $smwgIP . '/includes/SMW_QP_Table.php';
 88+ $wgAutoloadClasses['SMWListResultPrinter'] = $smwgIP . '/includes/SMW_QP_List.php';
 89+ $wgAutoloadClasses['SMWTimelineResultPrinter'] = $smwgIP . '/includes/SMW_QP_Timeline.php';
 90+ $wgAutoloadClasses['SMWEmbeddedResultPrinter'] = $smwgIP . '/includes/SMW_QP_Embedded.php';
 91+ $wgAutoloadClasses['SMWTemplateResultPrinter'] = $smwgIP . '/includes/SMW_QP_Template.php';
8892
8993 ///// register specials /////
9094 $wgAutoloadClasses['SMWAskPage'] = $smwgIP . '/specials/AskSpecial/SMW_SpecialAsk.php';
@@ -113,20 +117,16 @@
114118 require_once($smwgIP . '/includes/SMW_Hooks.php');
115119 require_once($smwgIP . '/includes/SMW_RefreshTab.php');
116120
117 - if ($smwgEnableTemplateSupport===true) {
118 - $wgHooks['InternalParseBeforeLinks'][] = 'smwfParserHook'; //patch required;
119 - } else {
120 - $wgHooks['ParserAfterStrip'][] = 'smwfParserHook'; //default setting
121 - }
 121+ $wgHooks['InternalParseBeforeLinks'][] = 'smwfParserHook'; // parse annotations
 122+ $wgHooks['ParserBeforeStrip'][] = 'smwfRegisterInlineQueries'; // register the <ask> parser hook
 123+ $wgHooks['ArticleSaveComplete'][] = 'smwfSaveHook'; // store annotations
 124+ $wgHooks['ArticleDelete'][] = 'smwfDeleteHook'; // delete annotations
 125+ $wgHooks['TitleMoveComplete'][]='smwfMoveHook'; // move annotations
 126+ $wgHooks['ParserAfterTidy'][] = 'smwfAddHTMLHeadersParser'; // add items to HTML header during parsing
 127+ $wgHooks['BeforePageDisplay'][]='smwfAddHTMLHeadersOutput'; // add items to HTML header during output
122128
123 - $wgHooks['ArticleSaveComplete'][] = 'smwfSaveHook';
124 - $wgHooks['ArticleDelete'][] = 'smwfDeleteHook';
125 - $wgHooks['TitleMoveComplete'][]='smwfMoveHook';
126 - $wgHooks['BeforePageDisplay'][]='smwfAddHTMLHeader';
127 - $wgHooks['ParserBeforeStrip'][] = 'smwfRegisterInlineQueries'; // a hook for registering the <ask> parser hook
128 - $wgHooks['ArticleFromTitle'][] = 'smwfShowListPage';
129 - $wgHooks['LoadAllMessages'][] = 'smwfLoadAllMessages'; // enable a complete display of all messages when requested by MW
130 - $wgHooks['ParserAfterTidy'][] = 'smwfInsertHTMLHeaders';
 129+ $wgHooks['ArticleFromTitle'][] = 'smwfShowListPage'; // special implementations for property/type articles
 130+ $wgHooks['LoadAllMessages'][] = 'smwfLoadAllMessages'; // complete setup of all messages when requested by MW
131131
132132 ///// credits (see "Special:Version") /////
133133 $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.]');
@@ -163,131 +163,77 @@
164164 /**********************************************/
165165
166166 /**
167 - * Hook function to insert HTML headers into parser output.
 167+ * Add some head items (e.g. JavaScripts) to the current list of things
 168+ * that SMW will add to the returned HTML page. The ID can be one of SMW's
 169+ * SMW_HEADER_... constants, or a string id followed by the actual item
 170+ * that should be added to the output html header. In the first case, the
 171+ * $item parameter should be left empty.
168172 */
169 -function smwfInsertHTMLHeaders(&$parser, &$text) {
 173+function smwfRequireHeadItem($id, $item = '') {
170174 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
 175+ if (is_numeric($id)) {
188176 global $smwgScriptPath;
189177 // Note: currently all scripts need the sorttable-script to work, and some tooltips require the timeline.
190178 // This is bad and will change.
191179 switch ($id) {
192 - case SMW_SCRIPT_TIMELINE:
193 - smwfRequireHeadItem(SMW_SCRIPT_SORTTABLE);
 180+ case SMW_HEADER_TIMELINE:
 181+ smwfRequireHeadItem(SMW_HEADER_STYLE);
 182+ smwfRequireHeadItem(SMW_HEADER_SORTTABLE); /// TODO: make obsolete
194183 $smwgHeadItems['smw_tl'] = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SimileTimeline/timeline-api.js"></script>';
195184 $smwgHeadItems['smw_tlhelper'] = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_timeline.js"></script>';
196185 return;
197 - case SMW_SCRIPT_TOOLTIP:
198 - smwfRequireHeadItem(SMW_SCRIPT_TIMELINE);
 186+ case SMW_HEADER_TOOLTIP:
 187+ smwfRequireHeadItem(SMW_HEADER_STYLE);
 188+ smwfRequireHeadItem(SMW_HEADER_TIMELINE); /// TODO: make obsolete
199189 $smwgHeadItems['smw_tt'] = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_tooltip.js"></script>';
200190 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>';
 191+ case SMW_HEADER_SORTTABLE:
 192+ smwfRequireHeadItem(SMW_HEADER_STYLE);
 193+ $smwgHeadItems['smw_st'] = '<script type="text/javascript" id="SMW_sorttable_script_inclusion" src="' . $smwgScriptPath . '/skins/SMW_sorttable.js"></script>';
203194 return;
 195+ case SMW_HEADER_STYLE:
 196+ global $wgContLang;
 197+ $smwgHeadItems['smw_css'] = '<link rel="stylesheet" type="text/css" media="screen, projection" href="' . $smwgScriptPath . '/skins/SMW_custom.css" />';
 198+ if ($wgContLang->isRTL()) { // right-to-left support
 199+ $smwgHeadItems['smw_cssrtl'] = '<link rel="stylesheet" type="text/css" media="screen, projection" href="' . $smwgScriptPath . '/skins/SMW_custom_rtl.css" />';
 200+ }
 201+ return;
204202 }
205 - } else { // custom head items
 203+ } else { // custom head item
206204 $smwgHeadItems[$id] = $item;
207205 }
208206 }
209207
210 - /**
211 - * This method is in charge of inserting additional CSS, JScript, and meta tags
212 - * into the html header of each page. It is either called after initialising wgout
213 - * (requiring a patch in MediaWiki), or during parsing. Calling it during parsing,
214 - * however, is not sufficient to get the header modifiactions into every page that
215 - * is shipped to a reader, since the parser cache can make parsing obsolete.
216 - *
217 - * $out is the modified OutputPage.
218 - */
219 - function smwfAddHTMLHeader(&$out) {
220 - wfProfileIn('smwfAddHTMLHeader (SMW)');
221 - global $smwgHeadersInPlace; // record whether headers were created already
222 - global $smwgArticleHeadersInPlace; // record whether article name specific headers are already there
223 - global $smwgScriptPath;
 208+/**
 209+ * Hook function to insert HTML headers (CSS, JavaScript, and meta tags) into parser
 210+ * output. This is our preferred method of working off the required scripts, since it
 211+ * exploits parser caching.
 212+ */
 213+function smwfAddHTMLHeadersParser(&$parser, &$text) {
 214+ global $smwgHeadItems;
 215+ foreach ($smwgHeadItems as $key => $item) {
 216+ $parser->mOutput->addHeadItem("\t\t" . $item . "\n", $key);
 217+ }
 218+ $smwgHeadItems = array(); // flush array so that smwfAddHTMLHeader does not take needless actions
 219+ return true;
 220+}
224221
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 -
236 - if (!$smwgHeadersInPlace) {
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);
241 -
242 -// $toolTipScript = '<script type="text/javascript" src="' . $smwgScriptPath . '/skins/SMW_tooltip.js"></script>';
243 -// $out->addScript($toolTipScript);
244 -
245 - // TODO: we should rather have a script that only pulls the whole Timeline on demand, if possible
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);
250 -
251 - // Also we add a custom CSS file for our needs
252 - $customCssUrl = $smwgScriptPath . '/skins/SMW_custom.css';
253 - $out->addLink(array(
254 - 'rel' => 'stylesheet',
255 - 'type' => 'text/css',
256 - 'media' => 'screen, projection',
257 - 'href' => $customCssUrl
258 - ));
259 -
260 - //BEGIN RTL PATCH
261 - global $wgContLang;
262 - if ($wgContLang->isRTL()) {
263 - $customCssUrl = $smwgScriptPath . '/skins/SMW_custom_rtl.css';
264 - $out->addLink(array(
265 - 'rel' => 'stylesheet',
266 - 'type' => 'text/css',
267 - 'media' => 'screen, projection',
268 - 'href' => $customCssUrl
269 - ));
270 - }
271 - //END RTL PATCH
272 - $smwgHeadersInPlace = true;
273 - }
274 -
275 - if ((!$smwgArticleHeadersInPlace) && ($out->mIsarticle) && ($out->mPagetitle!='')) {
276 - global $wgContLang, $wgServer, $wgScript;
277 -
278 - $out->addLink(array(
279 - 'rel' => 'alternate',
280 - 'type' => 'application/rdf+xml',
281 - 'title' => $out->mPagetitle,
282 - 'href' => $wgServer . $wgScript . '/' .
283 - $wgContLang->getNsText(NS_SPECIAL) . ':ExportRDF/' .
284 - str_replace('%2F', "/", urlencode(str_replace(' ', '_', $out->mPagetitle))) . '?xmlmime=rdf'
285 - ));
286 - $smwgArticleHeadersInPlace = true;
287 - }
288 - wfProfileOut('smwfAddHTMLHeader (SMW)');
289 -
290 - return true; // always return true, in order not to stop MW's hook processing!
 222+/**
 223+ * This method is in charge of inserting additional CSS, JavaScript, and meta tags
 224+ * into the HTML header of each page. This method is needed for pages that are not
 225+ * parsed, especially for special pages. All others get their headers with the parser
 226+ * output (exploiting parser caching).
 227+ */
 228+function smwfAddHTMLHeadersOutput(&$out) {
 229+ global $smwgHeadItems;
 230+ // Add scripts to output if not done already (should happen only if we are
 231+ // not using a parser, e.g on special pages).
 232+ foreach ($smwgHeadItems as $key => $item) {
 233+ $out->addHeadItem($key, "\t\t" . $item . "\n");
291234 }
 235+ $smwgHeadItems = array(); // flush array
 236+ return true; // always return true, in order not to stop MW's hook processing!
 237+}
292238
293239 /**********************************************/
294240 /***** namespace settings *****/
@@ -312,9 +258,7 @@
313259 define('SMW_NS_TYPE', $smwgNamespaceIndex+4);
314260 define('SMW_NS_TYPE_TALK', $smwgNamespaceIndex+5);
315261
316 - /// @DEPRECATED
317 - define('SMW_NS_ATTRIBUTE', $smwgNamespaceIndex+2);
318 - define('SMW_NS_ATTRIBUTE_TALK', $smwgNamespaceIndex+3);
 262+ /// For backwards compatibility. Might vanish at some point.
319263 define('SMW_NS_RELATION', $smwgNamespaceIndex);
320264 define('SMW_NS_RELATION_TALK', $smwgNamespaceIndex+1);
321265
@@ -479,7 +423,7 @@
480424 */
481425 function smwfEncodeMessages($msgarray) {
482426 if (count($msgarray) > 0) {
483 - smwfRequireHeadItem(SMW_SCRIPT_TOOLTIP);
 427+ smwfRequireHeadItem(SMW_HEADER_TOOLTIP);
484428 $msgs = implode(' ', $msgarray);
485429 return '<span class="smwttpersist"><span class="smwtticon">warning.png</span><span class="smwttcontent">' . $msgs . '</span></span>';
486430 } else {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Settings.php
@@ -142,16 +142,6 @@
143143 ##
144144
145145 ###
146 -# Set the following value to "true" if you want to enable support
147 -# for semantic annotations within templates. For the moment, this
148 -# will only work if after minor change in your MediaWiki files --
149 -# see INSTALL for details. Enabling this is necessary for normal
150 -# operation.
151 -##
152 -$smwgEnableTemplateSupport = true;
153 -##
154 -
155 -###
156146 # Setting this to true allows to translate all the labels within
157147 # the browser GIVEN that they have interwiki links.
158148 ##

Status & tagging log