GlossaryCacheRebuilder::rebuild()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
namespace SG\Maintenance;
4
5
use SG\PropertyRegistrationHelper;
6
use SG\Cache\GlossaryCache;
7
8
use SMWUpdateJob as UpdateJob;
9
use SMW\Store;
10
11
use SMWQuery as Query;
12
use SMWSomeProperty as SomeProperty;
13
use SMWDIProperty as DIProperty;
14
use SMWThingDescription as ThingDescription;
15
16
/**
17
 * Part of the `rebuildGlossaryCache.php` maintenance script
18
 *
19
 * @ingroup SG
20
 *
21
 * @license GNU GPL v2+
22
 * @since 1.1
23
 *
24
 * @author mwjames
25
 */
26
class GlossaryCacheRebuilder {
27
28
	/** @var Store */
29
	private $store;
30
31
	/** @var GlossaryCache */
32
	private $glossaryCache;
33
34
	private $reporter = null;
35
	private $rebuildCount = 0;
36
	private $verbose = false;
37
38
	/**
39
	 * @since 1.1
40
	 *
41
	 * @param Store $store
42
	 * @param GlossaryCache $glossaryCache
43
	 * @param $reporter
44
	 */
45 3
	public function __construct( Store $store, GlossaryCache $glossaryCache, $reporter = null ) {
46 3
		$this->store = $store;
47 3
		$this->glossaryCache = $glossaryCache;
48 3
		$this->reporter = $reporter; // Should be a MessageReporter instance
49 3
	}
50
51
	/**
52
	 * @since 1.1
53
	 *
54
	 * @param array $options
55
	 */
56 2
	public function setParameters( array $options ) {
57 2
		$this->verbose = array_key_exists( 'verbose', $options );
58 2
	}
59
60
	/**
61
	 * @since 1.1
62
	 *
63
	 * @return int
64
	 */
65 1
	public function getRebuildCount() {
66 1
		return $this->rebuildCount;
67
	}
68
69
	/**
70
	 * @since 1.1
71
	 *
72
	 * @return boolean
73
	 */
74 2
	public function rebuild() {
75
76 2
		$pages = $this->store->getQueryResult( $this->buildQuery() )->getResults();
77
78 2
		$this->removeEntitiesFromCache( $pages );
79 2
		$this->updateSelectedPages( $pages );
80
81 2
		return true;
82
	}
83
84 2
	private function updateSelectedPages( array $pages ) {
85
86 2
		$titleCache = array();
87
88 2
		foreach ( $pages as $page ) {
89
90 2
			$title = $page->getTitle();
91
92 2
			if ( $title !== null && !isset( $titleCache[ $title->getPrefixedDBkey() ] ) ) {
93
94 2
				$this->rebuildCount++;
95
96 2
				$this->reportMessage( "($this->rebuildCount) Processing page " . $title->getPrefixedDBkey() . " ...\n", $this->verbose );
97
98
				// FIXME Wrong approach, users outside of smw-core should not
99
				// directly create an instance and instead use a factory for
100
				// that purpose such as JobFactory::newUpdateJob( ... )
101 2
				$updatejob = new UpdateJob( $title );
102 2
				$updatejob->run();
103
104 2
				$titleCache[ $title->getPrefixedDBkey() ] = true;
105
			}
106
		}
107
108 2
		$this->reportMessage( "$this->rebuildCount pages refreshed.\n" );
109
110 2
		return true;
111
	}
112
113 2
	private function buildQuery() {
114
115 2
		$description = new SomeProperty(
116 2
			new DIProperty( PropertyRegistrationHelper::SG_TERM ),
117 2
			new ThingDescription()
118
		);
119
120 2
		$countQuery = new Query( $description, false, false );
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
121 2
		$countQuery->querymode = Query::MODE_COUNT;
122
123 2
		$queryResult = $this->store->getQueryResult( $countQuery );
124 2
		$numberOfPages = $queryResult instanceof \SMWQueryResult ? $queryResult->getCountValue() : $queryResult;
0 ignored issues
show
Bug introduced by
The class SMWQueryResult 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...
125
126 2
		$resultQuery = new Query(
127 2
			$description,
128 2
			false,
129 2
			false
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
130
		);
131
132 2
		$resultQuery->setUnboundLimit( $numberOfPages, false );
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::setUnboundLimit() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
133
134 2
		return $resultQuery;
135
	}
136
137 2
	private function removeEntitiesFromCache( array $pages ) {
138
139 2
		$cache = $this->glossaryCache->getCache();
140
141 2
		$cache->delete( $this->glossaryCache->getKeyForLingo() );
142
143 2
		foreach ( $pages as $page ) {
144 2
			$cache->delete( $this->glossaryCache->getKeyForSubject( $page ) );
145
		}
146
147 2
		$this->reportMessage( "\n" . ( count( $pages ) + 1 ) . " cache entities deleted.\n\n" );
148 2
	}
149
150
	/**
151
	 * @codeCoverageIgnore
152
	 */
153
	private function reportMessage( $message, $output = true ) {
154
		if ( is_callable( $this->reporter ) && $output ) {
155
			call_user_func( $this->reporter, $message );
156
		}
157
	}
158
159
}
160