r45890 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r45889‎ | r45890 | r45891 >
Date:12:25, 19 January 2009
Author:mkroetzsch
Status:deferred
Tags:
Comment:
various PHP runtime optimizations based on results of xdebug profiling for inline query processing
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Property.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_WikiPage.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2_Queries.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php
@@ -27,6 +27,7 @@
2828 protected $m_data; // data entries specifyin gwhat was requested (mixed type)
2929 protected $m_typeid = false; // id of the datatype of the printed objects, if applicable
3030 protected $m_outputformat; // output format string for formatting results, if applicable
 31+ protected $m_hash = false; // cache your hash (currently useful since SMWQueryResult accesses the hash many times, might be dropped at some point)
3132
3233 /**
3334 * Create a print request.
@@ -140,14 +141,16 @@
141142 * labels.
142143 */
143144 public function getHash() {
144 - $hash = $this->m_mode . ':' . $this->m_label . ':';
145 - if ($this->m_data instanceof Title) {
146 - $hash .= $this->m_data->getPrefixedText() . ':';
147 - } elseif ($this->m_data instanceof SMWDataValue) {
148 - $hash .= $this->m_data->getHash() . ':';
 145+ if ($this->m_hash === false) {
 146+ $this->m_hash = $this->m_mode . ':' . $this->m_label . ':';
 147+ if ($this->m_data instanceof Title) {
 148+ $this->m_hash .= $this->m_data->getPrefixedText() . ':';
 149+ } elseif ($this->m_data instanceof SMWDataValue) {
 150+ $this->m_hash .= $this->m_data->getHash() . ':';
 151+ }
 152+ $this->m_hash .= $this->m_outputformat . ':';
149153 }
150 - $hash .= $this->m_outputformat . ':';
151 - return $hash;
 154+ return $this->m_hash;
152155 }
153156
154157 /**
@@ -297,8 +300,8 @@
298301 /**
299302 * A dummy description that describes any object. Corresponds to
300303 * owl:thing, the class of all abstract objects. Note that it is
301 - * not used for datavalues of attributes in order to support type
302 - * hinting in the API: descriptions of data are always
 304+ * not used for datavalues of attributes in order to support type
 305+ * hinting in the API: descriptions of data are always
303306 * SMWValueDescription objects.
304307 * @ingroup SMWQuery
305308 */
@@ -322,7 +325,7 @@
323326
324327 /**
325328 * Description of a single class as given by a wiki category, or of a disjunction
326 - * of such classes. Corresponds to (disjunctions of) atomic classes in OWL and
 329+ * of such classes. Corresponds to (disjunctions of) atomic classes in OWL and
327330 * to (unions of) classes in RDF.
328331 * @ingroup SMWQuery
329332 */
@@ -405,7 +408,7 @@
406409
407410
408411 /**
409 - * Description of a single class as described by a concept page in the wiki. Corresponds to
 412+ * Description of a single class as described by a concept page in the wiki. Corresponds to
410413 * classes in (the EL fragment of) OWL DL, and to some extent to tree-shaped queries in SPARQL.
411414 * @ingroup SMWQuery
412415 */
@@ -486,10 +489,10 @@
487490 /**
488491 * Description of one data value, or of a range of data values.
489492 *
490 - * Technically this usually corresponds to nominal predicates or to unary
491 - * concrete domain predicates in OWL which are parametrised by one constant
 493+ * Technically this usually corresponds to nominal predicates or to unary
 494+ * concrete domain predicates in OWL which are parametrised by one constant
492495 * from the concrete domain.
493 - * In RDF, concrete domain predicates that define ranges (like "greater or
 496+ * In RDF, concrete domain predicates that define ranges (like "greater or
494497 * equal to") are not directly available.
495498 * @ingroup SMWQuery
496499 */
@@ -548,8 +551,8 @@
549552
550553 /**
551554 * Description of an ordered list of SMWDescription objects, used as
552 - * values for some n-ary property. NULL values are to be used for
553 - * unspecifed values. Corresponds to the built-in support for n-ary
 555+ * values for some n-ary property. NULL values are to be used for
 556+ * unspecifed values. Corresponds to the built-in support for n-ary
554557 * properties, i.e. can be viewed as a macro in OWL and RDF.
555558 * @ingroup SMWQuery
556559 */
@@ -916,7 +919,7 @@
917920 * fits another (sub)description.
918921 *
919922 * Corresponds to existential quatification ("some" restriction) on concrete properties
920 - * in OWL. In conjunctive queries (OWL) and SPARQL (RDF), it is represented by using
 923+ * in OWL. In conjunctive queries (OWL) and SPARQL (RDF), it is represented by using
921924 * variables in the object part of such properties.
922925 * @ingroup SMWQuery
923926 */
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2_Queries.php
@@ -304,8 +304,9 @@
305305 $cats = $this->m_store->getPropertyValues($qt,SMWPropertyValue::makeProperty('_INST'));
306306 }
307307 $found = '0';
 308+ $prkey = $pr->getData()->getDBkey();
308309 foreach ($cats as $cat) {
309 - if ($cat->getDBkey() == $pr->getData()->getDBkey()) {
 310+ if ($cat->getDBkey() == $prkey) {
310311 $found = '1';
311312 break;
312313 }
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php
@@ -114,7 +114,6 @@
115115 function getSemanticData($subject, $filter = false) {
116116 wfProfileIn("SMWSQLStore2::getSemanticData (SMW)");
117117 SMWSQLStore2::$in_getSemanticData++;
118 - $db =& wfGetDB( DB_SLAVE );
119118
120119 if ( $subject instanceof Title ) {
121120 $sid = $this->getSMWPageID($subject->getDBkey(),$subject->getNamespace(),$subject->getInterwiki());
@@ -156,6 +155,10 @@
157156 $this->m_sdstate[$sid] = $this->m_sdstate[$sid] | $tasks;
158157 $tasks = $newtasks;
159158 }
 159+
 160+ if ($tasks != 0) { // fetch DB handler only when really needed!
 161+ $db =& wfGetDB( DB_SLAVE );
 162+ }
160163 if ( (count($this->m_semdata) > 20) && (SMWSQLStore2::$in_getSemanticData == 1) ) {
161164 // prevent memory leak;
162165 // It is not so easy to find the sweet spot between cache size and performance gains (both memory and time),
@@ -333,18 +336,7 @@
334337 */
335338 function getPropertyValues($subject, SMWPropertyValue $property, $requestoptions = NULL, $outputformat = '') {
336339 wfProfileIn("SMWSQLStore2::getPropertyValues (SMW)");
337 - if ($subject !== NULL) {
338 - $sid = $this->getSMWPageID($subject->getDBkey(), $subject->getNamespace(),$subject->getInterwiki());
339 - } else {
340 - $sid = 0;
341 - }
342 - $pid = $this->getSMWPropertyID($property);
343 - if ( ( ($sid == 0) && ($subject !== NULL) ) || ($pid == 0)) {
344 - wfProfileOut("SMWSQLStore2::getPropertyValues (SMW)");
345 - return array();
346 - }
347 -
348 - if ($sid != 0) { // subject given, use semantic data cache:
 340+ if ($subject !== NULL) { // subject given, use semantic data cache:
349341 $sd = $this->getSemanticData($subject,array($property->getTypeID()));
350342 $result = $this->applyRequestOptions($sd->getPropertyValues($property),$requestoptions);
351343 if ($outputformat != '') { // reformat cached values
@@ -357,6 +349,11 @@
358350 $result = $newres;
359351 }
360352 } else { // no subject given, get all values for the given property
 353+ $pid = $this->getSMWPropertyID($property);
 354+ if ( $pid == 0 ) {
 355+ wfProfileOut("SMWSQLStore2::getPropertyValues (SMW)");
 356+ return array();
 357+ }
361358 $db =& wfGetDB( DB_SLAVE );
362359 $result = array();
363360 $mode = SMWSQLStore2::getStorageMode($property->getTypeID());
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Property.php
@@ -49,7 +49,8 @@
5050 /// If the property is associated with a wikipage, it is stored here. Otherwise NULL.
5151 protected $m_wikipage;
5252
53 - private $m_typevalue; // once calculated, remember the type of this property
 53+ private $prop_typevalue; // once calculated, remember the type of this property
 54+ private $prop_typeid; // once calculated, remember the type of this property
5455
5556 /**
5657 * Static function for creating a new property object from a
@@ -85,7 +86,8 @@
8687 * @todo Accept/enforce property namespace.
8788 */
8889 protected function parseUserValue($value) {
89 - $this->m_typevalue = NULL;
 90+ $this->prop_typevalue = NULL;
 91+ $this->prop_typeid = NULL;
9092 if ($this->m_caption === false) { // always use this as caption
9193 $this->m_caption = $value;
9294 }
@@ -109,7 +111,8 @@
110112 * internal property id accordingly.
111113 */
112114 protected function parseDBkeys($args) {
113 - $this->m_typevalue = NULL;
 115+ $this->prop_typevalue = NULL;
 116+ $this->prop_typeid = NULL;
114117 SMWPropertyValue::initProperties();
115118 if ($args[0]{0} == '_') { // internal id, use as is (and hope it is still known)
116119 $this->m_propertyid = $args[0];
@@ -221,7 +224,7 @@
222225 */
223226 public function getTypesValue() {
224227 global $smwgPDefaultType;
225 - if ($this->m_typevalue !== NULL) return $this->m_typevalue;
 228+ if ($this->prop_typevalue !== NULL) return $this->prop_typevalue;
226229 if (!$this->isValid()) { // errors in property, return invalid types value with same errors
227230 $result = SMWDataValueFactory::newTypeIDValue('__typ');
228231 $result->setXSDValue('__err');
@@ -247,7 +250,7 @@
248251 $result->setXSDValue('_str');
249252 }
250253 }
251 - $this->m_typevalue = $result;
 254+ $this->prop_typevalue = $result;
252255 return $result;
253256 }
254257
@@ -255,8 +258,11 @@
256259 * Quickly get the type id of some property without necessarily making another datavalue.
257260 */
258261 public function getTypeID() {
259 - $type = $this->getTypesValue();
260 - return $type->isUnary()?$type->getXSDValue():'__nry';
 262+ if ($this->prop_typeid === NULL) {
 263+ $type = $this->getTypesValue();
 264+ $this->prop_typeid = $type->isUnary()?end($type->getDBkeys()):'__nry';
 265+ }
 266+ return $this->prop_typeid;
261267 }
262268
263269 /**
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_WikiPage.php
@@ -20,7 +20,6 @@
2121 */
2222 class SMWWikiPageValue extends SMWDataValue {
2323
24 - protected $m_value = ''; // the raw string passed to that datavalue, rough version of prefixedtext
2524 protected $m_textform = ''; // the isolated title as text
2625 protected $m_dbkeyform = ''; // the isolated title in DB form
2726 protected $m_interwiki = ''; // interwiki prefix or '', actually stored in SMWSQLStore2
@@ -77,10 +76,17 @@
7877 protected function parseUserValue($value) {
7978 $value = ltrim(rtrim($value,' ]'),' ['); // support inputs like " [[Test]] "
8079 if ($value != '') {
81 - $this->m_value = $value;
82 - $this->m_title = NULL;
83 - $this->m_dbkeyform = NULL;
84 - if ($this->getTitle() !== NULL) {
 80+ $this->m_title = Title::newFromText($value, $this->m_fixNamespace);
 81+ ///TODO: Escape the text so users can see any punctuation problems (bug 11666).
 82+ if ($this->m_title === NULL) {
 83+ wfLoadExtensionMessages('SemanticMediaWiki');
 84+ $this->addError(wfMsgForContent('smw_notitle', $value));
 85+ } elseif ( ($this->m_fixNamespace != NS_MAIN) &&
 86+ ($this->m_fixNamespace != $this->m_title->getNamespace()) ) {
 87+ wfLoadExtensionMessages('SemanticMediaWiki');
 88+ $this->addError(wfMsgForContent('smw_wrong_namespace', $wgContLang->getNsText($this->m_fixNamespace)));
 89+ }
 90+ if ($this->m_title !== NULL) {
8591 $this->m_textform = $this->m_title->getText();
8692 $this->m_dbkeyform = $this->m_title->getDBkey();
8793 $this->m_interwiki = $this->m_title->getInterwiki();
@@ -91,7 +97,7 @@
9298 if ($this->m_caption === false) {
9399 $this->m_caption = $value;
94100 }
95 - } // else: no action, errors are reported by getTitle()
 101+ }
96102 } else {
97103 wfLoadExtensionMessages('SemanticMediaWiki');
98104 $this->addError(wfMsgForContent('smw_notitle', $value));
@@ -102,49 +108,40 @@
103109 }
104110
105111 protected function parseDBkeys($args) {
106 - global $wgContLang;
107112 $this->m_dbkeyform = $args[0];
108113 $this->m_namespace = array_key_exists(1,$args)?$args[1]:$this->m_fixNamespace;
109114 $this->m_interwiki = array_key_exists(2,$args)?$args[2]:'';
110115 $this->m_sortkey = array_key_exists(3,$args)?$args[3]:'';
111116 $this->m_textform = str_replace('_', ' ', $this->m_dbkeyform);
112 - if ($this->m_interwiki == '') {
113 - $this->m_title = Title::makeTitle($this->m_namespace, $this->m_dbkeyform);
114 - $this->m_prefixedtext = $this->m_title->getPrefixedText();
115 - } else { // interwiki title objects must be built from full input texts
116 - $nstext = $wgContLang->getNSText($this->m_namespace);
117 - $this->m_prefixedtext = $this->m_interwiki . ($this->m_interwiki != ''?':':'') .
118 - $nstext . ($nstext != ''?':':'') . $this->m_textform;
119 - $this->m_title = Title::newFromText($this->m_prefixedtext);
120 - }
121 - $this->m_caption = $this->m_prefixedtext;
122 - $this->m_value = $this->m_prefixedtext;
123117 $this->m_id = false;
 118+ $this->m_title = NULL;
 119+ $this->m_prefixedtext = false;
 120+ $this->m_caption = false;
124121 if ( ($this->m_fixNamespace != NS_MAIN) && ( $this->m_fixNamespace != $this->m_namespace) ) {
125122 wfLoadExtensionMessages('SemanticMediaWiki');
126 - $this->addError(wfMsgForContent('smw_notitle', $this->m_caption));
 123+ $this->addError(wfMsgForContent('smw_notitle', $this->getPrefixedText()));
127124 }
128125 }
129126
130127 public function getShortWikiText($linked = NULL) {
131128 $this->unstub();
132 - if ( ($linked === NULL) || ($linked === false) || (!$this->isValid()) || ($this->m_caption == '') ) {
133 - return $this->m_caption;
 129+ if ( ($linked === NULL) || ($linked === false) || (!$this->isValid()) || ($this->m_caption === '') ) {
 130+ return $this->getCaption();
134131 } else {
135 - return '[[:' . str_replace("'", ''', $this->m_prefixedtext) .
136 - ($this->m_fragment?"#$this->m_fragment":'') . '|' . $this->m_caption . ']]';
 132+ return '[[:' . str_replace("'", ''', $this->getPrefixedText()) .
 133+ ($this->m_fragment?"#$this->m_fragment":'') . '|' . $this->getCaption() . ']]';
137134 }
138135 }
139136
140137 public function getShortHTMLText($linker = NULL) {
141138 $this->unstub();
142 - if ( ($linker === NULL) || (!$this->isValid()) || ($this->m_caption == '') ) {
143 - return htmlspecialchars($this->m_caption);
 139+ if ( ($linker === NULL) || (!$this->isValid()) || ($this->m_caption === '') ) {
 140+ return htmlspecialchars($this->getCaption());
144141 } else {
145142 if ($this->getNamespace() == NS_MEDIA) { /// NOTE: this extra case is indeed needed
146 - return $linker->makeMediaLinkObj($this->getTitle(), $this->m_caption);
 143+ return $linker->makeMediaLinkObj($this->getTitle(), $this->getCaption());
147144 } else {
148 - return $linker->makeLinkObj($this->getTitle(), $this->m_caption);
 145+ return $linker->makeLinkObj($this->getTitle(), $this->getCaption());
149146 }
150147 }
151148 }
@@ -160,11 +157,11 @@
161158 return $this->getErrorText();
162159 }
163160 if ( ($linked === NULL) || ($linked === false) ) {
164 - return $this->m_prefixedtext;
 161+ return $this->getPrefixedText();
165162 } elseif ($this->m_namespace == NS_IMAGE) { // embed images instead of linking to their page
166 - return '[[' . str_replace("'", ''', $this->m_prefixedtext) . '|' . $this->m_textform . '|frameless|border|text-top]]';
 163+ return '[[' . str_replace("'", ''', $this->getPrefixedText()) . '|' . $this->m_textform . '|frameless|border|text-top]]';
167164 } else { // this takes care of all other cases, esp. it is right for Media:
168 - return '[[:' . str_replace("'", ''', $this->m_prefixedtext) . '|' . $this->m_textform . ']]';
 165+ return '[[:' . str_replace("'", ''', $this->getPrefixedText()) . '|' . $this->m_textform . ']]';
169166 }
170167 }
171168
@@ -174,7 +171,7 @@
175172 return $this->getErrorText();
176173 }
177174 if ($linker === NULL) {
178 - return htmlspecialchars($this->m_prefixedtext);
 175+ return htmlspecialchars($this->getPrefixedText());
179176 } else {
180177 if ($this->getNamespace() == NS_MEDIA) { // this extra case is really needed
181178 return $linker->makeMediaLinkObj($this->getTitle(), $this->m_textform);
@@ -195,16 +192,16 @@
196193 return $this->getText();
197194 } elseif ($this->m_namespace == NS_CATEGORY) {
198195 // escape to enable use in links; todo: not generally required/suitable :-/
199 - return ':' . $this->m_prefixedtext;
 196+ return ':' . $this->getPrefixedText();
200197 } else {
201 - return $this->m_prefixedtext;
 198+ return $this->getPrefixedText();
202199 }
203200 }
204201
205202 public function getHash() {
206203 $this->unstub();
207204 if ($this->isValid()) { // assume that XSD value + unit say all
208 - return $this->m_prefixedtext;
 205+ return $this->getPrefixedText();
209206 } else {
210207 return implode("\t", $this->m_errors);
211208 }
@@ -247,39 +244,21 @@
248245
249246 /**
250247 * Return according Title object or NULL if no valid value was set.
251 - * If using a base value, this method also checks whether the given namespace
252 - * is appropriate. Whenever this method sets the title page, it also implements
253 - * error reporting, i.e. the object might become invalid when calling this
254 - * function.
255248 */
256249 public function getTitle() {
257 - global $wgContLang;
258250 $this->unstub();
259 - if ($this->m_title === NULL){
260 - if ($this->m_dbkeyform != '') {
 251+ if ($this->m_title === NULL) {
 252+ if ($this->m_interwiki == '') {
261253 $this->m_title = Title::makeTitle($this->m_namespace, $this->m_dbkeyform);
262 - if ($this->m_title === NULL) { // should not normally happen, but anyway ...
263 - wfLoadExtensionMessages('SemanticMediaWiki');
264 - $this->addError(wfMsgForContent('smw_notitle', $wgContLang->getNsText($this->m_namespace) . ':' . $this->m_dbkeyform));
265 - $this->m_dbkeyform = '';
266 - }
267 - } elseif ($this->m_value != ''){
268 - $this->m_title = Title::newFromText($this->m_value, $this->m_fixNamespace);
269 - ///TODO: Escape the text so users can see any punctuation problems (bug 11666).
270 - if ($this->m_title === NULL) {
271 - wfLoadExtensionMessages('SemanticMediaWiki');
272 - $this->addError(wfMsgForContent('smw_notitle', $this->m_value));
273 - } elseif ( ($this->m_fixNamespace != NS_MAIN) &&
274 - ($this->m_fixNamespace != $this->m_title->getNamespace()) ) {
275 - wfLoadExtensionMessages('SemanticMediaWiki');
276 - $this->addError(wfMsgForContent('smw_wrong_namespace', $wgContLang->getNsText($this->m_fixNamespace)));
277 - }
278 - } else {
279 - wfLoadExtensionMessages('SemanticMediaWiki');
280 - $this->addError(wfMsgForContent('smw_notitle', ''));
281 - return NULL; //not possible to create title from empty string
 254+ } else { // interwiki title objects must be built from full input texts
 255+ $this->m_title = Title::newFromText($this->getPrefixedText());
282256 }
283257 }
 258+ if ($this->m_title === NULL) { // should not normally happen, but anyway ...
 259+ wfLoadExtensionMessages('SemanticMediaWiki');
 260+ $this->addError(wfMsgForContent('smw_notitle', $wgContLang->getNsText($this->m_namespace) . ':' . $this->m_dbkeyform));
 261+ $this->m_dbkeyform = '';
 262+ }
284263 return $this->m_title;
285264 }
286265
@@ -317,17 +296,28 @@
318297 * to use this method in places where only MediaWiki Title keys are allowed.
319298 */
320299 public function getDBkey() {
321 - return $this->getXSDValue();
 300+ $this->unstub();
 301+ return $this->m_dbkeyform;
322302 }
323303
324 - /**
325 - * Get text label for this value.
326 - */
 304+ /// Get text label for this value.
327305 public function getText() {
328306 $this->unstub();
329307 return str_replace('_',' ',$this->m_dbkeyform);
330308 }
331309
 310+ /// Get the prefixed text for this value, including a localised namespace prefix.
 311+ public function getPrefixedText() {
 312+ global $wgContLang;
 313+ $this->unstub();
 314+ if ($this->m_prefixedtext === false) {
 315+ $nstext = $wgContLang->getNSText($this->m_namespace);
 316+ $this->m_prefixedtext = $this->m_interwiki . ($this->m_interwiki != ''?':':'') .
 317+ $nstext . ($nstext != ''?':':'') . $this->m_textform;
 318+ }
 319+ return $this->m_prefixedtext;
 320+ }
 321+
332322 /**
333323 * Get interwiki prefix or empty string.
334324 */
@@ -360,6 +350,11 @@
361351 $this->m_title = $title;
362352 }
363353
 354+ /// Get the (default) caption for this value.
 355+ protected function getCaption() {
 356+ return $this->m_caption !== false?$this->m_caption:$this->getPrefixedText();
 357+ }
 358+
364359 /**
365360 * @deprecated Use setDBkeys()
366361 */

Status & tagging log