MediaWiki REL1_34
PageImages.php
Go to the documentation of this file.
1<?php
2
9class PageImages {
10
20 const PROP_NAME = 'page_image';
21
28 const PROP_NAME_FREE = 'page_image_free';
29
38 public static function getPropName( $isFree ) {
39 return $isFree ? self::PROP_NAME_FREE : self::PROP_NAME;
40 }
41
49 public static function getPageImage( Title $title ) {
50 if ( $title->inNamespace( NS_FILE ) ) {
51 return wfFindFile( $title );
52 }
53
55 $fileName = $dbr->selectField( 'page_props',
56 'pp_value',
57 [
58 'pp_page' => $title->getArticleID(),
59 'pp_propname' => [ self::PROP_NAME, self::PROP_NAME_FREE ]
60 ],
61 __METHOD__,
62 [ 'ORDER BY' => 'pp_propname' ]
63 );
64
65 $file = false;
66 if ( $fileName ) {
67 $file = wfFindFile( $fileName );
68 }
69
70 return $file;
71 }
72
81 public static function onInfoAction( IContextSource $context, &$pageInfo ) {
82 global $wgThumbLimits;
83
84 $imageFile = self::getPageImage( $context->getTitle() );
85 if ( !$imageFile ) {
86 // The page has no image
87 return;
88 }
89
90 $thumbSetting = $context->getUser()->getOption( 'thumbsize' );
91 $thumbSize = $wgThumbLimits[$thumbSetting];
92
93 $thumb = $imageFile->transform( [ 'width' => $thumbSize ] );
94 if ( !$thumb ) {
95 return;
96 }
97 $imageHtml = $thumb->toHtml(
98 [
99 'alt' => $imageFile->getTitle()->getText(),
100 'desc-link' => true,
101 ]
102 );
103
104 $pageInfo['header-basic'][] = [
105 $context->msg( 'pageimages-info-label' ),
106 $imageHtml
107 ];
108 }
109
115 public static function onApiOpenSearchSuggest( array &$results ) {
116 global $wgPageImagesExpandOpenSearchXml;
117
118 if ( !$wgPageImagesExpandOpenSearchXml || !count( $results ) ) {
119 return;
120 }
121
122 $pageIds = array_keys( $results );
123 $data = self::getImages( $pageIds, 50 );
124 foreach ( $pageIds as $id ) {
125 if ( isset( $data[$id]['thumbnail'] ) ) {
126 $results[$id]['image'] = $data[$id]['thumbnail'];
127 } else {
128 $results[$id]['image'] = null;
129 }
130 }
131 }
132
141 IContextSource $context, array $watchlist, array &$images
142 ) {
143 $ids = [];
144 foreach ( $watchlist as $ns => $pages ) {
145 foreach ( array_keys( $pages ) as $dbKey ) {
146 $title = Title::makeTitle( $ns, $dbKey );
147 // Getting page ID here is safe because SpecialEditWatchlist::getWatchlistInfo()
148 // uses LinkBatch
149 $id = $title->getArticleID();
150 if ( $id ) {
151 $ids[$id] = $dbKey;
152 }
153 }
154 }
155
156 $data = self::getImages( array_keys( $ids ) );
157 foreach ( $data as $id => $page ) {
158 if ( isset( $page['pageimage'] ) ) {
159 $images[ $page['ns'] ][ $ids[$id] ] = $page['pageimage'];
160 }
161 }
162 }
163
172 private static function getImages( array $pageIds, $size = 0 ) {
173 $ret = [];
174 foreach ( array_chunk( $pageIds, ApiBase::LIMIT_SML1 ) as $chunk ) {
175 $request = [
176 'action' => 'query',
177 'prop' => 'pageimages',
178 'piprop' => 'name',
179 'pageids' => implode( '|', $chunk ),
180 'pilimit' => 'max',
181 ];
182
183 if ( $size ) {
184 $request['piprop'] = 'thumbnail';
185 $request['pithumbsize'] = $size;
186 }
187
188 $api = new ApiMain( new FauxRequest( $request ) );
189 $api->execute();
190
191 $ret += (array)$api->getResult()->getResultData(
192 [ 'query', 'pages' ], [ 'Strip' => 'base' ]
193 );
194 }
195 return $ret;
196 }
197
202 public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
203 $imageFile = self::getPageImage( $out->getContext()->getTitle() );
204 if ( !$imageFile ) {
205 return;
206 }
207
208 // See https://developers.facebook.com/docs/sharing/best-practices?locale=en_US#tags
209 $thumb = $imageFile->transform( [ 'width' => 1200 ] );
210 if ( !$thumb ) {
211 return;
212 }
213
214 $out->addMeta( 'og:image', wfExpandUrl( $thumb->getUrl(), PROTO_CANONICAL ) );
215 }
216
217}
$wgThumbLimits
Adjust thumbnails on image pages according to a user setting.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfFindFile( $title, $options=[])
Find a file.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:41
getContext()
Get the base IContextSource object.
WebRequest clone which takes values from a provided array.
This is one of the Core classes and should be read at least once by any new developers.
addMeta( $name, $val)
Add a new "<meta>" tag To add an http-equiv meta tag, precede the name with "http:".
static getPageImage(Title $title)
Returns page image for a given title.
static onSpecialMobileEditWatchlistImages(IContextSource $context, array $watchlist, array &$images)
SpecialMobileEditWatchlist::images hook handler, adds images to mobile watchlist A-Z view.
static onApiOpenSearchSuggest(array &$results)
ApiOpenSearchSuggest hook handler, enhances ApiOpenSearch results with this extension's data.
static getImages(array $pageIds, $size=0)
Returns image information for pages with given ids.
static onInfoAction(IContextSource $context, &$pageInfo)
InfoAction hook handler, adds the page image to the info=action page.
const PROP_NAME_FREE
Page property used to store the best free page image information Note changing this value is not advi...
const PROP_NAME
Page property used to store the best page image information.
static onBeforePageDisplay(OutputPage &$out, Skin &$skin)
static getPropName( $isFree)
Get property name used in page_props table.
The main skin class which provides methods and properties for all other skins.
Definition Skin.php:38
Represents a title within MediaWiki.
Definition Title.php:42
const PROTO_CANONICAL
Definition Defines.php:212
const NS_FILE
Definition Defines.php:75
Interface for objects which can provide a MediaWiki context on request.
$context
Definition load.php:45
const DB_REPLICA
Definition defines.php:25
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42