| Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_TestStore.php |
| — | — | @@ -0,0 +1,251 @@ |
| | 2 | +<?php |
| | 3 | +/** |
| | 4 | + * Test implementation of SMW's storage abstraction layer. |
| | 5 | + * |
| | 6 | + * @author Markus Krötzsch |
| | 7 | + */ |
| | 8 | + |
| | 9 | +global $smwgIP; |
| | 10 | +require_once( "$smwgIP/includes/storage/SMW_Store.php" ); |
| | 11 | +require_once( "$smwgIP/includes/SMW_Datatype.php" ); |
| | 12 | +require_once( "$smwgIP/includes/SMW_DataValue.php" ); |
| | 13 | + |
| | 14 | +/** |
| | 15 | + * Storage access class for testing purposes. No persitent storage is implemented, but |
| | 16 | + * all methods return non-empty result sets that can be used for testing purposes. |
| | 17 | + */ |
| | 18 | +class SMWTestStore extends SMWStore { |
| | 19 | + |
| | 20 | +///// Reading methods ///// |
| | 21 | + |
| | 22 | + function getSpecialValues(Title $subject, $specialprop, $requestoptions = NULL) { |
| | 23 | + // TODO |
| | 24 | + if ($specialprop === SMW_SP_HAS_CATEGORY) { // category membership |
| | 25 | + return array(); |
| | 26 | + } elseif ($specialprop === SMW_SP_REDIRECTS_TO) { |
| | 27 | + return array(); // TODO: any better idea? |
| | 28 | + } elseif ($specialprop === SMW_SP_HAS_TYPE) { |
| | 29 | + return array(); |
| | 30 | + } else { |
| | 31 | + return array(); |
| | 32 | + } |
| | 33 | + } |
| | 34 | + |
| | 35 | + function getSpecialSubjects($specialprop, $value, $requestoptions = NULL) { |
| | 36 | + if ($specialprop === SMW_SP_HAS_CATEGORY) { // category membership |
| | 37 | + if ( !($value instanceof Title) || ($value->getNamespace() != NS_CATEGORY) ) { |
| | 38 | + return array(); |
| | 39 | + } |
| | 40 | + return $this->getTestTitles($requestoptions); |
| | 41 | + } elseif ($specialprop === SMW_SP_REDIRECTS_TO) { // redirections |
| | 42 | + return array(); // TODO: any better idea? |
| | 43 | + } elseif ($specialprop === SMW_SP_HAS_TYPE) { // redirections |
| | 44 | + return $this->getTestTitles($requestoptions, SMW_NS_ATTRIBUTE); |
| | 45 | + } else { |
| | 46 | + return $this->getTestTitles($requestoptions); |
| | 47 | + } |
| | 48 | + } |
| | 49 | + |
| | 50 | + |
| | 51 | + function getAttributeValues(Title $subject, Title $attribute, $requestoptions = NULL) { |
| | 52 | + // TODO |
| | 53 | + return array(); |
| | 54 | + } |
| | 55 | + |
| | 56 | + function getAttributeSubjects(Title $attribute, SMWDataValue $value, $requestoptions = NULL) { |
| | 57 | + if ( !$value->isValid() ) { |
| | 58 | + return array(); |
| | 59 | + } |
| | 60 | + return $this->getTestTitles($requestoptions); |
| | 61 | + } |
| | 62 | + |
| | 63 | + function getAllAttributeSubjects(Title $attribute, $requestoptions = NULL) { |
| | 64 | + return $this->getTestTitles($requestoptions); |
| | 65 | + } |
| | 66 | + |
| | 67 | + function getAttributes(Title $subject, $requestoptions = NULL) { |
| | 68 | + if ( ($requestoptions->limit == -1) || $requestoptions->limit > 8) { |
| | 69 | + $requestoptions->limit = 8; |
| | 70 | + } |
| | 71 | + return $this->getTestTitles($requestoptions, SMW_NS_ATTRIBUTE); |
| | 72 | + } |
| | 73 | + |
| | 74 | + function getRelationObjects(Title $subject, Title $relation, $requestoptions = NULL) { |
| | 75 | + return $this->getTestTitles($requestoptions); |
| | 76 | + } |
| | 77 | + |
| | 78 | + function getRelationSubjects(Title $relation, Title $object, $requestoptions = NULL) { |
| | 79 | + return $this->getTestTitles($requestoptions); |
| | 80 | + } |
| | 81 | + |
| | 82 | + function getAllRelationSubjects(Title $relation, $requestoptions = NULL) { |
| | 83 | + return $this->getTestTitles($requestoptions); |
| | 84 | + } |
| | 85 | + |
| | 86 | + function getOutRelations(Title $subject, $requestoptions = NULL) { |
| | 87 | + if ( ($requestoptions->limit == -1) || $requestoptions->limit > 6) { |
| | 88 | + $requestoptions->limit = 6; |
| | 89 | + } |
| | 90 | + return $this->getTestTitles($requestoptions, SMW_NS_RELATION); |
| | 91 | + } |
| | 92 | + |
| | 93 | + function getInRelations(Title $object, $requestoptions = NULL) { |
| | 94 | + return $this->getTestTitles($requestoptions, SMW_NS_RELATION); |
| | 95 | + } |
| | 96 | + |
| | 97 | +///// Writing methods ///// |
| | 98 | + |
| | 99 | + function deleteSubject(Title $subject) { |
| | 100 | + } |
| | 101 | + |
| | 102 | + function updateData(SMWSemanticData $data) { |
| | 103 | + } |
| | 104 | + |
| | 105 | + function changeTitle(Title $oldtitle, Title $newtitle, $keepid = true) { |
| | 106 | + } |
| | 107 | + |
| | 108 | +///// Query answering ///// |
| | 109 | + |
| | 110 | + function getQueryResult(SMWQuery $query) { |
| | 111 | + $prs = $query->getDescription()->getPrintrequests(); // ignore print requests at deepder levels |
| | 112 | + |
| | 113 | + // Here, the actual SQL query building and execution must happen. Loads of work. |
| | 114 | + // For testing purposes, we assume that the outcome is the following array of titles |
| | 115 | + // (the eventual query result format is quite certainly different) |
| | 116 | + $qr = array(Title::newFromText('Angola'), Title::newFromText('Namibia')); |
| | 117 | + |
| | 118 | + // create result by executing print statements for everything that was fetched |
| | 119 | + ///TODO: use limit and offset values |
| | 120 | + $result = new SMWQueryResult($prs); |
| | 121 | + foreach ($qr as $qt) { |
| | 122 | + $row = array(); |
| | 123 | + foreach ($prs as $pr) { |
| | 124 | + switch ($pr->getMode()) { |
| | 125 | + case SMW_PRINT_THIS: |
| | 126 | + $row[] = new SMWResultArray(array($qt), $pr); |
| | 127 | + break; |
| | 128 | + case SMW_PRINT_RELS: |
| | 129 | + $row[] = new SMWResultArray($this->getRelationObjects($qt,$pr->getTitle()), $pr); |
| | 130 | + break; |
| | 131 | + case SMW_PRINT_CATS: |
| | 132 | + $row[] = new SMWResultArray($this->getSpecialValues($qt,SMW_SP_HAS_CATEGORY), $pr); |
| | 133 | + break; |
| | 134 | + case SMW_PRINT_ATTS: |
| | 135 | + ///TODO: respect given datavalue (desired unit), needs extension of getAttributeValues() |
| | 136 | + $row[] = new SMWResultArray($this->getAttributeValues($qt,$pr->getTitle()), $pr); |
| | 137 | + break; |
| | 138 | + } |
| | 139 | + } |
| | 140 | + $result->addRow($row); |
| | 141 | + } |
| | 142 | + |
| | 143 | + return $result; |
| | 144 | + } |
| | 145 | + |
| | 146 | +///// Setup store ///// |
| | 147 | + |
| | 148 | + function setup() { |
| | 149 | + return true; |
| | 150 | + } |
| | 151 | + |
| | 152 | + |
| | 153 | +///// Private methods ///// |
| | 154 | + |
| | 155 | + /** |
| | 156 | + * Return a set of titles as a (random) answer to some request, |
| | 157 | + * but adhere to the given options (limit, sorting) |
| | 158 | + */ |
| | 159 | + private function getTestTitles($requestoptions, $namespace = -1) { |
| | 160 | + $result = array(); |
| | 161 | + for ($i=0; $i<300; $i++) { |
| | 162 | + global $wgContLang; |
| | 163 | + $firstchar = chr(65+($i*17)%25); |
| | 164 | + if ($namespace < 0) { |
| | 165 | + $ns = (($i%5)*2); |
| | 166 | + } else { |
| | 167 | + $ns = $namespace; |
| | 168 | + } |
| | 169 | + if ($ns == 0) { |
| | 170 | + $result[$firstchar . $i] = Title::newFromText($firstchar . $i . '_(Test)', $ns); |
| | 171 | + } else { |
| | 172 | + $result[$firstchar . $i] = Title::newFromText($firstchar . $i . '_(Test' . $wgContLang->getNsText($ns) . ')', $ns); |
| | 173 | + } |
| | 174 | + } |
| | 175 | + // the order of applying the following is crucial: |
| | 176 | + if ($requestoptions !== NULL) { |
| | 177 | + if ($requestoptions->boundary !== NULL) { |
| | 178 | + $newresult = array(); |
| | 179 | + foreach ($result as $key => $r) { |
| | 180 | + if ($requestoptions->ascending) { |
| | 181 | + if ($requestoptions->include_boundary) { |
| | 182 | + $ok = ($r->getText() >= $requestoptions->boundary); |
| | 183 | + } else { |
| | 184 | + $ok = ($r->getText() > $requestoptions->boundary); |
| | 185 | + } |
| | 186 | + } else { |
| | 187 | + if ($requestoptions->include_boundary) { |
| | 188 | + $ok = ($r->getText() <= $requestoptions->boundary); |
| | 189 | + } else { |
| | 190 | + $ok = ($r->getText() < $requestoptions->boundary); |
| | 191 | + } |
| | 192 | + } |
| | 193 | + if ($ok) { |
| | 194 | + $newresult[$key] = $r; |
| | 195 | + } |
| | 196 | + } |
| | 197 | + $result = $newresult; |
| | 198 | + } |
| | 199 | + if ($requestoptions->sort) { |
| | 200 | + if ($requestoptions->ascending) { |
| | 201 | + ksort($result); |
| | 202 | + } else { |
| | 203 | + krsort($result); |
| | 204 | + } |
| | 205 | + } |
| | 206 | + if ($requestoptions->offset > 0) { |
| | 207 | + $result = array_slice($result, $requestoptions->offset); |
| | 208 | + } |
| | 209 | + if ($requestoptions->limit >= 0) { |
| | 210 | + $result = array_slice($result, 0, $requestoptions->limit); |
| | 211 | + } |
| | 212 | + } |
| | 213 | + return array_values($result); |
| | 214 | + } |
| | 215 | + |
| | 216 | + /** |
| | 217 | + * Transform input parameters into a suitable string of additional SQL conditions. |
| | 218 | + * The parameter $valuecol defines the string name of the column to which |
| | 219 | + * value restrictions etc. are to be applied. |
| | 220 | + * @param $requestoptions object with options |
| | 221 | + * @param $valuecol name of SQL column to which conditions apply |
| | 222 | + * @param $labelcol name of SQL column to which string conditions apply, if any |
| | 223 | + */ |
| | 224 | +// protected function getSQLConditions($requestoptions, $valuecol, $labelcol = NULL) { |
| | 225 | +// $sql_conds = ''; |
| | 226 | +// if ($requestoptions !== NULL) { |
| | 227 | +// $db =& wfGetDB( DB_MASTER ); // TODO: use slave? |
| | 228 | +// // <snip> |
| | 229 | +// if ($labelcol !== NULL) { // apply string conditions |
| | 230 | +// foreach ($requestoptions->getStringConditions() as $strcond) { |
| | 231 | +// $string = str_replace(array('_', ' '), array('\_', '\_'), $strcond->string); |
| | 232 | +// switch ($strcond->condition) { |
| | 233 | +// case SMW_STRCOND_PRE: |
| | 234 | +// $string .= '%'; |
| | 235 | +// break; |
| | 236 | +// case SMW_STRCOND_POST: |
| | 237 | +// $string = '%' . $string; |
| | 238 | +// break; |
| | 239 | +// case SMW_STRCOND_MID: |
| | 240 | +// $string = '%' . $string . '%'; |
| | 241 | +// break; |
| | 242 | +// } |
| | 243 | +// $sql_conds .= ' AND ' . $labelcol . ' LIKE ' . $db->addQuotes($string); |
| | 244 | +// } |
| | 245 | +// } |
| | 246 | +// } |
| | 247 | +// return $sql_conds; |
| | 248 | +// } |
| | 249 | + |
| | 250 | +} |
| | 251 | + |
| | 252 | +?> |
| Property changes on: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_TestStore.php |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| 1 | 253 | + native |
| Index: trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php |
| — | — | @@ -67,11 +67,7 @@ |
| 68 | 68 | */ |
| 69 | 69 | $smwgStoreActive = true; |
| 70 | 70 | |
| 71 | | - // initialise main storage (there is no other storage implementation at the moment) |
| 72 | | - // Note: do never access this global variable directly! Use smwfGetStore() instead! |
| 73 | | - require_once($smwgIP . '/includes/storage/SMW_SQLStore.php'); |
| 74 | | - $smwgMasterStore = new SMWSQLStore(); |
| 75 | | - |
| | 71 | + smwfInitStore(); |
| 76 | 72 | smwfInitMessages(); |
| 77 | 73 | |
| 78 | 74 | /**********************************************/ |
| — | — | @@ -373,6 +369,25 @@ |
| 374 | 370 | } |
| 375 | 371 | |
| 376 | 372 | /** |
| | 373 | + * Initialise storage objects based on user settings. Called once during init. |
| | 374 | + */ |
| | 375 | + function smwfInitStore() { |
| | 376 | + global $smwgDefaultStore, $smwgMasterStore, $smwgIP; |
| | 377 | + // initialise main storage (there is no other storage implementation at the moment) |
| | 378 | + // Note: do never access this global variable directly! Use smwfGetStore() instead! |
| | 379 | + switch ($smwgDefaultStore) { |
| | 380 | + case (SMW_STORE_TESTING): |
| | 381 | + require_once($smwgIP . '/includes/storage/SMW_TestStore.php'); |
| | 382 | + $smwgMasterStore = new SMWTestStore(); |
| | 383 | + break; |
| | 384 | + case (SMW_STORE_MWDB): default: |
| | 385 | + require_once($smwgIP . '/includes/storage/SMW_SQLStore.php'); |
| | 386 | + $smwgMasterStore = new SMWSQLStore(); |
| | 387 | + break; |
| | 388 | + } |
| | 389 | + } |
| | 390 | + |
| | 391 | + /** |
| 377 | 392 | * Get a semantic storage object. Currently, it just returns one globally defined |
| 378 | 393 | * object, but the infrastructure allows to set up load balancing and task-dependent |
| 379 | 394 | * use of stores (e.g. using other stores for fast querying than for storing new facts), |
| Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Settings.php |
| — | — | @@ -95,9 +95,19 @@ |
| 96 | 96 | ## |
| 97 | 97 | |
| 98 | 98 | |
| | 99 | + |
| 99 | 100 | // some default settings which usually need no modification |
| 100 | 101 | |
| 101 | 102 | ### |
| | 103 | +# Use another storage backend for Semantic MediaWiki. Use SMW_STORE_TESTING |
| | 104 | +# to run tests without modifying your database at all. |
| | 105 | +## |
| | 106 | +define('SMW_STORE_MWDB',1); // uses the MediaWiki database, needs initialisation via Special:SMWAdmin. |
| | 107 | +define('SMW_STORE_TESTING',2); // dummy store for testing |
| | 108 | +$smwgDefaultStore = SMW_STORE_MWDB; |
| | 109 | +## |
| | 110 | + |
| | 111 | +### |
| 102 | 112 | # Set the following value to "true" if you want to enable support |
| 103 | 113 | # for semantic annotations within templates. For the moment, this |
| 104 | 114 | # will only work if after minor change in your MediaWiki files -- |