MediaWiki  1.34.0
PageImages.php
Go to the documentation of this file.
1 <?php
2 
9 class 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 
54  $dbr = wfGetDB( DB_REPLICA );
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 
140  public static function onSpecialMobileEditWatchlistImages(
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 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
$wgThumbLimits
$wgThumbLimits
Adjust thumbnails on image pages according to a user setting.
Definition: DefaultSettings.php:1474
PROTO_CANONICAL
const PROTO_CANONICAL
Definition: Defines.php:203
PageImages\PROP_NAME_FREE
const PROP_NAME_FREE
Page property used to store the best free page image information Note changing this value is not advi...
Definition: PageImages.php:28
PageImages\onSpecialMobileEditWatchlistImages
static onSpecialMobileEditWatchlistImages(IContextSource $context, array $watchlist, array &$images)
SpecialMobileEditWatchlist::images hook handler, adds images to mobile watchlist A-Z view.
Definition: PageImages.php:140
NS_FILE
const NS_FILE
Definition: Defines.php:66
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
PageImages\getPageImage
static getPageImage(Title $title)
Returns page image for a given title.
Definition: PageImages.php:49
PageImages
$dbr
$dbr
Definition: testCompression.php:50
PageImages\onBeforePageDisplay
static onBeforePageDisplay(OutputPage &$out, Skin &$skin)
Definition: PageImages.php:202
PageImages\onInfoAction
static onInfoAction(IContextSource $context, &$pageInfo)
InfoAction hook handler, adds the page image to the info=action page.
Definition: PageImages.php:81
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
PageImages\PROP_NAME
const PROP_NAME
Page property used to store the best page image information.
Definition: PageImages.php:20
wfFindFile
wfFindFile( $title, $options=[])
Find a file.
Definition: GlobalFunctions.php:2604
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
$context
$context
Definition: load.php:45
Title
Represents a title within MediaWiki.
Definition: Title.php:42
PageImages\getPropName
static getPropName( $isFree)
Get property name used in page_props table.
Definition: PageImages.php:38
PageImages\onApiOpenSearchSuggest
static onApiOpenSearchSuggest(array &$results)
ApiOpenSearchSuggest hook handler, enhances ApiOpenSearch results with this extension's data.
Definition: PageImages.php:115
PageImages\getImages
static getImages(array $pageIds, $size=0)
Returns image information for pages with given ids.
Definition: PageImages.php:172
Skin
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:38
ApiBase\LIMIT_SML1
const LIMIT_SML1
Slow query, standard limit.
Definition: ApiBase.php:263
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:491