/
ApiPageTriageTagCopyvio.php
96 lines (84 loc) · 2.66 KB
/
ApiPageTriageTagCopyvio.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
<?php
namespace MediaWiki\Extension\PageTriage\Api;
use ApiBase;
use ManualLogEntry;
use MediaWiki\Extension\PageTriage\ArticleMetadata;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionRecord;
use Title;
use Wikimedia\ParamValidator\ParamValidator;
class ApiPageTriageTagCopyvio extends ApiBase {
public function execute() {
$this->checkUserRightsAny( 'pagetriage-copyvio' );
$params = $this->extractRequestParams();
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
$revision = $revisionStore->getRevisionById( $params['revid'] );
if ( !$revision ) {
$this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
}
if ( !ArticleMetadata::validatePageIds( [ $revision->getPageId() ] ) ) {
$this->dieWithError( 'apierror-bad-pagetriage-page' );
}
$tags = ArticleMetadata::getValidTags();
if ( !$tags || !$tags['copyvio'] ) {
$this->dieWithError( 'apierror-pagetriage-missingtag' );
}
$row = [
'ptrpt_page_id' => $revision->getPageId(),
'ptrpt_tag_id' => $tags['copyvio'],
'ptrpt_value' => $revision->getId()
];
$dbw = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_PRIMARY );
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_REPLICA );
// If the revision ID hasn't been tagged with copyvio yet, then insert and log.
if ( $dbr->selectField( 'pagetriage_page_tags', 'ptrpt_page_id', $row, __METHOD__ ) === false ) {
$dbw->replace(
'pagetriage_page_tags',
[ [ 'ptrpt_page_id', 'ptrpt_tag_id' ] ],
$row,
__METHOD__
);
$this->logActivity( $revision );
$result = [ 'result' => 'success' ];
} else {
$result = [
'result' => 'done',
'pagetriage_unchanged_status' => $revision->getId(),
];
}
$this->getResult()->addValue( null, $this->getModuleName(), $result );
}
/**
* Log insertion activity for this API endpoint.
*
* @param RevisionRecord $revision
* @throws \MWException
*/
protected function logActivity( RevisionRecord $revision ) {
$logEntry = new ManualLogEntry( 'pagetriage-copyvio', 'insert' );
$logEntry->setPerformer( $this->getUser() );
$logEntry->setTarget( Title::newFromID( $revision->getPageId() ) );
$logEntry->setParameters( [
'4::revId' => $revision->getId(),
] );
$logEntry->addTags( 'pagetriage' );
$logEntry->publish( $logEntry->insert() );
}
public function needsToken() {
return 'csrf';
}
public function getAllowedParams() {
return [
'revid' => [
ParamValidator::PARAM_REQUIRED => true,
ParamValidator::PARAM_TYPE => 'integer'
]
];
}
public function mustBePosted() {
return true;
}
public function isWriteMode() {
return true;
}
}