-
Notifications
You must be signed in to change notification settings - Fork 0
/
DpleFeatureOrder.php
234 lines (193 loc) · 5.86 KB
/
DpleFeatureOrder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
/**
* @brief Class DpleFeatureOrder.
*
* @file
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*
* @author [RV1971](http://www.mediawiki.org/wiki/User:RV1971)
*
*/
/**
* @brief Sort the result records.
*
* Recognizes the parameters `order` (default `descending`) and
* `ordermethod` (default `categoryadd`). Some values are replaced by
* fallback values if no category was indicated, see
* parseOrdermethod(). Defaults and fallback values are chosen for
* compatibility with
* [Extension:DynamicPageList](https://www.mediawiki.org/wiki/Extension:DynamicPageList_(Wikimedia%29).
*
* In addition to the `ordermethod` values recognized by
* Extension:DynamicPageList, the value `title` is accepted as
* well. It sorts by title without namespace prefix.
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*/
class DpleFeatureOrder extends DpleFeatureBase
implements DpleFeatureInterface {
/* == private variables == */
private $order_; ///< ascending|descending.
private $ordermethod_; ///< Sorting criteria.
private $sqlOrder_; ///< ASC|DESC.
/* == magic methods == */
/// Constructor. Evaluate parameters.
public function __construct( array $params, array &$features ) {
parent::__construct( $features );
$this->order_ = $this->parseOrder( isset( $params['order'] )
? $params['order'] : null );
/** The interpretation of the `ordermethod` parameter depends
* on other features; therefore, this parameter is just stored
* in @ref $ordermethod_, and invocation of parseOrdermethod()
* is deferred to modifyQuery(), when all features have been
* constructed. */
$this->ordermethod_ = isset( $params['ordermethod'] )
? $params['ordermethod'] : null;
$this->sqlOrder_ = ($this->order_ == 'descending') ? 'DESC' : 'ASC';
}
/* == accessors == */
/// Get @ref $order_.
public function getOrder() {
return $this->order_;
}
/// Get @ref $ordermethod_.
public function getOrdermethod() {
return $this->ordermethod_;
}
/// Get @ref $sqlOrder_.
public function getSqlOrder() {
return $this->sqlOrder_;
}
/**
* @brief Get the database cost generated by this feature
* instance.
*
* If the method is not mentioned in @ref $wgDpleOrderCostMap,
* return 0.
*/
public function getCost() {
global $wgDpleOrderCostMap;
return isset(
$wgDpleOrderCostMap[$this->ordermethod_] )
? $wgDpleOrderCostMap[$this->ordermethod_]
: 0;
}
/* == operations == */
/**
* @brief Parse the `order` parameter.
*
* @param null|string $param Parameter value.
*
* @return string `ascending|descending`.
*/
public function parseOrder( $param ) {
switch ( $param ) {
case 'ascending':
case 'descending':
return $param;
/** Default is `descending`. */
default:
return 'descending';
}
}
/**
* @brief Parse the `ordermethod` parameter.
*
* @param null|string $param Parameter value.
*
* @return string Order method.
*/
public function parseOrdermethod( $param ) {
$categoryFeature = $this->getFeature( 'DpleFeatureCategory' );
$hasCategories = isset( $categoryFeature )
&& $categoryFeature->getLinkedCount();
$containsFeature = $this->getFeature( 'DpleFeatureContains' );
$hasContains = isset( $containsFeature )
&& $containsFeature->getLinkedCount();
switch ( $param ) {
case 'created':
case 'lastedit':
case 'length':
case 'title':
return $param;
case 'categoryadd':
/** If no categories are specified, replace
* `categoryadd` with `created`. */
return $hasCategories ? 'categoryadd' : 'created';
case 'categoryaddx':
/** If no categories are specified, replace
* `categoryaddx` with `created`. */
return $hasContains ? 'categoryaddx' : 'created';
case 'categorysortkey':
case 'sortkey':
/** If no categories are specified, replace
* `categorysortkey` with `created`. */
return $hasCategories ? 'categorysortkey' : 'created';
case 'categorysortkeyx':
case 'sortkeyx':
/** If no categories are specified, replace
* `categorysortkeyx` with `created`. */
return $hasContains ? 'categorysortkeyx' : 'created';
case 'popularity':
global $wgDisableCounters;
if ( !$wgDisableCounters ) {
return $param;
} else {
/** If `popularity` was requested but hit counters
* are disabled, invoke parseOrdermethod() again
* with parameter `categoryadd`. */
return $this->parseOrdermethod( 'categoryadd' );
}
/** Default: invoke parseOrdermethod() again with
* parameter `categoryadd`. */
default:
return $this->parseOrdermethod( 'categoryadd' );
}
}
/// Modify a given query. @copydetails DpleFeatureBase::modifyQuery()
public function modifyQuery( DpleQuery &$query ) {
/** Call parseOrdermethod(). */
$this->ordermethod_ = $this->parseOrdermethod( $this->ordermethod_ );
/** Set the ORDER BY clause based on $ordermethod_. */
switch ( $this->ordermethod_ ) {
case 'lastedit':
$sqlSort = 'page_touched';
break;
case 'length':
$sqlSort = 'page_len';
break;
case 'created':
$sqlSort = 'page_id'; // Since they're never reused
// and increasing
break;
case 'categorysortkey':
$sqlSort = "cl1.cl_type {$this->sqlOrder_}, cl1.cl_sortkey";
break;
case 'categorysortkeyx':
$sqlSort = "clx1.cl_type {$this->sqlOrder_}, clx1.cl_sortkey";
break;
case 'popularity':
$sqlSort = 'page_counter';
break;
case 'title':
$sqlSort =
$query->getDbr()->strreplace( 'page_title',
"'_'", "' '" );
break;
case 'categoryadd':
$sqlSort = 'cl1.cl_timestamp';
break;
case 'categoryaddx':
$sqlSort = 'clx1.cl_timestamp';
break;
}
$query->setOption( 'ORDER BY', "$sqlSort $this->sqlOrder_" );
}
}