Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
MobilePage
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
7 / 7
18
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getRevision
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getLatestTimestamp
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getLatestEdit
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 getTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSmallThumbnailHtml
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPageImageHtml
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace MobileFrontend\Models;
4
5use MediaWiki\FileRepo\File\File;
6use MediaWiki\Html\Html;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Revision\RevisionRecord;
9use 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 */
16class 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     * Retrieve the last edit to this page.
71     * @return array defining edit with keys:
72     * - string name
73     * - string timestamp (Unix format)
74     * - string gender
75     */
76    public function getLatestEdit() {
77        $rev = $this->getRevision();
78        $edit = [
79            'timestamp' => false,
80            'name' => '',
81            'gender' => '',
82        ];
83        if ( $rev ) {
84            $edit['timestamp'] = wfTimestamp( TS_UNIX, $rev->getTimestamp() );
85            $userIdentity = $rev->getUser();
86            if ( $userIdentity ) {
87                $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
88                $edit['name'] = $userIdentity->getName();
89                $edit['gender'] = $userOptionsLookup->getOption( $userIdentity, 'gender' );
90            }
91        }
92        return $edit;
93    }
94
95    /**
96     * Get the title of the page
97     *
98     * @return Title
99     */
100    public function getTitle() {
101        return $this->title;
102    }
103
104    /**
105     * Get a small sized thumbnail in div container.
106     *
107     * @param bool $useBackgroundImage Whether the thumbnail should have a background image
108     * @return string
109     */
110    public function getSmallThumbnailHtml( $useBackgroundImage = false ) {
111        return $this->getPageImageHtml( self::SMALL_IMAGE_WIDTH, $useBackgroundImage );
112    }
113
114    /**
115     * Get the thumbnail container for getMediumThumbnailHtml() and getSmallThumbnailHtml().
116     *
117     * @param int $size the width of the thumbnail
118     * @param bool $useBackgroundImage Whether the thumbnail should have a background image
119     * @return string
120     */
121    private function getPageImageHtml( $size, $useBackgroundImage = false ) {
122        if ( !$this->file ) {
123            return '';
124        }
125        // FIXME: Use more generic classes - no longer restricted to lists
126        $thumb = $this->file->transform( [ 'width' => $size ] );
127        if ( $thumb && $thumb->getUrl() ) {
128            $className = 'list-thumb ';
129            $className .= $thumb->getWidth() > $thumb->getHeight()
130                ? 'list-thumb-y'
131                : 'list-thumb-x';
132            $props = [
133                'class' => $className,
134            ];
135
136            $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
137            $imgUrl = $urlUtils->expand( (string)$thumb->getUrl(), PROTO_CURRENT ) ?? '';
138            if ( $useBackgroundImage ) {
139                $props['style'] = 'background-image: url("' . $urlUtils->expand( $imgUrl, PROTO_CURRENT ) . '")';
140                $text = '';
141            } else {
142                $props['src'] = $imgUrl;
143                $text = $this->title->getText();
144            }
145            return Html::element( $useBackgroundImage ? 'div' : 'img', $props, $text );
146        }
147        return '';
148    }
149}