-
Notifications
You must be signed in to change notification settings - Fork 5
/
DefaultQuestion.php
220 lines (178 loc) · 5.9 KB
/
DefaultQuestion.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
<?php
class DefaultQuestion {
function __construct( $question ) {
global $wgRequest, $wgEnableReviews;
// if it's all caps, tone it down!
if ( strtoupper( $question ) == $question ) {
$question = strtolower( $question );
}
// strip question marks and slashes
$question = Answer::q2s( $question );
$question = str_replace( '?', ' ', $question );
// remove "Ask a question" or equivalent if it got here
// question is in DBkey form, with underscores
$askAQuestion = wfMessage( 'ask_a_question' )->inContentLanguage()->text();
$search = str_replace( ' ', '_', $askAQuestion );
if ( strpos( $question, $search ) === 0 ) {
$question = substr( $question, strlen( $search ) );
$question = ltrim( $question );
}
if ( !empty( $wgEnableReviews ) && !preg_match( '/_/', $question ) ) {
// remove www. if typed at the beginning of the pagename
$question = preg_replace( '/^www\./i', '', $question );
// add a .com if there was no .com, .net, .org, .co.uk, .ca or .au at the end of the pagename
// (for the sake of simplicity add .com if there is on . or character)
if ( !preg_match( "/\.|\//", $question ) ) {
$question .= '.com';
}
}
$this->title = Title::makeTitleSafe( NS_MAIN, $question );
if ( !$this->title ) {
return null;
}
$this->question = $this->title->getText();
$this->categories = $wgRequest->getVal( 'categories' );
}
function create() {
global $wgOut, $wgUser, $wgContLang;
if ( wfReadOnly() ) {
return false;
}
if (
empty( $this->title ) ||
!$this->title->userCan( 'edit' ) ||
!$this->title->userCan( 'create' )
)
{
return false;
}
if ( $this->badWordsTest() ) {
return false;
}
if ( !wfRunHooks( 'CreateDefaultQuestionPageFilter', array( $this->title ) ) ) {
wfDebug( __METHOD__ . ": question '{$this->title}' filtered out by hook\n" );
return false;
}
if ( $this->searchTest() ) {
return false;
}
$default_text = Answer::getSpecialCategoryTag( 'unanswered' );
// add default category tags passed in
if ( $this->categories ) {
$categories_array = explode( '|', $this->categories );
foreach ( $categories_array as $category ) {
$default_text .= "\n[[" . $wgContLang->getNsText( NS_CATEGORY ) .
':' . ucfirst( $category ) . ']]';
}
}
$flags = EDIT_NEW;
$article = new Article( $this->title );
$article->doEdit(
$default_text,
wfMessage( 'new_question_comment' )->inContentLanguage()->parse(),
$flags
);
if ( $wgUser->isLoggedIn() ) {
// check user preferences before adding to watchlist (RT #45647)
$watchCreations = $wgUser->getOption( 'watchcreations' );
if ( !empty( $watchCreations ) ) {
$wgUser->addWatch( $this->title );
}
}
// store question in session so we can give attribution if they create an account afterwards
$_SESSION['wsQuestionAsk'] = '';
if ( $wgUser->isAnon() ) {
$_SESSION['wsQuestionAsk'] = $this->question;
}
return true;
}
// redirect one or two word questions to search
function searchTest() {
// on reviews wikis always redirect to search if there is a space inside
global $wgEnableReviews;
if ( !empty( $wgEnableReviews ) && preg_match( '/\s/', $this->question ) ) {
return true;
}
global $wgDisableAnswersShortQuestionsRedirect;
if ( !empty( $wgDisableAnswersShortQuestionsRedirect ) ) {
return false;
}
$words = explode( ' ', $this->question );
if ( count( $words ) < 3 ) {
return true;
}
return false;
}
function getBadWords() {
$swearContent = wfMessage( 'BadWords' )->inContentLanguage()->text();
$swearList = explode( "\n", $swearContent );
foreach ( $swearList as $swear ) {
if ( $swear ) {
$swearWords[] = $swear;
}
}
return $swearWords;
}
// don't allow swear words when creating new question / generating list of recenlty asked questions (via HPL)
function badWordsTest() {
// TODO: temporary check for Phalanx (don't perform additional filtering when enabled)
if ( class_exists( 'Phalanx' ) ) {
return false;
}
// remove punctations
$search_test = preg_replace( '/\pP+/', '', $this->question );
$search_test = preg_replace( '/\s+/', ' ', $search_test );
$found = preg_match(
'/\s(' . implode( '|', $this->getBadWords() ) . ')\s/i',
' ' . $search_test . ' '
);
if ( $found ) {
return true;
}
return false;
}
function getFilterWords() {
global $wgMemc;
$mkey = wfMemcKey( __METHOD__ );
$filtered_words = $wgMemc->get( $mkey );
if ( empty( $filtered_words ) ) {
$filtered = wfMessage( 'FilterWords' );
if ( !$filtered->isDisabled() ) {
$aFiltered = explode( "\n", $filtered->inContentLanguage()->text() );
foreach ( $aFiltered as $filter ) {
if ( $filter ) {
$filtered_words[] = $filter;
}
}
$wgMemc->set( $mkey, $filtered_words, 3 * 60 );
}
}
return $filtered_words;
}
// don't allow swear words when generating list of recently asked questions (via HPL)
function filterWordsTest() {
if ( !wfRunHooks( 'DefaultQuestion::filterWordsTest', array( $this->question ) ) ) {
wfDebug( __METHOD__ . ": question '{$this->question}' filtered out by hook\n" );
return true;
}
// TODO: temporary check for Phalanx (don't perform additional filtering when enabled)
if ( class_exists( 'Phalanx' ) ) {
return false;
}
// remove punctuations
$search_test = preg_replace( '/\pP+/', '', $this->question );
$search_test = preg_replace( '/\s+/', ' ', $search_test );
$words = $this->getFilterWords();
if ( !empty( $words ) ) {
$found = preg_match(
'/\s(' . implode( '|', $words ) . ')\s/i',
' ' . $search_test . ' '
);
if ( $found ) {
wfDebug( __METHOD__ . ": question '{$search_test}' filtered out\n" );
return true;
}
}
return false;
}
}