-
Notifications
You must be signed in to change notification settings - Fork 0
/
DpleFeatureTitlematch.php
194 lines (158 loc) · 5.45 KB
/
DpleFeatureTitlematch.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
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
/**
* @brief Class DpleFeatureTitlematch.
*
* @file
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*
* @author [RV1971](http://www.mediawiki.org/wiki/User:RV1971)
*
*/
/**
* @brief Selection by title prefix, suffix or fragment.
*
* Recognizes the parameters `titleprefix, nottitleprefix,
* titlesuffix, nottitlesuffix, titlecontains` and `nottitlecontains`,
* each of which may be a title substring or an array thereof. As
* usual, spaces and underscores in titles are equivalent.
*
* If `titleprefix, titlesuffix, titlecontains` is an array, the
* result is (obviously) the union of the record sets satisfying its
* elements, unlike other parameters (including `nottitleprefix`)
* where the result is the intersection.
*
* This class implements a `[not]title{prefix suffix contains}`
* condition but not a general `[not]titlematch` because escaping of
* the % and _ characters depends on the database backend, hence
* arbitrary LIKE patterns would raise portability isssues.
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*/
class DpleFeatureTitlematch extends DpleFeatureBase
implements DpleFeatureInterface {
/* == private variables == */
/// Array of title prefixes pages to select should have.
private $titleprefixes_;
/// Array of title prefixes pages to select should not have.
private $notTitleprefixes_;
/// Array of title suffixes pages to select should have.
private $titlesuffixes_;
/// Array of title suffixes pages to select should not have.
private $notTitlesuffixes_;
/// Array of strings that titles of pages to select should contain.
private $titlecontains_;
/// Array of strings that titles of pages to select should not contain.
private $notTitlecontains_;
/* == magic methods == */
/// Constructor. Evaluate parameters.
public function __construct( array $params, array &$features ) {
parent::__construct( $features );
foreach ( array(
'titleprefix' => 'titleprefixes_',
'nottitleprefix' => 'notTitleprefixes_',
'titlesuffix' => 'titlesuffixes_',
'nottitlesuffix' => 'notTitlesuffixes_',
'titlecontains' => 'titlecontains_',
'nottitlecontains' => 'notTitlecontains_'
) as $key => $member ) {
if ( isset( $params[$key] ) ) {
$this->$member =
array_map( array( $this, 'parseTitleSubstring' ),
(array)$params[$key] );
}
}
}
/* == accessors == */
/// Get @ref $titleprefixes_.
public function getTitleprefixes() {
return $this->titleprefixes_;
}
/// Get @ref $notTitleprefixes_.
public function getNotTitleprefixes() {
return $this->notTitleprefixes_;
}
/// Get @ref $titlesuffixes_.
public function getTitlesuffixes() {
return $this->titlesuffixes_;
}
/// Get @ref $notTitlesuffixes_.
public function getNotTitlesuffixes() {
return $this->notTitlesuffixes_;
}
/// Get @ref $titlecontains_.
public function getTitlecontains() {
return $this->titlecontains_;
}
/// Get @ref $notTitlecontains_.
public function getNotTitlecontains() {
return $this->notTitlecontains_;
}
/// Get the database cost generated by this feature instance.
public function getCost() {
/** If @ref $titleprefixes_, @ref $titlesuffixes_, @ref
* $titlecontains_ are arrays, the items in each array are
* put together with OR operators, which is typcially slow,
* therefore one unit of @ref
* DpleFeature::getCost() is accounted for
* each item. Items in @ref $notTitleprefixes_ etc. instead
* are put together with AND, which is typically efficient,
* and therefore only one unit is accounted for each
* array. */
return (count( $this->titleprefixes_ )
+ count( $this->titlesuffixes_ )
+ count( $this->titlecontains_ )
+ (int)(bool)$this->notTitleprefixes_
+ (int)(bool)$this->notTitlesuffixes_
+ (int)(bool)$this->notTitlecontains_) * parent::getCost();
}
/* == operations == */
/// Modify a given query. @copydetails DpleFeatureBase::modifyQuery()
public function modifyQuery( DpleQuery &$query ) {
$dbr = $query->getDbr();
if ( $this->titleprefixes_ ) {
$prefixConds = array();
foreach ( $this->titleprefixes_ as $prefix ) {
$prefixConds[] = 'page_title'
. $dbr->buildLike( $prefix, $dbr->anyString() );
}
$query->addConds( '(' . implode( ' OR ', $prefixConds ) . ')' );
}
foreach ( (array)$this->notTitleprefixes_ as $prefix ) {
$query->addConds( 'page_title NOT'
. $dbr->buildLike( $prefix, $dbr->anyString() ) );
}
if ( $this->titlesuffixes_ ) {
$suffixConds = array();
foreach ( $this->titlesuffixes_ as $suffix ) {
$suffixConds[] = 'page_title'
. $dbr->buildLike( $dbr->anyString(), $suffix );
}
$query->addConds( '(' . implode( ' OR ', $suffixConds ) . ')' );
}
foreach ( (array)$this->notTitlesuffixes_ as $suffix ) {
$query->addConds( 'page_title NOT'
. $dbr->buildLike( $dbr->anyString(), $suffix ) );
}
if ( $this->titlecontains_ ) {
$containsConds = array();
foreach ( $this->titlecontains_ as $contains ) {
$containsConds[] = 'page_title'
. $dbr->buildLike( $dbr->anyString(), $contains,
$dbr->anyString() );
}
$query->addConds( '(' . implode( ' OR ', $containsConds ) . ')' );
}
foreach ( (array)$this->notTitlecontains_ as $contains ) {
$query->addConds( 'page_title NOT'
. $dbr->buildLike( $dbr->anyString(), $contains,
$dbr->anyString() ) );
}
}
}