2015-08-20 13:14:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AllinfoboxesQueryPage extends PageQueryPage {
|
|
|
|
|
|
|
|
const LIMIT = 1000;
|
|
|
|
const ALL_INFOBOXES_TYPE = 'AllInfoboxes';
|
2015-09-16 10:14:21 +00:00
|
|
|
private static $subpagesBlacklist = [ 'doc', 'draft', 'test' ];
|
2015-08-20 13:14:23 +00:00
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
parent::__construct( self::ALL_INFOBOXES_TYPE );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isListed() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sortDescending() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isExpensive() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper for calling the querycache table
|
|
|
|
*
|
|
|
|
* @param bool $offset
|
|
|
|
* @param int $limit
|
|
|
|
*
|
|
|
|
* @return ResultWrapper
|
|
|
|
*/
|
|
|
|
public function doQuery( $offset = false, $limit = self::LIMIT ) {
|
|
|
|
return $this->fetchFromCache( $limit, $offset );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the querycache table
|
|
|
|
*
|
|
|
|
* @param bool $limit Only for consistency
|
|
|
|
* @param bool $ignoreErrors Only for consistency
|
|
|
|
*
|
2016-02-22 12:14:41 +00:00
|
|
|
* @return int number of rows updated
|
2015-08-20 13:14:23 +00:00
|
|
|
*/
|
|
|
|
public function recache( $limit = false, $ignoreErrors = true ) {
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
|
|
$infoboxes = $this->reallyDoQuery();
|
|
|
|
|
|
|
|
( new WikiaSQL() )
|
|
|
|
->DELETE( 'querycache' )
|
|
|
|
->WHERE( 'qc_type' )->EQUAL_TO( $this->getName() )
|
|
|
|
->run( $dbw );
|
|
|
|
|
|
|
|
if ( !empty( $infoboxes ) ) {
|
|
|
|
( new WikiaSQL() )
|
|
|
|
->INSERT()->INTO( 'querycache', [
|
|
|
|
'qc_type',
|
|
|
|
'qc_value',
|
|
|
|
'qc_namespace',
|
|
|
|
'qc_title'
|
|
|
|
] )
|
|
|
|
->VALUES( $infoboxes )
|
|
|
|
->run( $dbw );
|
|
|
|
}
|
|
|
|
|
2017-07-12 12:21:06 +00:00
|
|
|
Hooks::run( 'AllInfoboxesQueryRecached' );
|
2015-09-08 17:42:49 +00:00
|
|
|
|
2016-02-22 12:14:41 +00:00
|
|
|
return count( $infoboxes );
|
2015-08-20 13:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries all templates and get only those with portable infoboxes
|
|
|
|
*
|
|
|
|
* @param bool $limit Only for consistency
|
|
|
|
* @param bool $offset Only for consistency
|
|
|
|
*
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
|
|
|
public function reallyDoQuery( $limit = false, $offset = false ) {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE, [ $this->getName(), __METHOD__, 'vslow' ] );
|
|
|
|
$result = ( new WikiaSQL() )
|
|
|
|
->SELECT( 'page_id', 'page_title', 'page_namespace' )
|
|
|
|
->FROM( 'page' )
|
|
|
|
->WHERE( 'page_namespace' )->EQUAL_TO( NS_TEMPLATE )
|
|
|
|
->AND_( 'page_is_redirect' )->EQUAL_TO( 0 )
|
|
|
|
->run( $dbr, function ( ResultWrapper $result ) {
|
|
|
|
$out = [ ];
|
|
|
|
while ( $row = $result->fetchRow() ) {
|
|
|
|
$out[] = [ 'type' => $this->getName(),
|
|
|
|
'pageid' => $row[ 'page_id' ],
|
|
|
|
'ns' => $row[ 'page_namespace' ],
|
|
|
|
'title' => $row[ 'page_title' ] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
} );
|
|
|
|
|
2016-04-06 08:50:01 +00:00
|
|
|
return array_filter( $result, [ $this, 'filterInfoboxes' ] );
|
2015-09-17 09:22:12 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 08:50:01 +00:00
|
|
|
public function addTitleToCache( Title $title ) {
|
|
|
|
if ( !$this->hasInfobox( $title ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-17 09:22:12 +00:00
|
|
|
|
2016-04-06 08:50:01 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
( new WikiaSQL() )
|
|
|
|
->INSERT()->INTO( 'querycache', [
|
|
|
|
'qc_type',
|
|
|
|
'qc_value',
|
|
|
|
'qc_namespace',
|
|
|
|
'qc_title'
|
|
|
|
] )
|
|
|
|
->VALUES( [ [
|
2016-04-06 09:54:04 +00:00
|
|
|
$this->getName(),
|
2016-04-06 08:50:01 +00:00
|
|
|
$title->getArticleID(),
|
|
|
|
$title->getNamespace(),
|
|
|
|
$title->getDBkey(),
|
|
|
|
] ] )
|
|
|
|
->run( $dbw );
|
2015-09-09 13:28:22 +00:00
|
|
|
|
2017-07-12 12:21:06 +00:00
|
|
|
Hooks::run( 'AllInfoboxesQueryRecached' );
|
2016-04-06 08:50:01 +00:00
|
|
|
}
|
2015-09-17 09:22:12 +00:00
|
|
|
|
2016-04-06 09:54:04 +00:00
|
|
|
private function hasInfobox( Title $title ) {
|
2016-04-06 08:50:01 +00:00
|
|
|
// omit subages from blacklist
|
|
|
|
return !(
|
|
|
|
$title->isSubpage() &&
|
|
|
|
in_array( mb_strtolower( $title->getSubpageText() ), self::$subpagesBlacklist )
|
|
|
|
) &&
|
2016-04-06 09:54:04 +00:00
|
|
|
!empty( PortableInfoboxDataService::newFromTitle( $title )->getData() );
|
2016-04-06 08:50:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 09:54:04 +00:00
|
|
|
private function filterInfoboxes( $tmpl ) {
|
2016-04-06 08:50:01 +00:00
|
|
|
$title = Title::newFromID( $tmpl[ 'pageid' ] );
|
|
|
|
|
|
|
|
return $title &&
|
|
|
|
$title->exists() &&
|
|
|
|
$this->hasInfobox( $title );
|
2015-08-20 13:14:23 +00:00
|
|
|
}
|
|
|
|
}
|