Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
50 / 50 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
MobilePage | |
100.00% |
50 / 50 |
|
100.00% |
10 / 10 |
22 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getRevision | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getLatestTimestamp | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
setLatestTimestamp | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLatestEdit | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
getTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPlaceHolderThumbnailHtml | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
hasThumbnail | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getSmallThumbnailHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPageImageHtml | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | |
3 | namespace MobileFrontend\Models; |
4 | |
5 | use File; |
6 | use MediaWiki\Html\Html; |
7 | use MediaWiki\MediaWikiServices; |
8 | use MediaWiki\Revision\RevisionRecord; |
9 | use MediaWiki\Title\Title; |
10 | |
11 | /** |
12 | * Retrieves information specific to a mobile page |
13 | * Currently this only provides helper functions for creating Page Thumbnail |
14 | * @todo FIXME: Rename this class when its purpose becomes clearer |
15 | */ |
16 | class MobilePage { |
17 | public const SMALL_IMAGE_WIDTH = 220; |
18 | public const TINY_IMAGE_WIDTH = 120; |
19 | |
20 | /** |
21 | * @var Title |
22 | */ |
23 | private $title; |
24 | /** |
25 | * @var RevisionRecord|bool|null |
26 | */ |
27 | private $rev = false; |
28 | /** |
29 | * @var string|bool |
30 | */ |
31 | private $revisionTimestamp; |
32 | /** |
33 | * @var File|false Associated page image file (see PageImages extension) |
34 | */ |
35 | private $file; |
36 | |
37 | /** |
38 | * @param Title $title Page title |
39 | * @param File|false $file Page image file |
40 | */ |
41 | public function __construct( Title $title, $file = false ) { |
42 | $this->title = $title; |
43 | $this->file = $file; |
44 | } |
45 | |
46 | /** |
47 | * @return RevisionRecord|null |
48 | */ |
49 | private function getRevision() { |
50 | if ( $this->rev === false ) { |
51 | $this->rev = MediaWikiServices::getInstance()->getRevisionStore() |
52 | ->getRevisionByTitle( $this->title ); |
53 | } |
54 | return $this->rev; |
55 | } |
56 | |
57 | /** |
58 | * Retrieve timestamp when the page content was last modified. Does not reflect null edits. |
59 | * @return string|bool Timestamp (MW format) or false |
60 | */ |
61 | public function getLatestTimestamp() { |
62 | if ( $this->revisionTimestamp === null ) { |
63 | $rev = $this->getRevision(); |
64 | $this->revisionTimestamp = $rev ? $rev->getTimestamp() : false; |
65 | } |
66 | return $this->revisionTimestamp; |
67 | } |
68 | |
69 | /** |
70 | * Set rev_timestamp of latest edit to this page |
71 | * @param string $timestamp Timestamp (MW format) |
72 | */ |
73 | public function setLatestTimestamp( $timestamp ) { |
74 | $this->revisionTimestamp = $timestamp; |
75 | } |
76 | |
77 | /** |
78 | * Retrieve the last edit to this page. |
79 | * @return array defining edit with keys: |
80 | * - string name |
81 | * - string timestamp (Unix format) |
82 | * - string gender |
83 | */ |
84 | public function getLatestEdit() { |
85 | $rev = $this->getRevision(); |
86 | $edit = [ |
87 | 'timestamp' => false, |
88 | 'name' => '', |
89 | 'gender' => '', |
90 | ]; |
91 | if ( $rev ) { |
92 | $edit['timestamp'] = wfTimestamp( TS_UNIX, $rev->getTimestamp() ); |
93 | $userIdentity = $rev->getUser(); |
94 | if ( $userIdentity ) { |
95 | $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
96 | $edit['name'] = $userIdentity->getName(); |
97 | $edit['gender'] = $userOptionsLookup->getOption( $userIdentity, 'gender' ); |
98 | } |
99 | } |
100 | return $edit; |
101 | } |
102 | |
103 | /** |
104 | * Get the title of the page |
105 | * |
106 | * @return Title |
107 | */ |
108 | public function getTitle() { |
109 | return $this->title; |
110 | } |
111 | |
112 | /** |
113 | * Get a placeholder div container for thumbnails |
114 | * @param string $className Class for element |
115 | * @param string $iconClassName controls size of thumbnail, defaults to empty string |
116 | * @return string |
117 | */ |
118 | public static function getPlaceHolderThumbnailHtml( $className, $iconClassName = '' ) { |
119 | return Html::element( 'div', [ |
120 | 'class' => 'list-thumb list-thumb-placeholder ' . $iconClassName . ' ' . $className, |
121 | ] ); |
122 | } |
123 | |
124 | /** |
125 | * Check whether a page has a thumbnail associated with it |
126 | * |
127 | * @return bool whether the page has an image associated with it |
128 | */ |
129 | public function hasThumbnail() { |
130 | return $this->file ? true : false; |
131 | } |
132 | |
133 | /** |
134 | * Get a small sized thumbnail in div container. |
135 | * |
136 | * @param bool $useBackgroundImage Whether the thumbnail should have a background image |
137 | * @return string |
138 | */ |
139 | public function getSmallThumbnailHtml( $useBackgroundImage = false ) { |
140 | return $this->getPageImageHtml( self::SMALL_IMAGE_WIDTH, $useBackgroundImage ); |
141 | } |
142 | |
143 | /** |
144 | * Get the thumbnail container for getMediumThumbnailHtml() and getSmallThumbnailHtml(). |
145 | * |
146 | * @param int $size the width of the thumbnail |
147 | * @param bool $useBackgroundImage Whether the thumbnail should have a background image |
148 | * @return string |
149 | */ |
150 | private function getPageImageHtml( $size, $useBackgroundImage = false ) { |
151 | if ( !$this->file ) { |
152 | return ''; |
153 | } |
154 | // FIXME: Use more generic classes - no longer restricted to lists |
155 | $thumb = $this->file->transform( [ 'width' => $size ] ); |
156 | if ( $thumb && $thumb->getUrl() ) { |
157 | $className = 'list-thumb '; |
158 | $className .= $thumb->getWidth() > $thumb->getHeight() |
159 | ? 'list-thumb-y' |
160 | : 'list-thumb-x'; |
161 | $props = [ |
162 | 'class' => $className, |
163 | ]; |
164 | |
165 | $imgUrl = wfExpandUrl( $thumb->getUrl(), PROTO_CURRENT ); |
166 | if ( $useBackgroundImage ) { |
167 | $props['style'] = 'background-image: url("' . wfExpandUrl( $imgUrl, PROTO_CURRENT ) . '")'; |
168 | $text = ''; |
169 | } else { |
170 | $props['src'] = $imgUrl; |
171 | $text = $this->title->getText(); |
172 | } |
173 | return Html::element( $useBackgroundImage ? 'div' : 'img', $props, $text ); |
174 | } |
175 | return ''; |
176 | } |
177 | } |