prefetchFederatedProperties()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
declare( strict_types = 1 );
3
4
namespace Wikibase\Repo\FederatedProperties;
5
6
use LogicException;
7
use MediaWiki\Storage\RevisionRecord;
8
use Wikibase\DataAccess\PrefetchingTermLookup;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikimedia\Rdbms\IResultWrapper;
11
12
/**
13
 * A helper class for parsing and prefetching properties from summaries for federated properties
14
 *
15
 * @license GPL-2.0-or-later
16
 */
17
class SummaryParsingPrefetchHelper {
18
19
	/**
20
	 * @var PrefetchingTermLookup
21
	 */
22
	private $prefetchingLookup;
23
24
	public function __construct(
25
		PrefetchingTermLookup $prefetchingLookup
26
27
	) {
28
		$this->prefetchingLookup = $prefetchingLookup;
29
	}
30
31
	/**
32
	 * Matching links to properties in in edit summaries, such as "[[Property:P123]]" or "[[wdbeta:Special:EntityPage/P123]]"
33
	 */
34
	public const PROPERTY_SUMMARY_REGEXP = '/\[\[(\S+)(P[1-9]\d*)\]\]/';
35
36
	/**
37
	 * @param IResultWrapper|array $rows
38
	 * @param array $languageCodes
39
	 * @param array $termTypes
40
	 */
41
	public function prefetchFederatedProperties( $rows, array $languageCodes, array $termTypes ) : void {
42
		$resultProperties = $this->extractSummaryProperties( $rows );
43
		if ( empty( $resultProperties ) ) {
44
			return;
45
		}
46
		try {
47
			$this->prefetchingLookup->prefetchTerms(
48
				$resultProperties,
49
				$termTypes,
50
				$languageCodes
51
			);
52
		} catch ( FederatedPropertiesException $ex ) {
53
			wfLogWarning(
54
				__METHOD__ . ': Prefetching failed for federated properties: ' . implode( ',', $resultProperties )
55
			);
56
		}
57
	}
58
59
	/**
60
	 * @param IResultWrapper|array $result
61
	 * @return PropertyId[]
62
	 */
63
	public function extractSummaryProperties( $result ) : array {
64
		$propertyIds = [];
65
		foreach ( $result as $revisionRow ) {
66
67
			$comment = $this->getCommentText( $revisionRow );
68
			if ( $comment === null ) {
69
				continue;
70
			}
71
72
			$matches = [];
73
			preg_match( self::PROPERTY_SUMMARY_REGEXP, $comment, $matches );
74
			if ( count( $matches ) === 3 ) {
75
				$propertyId = $matches[2];
76
				$propertyIds[] = new PropertyId( $propertyId );
77
			}
78
		}
79
		return $propertyIds;
80
	}
81
82
	private function getCommentText( $revisionRow ) {
83
		if ( $revisionRow === null ) {
84
			return null;
85
		}
86
87
		if ( $revisionRow instanceof RevisionRecord ) {
0 ignored issues
show
Bug introduced by
The class MediaWiki\Storage\RevisionRecord does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
88
			$comment = $revisionRow->getComment();
89
			return $comment === null ? null : $comment->text;
90
		}
91
92
		if ( property_exists( $revisionRow, 'rc_comment_text' ) ) {
93
			return $revisionRow->rc_comment_text;
94
		} elseif ( property_exists( $revisionRow, 'rev_comment_text' ) ) {
95
			return $revisionRow->rev_comment_text;
96
		}
97
98
		throw new LogicException( 'Rows should have either rc_comment_text or rev_comment_text field' );
99
	}
100
}
101