Completed
Pull Request — master (#7882)
by Robin
17:28
created
lib/private/Files/Cache/QuerySearchHelper.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -85,7 +85,7 @@
 block discarded – undo
85 85
 
86 86
 	/**
87 87
 	 * @param IQueryBuilder $builder
88
-	 * @param ISearchOperator $operator
88
+	 * @param ISearchOperator $operators
89 89
 	 */
90 90
 	public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
91 91
 		return array_map(function ($operator) use ($builder) {
Please login to merge, or discard this patch.
Indentation   +176 added lines, -176 removed lines patch added patch discarded remove patch
@@ -34,180 +34,180 @@
 block discarded – undo
34 34
  * Tools for transforming search queries into database queries
35 35
  */
36 36
 class QuerySearchHelper {
37
-	static protected $searchOperatorMap = [
38
-		ISearchComparison::COMPARE_LIKE => 'iLike',
39
-		ISearchComparison::COMPARE_EQUAL => 'eq',
40
-		ISearchComparison::COMPARE_GREATER_THAN => 'gt',
41
-		ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'gte',
42
-		ISearchComparison::COMPARE_LESS_THAN => 'lt',
43
-		ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lte'
44
-	];
45
-
46
-	static protected $searchOperatorNegativeMap = [
47
-		ISearchComparison::COMPARE_LIKE => 'notLike',
48
-		ISearchComparison::COMPARE_EQUAL => 'neq',
49
-		ISearchComparison::COMPARE_GREATER_THAN => 'lte',
50
-		ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'lt',
51
-		ISearchComparison::COMPARE_LESS_THAN => 'gte',
52
-		ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lt'
53
-	];
54
-
55
-	const TAG_FAVORITE = '_$!<Favorite>!$_';
56
-
57
-	/** @var IMimeTypeLoader */
58
-	private $mimetypeLoader;
59
-
60
-	/**
61
-	 * QuerySearchUtil constructor.
62
-	 *
63
-	 * @param IMimeTypeLoader $mimetypeLoader
64
-	 */
65
-	public function __construct(IMimeTypeLoader $mimetypeLoader) {
66
-		$this->mimetypeLoader = $mimetypeLoader;
67
-	}
68
-
69
-	/**
70
-	 * Whether or not the tag tables should be joined to complete the search
71
-	 *
72
-	 * @param ISearchOperator $operator
73
-	 * @return boolean
74
-	 */
75
-	public function shouldJoinTags(ISearchOperator $operator) {
76
-		if ($operator instanceof ISearchBinaryOperator) {
77
-			return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) {
78
-				return $shouldJoin || $this->shouldJoinTags($operator);
79
-			}, false);
80
-		} else if ($operator instanceof ISearchComparison) {
81
-			return $operator->getField() === 'tagname' || $operator->getField() === 'favorite';
82
-		}
83
-		return false;
84
-	}
85
-
86
-	/**
87
-	 * @param IQueryBuilder $builder
88
-	 * @param ISearchOperator $operator
89
-	 */
90
-	public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
91
-		return array_map(function ($operator) use ($builder) {
92
-			return $this->searchOperatorToDBExpr($builder, $operator);
93
-		}, $operators);
94
-	}
95
-
96
-	public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) {
97
-		$expr = $builder->expr();
98
-		if ($operator instanceof ISearchBinaryOperator) {
99
-			switch ($operator->getType()) {
100
-				case ISearchBinaryOperator::OPERATOR_NOT:
101
-					$negativeOperator = $operator->getArguments()[0];
102
-					if ($negativeOperator instanceof ISearchComparison) {
103
-						return $this->searchComparisonToDBExpr($builder, $negativeOperator, self::$searchOperatorNegativeMap);
104
-					} else {
105
-						throw new \InvalidArgumentException('Binary operators inside "not" is not supported');
106
-					}
107
-				case ISearchBinaryOperator::OPERATOR_AND:
108
-					return call_user_func_array([$expr, 'andX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
109
-				case ISearchBinaryOperator::OPERATOR_OR:
110
-					return call_user_func_array([$expr, 'orX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
111
-				default:
112
-					throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
113
-			}
114
-		} else if ($operator instanceof ISearchComparison) {
115
-			return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap);
116
-		} else {
117
-			throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator));
118
-		}
119
-	}
120
-
121
-	private function searchComparisonToDBExpr(IQueryBuilder $builder, ISearchComparison $comparison, array $operatorMap) {
122
-		$this->validateComparison($comparison);
123
-
124
-		list($field, $value, $type) = $this->getOperatorFieldAndValue($comparison);
125
-		if (isset($operatorMap[$type])) {
126
-			$queryOperator = $operatorMap[$type];
127
-			return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value));
128
-		} else {
129
-			throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType());
130
-		}
131
-	}
132
-
133
-	private function getOperatorFieldAndValue(ISearchComparison $operator) {
134
-		$field = $operator->getField();
135
-		$value = $operator->getValue();
136
-		$type = $operator->getType();
137
-		if ($field === 'mimetype') {
138
-			if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) {
139
-				$value = $this->mimetypeLoader->getId($value);
140
-			} else if ($operator->getType() === ISearchComparison::COMPARE_LIKE) {
141
-				// transform "mimetype='foo/%'" to "mimepart='foo'"
142
-				if (preg_match('|(.+)/%|', $value, $matches)) {
143
-					$field = 'mimepart';
144
-					$value = $this->mimetypeLoader->getId($matches[1]);
145
-					$type = ISearchComparison::COMPARE_EQUAL;
146
-				}
147
-				if (strpos($value, '%') !== false) {
148
-					throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
149
-				}
150
-			}
151
-		} else if ($field === 'favorite') {
152
-			$field = 'tag.category';
153
-			$value = self::TAG_FAVORITE;
154
-		} else if ($field === 'tagname') {
155
-			$field = 'tag.category';
156
-		}
157
-		return [$field, $value, $type];
158
-	}
159
-
160
-	private function validateComparison(ISearchComparison $operator) {
161
-		$types = [
162
-			'mimetype' => 'string',
163
-			'mtime' => 'integer',
164
-			'name' => 'string',
165
-			'size' => 'integer',
166
-			'tagname' => 'string',
167
-			'favorite' => 'boolean',
168
-			'fileid' => 'integer'
169
-		];
170
-		$comparisons = [
171
-			'mimetype' => ['eq', 'like'],
172
-			'mtime' => ['eq', 'gt', 'lt', 'gte', 'lte'],
173
-			'name' => ['eq', 'like'],
174
-			'size' => ['eq', 'gt', 'lt', 'gte', 'lte'],
175
-			'tagname' => ['eq', 'like'],
176
-			'favorite' => ['eq'],
177
-			'fileid' => ['eq']
178
-		];
179
-
180
-		if (!isset($types[$operator->getField()])) {
181
-			throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField());
182
-		}
183
-		$type = $types[$operator->getField()];
184
-		if (gettype($operator->getValue()) !== $type) {
185
-			throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField());
186
-		}
187
-		if (!in_array($operator->getType(), $comparisons[$operator->getField()])) {
188
-			throw new \InvalidArgumentException('Unsupported comparison for field  ' . $operator->getField() . ': ' . $operator->getType());
189
-		}
190
-	}
191
-
192
-	private function getParameterForValue(IQueryBuilder $builder, $value) {
193
-		if ($value instanceof \DateTime) {
194
-			$value = $value->getTimestamp();
195
-		}
196
-		if (is_numeric($value)) {
197
-			$type = IQueryBuilder::PARAM_INT;
198
-		} else {
199
-			$type = IQueryBuilder::PARAM_STR;
200
-		}
201
-		return $builder->createNamedParameter($value, $type);
202
-	}
203
-
204
-	/**
205
-	 * @param IQueryBuilder $query
206
-	 * @param ISearchOrder[] $orders
207
-	 */
208
-	public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) {
209
-		foreach ($orders as $order) {
210
-			$query->addOrderBy($order->getField(), $order->getDirection());
211
-		}
212
-	}
37
+    static protected $searchOperatorMap = [
38
+        ISearchComparison::COMPARE_LIKE => 'iLike',
39
+        ISearchComparison::COMPARE_EQUAL => 'eq',
40
+        ISearchComparison::COMPARE_GREATER_THAN => 'gt',
41
+        ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'gte',
42
+        ISearchComparison::COMPARE_LESS_THAN => 'lt',
43
+        ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lte'
44
+    ];
45
+
46
+    static protected $searchOperatorNegativeMap = [
47
+        ISearchComparison::COMPARE_LIKE => 'notLike',
48
+        ISearchComparison::COMPARE_EQUAL => 'neq',
49
+        ISearchComparison::COMPARE_GREATER_THAN => 'lte',
50
+        ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'lt',
51
+        ISearchComparison::COMPARE_LESS_THAN => 'gte',
52
+        ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lt'
53
+    ];
54
+
55
+    const TAG_FAVORITE = '_$!<Favorite>!$_';
56
+
57
+    /** @var IMimeTypeLoader */
58
+    private $mimetypeLoader;
59
+
60
+    /**
61
+     * QuerySearchUtil constructor.
62
+     *
63
+     * @param IMimeTypeLoader $mimetypeLoader
64
+     */
65
+    public function __construct(IMimeTypeLoader $mimetypeLoader) {
66
+        $this->mimetypeLoader = $mimetypeLoader;
67
+    }
68
+
69
+    /**
70
+     * Whether or not the tag tables should be joined to complete the search
71
+     *
72
+     * @param ISearchOperator $operator
73
+     * @return boolean
74
+     */
75
+    public function shouldJoinTags(ISearchOperator $operator) {
76
+        if ($operator instanceof ISearchBinaryOperator) {
77
+            return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) {
78
+                return $shouldJoin || $this->shouldJoinTags($operator);
79
+            }, false);
80
+        } else if ($operator instanceof ISearchComparison) {
81
+            return $operator->getField() === 'tagname' || $operator->getField() === 'favorite';
82
+        }
83
+        return false;
84
+    }
85
+
86
+    /**
87
+     * @param IQueryBuilder $builder
88
+     * @param ISearchOperator $operator
89
+     */
90
+    public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
91
+        return array_map(function ($operator) use ($builder) {
92
+            return $this->searchOperatorToDBExpr($builder, $operator);
93
+        }, $operators);
94
+    }
95
+
96
+    public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) {
97
+        $expr = $builder->expr();
98
+        if ($operator instanceof ISearchBinaryOperator) {
99
+            switch ($operator->getType()) {
100
+                case ISearchBinaryOperator::OPERATOR_NOT:
101
+                    $negativeOperator = $operator->getArguments()[0];
102
+                    if ($negativeOperator instanceof ISearchComparison) {
103
+                        return $this->searchComparisonToDBExpr($builder, $negativeOperator, self::$searchOperatorNegativeMap);
104
+                    } else {
105
+                        throw new \InvalidArgumentException('Binary operators inside "not" is not supported');
106
+                    }
107
+                case ISearchBinaryOperator::OPERATOR_AND:
108
+                    return call_user_func_array([$expr, 'andX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
109
+                case ISearchBinaryOperator::OPERATOR_OR:
110
+                    return call_user_func_array([$expr, 'orX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
111
+                default:
112
+                    throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
113
+            }
114
+        } else if ($operator instanceof ISearchComparison) {
115
+            return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap);
116
+        } else {
117
+            throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator));
118
+        }
119
+    }
120
+
121
+    private function searchComparisonToDBExpr(IQueryBuilder $builder, ISearchComparison $comparison, array $operatorMap) {
122
+        $this->validateComparison($comparison);
123
+
124
+        list($field, $value, $type) = $this->getOperatorFieldAndValue($comparison);
125
+        if (isset($operatorMap[$type])) {
126
+            $queryOperator = $operatorMap[$type];
127
+            return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value));
128
+        } else {
129
+            throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType());
130
+        }
131
+    }
132
+
133
+    private function getOperatorFieldAndValue(ISearchComparison $operator) {
134
+        $field = $operator->getField();
135
+        $value = $operator->getValue();
136
+        $type = $operator->getType();
137
+        if ($field === 'mimetype') {
138
+            if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) {
139
+                $value = $this->mimetypeLoader->getId($value);
140
+            } else if ($operator->getType() === ISearchComparison::COMPARE_LIKE) {
141
+                // transform "mimetype='foo/%'" to "mimepart='foo'"
142
+                if (preg_match('|(.+)/%|', $value, $matches)) {
143
+                    $field = 'mimepart';
144
+                    $value = $this->mimetypeLoader->getId($matches[1]);
145
+                    $type = ISearchComparison::COMPARE_EQUAL;
146
+                }
147
+                if (strpos($value, '%') !== false) {
148
+                    throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
149
+                }
150
+            }
151
+        } else if ($field === 'favorite') {
152
+            $field = 'tag.category';
153
+            $value = self::TAG_FAVORITE;
154
+        } else if ($field === 'tagname') {
155
+            $field = 'tag.category';
156
+        }
157
+        return [$field, $value, $type];
158
+    }
159
+
160
+    private function validateComparison(ISearchComparison $operator) {
161
+        $types = [
162
+            'mimetype' => 'string',
163
+            'mtime' => 'integer',
164
+            'name' => 'string',
165
+            'size' => 'integer',
166
+            'tagname' => 'string',
167
+            'favorite' => 'boolean',
168
+            'fileid' => 'integer'
169
+        ];
170
+        $comparisons = [
171
+            'mimetype' => ['eq', 'like'],
172
+            'mtime' => ['eq', 'gt', 'lt', 'gte', 'lte'],
173
+            'name' => ['eq', 'like'],
174
+            'size' => ['eq', 'gt', 'lt', 'gte', 'lte'],
175
+            'tagname' => ['eq', 'like'],
176
+            'favorite' => ['eq'],
177
+            'fileid' => ['eq']
178
+        ];
179
+
180
+        if (!isset($types[$operator->getField()])) {
181
+            throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField());
182
+        }
183
+        $type = $types[$operator->getField()];
184
+        if (gettype($operator->getValue()) !== $type) {
185
+            throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField());
186
+        }
187
+        if (!in_array($operator->getType(), $comparisons[$operator->getField()])) {
188
+            throw new \InvalidArgumentException('Unsupported comparison for field  ' . $operator->getField() . ': ' . $operator->getType());
189
+        }
190
+    }
191
+
192
+    private function getParameterForValue(IQueryBuilder $builder, $value) {
193
+        if ($value instanceof \DateTime) {
194
+            $value = $value->getTimestamp();
195
+        }
196
+        if (is_numeric($value)) {
197
+            $type = IQueryBuilder::PARAM_INT;
198
+        } else {
199
+            $type = IQueryBuilder::PARAM_STR;
200
+        }
201
+        return $builder->createNamedParameter($value, $type);
202
+    }
203
+
204
+    /**
205
+     * @param IQueryBuilder $query
206
+     * @param ISearchOrder[] $orders
207
+     */
208
+    public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) {
209
+        foreach ($orders as $order) {
210
+            $query->addOrderBy($order->getField(), $order->getDirection());
211
+        }
212
+    }
213 213
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 	 */
75 75
 	public function shouldJoinTags(ISearchOperator $operator) {
76 76
 		if ($operator instanceof ISearchBinaryOperator) {
77
-			return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) {
77
+			return array_reduce($operator->getArguments(), function($shouldJoin, ISearchOperator $operator) {
78 78
 				return $shouldJoin || $this->shouldJoinTags($operator);
79 79
 			}, false);
80 80
 		} else if ($operator instanceof ISearchComparison) {
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
 	 * @param ISearchOperator $operator
89 89
 	 */
90 90
 	public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
91
-		return array_map(function ($operator) use ($builder) {
91
+		return array_map(function($operator) use ($builder) {
92 92
 			return $this->searchOperatorToDBExpr($builder, $operator);
93 93
 		}, $operators);
94 94
 	}
@@ -109,12 +109,12 @@  discard block
 block discarded – undo
109 109
 				case ISearchBinaryOperator::OPERATOR_OR:
110 110
 					return call_user_func_array([$expr, 'orX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
111 111
 				default:
112
-					throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
112
+					throw new \InvalidArgumentException('Invalid operator type: '.$operator->getType());
113 113
 			}
114 114
 		} else if ($operator instanceof ISearchComparison) {
115 115
 			return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap);
116 116
 		} else {
117
-			throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator));
117
+			throw new \InvalidArgumentException('Invalid operator type: '.get_class($operator));
118 118
 		}
119 119
 	}
120 120
 
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 			$queryOperator = $operatorMap[$type];
127 127
 			return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value));
128 128
 		} else {
129
-			throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType());
129
+			throw new \InvalidArgumentException('Invalid operator type: '.$comparison->getType());
130 130
 		}
131 131
 	}
132 132
 
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
 					$type = ISearchComparison::COMPARE_EQUAL;
146 146
 				}
147 147
 				if (strpos($value, '%') !== false) {
148
-					throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
148
+					throw new \InvalidArgumentException('Unsupported query value for mimetype: '.$value.', only values in the format "mime/type" or "mime/%" are supported');
149 149
 				}
150 150
 			}
151 151
 		} else if ($field === 'favorite') {
@@ -178,14 +178,14 @@  discard block
 block discarded – undo
178 178
 		];
179 179
 
180 180
 		if (!isset($types[$operator->getField()])) {
181
-			throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField());
181
+			throw new \InvalidArgumentException('Unsupported comparison field '.$operator->getField());
182 182
 		}
183 183
 		$type = $types[$operator->getField()];
184 184
 		if (gettype($operator->getValue()) !== $type) {
185
-			throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField());
185
+			throw new \InvalidArgumentException('Invalid type for field '.$operator->getField());
186 186
 		}
187 187
 		if (!in_array($operator->getType(), $comparisons[$operator->getField()])) {
188
-			throw new \InvalidArgumentException('Unsupported comparison for field  ' . $operator->getField() . ': ' . $operator->getType());
188
+			throw new \InvalidArgumentException('Unsupported comparison for field  '.$operator->getField().': '.$operator->getType());
189 189
 		}
190 190
 	}
191 191
 
Please login to merge, or discard this patch.