Completed
Push — master ( bb3081...b26d3d )
by mw
9s
created

PageContentLanguageDbModifier::doUpdate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
ccs 14
cts 16
cp 0.875
rs 8.8571
cc 2
eloc 15
nc 2
nop 2
crap 2.0078
1
<?php
2
3
namespace SIL;
4
5
use LinkCache;
6
use Title;
7
use DatabaseBase;
8
9
/**
10
 * Handling Title::getDbPageLanguageCode and Special:PageLanguage to avoid possible
11
 * contradictory results when $wgPageLanguageUseDB is enabled.
12
 *
13
 * If wgPageLanguageUseDB is enabled then the PageContentLanguage hook is not
14
 * going to be called in case Special:PageLanguage assigned a pagelanguage which
15
 * could create a possible deviation between SIL annotation and the stored DB
16
 * `page_lang`.
17
 *
18
 * @license GNU GPL v2+
19
 * @since 1.3
20
 *
21
 * @author mwjames
22
 */
23
class PageContentLanguageDbModifier {
24
25
	/**
26
	 * @var Title
27
	 */
28
	private $title;
29
30
	/**
31
	 * @var DatabaseBase
32
	 */
33
	private $connection;
34
35
	/**
36
	 * @var LinkCache
37
	 */
38
	private $linkCache;
39
40
	/**
41
	 * @var boolean
42
	 */
43
	private $isDbPageLanguage = false;
44
45
	/**
46
	 * @var string|false
47
	 */
48
	private $dbPageLanguage = false;
49
50
	/**
51
	 * @since 1.3
52
	 *
53
	 * @param Title $title
54
	 * @param DatabaseBase|null $connection
55
	 * @param LinkCache|null $linkCache
56
	 */
57 11
	public function __construct( Title $title, DatabaseBase $connection = null, LinkCache $linkCache = null ) {
58 11
		$this->title = $title;
59 11
		$this->connection = $connection;
60 11
		$this->linkCache = $linkCache;
61 11
	}
62
63
	/**
64
	 * @since 1.3
65
	 *
66
	 * @param boolean $isDbPageLanguage
67
	 */
68 10
	public function markAsPageLanguageByDB( $isDbPageLanguage ) {
69 10
		$this->isDbPageLanguage = $isDbPageLanguage;
70 10
	}
71
72
	/**
73
	 * @since 1.3
74
	 *
75
	 * @param string $expectedLanguageCode
76
	 */
77 10
	public function updatePageLanguage( $expectedLanguageCode ) {
78
79 10
		if ( !$this->isDbPageLanguage ) {
80 8
			return null;
81
		}
82
83 2
		$expectedLanguageCode = strtolower( $expectedLanguageCode );
84
85
		// If the pagelanguage added via Special:PageLanguage is different from
86
		// what SIL is expecting then push for a DB update
87 2
		if ( $this->getDbPageLanguageCode() && $this->dbPageLanguage !== $expectedLanguageCode ) {
88 1
			$this->doUpdate( $expectedLanguageCode, $this->dbPageLanguage );
89 1
		}
90 2
	}
91
92
	// @see Title::getDbPageLanguageCode
93 2
	private function getDbPageLanguageCode() {
94
95 2
		if ( $this->linkCache === null ) {
96
			$this->linkCache = LinkCache::singleton();
97
		}
98
99
		// check, if the page language could be saved in the database, and if so and
100
		// the value is not requested already, lookup the page language using LinkCache
101 2
		if ( $this->isDbPageLanguage && $this->dbPageLanguage === false ) {
102 2
			$this->linkCache->addLinkObj( $this->title );
103 2
			$this->dbPageLanguage = $this->linkCache->getGoodLinkFieldObj( $this->title, 'lang' );
104 2
		}
105
106 2
		return $this->dbPageLanguage;
107
	}
108
109
	// @see Special:PageLanguage::onSubmit
110 1
	private function doUpdate( $expectedLanguageCode, $dbPageLanguage ) {
111
112 1
		$connection = $this->connection;
113 1
		$title = $this->title;
114
115 1
		if ( $connection === null ) {
116
			 $connection = wfGetDB( DB_MASTER );
117
		}
118
119 1
		$connection->onTransactionIdle( function() use ( $connection, $expectedLanguageCode, $dbPageLanguage, $title ) {
120
121 1
			$pageId = $title->getArticleID();
122
123 1
			$connection->update(
124 1
				'page',
125
				array(
126
					'page_lang' => $expectedLanguageCode
127 1
				),
128
				array(
129 1
					'page_id'   => $pageId,
130
					'page_lang' => $dbPageLanguage
131 1
				),
132
				__METHOD__
133 1
			);
134 1
		} );
135 1
	}
136
137
}
138