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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
<?php
namespace SMW\MediaWiki\Hooks;
use File;
use Hooks;
use ParserOptions;
use SMW\ApplicationFactory;
use SMW\Localizer;
use SMW\NamespaceExaminer;
use Title;
use User;
/**
* Fires when a local file upload occurs
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/FileUpload
*
* @license GNU GPL v2+
* @since 1.9
*
* @author mwjames
*/
class FileUpload extends HookHandler {
/**
* @var NamespaceExaminer
*/
private $namespaceExaminer;
/**
* @since 1.9
*
* @param NamespaceExaminer $namespaceExaminer
*/
public function __construct( NamespaceExaminer $namespaceExaminer ) {
$this->namespaceExaminer = $namespaceExaminer;
}
/**
* @since 3.0
*
* @param File $file
* @param boolean $reUploadStatus
*
* @return true
*/
public function process( File $file, $reUploadStatus = false ) {
if ( $this->canProcess( $file->getTitle() ) ) {
$this->doProcess( $file, $reUploadStatus );
}
return true;
}
private function canProcess( $title ) {
return $title !== null && $this->namespaceExaminer->isSemanticEnabled( $title->getNamespace() );
}
private function doProcess( $file, $reUploadStatus = false ) {
$applicationFactory = ApplicationFactory::getInstance();
$filePage = $this->makeFilePage( $file, $reUploadStatus );
// Avoid WikiPage.php: The supplied ParserOptions are not safe to cache.
// Fix the options or set $forceParse = true.
$forceParse = true;
$parserData = $applicationFactory->newParserData(
$file->getTitle(),
$filePage->getParserOutput( $this->makeCanonicalParserOptions(), null, $forceParse )
);
$pageInfoProvider = $applicationFactory->newMwCollaboratorFactory()->newPageInfoProvider(
$filePage
);
$propertyAnnotatorFactory = $applicationFactory->singleton( 'PropertyAnnotatorFactory' );
$semanticData = $parserData->getSemanticData();
$semanticData->setOption( 'is.fileupload', true );
$propertyAnnotator = $propertyAnnotatorFactory->newNullPropertyAnnotator(
$semanticData
);
$propertyAnnotator = $propertyAnnotatorFactory->newPredefinedPropertyAnnotator(
$propertyAnnotator,
$pageInfoProvider
);
$propertyAnnotator->addAnnotation();
// 2.4+
Hooks::run( 'SMW::FileUpload::BeforeUpdate', [ $filePage, $semanticData ] );
$parserData->setOrigin( 'FileUpload' );
$parserData->pushSemanticDataToParserOutput();
$parserData->updateStore( true );
return true;
}
private function makeFilePage( $file, $reUploadStatus ) {
$filePage = ApplicationFactory::getInstance()->newPageCreator()->createFilePage(
$file->getTitle()
);
$filePage->setFile( $file );
$filePage->smwFileReUploadStatus = $reUploadStatus;
return $filePage;
}
/**
* Anonymous user with default preferences and content language
*/
private function makeCanonicalParserOptions() {
return ParserOptions::newFromUserAndLang(
new User(),
Localizer::getInstance()->getContentLanguage()
);
}
}
|