forked from Brickimedia/Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnswersClass.php
417 lines (363 loc) · 10.9 KB
/
AnswersClass.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
class Answer {
/**
* @var Title
*/
public $title;
/**
* @var Array: array containing the localized names of the
* answered/unanswered categories
*/
static $special_categories = array();
/**
* Set the title class member variable.
*
* @param $title Title
*/
function __construct( Title $title ) {
$this->title = $title;
}
/**
* Create a new Answer object from the given Title object.
*
* @param $title Title
* @return Answer
*/
public static function newFromTitle( $title ) {
$a = new Answer( $title );
return $a;
}
/**
* Is the current Title a question page? Existing pages in the main
* namespace that are not the main page are counted as questions.
*
* @param $checkExistence Boolean: check whether the page exists?
* @param $checkAction Boolean: check the action parameter too?
* @return Boolean: true if it's a question, else false
*/
public function isQuestion( $checkExistence = false, $checkAction = true ) {
if ( $checkExistence ) {
if ( $this->title->getArticleID() == 0 ) {
return false;
}
}
if ( $checkAction ) {
global $wgRequest;
$action = $wgRequest->getVal( 'action', 'view' );
if ( !in_array( $action, array( 'view', 'purge' ) ) ) {
return false;
}
}
if ( $this->title->getText() == wfMsgForContent( 'mainpage' ) ) {
return false;
}
if ( $this->title->getNamespace() == NS_MAIN ) {
return true;
}
return false;
}
protected static function setSpecialCategories() {
self::$special_categories = array(
'unanswered' => wfMsgForContent( 'unanswered_category' ),
'answered' => wfMsgForContent( 'answered_category' )
);
}
public static function getSpecialCategory( $categoryKey ) {
self::setSpecialCategories();
return self::$special_categories[$categoryKey];
}
public static function getSpecialCategoryTag( $categoryKey ) {
global $wgContLang;
$categoryTag = '[[' . $wgContLang->getNsText( NS_CATEGORY ) . ':' .
self::getSpecialCategory( $categoryKey ) . ']]';
return $categoryTag;
}
/**
* Remove categories from the given text.
*
* @param $content String
* @return String
*/
public static function stripCategories( $content ) {
global $wgContLang;
$content = preg_replace(
"@\[\[" . $wgContLang->getNsText( NS_CATEGORY ) . ":[^\]]*?].*?\]@si",
'',
$content
);
$content = trim( $content );
return $content;
}
/**
* Remove [[File:]]s from the given text.
*
* @param $content String
* @return String
*/
public static function stripImages( $content ) {
global $wgContLang;
$content = preg_replace(
"@\[\[" . $wgContLang->getNsText( NS_FILE ) . ":[^\]]*?].*?\]@si",
'',
$content
);
$content = trim( $content );
return $content;
}
/**
* Remove interlanguage links from the given text.
*
* @param $content String
* @return String
*/
public static function stripInterlang( $content ) {
global $wgContLang;
$lang_mask = '(' . join( '|', array_keys( Language::getLanguageNames() ) ) . ')';
$content = preg_replace(
"@\[\[" . $lang_mask . ":[^\]]*?].*?\]@si",
'',
$content
);
$content = trim( $content );
return $content;
}
/**
* @return Boolean: is this a question page and has the question been
* answered?
*/
public function isArticleAnswered() {
if ( !self::isQuestion() ) {
return false;
}
$article = new Article( $this->title );
$content = $article->getContent();
return self::isContentAnswered( $content );
}
/**
* Get the name of the deletion template from [[MediaWiki:DeleteTemplate]].
*
* @return String: delete template text or false on failure
*/
public static function getDeletionTemplateName() {
$deleteNameTitle = Title::makeTitle( NS_MEDIAWIKI, 'DeleteTemplate' );
$deleteNameArticle = new Article( $deleteNameTitle );
$deleteTemplate = $deleteNameArticle->followRedirect();
if ( is_object( $deleteTemplate ) ) {
return $deleteTemplate->getText();
}
return false;
}
/**
* Check whether the given page is marked for deletion.
*
* @param $content String: page content
* @return Boolean: true if it's marked for deletion, else false
*/
public static function isMarkedForDeletion( $content ) {
$marked = preg_match(
'/\s{{' . self::getDeletionTemplateName() . '(\|.*?)?}}/i',
$content
);
if ( $marked ) {
return true;
} else {
return false;
}
}
/**
* Has the given question been answered?
*
* @param $content String
* @return Boolean: true if it's been answered, else false
*/
public static function isContentAnswered( $content ) {
$content = self::stripCategories( $content );
$content = self::stripImages( $content );
$content = self::stripInterlang( $content );
if ( $content != '' ) {
return true;
} else {
return false;
}
}
function getOriginalAuthor() {
global $wgMemc;
$page_title_id = $this->title->getArticleID();
$key = wfMemcKey( 'answer_author', $page_title_id, 3 );
$data = $wgMemc->get( $key );
$author = array();
if ( empty( $data ) ) {
wfDebug( "Loading author for question {$page_title_id} from DB\n" );
$dbr = wfGetDB( DB_SLAVE );
$s = $dbr->selectRow(
'revision',
array( 'rev_user', 'rev_user_text', 'rev_timestamp' ),
array( 'rev_page' => $page_title_id ),
__METHOD__,
array( 'ORDER BY' => 'rev_id ASC', 'LIMIT' => 1 )
);
$avatarImg = Answer::getUserAvatar( $s->rev_user, 30, 30 );
$user_title = Title::makeTitle( NS_USER, $s->rev_user_text );
$author = array(
'user_id' => $s->rev_user,
'user_name' => $s->rev_user_text,
'title' => $user_title,
'avatar' => $avatarImg,
'ts' => $s->rev_timestamp
);
$wgMemc->set( $key, $author, 60 * 60 );
} else {
wfDebug( "Loading author for question for page {$page_title_id} from cache\n" );
$author = $data;
}
return $author;
}
public function getContributors() {
return AttributionCache::getInstance()->getArticleContribs( $this->title );
}
public static function getUserEditPoints( $userId ) {
return AttributionCache::getInstance()->getUserEditPoints( $userId );
}
public static function getUserAvatar( $userId, $width = 50, $height = 50 ) {
global $wgStylePath;
wfProfileIn( __METHOD__ );
if ( class_exists( 'wAvatar' ) ) { // SocialProfile's avatar class
$avatar = new wAvatar( $userId, 'm' );
$img = $avatar->getAvatarURL();
} elseif ( class_exists( 'Masthead' ) ) {
$avatar = Masthead::newFromUserID( $userId );
$avatar->getDefaultAvatars();
$avatar->mDefaultAvatars = array( "{$wgStylePath}/answers/images/default_avatar.png?1" );
$img = $avatar->getImageTag( $width, $height );
} else {
$avatarImgSrc = $wgStylePath . '/Monaco/monaco/images/blank.gif';
$img = Xml::element( 'img', array(
'src' => $avatarImgSrc,
'height' => $height,
'width' => $width,
) );
}
wfProfileOut( __METHOD__ );
return $img;
}
// Convenience function for getting the non-image version of the badge.
public static function getSmallUserBadge( $userData ) {
return getUserBadge( $userData, false );
}
/**
* Given an array of user-data, returns the HTML for a badge representation.
* If largeFormat is true (default), then the larger format including the
* avatar image will be used; otherwise, a compact version will be used.
*
* The userData is expected to be an associative array containing the keys
* 'user_id', 'user_name', and 'edits'.
*/
public static function getUserBadge( $userData, $largeFormat = true ) {
$ret = '';
// get avatar
$ret .= Xml::openElement( 'div', array( 'class' => 'userInfoBadge' ) );
if ( $largeFormat ) {
$ret .= Xml::openElement( 'div', array( 'class' => 'userInfoBadgeAvatarWrapper' ) );
$avatarImg = Answer::getUserAvatar( $userData['user_id'] );
$ret .= $avatarImg;
$ret .= Xml::closeElement( 'div' );
}
// render user info
$class = 'userInfo';
$class .= ( $largeFormat? '' : ' userInfoNoAvatar' );
$ret .= Xml::openElement( 'div', array( 'class' => $class ) );
if ( $userData['user_name'] == 'helper' ) {
// anonymous users
$ret .= Xml::element(
'span',
array( 'class' => 'userPageLink' ),
wfMsg( 'unregistered' )
);
$ret .= Xml::element(
'span',
array( 'class' => 'anonEditPoints' ),
wfMsgExt( 'anonymous_edit_points', 'parsemag', array( $userData['edits'] ) )
);
} else {
// link to user page
$userPage = Title::newFromText( $userData['user_name'], NS_USER );
$userPageLink = !empty( $userPage ) ? $userPage->getLocalURL() : '';
if ( $largeFormat ) {
$ret .= Xml::element(
'a',
array(
'href' => $userPageLink,
'class' => 'userPageLink'
),
$userData['user_name']
);
// user points
$ret .= Xml::openElement( 'div', array( 'class' => 'userEditPoints' ) );
$ret .= Xml::openElement( 'nobr' );
$ret .= Xml::element(
'span',
array(
'class' => "userPoints userDatas-user-points-{$userData['user_id']}",
'timestamp' => wfTimestampNow()
),
$userData['edits']
);
$ret .= ' '; // space for graceful degradation
$ret .= wfMsgExt( 'edit_points', 'parsemag', array( $userData['edits'] ) );
$ret .= Xml::closeElement( 'nobr' );
$ret .= Xml::closeElement( 'div' ); // END .userEditPoints
} else {
$ret .= Xml::openElement( 'div', array( 'style' => 'float:left;' ) );
$ret .= Xml::element(
'a',
array(
'href' => $userPageLink,
'class' => 'userPageLink'
),
$userData['user_name']
);
$ret .= Xml::closeElement( 'div' ); // END .userEditPoints
$ret .= ' '; // space for graceful degradation
$ret .= Xml::element(
'div',
array(
'class' => "userPoints userDatas-user-points-{$userData['user_id']}",
'style' => 'display:inline',
'timestamp' => wfTimestampNow()
),
$userData['edits']
);
}
}
$ret .= Xml::closeElement( 'div' ); // END .userInfo
$ret .= Xml::closeElement( 'div' ); // END .userInfoBadge
return $ret;
}
/**
* Converts a string to a question (adds language-appropriate question marks).
*
* @param $string String: question title
* @return String
*/
public static function s2q( $string ) {
$string = self::q2s( $string );
global $wgContLang;
$lang = $wgContLang->getCode();
if ( $lang == 'es' ) {
return '¿' . $string . '?';
}
if ( $lang == 'fr' ) {
return $string . ' ?';
}
return $string . '?';
}
/**
* Converts a question to a string w/out question marks.
*
* @param $string String: question title
* @return String
*/
public static function q2s( $question ) {
return ucfirst( trim( $question, "¿? \t\n\r\0\x0B" ) );
}
}