| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Page\File; |
| 4 |
|
| 5 |
use MediaWiki\HookContainer\HookContainer; |
| 6 |
use MediaWiki\HookContainer\HookRunner; |
| 7 |
use MediaWiki\Linker\LinkTarget; |
| 8 |
use MediaWiki\Title\MalformedTitleException; |
| 9 |
use MediaWiki\Title\TitleParser; |
| 10 |
use RepoGroup; |
| 11 |
use Wikimedia\ObjectCache\BagOStuff; |
| 12 |
|
| 13 |
class BadFileLookup { |
| 14 |
|
| 15 |
private $listCallback; |
| 16 |
|
| 17 |
private BagOStuff $cache; |
| 18 |
private RepoGroup $repoGroup; |
| 19 |
private TitleParser $titleParser; |
| 20 |
private HookRunner $hookRunner; |
| 21 |
|
| 22 |
|
| 23 |
private $badFiles; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function __construct( |
| 35 |
callable $listCallback, |
| 36 |
BagOStuff $cache, |
| 37 |
RepoGroup $repoGroup, |
| 38 |
TitleParser $titleParser, |
| 39 |
HookContainer $hookContainer |
| 40 |
) { |
| 41 |
$this->listCallback = $listCallback; |
| 42 |
$this->cache = $cache; |
| 43 |
$this->repoGroup = $repoGroup; |
| 44 |
$this->titleParser = $titleParser; |
| 45 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function isBadFile( $name, ?LinkTarget $contextTitle = null ) { |
| 62 |
|
| 63 |
|
| 64 |
$file = $this->repoGroup->findFile( $name ); |
| 65 |
|
| 66 |
|
| 67 |
if ( $file ) { |
| 68 |
$name = $file->getTitle()->getDBkey(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
$bad = false; |
| 73 |
if ( !$this->hookRunner->onBadImage( $name, $bad ) ) { |
| 74 |
return (bool)$bad; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $this->badFiles === null ) { |
| 78 |
$list = ( $this->listCallback )(); |
| 79 |
$key = $this->cache->makeKey( 'bad-image-list', sha1( $list ) ); |
| 80 |
$this->badFiles = $this->cache->getWithSetCallback( |
| 81 |
$key, |
| 82 |
BagOStuff::TTL_DAY, |
| 83 |
function () use ( $list ) { |
| 84 |
return $this->buildBadFilesList( $list ); |
| 85 |
} |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
return isset( $this->badFiles[$name] ) && ( !$contextTitle || |
| 90 |
!isset( $this->badFiles[$name][$contextTitle->getNamespace()][$contextTitle->getDBkey()] ) ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private function buildBadFilesList( string $list ): array { |
| 98 |
$ret = []; |
| 99 |
$lines = explode( "\n", $list ); |
| 100 |
foreach ( $lines as $line ) { |
| 101 |
|
| 102 |
if ( substr( $line, 0, 1 ) !== '*' ) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
$m = []; |
| 108 |
|
| 109 |
if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
$fileDBkey = null; |
| 114 |
$exceptions = []; |
| 115 |
foreach ( $m[1] as $i => $titleText ) { |
| 116 |
try { |
| 117 |
$title = $this->titleParser->parseTitle( $titleText ); |
| 118 |
} catch ( MalformedTitleException $e ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
if ( $i == 0 ) { |
| 122 |
$fileDBkey = $title->getDBkey(); |
| 123 |
} else { |
| 124 |
$exceptions[$title->getNamespace()][$title->getDBkey()] = true; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if ( $fileDBkey !== null ) { |
| 129 |
$ret[$fileDBkey] = $exceptions; |
| 130 |
} |
| 131 |
} |
| 132 |
return $ret; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
class_alias( BadFileLookup::class, 'MediaWiki\\BadFileLookup' ); |