2012-02-29 10:50:36 +00:00
|
|
|
<?php
|
|
|
|
|
2020-04-19 22:41:19 +00:00
|
|
|
namespace PageImages;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
|
|
|
use FauxRequest;
|
|
|
|
use File;
|
|
|
|
use IContextSource;
|
2022-06-15 16:08:43 +00:00
|
|
|
use MediaWiki\Api\Hook\ApiOpenSearchSuggestHook;
|
|
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
|
|
|
use MediaWiki\Hook\InfoActionHook;
|
2020-04-04 15:34:58 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-04-19 22:41:19 +00:00
|
|
|
use OutputPage;
|
|
|
|
use Skin;
|
|
|
|
use Title;
|
2020-04-04 15:34:58 +00:00
|
|
|
|
2015-11-16 14:59:34 +00:00
|
|
|
/**
|
2018-05-25 04:42:29 +00:00
|
|
|
* @license WTFPL
|
2015-11-17 08:54:24 +00:00
|
|
|
* @author Max Semenik
|
2015-11-16 14:59:34 +00:00
|
|
|
* @author Brad Jorsch
|
2017-11-24 07:33:49 +00:00
|
|
|
* @author Thiemo Kreuz
|
2015-11-16 14:59:34 +00:00
|
|
|
*/
|
2022-06-15 16:08:43 +00:00
|
|
|
class PageImages implements
|
|
|
|
ApiOpenSearchSuggestHook,
|
|
|
|
BeforePageDisplayHook,
|
|
|
|
InfoActionHook
|
|
|
|
{
|
2020-04-20 13:34:12 +00:00
|
|
|
/**
|
|
|
|
* @const value for free images
|
|
|
|
*/
|
2020-05-19 23:27:08 +00:00
|
|
|
public const LICENSE_FREE = 'free';
|
2020-04-20 13:34:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @const value for images with any type of license
|
|
|
|
*/
|
2020-05-19 23:27:08 +00:00
|
|
|
public const LICENSE_ANY = 'any';
|
2015-11-16 14:59:34 +00:00
|
|
|
|
2013-11-04 22:00:38 +00:00
|
|
|
/**
|
2016-11-10 00:02:00 +00:00
|
|
|
* Page property used to store the best page image information.
|
|
|
|
* If the best image is the same as the best image with free license,
|
|
|
|
* then nothing is stored under this property.
|
2017-01-23 21:53:05 +00:00
|
|
|
* Note changing this value is not advised as it will invalidate all
|
|
|
|
* existing page property names on a production instance
|
|
|
|
* and cause them to be regenerated.
|
2016-11-10 00:02:00 +00:00
|
|
|
* @see PageImages::PROP_NAME_FREE
|
2013-11-04 22:00:38 +00:00
|
|
|
*/
|
2020-05-19 23:27:08 +00:00
|
|
|
public const PROP_NAME = 'page_image';
|
2017-01-31 12:59:16 +00:00
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
/**
|
|
|
|
* Page property used to store the best free page image information
|
2017-01-23 21:53:05 +00:00
|
|
|
* Note changing this value is not advised as it will invalidate all
|
|
|
|
* existing page property names on a production instance
|
|
|
|
* and cause them to be regenerated.
|
2016-11-10 00:02:00 +00:00
|
|
|
*/
|
2020-05-19 23:27:08 +00:00
|
|
|
public const PROP_NAME_FREE = 'page_image_free';
|
2016-11-10 00:02:00 +00:00
|
|
|
|
|
|
|
/**
|
2017-01-23 21:53:05 +00:00
|
|
|
* Get property name used in page_props table. When a page image
|
|
|
|
* is stored it will be stored under this property name on the corresponding
|
|
|
|
* article.
|
2016-11-10 00:02:00 +00:00
|
|
|
*
|
|
|
|
* @param bool $isFree Whether the image is a free-license image
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getPropName( $isFree ) {
|
|
|
|
return $isFree ? self::PROP_NAME_FREE : self::PROP_NAME;
|
|
|
|
}
|
2013-11-04 22:00:38 +00:00
|
|
|
|
2020-04-20 13:34:12 +00:00
|
|
|
/**
|
|
|
|
* Get property names used in page_props table
|
|
|
|
*
|
|
|
|
* If the license is free, then only the free property name will be returned,
|
|
|
|
* otherwise both free and non-free property names will be returned. That's
|
|
|
|
* because we save the image name only once if it's free and the best image.
|
|
|
|
*
|
|
|
|
* @param string $license either LICENSE_FREE or LICENSE_ANY,
|
|
|
|
* specifying whether to return the non-free property name or not
|
|
|
|
* @return string|array
|
|
|
|
*/
|
|
|
|
public static function getPropNames( $license ) {
|
|
|
|
if ( $license === self::LICENSE_FREE ) {
|
|
|
|
return self::getPropName( true );
|
|
|
|
}
|
|
|
|
return [ self::getPropName( true ), self::getPropName( false ) ];
|
|
|
|
}
|
|
|
|
|
2013-11-04 22:00:38 +00:00
|
|
|
/**
|
|
|
|
* Returns page image for a given title
|
|
|
|
*
|
2017-09-01 04:54:58 +00:00
|
|
|
* @param Title $title Title to get page image for
|
2013-11-04 22:00:38 +00:00
|
|
|
*
|
2014-06-18 19:58:12 +00:00
|
|
|
* @return File|bool
|
2013-11-04 22:00:38 +00:00
|
|
|
*/
|
|
|
|
public static function getPageImage( Title $title ) {
|
2019-10-31 19:38:11 +00:00
|
|
|
// Do not query for special pages or other titles never in the database
|
|
|
|
if ( !$title->canExist() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:31:18 +00:00
|
|
|
if ( $title->inNamespace( NS_FILE ) ) {
|
2020-04-04 15:34:58 +00:00
|
|
|
return MediaWikiServices::getInstance()->getRepoGroup()->findFile( $title );
|
2019-08-15 12:31:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 19:38:11 +00:00
|
|
|
if ( !$title->exists() ) {
|
|
|
|
// No page id to select from
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-24 05:30:58 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2016-12-03 01:57:30 +00:00
|
|
|
$fileName = $dbr->selectField( 'page_props',
|
2019-02-28 20:21:11 +00:00
|
|
|
'pp_value',
|
2017-05-30 19:49:44 +00:00
|
|
|
[
|
|
|
|
'pp_page' => $title->getArticleID(),
|
|
|
|
'pp_propname' => [ self::PROP_NAME, self::PROP_NAME_FREE ]
|
|
|
|
],
|
2016-12-03 01:57:30 +00:00
|
|
|
__METHOD__,
|
|
|
|
[ 'ORDER BY' => 'pp_propname' ]
|
2013-11-04 22:00:38 +00:00
|
|
|
);
|
2015-11-16 14:59:34 +00:00
|
|
|
|
2014-06-18 19:58:12 +00:00
|
|
|
$file = false;
|
2016-12-03 01:57:30 +00:00
|
|
|
if ( $fileName ) {
|
2020-04-04 15:34:58 +00:00
|
|
|
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $fileName );
|
2013-11-04 22:00:38 +00:00
|
|
|
}
|
2015-11-16 14:59:34 +00:00
|
|
|
|
2013-11-04 22:00:38 +00:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2014-05-02 06:29:28 +00:00
|
|
|
/**
|
|
|
|
* InfoAction hook handler, adds the page image to the info=action page
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2014-05-02 06:29:28 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/InfoAction
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2017-12-06 21:02:05 +00:00
|
|
|
* @param IContextSource $context Context, used to extract the title of the page
|
|
|
|
* @param array[] &$pageInfo Auxillary information about the page.
|
2014-05-02 06:29:28 +00:00
|
|
|
*/
|
2022-06-15 16:08:43 +00:00
|
|
|
public function onInfoAction( $context, &$pageInfo ) {
|
2016-01-26 18:07:39 +00:00
|
|
|
global $wgThumbLimits;
|
2014-05-02 06:29:28 +00:00
|
|
|
|
|
|
|
$imageFile = self::getPageImage( $context->getTitle() );
|
2014-06-18 19:58:12 +00:00
|
|
|
if ( !$imageFile ) {
|
2014-05-02 06:29:28 +00:00
|
|
|
// The page has no image
|
2016-03-07 08:47:51 +00:00
|
|
|
return;
|
2014-05-02 06:29:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 11:04:14 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
$thumbSetting = $userOptionsLookup->getOption( $context->getUser(), 'thumbsize' );
|
2014-05-02 06:29:28 +00:00
|
|
|
$thumbSize = $wgThumbLimits[$thumbSetting];
|
|
|
|
|
2016-11-21 23:29:28 +00:00
|
|
|
$thumb = $imageFile->transform( [ 'width' => $thumbSize ] );
|
2014-06-18 19:58:12 +00:00
|
|
|
if ( !$thumb ) {
|
2016-03-07 08:47:51 +00:00
|
|
|
return;
|
2014-06-18 19:58:12 +00:00
|
|
|
}
|
|
|
|
$imageHtml = $thumb->toHtml(
|
2016-11-21 23:29:28 +00:00
|
|
|
[
|
2014-05-02 06:29:28 +00:00
|
|
|
'alt' => $imageFile->getTitle()->getText(),
|
|
|
|
'desc-link' => true,
|
2016-11-21 23:29:28 +00:00
|
|
|
]
|
2014-05-02 06:29:28 +00:00
|
|
|
);
|
|
|
|
|
2016-11-21 23:29:28 +00:00
|
|
|
$pageInfo['header-basic'][] = [
|
2014-05-02 06:29:28 +00:00
|
|
|
$context->msg( 'pageimages-info-label' ),
|
|
|
|
$imageHtml
|
2016-11-21 23:29:28 +00:00
|
|
|
];
|
2014-05-02 06:29:28 +00:00
|
|
|
}
|
|
|
|
|
2012-05-08 21:42:07 +00:00
|
|
|
/**
|
2014-11-05 22:18:30 +00:00
|
|
|
* ApiOpenSearchSuggest hook handler, enhances ApiOpenSearch results with this extension's data
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2017-12-06 21:02:05 +00:00
|
|
|
* @param array[] &$results Array of results to add page images too
|
2012-05-08 21:42:07 +00:00
|
|
|
*/
|
2022-06-15 16:08:43 +00:00
|
|
|
public function onApiOpenSearchSuggest( &$results ) {
|
2012-03-08 14:01:00 +00:00
|
|
|
global $wgPageImagesExpandOpenSearchXml;
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-03-08 14:01:00 +00:00
|
|
|
if ( !$wgPageImagesExpandOpenSearchXml || !count( $results ) ) {
|
2016-03-07 08:47:51 +00:00
|
|
|
return;
|
2012-03-08 14:01:00 +00:00
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2012-03-08 14:01:00 +00:00
|
|
|
$pageIds = array_keys( $results );
|
2014-09-19 18:41:48 +00:00
|
|
|
$data = self::getImages( $pageIds, 50 );
|
2012-03-08 14:01:00 +00:00
|
|
|
foreach ( $pageIds as $id ) {
|
2014-09-19 18:41:48 +00:00
|
|
|
if ( isset( $data[$id]['thumbnail'] ) ) {
|
|
|
|
$results[$id]['image'] = $data[$id]['thumbnail'];
|
2012-03-08 14:01:00 +00:00
|
|
|
} else {
|
|
|
|
$results[$id]['image'] = null;
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 21:42:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 18:41:48 +00:00
|
|
|
/**
|
|
|
|
* SpecialMobileEditWatchlist::images hook handler, adds images to mobile watchlist A-Z view
|
|
|
|
*
|
2017-12-06 21:02:05 +00:00
|
|
|
* @param IContextSource $context Context object. Ignored
|
|
|
|
* @param array[] $watchlist Array of relevant pages on the watchlist, sorted by namespace
|
|
|
|
* @param array[] &$images Array of images to populate
|
2014-09-19 18:41:48 +00:00
|
|
|
*/
|
2017-12-06 22:11:25 +00:00
|
|
|
public static function onSpecialMobileEditWatchlistImages(
|
2016-11-21 23:29:28 +00:00
|
|
|
IContextSource $context, array $watchlist, array &$images
|
2014-09-19 18:41:48 +00:00
|
|
|
) {
|
2016-11-21 23:29:28 +00:00
|
|
|
$ids = [];
|
2014-09-19 18:41:48 +00:00
|
|
|
foreach ( $watchlist as $ns => $pages ) {
|
|
|
|
foreach ( array_keys( $pages ) as $dbKey ) {
|
|
|
|
$title = Title::makeTitle( $ns, $dbKey );
|
|
|
|
// Getting page ID here is safe because SpecialEditWatchlist::getWatchlistInfo()
|
|
|
|
// uses LinkBatch
|
|
|
|
$id = $title->getArticleID();
|
|
|
|
if ( $id ) {
|
|
|
|
$ids[$id] = $dbKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2014-09-19 18:41:48 +00:00
|
|
|
$data = self::getImages( array_keys( $ids ) );
|
|
|
|
foreach ( $data as $id => $page ) {
|
|
|
|
if ( isset( $page['pageimage'] ) ) {
|
|
|
|
$images[ $page['ns'] ][ $ids[$id] ] = $page['pageimage'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns image information for pages with given ids
|
|
|
|
*
|
2015-10-26 10:41:13 +00:00
|
|
|
* @param int[] $pageIds
|
|
|
|
* @param int $size
|
2015-11-16 14:59:34 +00:00
|
|
|
*
|
2015-10-26 10:41:13 +00:00
|
|
|
* @return array[]
|
2014-09-19 18:41:48 +00:00
|
|
|
*/
|
|
|
|
private static function getImages( array $pageIds, $size = 0 ) {
|
2017-05-26 21:23:42 +00:00
|
|
|
$ret = [];
|
|
|
|
foreach ( array_chunk( $pageIds, ApiBase::LIMIT_SML1 ) as $chunk ) {
|
|
|
|
$request = [
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'pageimages',
|
|
|
|
'piprop' => 'name',
|
|
|
|
'pageids' => implode( '|', $chunk ),
|
|
|
|
'pilimit' => 'max',
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( $size ) {
|
|
|
|
$request['piprop'] = 'thumbnail';
|
|
|
|
$request['pithumbsize'] = $size;
|
|
|
|
}
|
2015-11-16 14:59:34 +00:00
|
|
|
|
2017-05-26 21:23:42 +00:00
|
|
|
$api = new ApiMain( new FauxRequest( $request ) );
|
|
|
|
$api->execute();
|
2015-11-16 14:59:34 +00:00
|
|
|
|
2018-04-25 20:01:49 +00:00
|
|
|
$ret += (array)$api->getResult()->getResultData(
|
|
|
|
[ 'query', 'pages' ], [ 'Strip' => 'base' ]
|
|
|
|
);
|
2014-09-19 18:41:48 +00:00
|
|
|
}
|
2017-05-26 21:23:42 +00:00
|
|
|
return $ret;
|
2014-09-19 18:41:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 12:59:16 +00:00
|
|
|
/**
|
2022-06-15 16:08:43 +00:00
|
|
|
* @param OutputPage $out The page being output.
|
|
|
|
* @param Skin $skin Skin object used to generate the page. Ignored
|
2017-01-31 12:59:16 +00:00
|
|
|
*/
|
2022-06-15 16:08:43 +00:00
|
|
|
public function onBeforePageDisplay( $out, $skin ): void {
|
2022-07-28 15:27:15 +00:00
|
|
|
if ( !$out->getConfig()->get( 'PageImagesOpenGraph' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-28 15:43:55 +00:00
|
|
|
$imageFile = self::getPageImage( $out->getContext()->getTitle() );
|
|
|
|
if ( !$imageFile ) {
|
2021-06-18 03:16:36 +00:00
|
|
|
$fallback = $out->getConfig()->get( 'PageImagesOpenGraphFallbackImage' );
|
|
|
|
if ( $fallback ) {
|
|
|
|
$out->addMeta( 'og:image', wfExpandUrl( $fallback, PROTO_CANONICAL ) );
|
|
|
|
}
|
2017-01-28 15:43:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-26 10:49:57 +00:00
|
|
|
// Open Graph protocol -- https://ogp.me/
|
|
|
|
// Multiple images are supported according to https://ogp.me/#array
|
|
|
|
// See https://developers.facebook.com/docs/sharing/best-practices?locale=en_US#images
|
|
|
|
// See T282065: WhatsApp expects an image <300kB
|
|
|
|
foreach ( [ 1200, 800, 640 ] as $width ) {
|
|
|
|
$thumb = $imageFile->transform( [ 'width' => $width ] );
|
|
|
|
if ( !$thumb ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$out->addMeta( 'og:image', wfExpandUrl( $thumb->getUrl(), PROTO_CANONICAL ) );
|
|
|
|
$out->addMeta( 'og:image:width', strval( $thumb->getWidth() ) );
|
|
|
|
$out->addMeta( 'og:image:height', strval( $thumb->getHeight() ) );
|
2017-01-28 15:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-29 10:50:36 +00:00
|
|
|
}
|