forked from Niharika29/Assessments
/
PageAssessmentsBody.php
131 lines (118 loc) · 3.64 KB
/
PageAssessmentsBody.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
<?php
/**
* PageAssessments extension body
*
* @file
* @ingroup Extensions
*/
class PageAssessmentsBody {
/**
* Driver function
*/
public static function execute ( &$parser, $project = '', $class = '', $importance = '' ) {
$newRecord = false;
// Title class object for the Main page of this Talk page
$pageObj = $parser->getTitle()->getSubjectPage();
$pageTitle = $pageObj->getText();
$exists = PageAssessmentsBody::checkIfExists( $pageTitle, $project, $class, $importance );
switch ( $exists ) {
case 'nochange':
return;
case 'change':
break;
default:
$newRecord = true;
break;
}
$pageNamespace = $pageObj->getNamespace();
$pageId = $pageObj->getArticleID();
$revisionId = $pageObj->getLatestRevID();
// Compile the array to be inserted to the DB
$values = array(
'pa_page_id' => $pageId,
'pa_page_name' => $pageTitle,
'pa_page_namespace' => $pageNamespace,
'pa_project' => $project,
'pa_class' => $class,
'pa_importance' => $importance,
'pa_page_revision' => $revisionId
);
if ( $newRecord ) {
PageAssessmentsBody::insertRecord( $values );
} else {
PageAssessmentsBody::updateRecord( $values );
}
$values['pa_user_id'] = $parser->getRevisionUser();
PageAssessmentsBody::insertLogRecord( $values );
return;
}
/**
* Update record in DB with new values
* @param array $values New values to be entered into the DB
* @return bool True/False on query success/fail
*/
public static function updateRecord ( $values ) {
$dbw = wfGetDB( DB_MASTER );
$conds = array(
'pa_page_name' => $values['pa_page_name'],
'pa_project' => $values['pa_project']
);
$dbw->update( 'page_assessments', $values, $conds, __METHOD__ );
return true;
}
/**
* Insert a new record in DB
* @param array $values New values to be entered into the DB
* @return bool True/False on query success/fail
*/
public static function insertRecord ( $values ) {
$dbw = wfGetDB( DB_MASTER );
$dbw->insert( 'page_assessments', $values, __METHOD__ );
return true;
}
/**
* Insert to the logging table
* @param array $values Values to be entered to the DB
* @return bool True/False on query success/fail
*/
public static function insertLogRecord ( $values ) {
$logValues = array(
'pa_page_id' => $values['pa_page_id'],
'pa_user_id' => $values['pa_user'],
'pa_page_revision' => $values['pa_page_revision'],
'pa_project' => $values['pa_project'],
'pa_class' => $values['pa_class'],
'pa_importance' => $values['pa_importance'],
'pa_timestamp' => wfTimestampNow()
);
$dbw = wfGetDB( DB_MASTER );
$dbw->insert( 'page_assessments_log', $logValues, __METHOD__ );
return true;
}
/**
* Check if the record already exists and is changed
* @param string $pageTitle Title of the page
* @param string $project Name of the Wikiproject associated
* @param string $class Class attribute of the review
* @param string $importance Importance attribute of the review
* @return string nochange|change|noexist No changes/Changes to existing record/New record
*/
public static function checkIfExists ( $pageTitle, $project, $class, $importance ) {
$dbw = wfGetDB( DB_SLAVE ); // Read only query
$res = $dbw->select(
'page_assessments',
'*',
'pa_page_name = "' . $pageTitle . '" AND pa_project = "' . $project . '"'
);
if ( $res ) {
foreach ( $res as $row ) {
if ( $row->pa_class == $class && $row->pa_importance == $importance ) {
return 'nochange'; // Record is same as new
} elseif ( $row->pa_class != $class || $row->pa_importance != $importance ) {
return 'change'; // Record has changed
}
}
}
return 'noexist'; // New record
}
}