$summary = parent::getDefinitionSummary( $context );
$summary[] = [
'pages' => $this->getPages( $context ),
- // Includes SHA1 of content
+ // Includes meta data of current revisions
'titleInfo' => $this->getTitleInfo( $context ),
];
return $summary;
// For user modules, don't needlessly load if there are no non-empty pages
if ( $this->getGroup() === 'user' ) {
foreach ( $revisions as $revision ) {
- if ( $revision['rev_len'] > 0 ) {
+ if ( $revision['page_len'] > 0 ) {
// At least one non-empty page, module should be loaded
return false;
}
return count( $revisions ) === 0;
}
+ private function setTitleInfo( $key, array $titleInfo ) {
+ $this->titleInfo[$key] = $titleInfo;
+ }
+
/**
* Get the information about the wiki pages for a given context.
* @param ResourceLoaderContext $context
- * @return array Keyed by page name. Contains arrays with 'rev_len' and 'rev_sha1' keys
+ * @return array Keyed by page name
*/
protected function getTitleInfo( ResourceLoaderContext $context ) {
$dbr = $this->getDB();
return [];
}
- $pages = $this->getPages( $context );
- $key = implode( '|', array_keys( $pages ) );
+ $pageNames = array_keys( $this->getPages( $context ) );
+ sort( $pageNames );
+ $key = implode( '|', $pageNames );
if ( !isset( $this->titleInfo[$key] ) ) {
- $this->titleInfo[$key] = [];
- $batch = new LinkBatch;
- foreach ( $pages as $titleText => $options ) {
- $batch->addObj( Title::newFromText( $titleText ) );
+ $this->titleInfo[$key] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
+ }
+ return $this->titleInfo[$key];
+ }
+
+ protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
+ $titleInfo = [];
+ $batch = new LinkBatch;
+ foreach ( $pages as $titleText ) {
+ $title = Title::newFromText( $titleText );
+ if ( $title ) {
+ // Page name may be invalid if user-provided (e.g. gadgets)
+ $batch->addObj( $title );
+ }
+ }
+ if ( !$batch->isEmpty() ) {
+ $res = $db->select( 'page',
+ // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
+ [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
+ $batch->constructSet( 'page', $db ),
+ $fname
+ );
+ 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()] = [
+ 'page_len' => $row->page_len,
+ 'page_latest' => $row->page_latest,
+ 'page_touched' => $row->page_touched,
+ ];
}
+ }
+ return $titleInfo;
+ }
- if ( !$batch->isEmpty() ) {
- $res = $dbr->select( [ 'page', 'revision' ],
- // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
- [ 'page_namespace', 'page_title', 'page_touched', 'rev_len', 'rev_sha1' ],
- $batch->constructSet( 'page', $dbr ),
- __METHOD__,
- [],
- [ 'revision' => [ 'INNER JOIN', [ 'page_latest=rev_id' ] ] ]
- );
- 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 );
- $this->titleInfo[$key][$title->getPrefixedText()] = [
- 'rev_len' => $row->rev_len,
- 'rev_sha1' => $row->rev_sha1,
- 'page_touched' => $row->page_touched,
- ];
+ /**
+ * @since 1.28
+ * @param ResourceLoaderContext $context
+ * @param IDatabase $db
+ * @param string[] $modules
+ */
+ public static function preloadTitleInfo(
+ ResourceLoaderContext $context, IDatabase $db, array $moduleNames
+ ) {
+ $rl = $context->getResourceLoader();
+ // getDB() can be overridden to point to a foreign database.
+ // For now, only preload local. In the future, we could preload by wikiID.
+ $allPages = [];
+ $wikiModules = [];
+ foreach ( $moduleNames as $name ) {
+ $module = $rl->getModule( $name );
+ if ( $module instanceof self ) {
+ $mDB = $module->getDB();
+ // Subclasses may disable getDB and implement getTitleInfo differently
+ if ( $mDB && $mDB->getWikiID() === $db->getWikiID() ) {
+ $wikiModules[] = $module;
+ $allPages += $module->getPages( $context );
}
}
}
- return $this->titleInfo[$key];
+ $allInfo = static::fetchTitleInfo( $db, array_keys( $allPages ), __METHOD__ );
+ foreach ( $wikiModules as $module ) {
+ $pages = $module->getPages( $context );
+ // Before we intersect, map the names to canonical form (T145673).
+ $intersect = [];
+ foreach ( $pages as $page => $unused ) {
+ $title = Title::newFromText( $page );
+ if ( $title ) {
+ $intersect[ $title->getPrefixedText() ] = 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 ]
+ );
+ }
+ }
+ $info = array_intersect_key( $allInfo, $intersect );
+
+ $pageNames = array_keys( $pages );
+ sort( $pageNames );
+ $key = implode( '|', $pageNames );
+ $module->setTitleInfo( $key, $info );
+ }
+ return $allInfo;
}
/**