forked from wikimedia/mediawiki-extensions-Widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartyResourceWiki.php
More file actions
91 lines (79 loc) · 2.51 KB
/
Copy pathSmartyResourceWiki.php
File metadata and controls
91 lines (79 loc) · 2.51 KB
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
<?php
use MediaWiki\MediaWikiServices;
/**
* Class holding functions for displaying widgets.
*/
class SmartyResourceWiki extends Smarty_Resource_Custom {
/** @var Parser */
private Parser $parser;
/**
* @param Parser $parser
*/
public function __construct( Parser $parser ) {
$this->parser = $parser;
}
/**
* @param string $widgetName
*
* @return Title
*/
private function makeWidgetTitle( $widgetName ) {
if ( class_exists( 'MediaWiki\Title\Title' ) ) {
// MW 1.40+
$titleClass = 'MediaWiki\Title\Title';
} else {
$titleClass = 'Title';
}
return $titleClass::makeTitleSafe( NS_WIDGET, $widgetName );
}
/**
* @param string $widgetName
* @param string &$widgetCode Set to null to mean not found
* @param int &$mtime Unix timestamp of last mod. Set to null for not found.
*
* @return void
*/
public function fetch( $widgetName, &$widgetCode, &$mtime ) {
global $wgWidgetsUseFlaggedRevs;
$widgetTitle = $this->makeWidgetTitle( $widgetName );
if ( $widgetTitle && $widgetTitle->exists() ) {
if ( $wgWidgetsUseFlaggedRevs ) {
$flaggedWidgetArticle = FlaggableWikiPage::getTitleInstance( $widgetTitle );
$flaggedWidgetArticleRevision = $flaggedWidgetArticle->getStableRev();
if ( $flaggedWidgetArticleRevision ) {
$widgetCode = $flaggedWidgetArticleRevision->getRevText();
// Unclear if ->getTimestamp() or ->getRevTimestamp() makes more sense.
$mtime = wfTimestamp( TS_UNIX, $flaggedWidgetArticleRevision->getTimestamp() );
} else {
$widgetCode = '';
}
} else {
$widgetWikiPage = MediaWikiServices::getInstance()->getWikiPageFactory()
->newFromTitle( $widgetTitle );
$widgetCode = $widgetWikiPage->getContent()->getText();
$mtime = wfTimestamp( TS_UNIX, $widgetWikiPage->getTouched() );
}
// Remove <noinclude> sections and <includeonly> tags from form definition
$widgetCode = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $widgetCode );
$widgetCode = strtr( $widgetCode, [ '<includeonly>' => '', '</includeonly>' => '' ] );
} else {
$widgetCode = null;
$mtime = null;
}
}
/**
* @param string $widgetName
*
* @return bool|int
*/
protected function fetchTimestamp( $widgetName ) {
$widgetTitle = $this->makeWidgetTitle( $widgetName );
if ( $widgetTitle && $widgetTitle->exists() ) {
$widgetArticle = new Article( $widgetTitle, 0 );
$widgetTimestamp = wfTimestamp( TS_UNIX, $widgetArticle->getPage()->getTouched() );
return $widgetTimestamp;
} else {
return false;
}
}
}