* @author Roan Kattouw
*/
+use MediaWiki\Linker\LinkTarget;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IDatabase;
// Origin defaults to users with sitewide authority
protected $origin = self::ORIGIN_USER_SITEWIDE;
- // In-process cache for title info
+ // In-process cache for title info, structured as an array
+ // [
+ // <batchKey> // Pipe-separated list of sorted keys from getPages
+ // => [
+ // <titleKey> => [ // Normalised title key
+ // 'page_len' => ..,
+ // 'page_latest' => ..,
+ // 'page_touched' => ..,
+ // ]
+ // ]
+ // ]
+ // @see self::fetchTitleInfo()
+ // @see self::makeTitleKey()
protected $titleInfo = [];
// List of page names that contain CSS
return count( $revisions ) === 0;
}
- private function setTitleInfo( $key, array $titleInfo ) {
- $this->titleInfo[$key] = $titleInfo;
+ private function setTitleInfo( $batchKey, array $titleInfo ) {
+ $this->titleInfo[$batchKey] = $titleInfo;
+ }
+
+ private static function makeTitleKey( LinkTarget $title ) {
+ // Used for keys in titleInfo.
+ return "{$title->getNamespace()}:{$title->getDBkey()}";
}
/**
$pageNames = array_keys( $this->getPages( $context ) );
sort( $pageNames );
- $key = implode( '|', $pageNames );
- if ( !isset( $this->titleInfo[$key] ) ) {
- $this->titleInfo[$key] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
+ $batchKey = implode( '|', $pageNames );
+ if ( !isset( $this->titleInfo[$batchKey] ) ) {
+ $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
}
- return $this->titleInfo[$key];
+ return $this->titleInfo[$batchKey];
}
protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
foreach ( $res as $row ) {
// Avoid including ids or timestamps of revision/page tables so
// that versions are not wasted
- $title = Title::makeTitle( $row->page_namespace, $row->page_title );
- $titleInfo[$title->getPrefixedText()] = [
+ $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
+ $titleInfo[ self::makeTitleKey( $title ) ] = [
'page_len' => $row->page_len,
'page_latest' => $row->page_latest,
'page_touched' => $row->page_touched,
$pages = $wikiModule->getPages( $context );
// Before we intersect, map the names to canonical form (T145673).
$intersect = [];
- foreach ( $pages as $page => $unused ) {
- $title = Title::newFromText( $page );
+ foreach ( $pages as $pageName => $unused ) {
+ $title = Title::newFromText( $pageName );
if ( $title ) {
- $intersect[ $title->getPrefixedText() ] = 1;
+ $intersect[ self::makeTitleKey( $title ) ] = 1;
} else {
// Page name may be invalid if user-provided (e.g. gadgets)
$rl->getLogger()->info(
'Invalid wiki page title "{title}" in ' . __METHOD__,
- [ 'title' => $page ]
+ [ 'title' => $pageName ]
);
}
}
$info = array_intersect_key( $allInfo, $intersect );
$pageNames = array_keys( $pages );
sort( $pageNames );
- $key = implode( '|', $pageNames );
- $wikiModule->setTitleInfo( $key, $info );
+ $batchKey = implode( '|', $pageNames );
+ $wikiModule->setTitleInfo( $batchKey, $info );
}
}
$this->assertInstanceOf( ResourceLoaderWikiModule::class, $module );
}
+ private function prepareTitleInfo( array $mockInfo ) {
+ $module = TestingAccessWrapper::newFromClass( ResourceLoaderWikiModule::class );
+ $info = [];
+ foreach ( $mockInfo as $key => $val ) {
+ $info[ $module->makeTitleKey( Title::newFromText( $key ) ) ] = $val;
+ }
+ return $info;
+ }
+
public static function provideConstructor() {
return [
// Nothing
->getMock();
$module->expects( $this->any() )
->method( 'getTitleInfo' )
- ->will( $this->returnValue( $titleInfo ) );
+ ->will( $this->returnValue( $this->prepareTitleInfo( $titleInfo ) ) );
$module->expects( $this->any() )
->method( 'getGroup' )
->will( $this->returnValue( $group ) );
'MediaWiki:Common.css' => [ 'type' => 'styles' ],
'mediawiki: fallback.css' => [ 'type' => 'styles' ],
];
- $titleInfo = [
+ $titleInfo = $this->prepareTitleInfo( [
'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
- ];
+ ] );
$expected = $titleInfo;
$module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
// doing an intersect on the canonical result, producing an empty array.
'mediawiki: fallback.css' => [ 'type' => 'styles' ],
];
- $titleInfo = [
+ $titleInfo = $this->prepareTitleInfo( [
'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
- ];
+ ] );
$expected = $titleInfo;
$module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )