r75296 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75295‎ | r75296 | r75297 >
Date:22:17, 23 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Follow up to r75295 - forgot to svn add this class
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryLanguage.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryLanguage.php
@@ -0,0 +1,106 @@
 2+<?php
 3+
 4+/**
 5+ * Static class for functions related to the SMW query language.
 6+ *
 7+ * @since 1.5.3
 8+ *
 9+ * @file SMW_QueryLanguage.php
 10+ * @ingroup SMW
 11+ *
 12+ * @author Jeroen De Dauw
 13+ */
 14+final class SMWQueryLanguage {
 15+
 16+ /**
 17+ * Associative array that maps the comparator strings (keys) to
 18+ * the SMW_CMP_ enum (values). Call initializeComparators before using.
 19+ *
 20+ * @since 1.5.3
 21+ *
 22+ * @var array
 23+ */
 24+ protected static $comparators = array();
 25+
 26+ /**
 27+ * Gets the SMW_CMP_ for a string comparator, falling back to the
 28+ * $defaultComparator when none is found.
 29+ *
 30+ * @since 1.5.3
 31+ *
 32+ * @param string $string
 33+ * @param integer $defaultComparator Item of the SMW_CMP_ enum
 34+ *
 35+ * @return integer Item of the SMW_CMP_ enum
 36+ */
 37+ public static function getComparatorFromString( $string, $defaultComparator = SMW_CMP_EQ ) {
 38+ self::initializeComparators();
 39+ return array_key_exists( $string, self::$comparators ) ? self::$comparators[$string] : $defaultComparator;
 40+ }
 41+
 42+ /**
 43+ * Gets the comparator string for a comparator.
 44+ *
 45+ * @since 1.5.3
 46+ *
 47+ * @param $comparator
 48+ *
 49+ * @return string
 50+ */
 51+ public static function getStringForComparator( $comparator ) {
 52+ self::initializeComparators();
 53+ static $reverseCache = false;
 54+
 55+ if ( $reverseCache === false ) {
 56+ $reverseCache = array_reverse( self::$comparators );
 57+ }
 58+
 59+ if ( array_key_exists( $comparator, $reverseCache ) ) {
 60+ return $reverseCache[$comparator];
 61+ }
 62+ else {
 63+ throw new Exception( "Comparator $comparator does not have a string representatation" );
 64+ }
 65+ }
 66+
 67+ /**
 68+ * Initializes the $comparators field.
 69+ *
 70+ * @since 1.5.3
 71+ */
 72+ protected static function initializeComparators() {
 73+ global $smwgQComparators, $smwStrictComparators;
 74+ static $initialized = false;
 75+
 76+ if ( $initialized ) {
 77+ return;
 78+ }
 79+
 80+ $initialized = true;
 81+
 82+ $comparators = array(
 83+ '' => SMW_CMP_EQ,
 84+ '<' => $smwStrictComparators ? SMW_CMP_LESS : SMW_CMP_LEQ,
 85+ '>' => $smwStrictComparators ? SMW_CMP_GRTR : SMW_CMP_GEQ,
 86+ '≤' => SMW_CMP_LEQ,
 87+ '≥' => SMW_CMP_GEQ,
 88+ '!' => SMW_CMP_NEQ,
 89+ '~' => SMW_CMP_LIKE,
 90+ '!~' => SMW_CMP_NLKE,
 91+ '<<' => SMW_CMP_LESS,
 92+ '>>' => SMW_CMP_GRTR,
 93+ );
 94+
 95+ $allowedComparators = explode( '|', $smwgQComparators );
 96+
 97+ // Remove the comparators that are not allowed.
 98+ foreach ( $comparators as $string => $comparator ) {
 99+ if ( !in_array( $string, $allowedComparators ) ) {
 100+ unset( $comparators[$string] );
 101+ }
 102+ }
 103+
 104+ self::$comparators = $comparators;
 105+ }
 106+
 107+}
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryLanguage.php
___________________________________________________________________
Added: svn:eol-style
1108 + native

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r75295Rewrote comparator handling and added strictly strict comparators :)jeroendedauw22:17, 23 October 2010

Status & tagging log