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
|
6 |
|
public function __construct( Title $title, DatabaseBase $connection = null, LinkCache $linkCache = null ) { |
58
|
6 |
|
$this->title = $title; |
59
|
6 |
|
$this->connection = $connection; |
60
|
6 |
|
$this->linkCache = $linkCache; |
61
|
6 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @since 1.3 |
65
|
|
|
* |
66
|
|
|
* @param boolean $isDbPageLanguage |
67
|
|
|
*/ |
68
|
5 |
|
public function markAsPageLanguageByDB( $isDbPageLanguage ) { |
69
|
5 |
|
$this->isDbPageLanguage = $isDbPageLanguage; |
70
|
5 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @since 1.3 |
74
|
|
|
* |
75
|
|
|
* @param string $expectedLanguageCode |
76
|
|
|
*/ |
77
|
5 |
|
public function updatePageLanguage( $expectedLanguageCode ) { |
78
|
|
|
|
79
|
5 |
|
if ( !$this->isDbPageLanguage ) { |
80
|
3 |
|
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
|
|
|
} |
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
|
|
|
} |
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
|
|
|
[ |
126
|
1 |
|
'page_lang' => $expectedLanguageCode |
127
|
|
|
], |
128
|
|
|
[ |
129
|
1 |
|
'page_id' => $pageId, |
130
|
1 |
|
'page_lang' => $dbPageLanguage |
131
|
|
|
], |
132
|
1 |
|
__METHOD__ |
133
|
|
|
); |
134
|
1 |
|
} ); |
135
|
1 |
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|