r45316 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r45315‎ | r45316 | r45317 >
Date:15:36, 2 January 2009
Author:yaron
Status:deferred
Tags:
Comment:
Helper functions moved to new file, SD_Utils.inc
Modified paths:
  • /trunk/extensions/SemanticDrilldown/includes/SD_GlobalFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticDrilldown/includes/SD_GlobalFunctions.php
@@ -7,7 +7,7 @@
88
99 if (!defined('MEDIAWIKI')) die();
1010
11 -define('SD_VERSION','0.5.2');
 11+define('SD_VERSION','0.5.3');
1212
1313 // constants for special properties
1414 define('SD_SP_HAS_FILTER', 1);
@@ -50,6 +50,7 @@
5151 $wgAutoloadClasses['SDBrowseData'] = $sdgIP . '/specials/SD_BrowseData.php';
5252 $wgSpecialPageGroups['BrowseData'] = 'sd_group';
5353
 54+$wgAutoloadClasses['SDUtils'] = $sdgIP . '/includes/SD_Utils.inc';
5455 $wgAutoloadClasses['SDFilter'] = $sdgIP . '/includes/SD_Filter.php';
5556 $wgAutoloadClasses['SDFilterValue'] = $sdgIP . '/includes/SD_FilterValue.php';
5657 $wgAutoloadClasses['SDAppliedFilter'] = $sdgIP . '/includes/SD_AppliedFilter.php';
@@ -160,10 +161,6 @@
161162 }
162163 }
163164
164 -/**********************************************/
165 -/***** other global helpers *****/
166 -/**********************************************/
167 -
168165 function sdfInitProperties() {
169166 global $sdgContLang;
170167 $sd_props = $sdgContLang->getPropertyLabels();
@@ -179,333 +176,3 @@
180177
181178 return true;
182179 }
183 -
184 -/**
185 - * Based on Semantic Forms' sffCreateProperty()
186 - */
187 -function sdfCreateProperty($property_name) {
188 - if (method_exists('SMWPropertyValue', 'makeProperty'))
189 - return SMWPropertyValue::makeProperty($property_name);
190 - else
191 - return Title::newFromText($property_name, SMW_NS_PROPERTY);
192 -}
193 -
194 -/**
195 - * Based on Semantic Forms' sffGetPropertyName()
196 - */
197 -function sdfGetPropertyName($property) {
198 - if ($property instanceof Title)
199 - return $property->getText();
200 - else // $property instanceof SMWPropertyValue
201 - return $property->getWikiValue();
202 -}
203 -
204 -/**
205 - * Gets a list of the names of all categories in the wiki that aren't
206 - * children of some other category
207 - */
208 -function sdfGetTopLevelCategories() {
209 - $categories = array();
210 - $dbr = wfGetDB( DB_SLAVE );
211 - extract($dbr->tableNames('page', 'categorylinks'));
212 - $cat_ns = NS_CATEGORY;
213 - $sql = "SELECT page_title FROM $page p LEFT OUTER JOIN $categorylinks cl ON p.page_id = cl.cl_from WHERE p.page_namespace = $cat_ns AND cl.cl_to IS NULL";
214 - $res = $dbr->query($sql);
215 - if ($dbr->numRows( $res ) > 0) {
216 - while ($row = $dbr->fetchRow($res)) {
217 - $categories[] = str_replace('_', ' ', $row[0]);
218 - }
219 - }
220 - $dbr->freeResult($res);
221 - return $categories;
222 -}
223 -
224 -/**
225 - * Gets a list of the names of all properties in the wiki
226 - */
227 -function sdfGetSemanticProperties() {
228 - global $smwgContLang;
229 - $smw_namespace_labels = $smwgContLang->getNamespaces();
230 - $all_properties = array();
231 -
232 - // set limit on results - a temporary fix until SMW's getProperties()
233 - // functions stop requiring a limit
234 - global $smwgIP;
235 - include_once($smwgIP . '/includes/storage/SMW_Store.php');
236 - $options = new SMWRequestOptions();
237 - $options->limit = 10000;
238 - $used_properties = smwfGetStore()->getPropertiesSpecial($options);
239 - foreach ($used_properties as $property) {
240 - $property_name = sdfGetPropertyName($property[0]);
241 - $all_properties[$property_name] = $smw_namespace_labels[SMW_NS_PROPERTY];
242 - }
243 - $unused_properties = smwfGetStore()->getUnusedPropertiesSpecial($options);
244 - foreach ($unused_properties as $property) {
245 - $property_name = sdfGetPropertyName($property);
246 - $all_properties[$property_name] = $smw_namespace_labels[SMW_NS_PROPERTY];
247 - }
248 - // remove the special properties of Semantic Drilldown from this list...
249 - global $sdgContLang;
250 - $sd_props = $sdgContLang->getPropertyLabels();
251 - $sd_prop_aliases = $sdgContLang->getPropertyAliases();
252 - foreach (array_keys($all_properties) as $prop_name) {
253 - foreach ($sd_props as $prop => $label) {
254 - if ($prop_name == $label) {
255 - unset($all_properties[$prop_name]);
256 - }
257 - }
258 - foreach ($sd_prop_aliases as $alias => $cur_prop) {
259 - if ($prop_name == $alias) {
260 - unset($all_properties[$prop_name]);
261 - }
262 - }
263 - }
264 -
265 - // sort properties array by the key, which is the property name
266 - ksort($all_properties);
267 - return $all_properties;
268 -}
269 -
270 -/**
271 - *
272 - */
273 -function sdfGetFilters() {
274 - $filters = array();
275 - $filter_ns = SD_NS_FILTER;
276 - $dbr = wfGetDB( DB_SLAVE );
277 - $page = $dbr->tableName( 'page' );
278 - $sql = "SELECT page_title FROM $page
279 - WHERE page_namespace = $filter_ns";
280 - $res = $dbr->query($sql);
281 - while ($row = $dbr->fetchRow($res)) {
282 - $filters[] = $row[0];
283 - }
284 - $dbr->freeResult($res);
285 - return $filters;
286 -}
287 -
288 -/**
289 - * Generic database-access function - gets all the values that a specific
290 - * page points to with a specific property, that also match some other
291 - * constraints
292 - */
293 -function sdfGetValuesForProperty($subject, $subject_namespace, $special_prop, $prop, $object_namespace) {
294 - $store = smwfGetStore();
295 - $subject_title = Title::newFromText($subject, $subject_namespace);
296 -
297 - // we can do this easily if we're using SMW 1.4 or higher
298 - if (class_exists('SMWPropertyValue')) {
299 - $property = SMWPropertyValue::makeProperty($special_prop);
300 - $res = $store->getPropertyValues($subject_title, $property);
301 - // there could be multiple alternate forms
302 - $values = array();
303 - foreach ($res as $prop_val) {
304 - $values[] = html_entity_decode(str_replace('_', ' ', $prop_val->getXSDValue()));
305 - }
306 - return $values;
307 - }
308 -
309 - // otherwise, it's a bit more complicated
310 - global $sdgContLang;
311 -
312 - $sd_props = $sdgContLang->getPropertyLabels();
313 - $values = array();
314 - if (array_key_exists($prop, $sd_props)) {
315 - $property = $sd_props[$prop];
316 - } else {
317 - $property = "";
318 - }
319 - if ($property != '') {
320 - $prop = sdfCreateProperty($property, SMW_NS_PROPERTY);
321 - $prop_vals = $store->getPropertyValues($subject_title, $prop);
322 - foreach ($prop_vals as $prop_val) {
323 - // html_entity_decode() is needed to get around temporary bug in SMWSQLStore2
324 - $values[] = html_entity_decode(str_replace('_', ' ', $prop_val->getXSDValue()));
325 - }
326 - }
327 - // try aliases as well
328 - foreach ($sdgContLang->getPropertyAliases() as $alias => $cur_prop) {
329 - // make sure alias doesn't match actual property name - this
330 - // is an issue for English, since the English-language values
331 - // are used for aliases
332 - if (($alias != $property) && (! $prop instanceof Title) && ($cur_prop == $prop)) {
333 - $prop = sdfCreateProperty($alias, SMW_NS_PROPERTY);
334 - $prop_vals = $store->getPropertyValues($subject_title, $prop);
335 - foreach ($prop_vals as $prop_val) {
336 - // make sure it's in the right namespace
337 - if ($prop_val->getNamespace() == $object_namespace) {
338 - $values[] = $prop_val->getTitle()->getText();
339 - }
340 - }
341 - }
342 - }
343 - return $values;
344 -}
345 -
346 -/**
347 - * Gets all the filters specified for a category.
348 - */
349 -function sdfLoadFiltersForCategory($category) {
350 - $filters = array();
351 - $filter_names = sdfGetValuesForProperty(str_replace(' ', '_', $category), NS_CATEGORY, '_SD_F', SD_SP_HAS_FILTER, SD_NS_FILTER);
352 - foreach ($filter_names as $filter_name) {
353 - $filters[] = SDFilter::load($filter_name);
354 - }
355 - return $filters;
356 -}
357 -
358 -function sdfGetCategoryChildren($category_name, $get_categories, $levels) {
359 - if ($levels == 0) {
360 - return array();
361 - }
362 - $pages = array();
363 - $subcategories = array();
364 - $dbr = wfGetDB( DB_SLAVE );
365 - extract($dbr->tableNames('page', 'categorylinks'));
366 - $cat_ns = NS_CATEGORY;
367 - $query_category = str_replace(' ', '_', $category_name);
368 - $query_category = str_replace("'", "\'", $query_category);
369 - $sql = "SELECT p.page_title, p.page_namespace FROM $categorylinks cl
370 - JOIN $page p on cl.cl_from = p.page_id
371 - WHERE cl.cl_to = '$query_category'\n";
372 - if ($get_categories)
373 - $sql .= "AND p.page_namespace = $cat_ns\n";
374 - $sql .= "ORDER BY cl.cl_sortkey";
375 - $res = $dbr->query($sql);
376 - while ($row = $dbr->fetchRow($res)) {
377 - if ($get_categories) {
378 - $subcategories[] = $row[0];
379 - $pages[] = $row[0];
380 - } else {
381 - if ($row[1] == $cat_ns)
382 - $subcategories[] = $row[0];
383 - else
384 - $pages[] = $row[0];
385 - }
386 - }
387 - $dbr->freeResult($res);
388 - foreach ($subcategories as $subcategory) {
389 - $pages = array_merge($pages, sdfGetCategoryChildren($subcategory, $get_categories, $levels - 1));
390 - }
391 - return $pages;
392 -}
393 -
394 -function sdfMonthToString($month) {
395 - if ($month == 1) {
396 - return wfMsg('january');
397 - } elseif ($month == 2) {
398 - return wfMsg('february');
399 - } elseif ($month == 3) {
400 - return wfMsg('march');
401 - } elseif ($month == 4) {
402 - return wfMsg('april');
403 - } elseif ($month == 5) {
404 - return wfMsg('may');
405 - } elseif ($month == 6) {
406 - return wfMsg('june');
407 - } elseif ($month == 7) {
408 - return wfMsg('july');
409 - } elseif ($month == 8) {
410 - return wfMsg('august');
411 - } elseif ($month == 9) {
412 - return wfMsg('september');
413 - } elseif ($month == 10) {
414 - return wfMsg('october');
415 - } elseif ($month == 11) {
416 - return wfMsg('november');
417 - } else { //if ($month == 12) {
418 - return wfMsg('december');
419 - }
420 -}
421 -
422 -function sdfStringToMonth($str) {
423 - if ($str == wfMsg('january')) {
424 - return 1;
425 - } elseif ($str == wfMsg('february')) {
426 - return 2;
427 - } elseif ($str == wfMsg('march')) {
428 - return 3;
429 - } elseif ($str == wfMsg('april')) {
430 - return 4;
431 - } elseif ($str == wfMsg('may')) {
432 - return 5;
433 - } elseif ($str == wfMsg('june')) {
434 - return 6;
435 - } elseif ($str == wfMsg('july')) {
436 - return 7;
437 - } elseif ($str == wfMsg('august')) {
438 - return 8;
439 - } elseif ($str == wfMsg('september')) {
440 - return 9;
441 - } elseif ($str == wfMsg('october')) {
442 - return 10;
443 - } elseif ($str == wfMsg('november')) {
444 - return 11;
445 - } else { //if ($strmonth == wfMsg('december')) {
446 - return 12;
447 - }
448 -}
449 -
450 -function sdfBooleanToString($bool_value) {
451 - $words_field_name = ($bool_value == true) ? 'smw_true_words' : 'smw_false_words';
452 - $words_array = explode(',', wfMsgForContent($words_field_name));
453 - // go with the value in the array that tends to be "yes" or "no" -
454 - // for SMW 0.7 it's the 2nd word, and for SMW 1.0 it's the 3rd
455 - $index_of_word = 2;
456 - // capitalize first letter of word
457 - if (count($words_array) > $index_of_word) {
458 - $string_value = ucwords($words_array[$index_of_word]);
459 - } elseif (count($words_array) == 0) {
460 - $string_value = $bool_value; // a safe value if no words are found
461 - } else {
462 - $string_value = ucwords($words_array[0]);
463 - }
464 - return $string_value;
465 -}
466 -
467 -/**
468 - * Prints the mini-form contained at the bottom of various pages, that
469 - * allows pages to spoof a normal edit page, that can preview, save,
470 - * etc.
471 - */
472 -function sdfPrintRedirectForm($title, $page_contents, $edit_summary, $is_save, $is_preview, $is_diff, $is_minor_edit, $watch_this) {
473 - $article = new Article($title);
474 - $new_url = $title->getLocalURL('action=submit');
475 - $starttime = wfTimestampNow();
476 - $edittime = $article->getTimestamp();
477 - global $wgUser;
478 - if ( $wgUser->isLoggedIn() )
479 - $token = htmlspecialchars($wgUser->editToken());
480 - else
481 - $token = EDIT_TOKEN_SUFFIX;
482 -
483 - if ($is_save)
484 - $action = "wpSave";
485 - elseif ($is_preview)
486 - $action = "wpPreview";
487 - else // $is_diff
488 - $action = "wpDiff";
489 -
490 - $text =<<<END
491 - <form id="editform" name="editform" method="post" action="$new_url">
492 - <input type="hidden" name="wpTextbox1" id="wpTextbox1" value="$page_contents" />
493 - <input type="hidden" name="wpSummary" value="$edit_summary" />
494 - <input type="hidden" name="wpStarttime" value="$starttime" />
495 - <input type="hidden" name="wpEdittime" value="$edittime" />
496 - <input type="hidden" name="wpEditToken" value="$token" />
497 - <input type="hidden" name="$action" />
498 -
499 -END;
500 - if ($is_minor_edit)
501 - $text .= ' <input type="hidden" name="wpMinoredit">' . "\n";
502 - if ($watch_this)
503 - $text .= ' <input type="hidden" name="wpWatchthis">' . "\n";
504 - $text .=<<<END
505 - </form>
506 - <script type="text/javascript">
507 - document.editform.submit();
508 - </script>
509 -
510 -END;
511 - return $text;
512 -}

Status & tagging log