DynamicPageList3/includes/Query.php

2248 lines
56 KiB
PHP
Raw Normal View History

2020-11-22 20:00:48 +00:00
<?php
2021-10-01 22:52:30 +00:00
namespace MediaWiki\Extension\DynamicPageList3;
2020-11-22 20:00:48 +00:00
2021-10-01 22:52:30 +00:00
use DateInterval;
use DateTime;
use Exception;
use ExtensionRegistry;
2021-05-30 18:33:21 +00:00
use MediaWiki\MediaWikiServices;
2021-10-06 18:44:11 +00:00
use MediaWiki\User\UserFactory;
2021-10-01 22:52:30 +00:00
use MWException;
use PoolCounterWorkViaCallback;
use WANObjectCache;
use WikiMap;
use Wikimedia\Rdbms\Database;
2021-10-01 22:52:30 +00:00
use Wikimedia\Rdbms\IDatabase;
2021-05-30 18:33:21 +00:00
2020-11-22 20:00:48 +00:00
class Query {
/**
* Parameters Object
*
2021-10-01 22:52:30 +00:00
* @var Parameters
2020-11-22 20:00:48 +00:00
*/
private $parameters;
/**
* Mediawiki DB Object
*
2021-10-01 22:52:30 +00:00
* @var IDatabase
2020-11-22 20:00:48 +00:00
*/
private $dbr;
2020-11-22 20:00:48 +00:00
/**
* Array of prefixed and escaped table names.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $tableNames = [];
/**
* Parameters that have already been processed.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $parametersProcessed = [];
/**
* Select Fields
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $select = [];
/**
* The generated SQL Query.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
private $sqlQuery = '';
/**
* Selected Fields - An array to look up keys against for speed optimization.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $selectedFields = [];
/**
* Prefixed and escaped table names.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $tables = [];
/**
* Where Clauses
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $where = [];
/**
* Group By Clauses
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $groupBy = [];
/**
* Order By Clauses
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $orderBy = [];
/**
* Join Clauses
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $join = [];
/**
* Limit
*
2021-10-01 22:52:30 +00:00
* @var int|bool
2020-11-22 20:00:48 +00:00
*/
private $limit = false;
/**
* Offset
*
2021-10-01 22:52:30 +00:00
* @var int|bool
2020-11-22 20:00:48 +00:00
*/
private $offset = false;
/**
* Order By Direction
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
private $direction = 'ASC';
/**
* Distinct Results
*
2021-02-22 23:48:01 +00:00
* @var bool
2020-11-22 20:00:48 +00:00
*/
private $distinct = true;
/**
* Character Set Collation
*
2021-10-01 22:52:30 +00:00
* @var string|bool
2020-11-22 20:00:48 +00:00
*/
private $collation = false;
/**
* Number of Rows Found
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
private $foundRows = 0;
/**
* Was the revision auxiliary table select added for firstedit and lastedit?
*
2021-02-22 23:48:01 +00:00
* @var bool
2020-11-22 20:00:48 +00:00
*/
private $revisionAuxWhereAdded = false;
2021-10-06 18:44:11 +00:00
/**
* UserFactory object
*
* @var UserFactory
*/
private $userFactory;
2020-11-22 20:00:48 +00:00
/**
2021-10-01 22:52:30 +00:00
* @param Parameters $parameters
2021-10-06 18:44:11 +00:00
*/
public function __construct( Parameters $parameters ) {
2020-11-22 20:00:48 +00:00
$this->parameters = $parameters;
$this->tableNames = self::getTableNames();
$this->dbr = wfGetDB( DB_REPLICA, 'dpl' );
2021-10-06 18:44:11 +00:00
$this->userFactory = MediaWikiServices::getInstance()->getUserFactory();
2020-11-22 20:00:48 +00:00
}
/**
* Start a query build. Returns found rows.
2020-11-22 20:00:48 +00:00
*
2021-10-01 22:52:30 +00:00
* @param bool $calcRows
* @return array|bool
2020-11-22 20:00:48 +00:00
*/
public function buildAndSelect( bool $calcRows = false ) {
2020-11-22 20:00:48 +00:00
global $wgNonincludableNamespaces;
$options = [];
$parameters = $this->parameters->getAllParameters();
2021-02-22 23:48:01 +00:00
foreach ( $parameters as $parameter => $option ) {
$function = '_' . $parameter;
// Some parameters do not modify the query so we check if the function to modify the query exists first.
2020-11-22 20:00:48 +00:00
$success = true;
2021-02-22 23:48:01 +00:00
if ( method_exists( $this, $function ) ) {
$success = $this->$function( $option );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $success === false ) {
2021-10-01 22:52:30 +00:00
throw new MWException( __METHOD__ . ": SQL Build Error returned from {$function} for " . serialize( $option ) . "." );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->parametersProcessed[$parameter] = true;
}
2021-02-22 23:48:01 +00:00
if ( !$this->parameters->getParameter( 'openreferences' ) ) {
2021-10-01 22:52:30 +00:00
// Add things that are always part of the query.
2021-02-22 23:48:01 +00:00
$this->addTable( 'page', $this->tableNames['page'] );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-12-15 20:22:38 +00:00
'page_namespace' => $this->tableNames['page'] . '.page_namespace',
'page_id' => $this->tableNames['page'] . '.page_id',
'page_title' => $this->tableNames['page'] . '.page_title'
2020-11-22 20:00:48 +00:00
]
);
}
2021-10-01 22:52:30 +00:00
// Always add nonincludeable namespaces.
2021-02-22 23:48:01 +00:00
if ( is_array( $wgNonincludableNamespaces ) && count( $wgNonincludableNamespaces ) ) {
2020-11-22 20:00:48 +00:00
$this->addNotWhere(
[
$this->tableNames['page'] . '.page_namespace' => $wgNonincludableNamespaces
]
);
}
2021-02-22 23:48:01 +00:00
if ( $this->offset !== false ) {
2020-11-22 20:00:48 +00:00
$options['OFFSET'] = $this->offset;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->limit !== false ) {
2020-11-22 20:00:48 +00:00
$options['LIMIT'] = $this->limit;
2022-03-20 02:52:03 +00:00
} elseif ( $this->offset !== false ) {
2021-09-25 02:56:23 +00:00
$options['LIMIT'] = $this->parameters->getParameter( 'count' );
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'openreferences' ) ) {
if ( count( $this->parameters->getParameter( 'imagecontainer' ) ) > 0 ) {
2020-11-22 20:00:48 +00:00
$tables = [
2021-12-15 20:22:38 +00:00
'ic' => 'imagelinks'
2020-11-22 20:00:48 +00:00
];
} else {
$this->addSelect(
[
'pl_namespace',
'pl_title'
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$tables = [
'pagelinks'
];
}
} else {
$tables = $this->tables;
2021-02-22 23:48:01 +00:00
if ( count( $this->groupBy ) ) {
2020-11-22 20:00:48 +00:00
$options['GROUP BY'] = $this->groupBy;
}
2021-02-22 23:48:01 +00:00
if ( count( $this->orderBy ) ) {
2020-11-22 20:00:48 +00:00
$options['ORDER BY'] = $this->orderBy;
2021-02-22 23:48:01 +00:00
foreach ( $options['ORDER BY'] as $key => $value ) {
$options['ORDER BY'][$key] .= ' ' . $this->direction;
2020-11-22 20:00:48 +00:00
}
}
}
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'goal' ) == 'categories' ) {
2020-11-22 20:00:48 +00:00
$categoriesGoal = true;
$fields = [
2020-11-22 20:00:48 +00:00
$this->tableNames['page'] . '.page_id'
];
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$options[] = 'DISTINCT';
} else {
2021-02-22 23:48:01 +00:00
if ( $calcRows ) {
2020-11-22 20:00:48 +00:00
$options[] = 'SQL_CALC_FOUND_ROWS';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->distinct ) {
2020-11-22 20:00:48 +00:00
$options[] = 'DISTINCT';
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$categoriesGoal = false;
$fields = $this->select;
2020-11-22 20:00:48 +00:00
}
$queryError = false;
try {
2021-02-22 23:48:01 +00:00
if ( $categoriesGoal ) {
$res = $this->dbr->select(
2020-11-22 20:00:48 +00:00
$tables,
$fields,
2020-11-22 20:00:48 +00:00
$this->where,
__METHOD__,
$options,
$this->join
);
2021-10-01 22:52:30 +00:00
$pageIds = [];
foreach ( $res as $row ) {
$pageIds[] = $row->page_id;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$query = $this->dbr->selectSQLText(
2020-11-22 20:00:48 +00:00
[
2021-10-01 22:52:30 +00:00
'clgoal' => 'categorylinks'
2020-11-22 20:00:48 +00:00
],
[
'clgoal.cl_to'
],
[
2021-10-01 22:52:30 +00:00
'clgoal.cl_from' => $pageIds
2020-11-22 20:00:48 +00:00
],
__METHOD__,
[
2021-10-01 22:52:30 +00:00
'ORDER BY' => 'clgoal.cl_to ' . $this->direction
2020-11-22 20:00:48 +00:00
]
);
} else {
$query = $this->dbr->selectSQLText(
2020-11-22 20:00:48 +00:00
$tables,
$fields,
2020-11-22 20:00:48 +00:00
$this->where,
__METHOD__,
$options,
$this->join
);
}
$this->sqlQuery = $query;
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
if ( $calcRows ) {
$calcRowsResult = $this->dbr->query( 'SELECT FOUND_ROWS() AS rowcount', __METHOD__ );
$total = $calcRowsResult->fetchRow();
2021-02-22 23:48:01 +00:00
$this->foundRows = intval( $total['rowcount'] );
$calcRowsResult->free();
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
} catch ( Exception $e ) {
throw new MWException( __METHOD__ . ': ' . wfMessage( 'dpl_query_error', Hooks::getVersion(), $this->dbr->lastError() )->text() );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// Partially taken from intersection
$queryCacheTime = Config::getSetting( 'queryCacheTime' );
$maxQueryTime = Config::getSetting( 'maxQueryTime' );
if ( $maxQueryTime ) {
$options['MAX_EXECUTION_TIME'] = $maxQueryTime;
}
$parser = MediaWikiServices::getInstance()->getParser();
$pageName = str_replace( [ '*', '/' ], '-', $parser->getTitle()->getPrefixedDBkey() );
$qname = __METHOD__ . ' - ' . $pageName;
$where = $this->where;
$join = $this->join;
$dbr = $this->dbr;
$doQuery = static function () use ( $qname, $dbr, $tables, $fields, $where, $options, $join ) {
$res = $dbr->select( $tables, $fields, $where, $qname, $options, $join );
return iterator_to_array( $res );
};
$poolCounterKey = 'nowait:dpl3-query:' . WikiMap::getCurrentWikiId();
$worker = new PoolCounterWorkViaCallback( 'DPL3', $poolCounterKey, [
'doWork' => $doQuery,
] );
if ( $queryCacheTime <= 0 ) {
return $worker->execute();
2020-11-22 20:00:48 +00:00
}
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
return $cache->getWithSetCallback(
$cache->makeKey( 'DPL3Query', hash( 'sha256', $query ) ),
$queryCacheTime,
static function ( $oldVal, &$ttl, &$setOpts ) use ( $worker, $dbr ){
$setOpts += Database::getCacheSetOptions( $dbr );
$res = $worker->execute();
if ( $res === false ) {
// Do not cache errors.
$ttl = WANObjectCache::TTL_UNCACHEABLE;
// If we have oldVal, prefer it to error
if ( is_array( $oldVal ) ) {
return $oldVal;
}
}
return $res;
},
[
'lowTTL' => min( $cache::TTL_MINUTE, floor( $queryCacheTime * 0.75 ) ),
'pcTTL' => min( $cache::TTL_PROC_LONG, $queryCacheTime )
]
);
2020-11-22 20:00:48 +00:00
}
/**
* Return the number of found rows.
*
2021-10-01 22:52:30 +00:00
* @return int
2020-11-22 20:00:48 +00:00
*/
public function getFoundRows() {
return $this->foundRows;
}
/**
* Returns the generated SQL Query
*
2021-10-01 22:52:30 +00:00
* @return string
2020-11-22 20:00:48 +00:00
*/
public function getSqlQuery() {
return $this->sqlQuery;
}
/**
* Return prefixed and quoted tables that are needed.
*
2021-10-01 22:52:30 +00:00
* @return array
2020-11-22 20:00:48 +00:00
*/
public static function getTableNames() {
$dbr = wfGetDB( DB_REPLICA, 'dpl' );
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$tables = [
'categorylinks',
'dpl_clview',
'externallinks',
'flaggedpages',
'imagelinks',
'page',
'pagelinks',
'recentchanges',
'revision',
2021-10-06 18:44:11 +00:00
'revision_actor_temp',
2020-11-22 20:00:48 +00:00
'templatelinks'
];
$tableNames = [];
2021-02-22 23:48:01 +00:00
foreach ( $tables as $table ) {
$tableNames[$table] = $dbr->tableName( $table );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return $tableNames;
}
/**
* Add a table to the output.
*
2021-10-01 22:52:30 +00:00
* @param string $table
* @param string $alias
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function addTable( $table, $alias ) {
if ( empty( $table ) ) {
2021-10-01 22:52:30 +00:00
throw new MWException( __METHOD__ . ': An empty table name was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( empty( $alias ) || is_numeric( $alias ) ) {
2021-10-01 22:52:30 +00:00
throw new MWException( __METHOD__ . ': An empty or numeric table alias was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( !isset( $this->tables[$alias] ) ) {
$this->tables[$alias] = $this->dbr->tableName( $table );
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
} else {
return false;
}
}
2021-10-01 22:52:30 +00:00
/**
* Add multiple tables to the output.
*
* @param array $tablesByAlias
*/
public function addTables( array $tablesByAlias ) {
foreach ( $tablesByAlias as $alias => $table ) {
$this->addTable( $table, $alias );
}
}
2020-11-22 20:00:48 +00:00
/**
* Add a where clause to the output.
2021-10-01 22:52:30 +00:00
* Where clauses get imploded together with AND at the end. Any custom where clauses should be preformed before placed into here.
2020-11-22 20:00:48 +00:00
*
2021-10-01 22:52:30 +00:00
* @param array|string $where
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function addWhere( $where ) {
if ( empty( $where ) ) {
2021-10-01 22:52:30 +00:00
throw new MWException( __METHOD__ . ': An empty where clause was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( is_string( $where ) ) {
2020-11-22 20:00:48 +00:00
$this->where[] = $where;
2021-02-22 23:48:01 +00:00
} elseif ( is_array( $where ) ) {
$this->where = array_merge( $this->where, $where );
2020-11-22 20:00:48 +00:00
} else {
2021-10-01 22:52:30 +00:00
throw new MWException( __METHOD__ . ': An invalid where clause was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Add a where clause to the output that uses NOT IN or !=.
*
2021-10-01 22:52:30 +00:00
* @param array $where
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function addNotWhere( $where ) {
if ( empty( $where ) ) {
2021-10-01 22:52:30 +00:00
throw new MWException( __METHOD__ . ': An empty not where clause was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( is_array( $where ) ) {
foreach ( $where as $field => $values ) {
$this->where[] = $field . ( count( $values ) > 1 ? ' NOT IN(' . $this->dbr->makeList( $values ) . ')' : ' != ' . $this->dbr->addQuotes( current( $values ) ) );
2020-11-22 20:00:48 +00:00
}
} else {
throw new MWException( __METHOD__ . ': An invalid NOT WHERE clause was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Add a field to select.
* Will ignore duplicate values if the exact same alias and exact same field are passed.
*
2021-10-01 22:52:30 +00:00
* @param array $fields
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function addSelect( $fields ) {
if ( !is_array( $fields ) ) {
2021-10-01 22:52:30 +00:00
throw new MWException( __METHOD__ . ': A non-array was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $fields as $alias => $field ) {
if ( !is_numeric( $alias ) && array_key_exists( $alias, $this->select ) && $this->select[$alias] != $field ) {
2021-10-01 22:52:30 +00:00
// In case of a code bug that is overwriting an existing field alias throw an exception.
throw new MWException( __METHOD__ . ": Attempted to overwrite existing field alias `{$this->select[$alias]}` AS `{$alias}` with `{$field}` AS `{$alias}`." );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// String alias and does not exist already.
2021-02-22 23:48:01 +00:00
if ( !is_numeric( $alias ) && !array_key_exists( $alias, $this->select ) ) {
2020-11-22 20:00:48 +00:00
$this->select[$alias] = $field;
}
2021-10-01 22:52:30 +00:00
// Speed up by not using in_array() or array_key_exists(). Toss the field names into their own array as keys => true to exploit a speedy look up with isset().
2021-02-22 23:48:01 +00:00
if ( is_numeric( $alias ) && !isset( $this->selectedFields[$field] ) ) {
2020-11-22 20:00:48 +00:00
$this->select[] = $field;
$this->selectedFields[$field] = true;
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Add a GROUP BY clause to the output.
*
2021-10-01 22:52:30 +00:00
* @param string $groupBy
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function addGroupBy( $groupBy ) {
if ( empty( $groupBy ) ) {
throw new MWException( __METHOD__ . ': An empty GROUP BY clause was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->groupBy[] = $groupBy;
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Add a ORDER BY clause to the output.
*
2021-10-01 22:52:30 +00:00
* @param string $orderBy
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function addOrderBy( $orderBy ) {
if ( empty( $orderBy ) ) {
throw new MWException( __METHOD__ . ': An empty ORDER BY clause was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->orderBy[] = $orderBy;
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Add a JOIN clause to the output.
*
2021-10-01 22:52:30 +00:00
* @param string $tableAlias
* @param array $joinConditions
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function addJoin( $tableAlias, $joinConditions ) {
if ( empty( $tableAlias ) || empty( $joinConditions ) ) {
throw new MWException( __METHOD__ . ': An empty JOIN clause was passed.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( isset( $this->join[$tableAlias] ) ) {
throw new MWException( __METHOD__ . ': Attempted to overwrite existing JOIN clause.' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->join[$tableAlias] = $joinConditions;
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
2021-10-01 22:52:30 +00:00
public function addJoins( array $joins ) {
foreach ( $joins as $alias => $conds ) {
$this->addJoin( $alias, $conds );
}
}
2020-11-22 20:00:48 +00:00
/**
* Set the limit.
*
2021-10-01 22:52:30 +00:00
* @param mixed $limit
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function setLimit( $limit ) {
if ( is_numeric( $limit ) ) {
$this->limit = intval( $limit );
2020-11-22 20:00:48 +00:00
} else {
$this->limit = false;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Set the offset.
*
2021-10-01 22:52:30 +00:00
* @param mixed $offset
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function setOffset( $offset ) {
if ( is_numeric( $offset ) ) {
$this->offset = intval( $offset );
2020-11-22 20:00:48 +00:00
} else {
$this->offset = false;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Set the ORDER BY direction
*
2021-10-01 22:52:30 +00:00
* @param string $direction
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function setOrderDir( $direction ) {
2020-11-22 20:00:48 +00:00
$this->direction = $direction;
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Set the character set collation.
*
2021-10-01 22:52:30 +00:00
* @param string $collation
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function setCollation( $collation ) {
2020-11-22 20:00:48 +00:00
$this->collation = $collation;
}
/**
* Return SQL prefixed collation.
*
2021-10-01 22:52:30 +00:00
* @return string|null
2020-11-22 20:00:48 +00:00
*/
public function getCollateSQL() {
2021-02-22 23:48:01 +00:00
return ( $this->collation !== false ? 'COLLATE ' . $this->collation : null );
2020-11-22 20:00:48 +00:00
}
/**
* Recursively get and return an array of subcategories.
*
2021-10-01 22:52:30 +00:00
* @param string $categoryName
* @param int $depth
* @return array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public static function getSubcategories( $categoryName, $depth = 1 ) {
$dbr = wfGetDB( DB_REPLICA, 'dpl' );
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
if ( $depth > 2 ) {
2021-10-01 22:52:30 +00:00
// Hard constrain depth because lots of recursion is bad.
2020-11-22 20:00:48 +00:00
$depth = 2;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$categories = [];
$res = $dbr->select(
2021-02-22 23:48:01 +00:00
[ 'page', 'categorylinks' ],
[ 'page_title' ],
2020-11-22 20:00:48 +00:00
[
2021-12-15 20:22:38 +00:00
'page_namespace' => intval( NS_CATEGORY ),
2021-12-16 22:18:01 +00:00
'cl_to' => str_replace( ' ', '_', $categoryName )
2020-11-22 20:00:48 +00:00
],
__METHOD__,
2021-02-22 23:48:01 +00:00
[ 'DISTINCT' ],
2020-11-22 20:00:48 +00:00
[
'categorylinks' => [
'INNER JOIN',
2021-12-16 22:18:01 +00:00
'page_id = cl_from'
2020-11-22 20:00:48 +00:00
]
]
);
2021-10-01 22:52:30 +00:00
foreach ( $res as $row ) {
$categories[] = $row->page_title;
2021-02-22 23:48:01 +00:00
if ( $depth > 1 ) {
$categories = array_merge( $categories, self::getSubcategories( $row->page_title, $depth - 1 ) );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$categories = array_unique( $categories );
$res->free();
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return $categories;
}
/**
* Helper method to handle relative timestamps.
*
2021-10-01 22:52:30 +00:00
* @param mixed $inputDate
* @return int|string
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function convertTimestamp( $inputDate ) {
2020-11-22 20:00:48 +00:00
$timestamp = $inputDate;
2021-02-22 23:48:01 +00:00
switch ( $inputDate ) {
2020-11-22 20:00:48 +00:00
case 'today':
2021-02-22 23:48:01 +00:00
$timestamp = date( 'YmdHis' );
2020-11-22 20:00:48 +00:00
break;
case 'last hour':
2021-10-01 22:52:30 +00:00
$date = new DateTime();
$date->sub( new DateInterval( 'P1H' ) );
2021-02-22 23:48:01 +00:00
$timestamp = $date->format( 'YmdHis' );
2020-11-22 20:00:48 +00:00
break;
case 'last day':
2021-10-01 22:52:30 +00:00
$date = new DateTime();
$date->sub( new DateInterval( 'P1D' ) );
2021-02-22 23:48:01 +00:00
$timestamp = $date->format( 'YmdHis' );
2020-11-22 20:00:48 +00:00
break;
case 'last week':
2021-10-01 22:52:30 +00:00
$date = new DateTime();
$date->sub( new DateInterval( 'P7D' ) );
2021-02-22 23:48:01 +00:00
$timestamp = $date->format( 'YmdHis' );
2020-11-22 20:00:48 +00:00
break;
case 'last month':
2021-10-01 22:52:30 +00:00
$date = new DateTime();
$date->sub( new DateInterval( 'P1M' ) );
2021-02-22 23:48:01 +00:00
$timestamp = $date->format( 'YmdHis' );
2020-11-22 20:00:48 +00:00
break;
case 'last year':
2021-10-01 22:52:30 +00:00
$date = new DateTime();
$date->sub( new DateInterval( 'P1Y' ) );
2021-02-22 23:48:01 +00:00
$timestamp = $date->format( 'YmdHis' );
2020-11-22 20:00:48 +00:00
break;
}
2021-02-22 23:48:01 +00:00
if ( is_numeric( $timestamp ) ) {
return $this->dbr->addQuotes( $timestamp );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return 0;
}
/**
* Set SQL for 'addauthor' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addauthor( $option ) {
2021-10-01 22:52:30 +00:00
// Addauthor can not be used with addlasteditor.
2021-02-22 23:48:01 +00:00
if ( !isset( $this->parametersProcessed['addlasteditor'] ) || !$this->parametersProcessed['addlasteditor'] ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'rev' );
$this->addWhere(
[
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp = (SELECT MIN(rev_aux_min.revactor_timestamp) FROM ' . $this->tableNames['revision_actor_temp'] . ' AS rev_aux_min WHERE rev_aux_min.revactor_page = rev.revactor_page)'
]
2020-11-22 20:00:48 +00:00
);
2021-10-06 18:44:11 +00:00
$this->_adduser( null, 'rev' );
2020-11-22 20:00:48 +00:00
}
}
/**
* Set SQL for 'addcategories' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addcategories( $option ) {
$this->addTable( 'categorylinks', 'cl_gc' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'cats' => "GROUP_CONCAT(DISTINCT cl_gc.cl_to ORDER BY cl_gc.cl_to ASC SEPARATOR ' | ')"
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->addJoin(
'cl_gc',
[
'LEFT OUTER JOIN',
'page_id = cl_gc.cl_from'
]
);
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addGroupBy( $this->tableNames['page'] . '.page_id' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'addcontribution' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addcontribution( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'recentchanges', 'rc' );
$this->addSelect(
2021-10-06 18:44:11 +00:00
[
2021-12-15 20:22:38 +00:00
'contribution' => 'SUM(ABS(rc.rc_new_len - rc.rc_old_len))',
'contributor' => 'rc.rc_actor'
2021-10-06 18:44:11 +00:00
]
);
2020-11-22 20:00:48 +00:00
2021-10-06 18:44:11 +00:00
$this->addWhere(
[
$this->tableNames['page'] . '.page_id = rc.rc_cur_id'
]
);
2020-11-22 20:00:48 +00:00
2021-10-06 18:44:11 +00:00
$this->addGroupBy( 'rc.rc_cur_id' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'addeditdate' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addeditdate( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'rev' );
$this->addSelect( [ 'rev.revactor_timestamp' ] );
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
2021-10-06 18:44:11 +00:00
$this->tableNames['page'] . '.page_id = rev.revactor_page',
2020-11-22 20:00:48 +00:00
]
);
}
/**
* Set SQL for 'addfirstcategorydate' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addfirstcategorydate( $option ) {
2021-10-01 22:52:30 +00:00
// @TODO: This should be programmatically determining which categorylink table to use instead of assuming the first one.
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-12-15 20:22:38 +00:00
'cl_timestamp' => "DATE_FORMAT(cl1.cl_timestamp, '%Y%m%d%H%i%s')"
2020-11-22 20:00:48 +00:00
]
);
}
/**
* Set SQL for 'addlasteditor' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addlasteditor( $option ) {
2021-10-01 22:52:30 +00:00
// Addlasteditor can not be used with addauthor.
2021-02-22 23:48:01 +00:00
if ( !isset( $this->parametersProcessed['addauthor'] ) || !$this->parametersProcessed['addauthor'] ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'rev' );
$this->addWhere(
[
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp = (SELECT MAX(rev_aux_max.revactor_timestamp) FROM ' . $this->tableNames['revision_actor_temp'] . ' AS rev_aux_max WHERE rev_aux_max.revactor_page = rev.revactor_page)'
]
);
$this->_adduser( null, 'rev' );
2020-11-22 20:00:48 +00:00
}
}
/**
* Set SQL for 'addpagecounter' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addpagecounter( $option ) {
if ( ExtensionRegistry::getInstance()->isLoaded( 'HitCounters' ) ) {
2021-02-22 23:48:01 +00:00
$this->addTable( 'hit_counter', 'hit_counter' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'page_counter' => 'hit_counter.page_counter'
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( !isset( $this->join['hit_counter'] ) ) {
2020-11-22 20:00:48 +00:00
$this->addJoin(
'hit_counter',
[
'LEFT JOIN',
'hit_counter.page_id = ' . $this->tableNames['page'] . '.page_id'
2020-11-22 20:00:48 +00:00
]
);
}
}
}
/**
* Set SQL for 'addpagesize' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addpagesize( $option ) {
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'page_len' => "{$this->tableNames['page']}.page_len"
2020-11-22 20:00:48 +00:00
]
);
}
/**
* Set SQL for 'addpagetoucheddate' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _addpagetoucheddate( $option ) {
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'page_touched' => "{$this->tableNames['page']}.page_touched"
2020-11-22 20:00:48 +00:00
]
);
}
2021-10-06 18:44:11 +00:00
/**
* Set SQL for 'adduser' parameter.
*
* @param mixed $option
* @param string $tableAlias
*/
private function _adduser( $option, $tableAlias = '' ) {
$tableAlias = ( !empty( $tableAlias ) ? $tableAlias . '.' : '' );
$this->addSelect(
[
$tableAlias . 'revactor_actor',
]
);
}
2020-11-22 20:00:48 +00:00
/**
* Set SQL for 'allrevisionsbefore' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _allrevisionsbefore( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'rev' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-10-06 18:44:11 +00:00
'rev.revactor_rev',
'rev.revactor_timestamp'
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2021-10-06 18:44:11 +00:00
$this->addOrderBy( 'rev.revactor_rev' );
2021-02-22 23:48:01 +00:00
$this->setOrderDir( 'DESC' );
2021-10-06 18:44:11 +00:00
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
2021-10-06 18:44:11 +00:00
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp < ' . $this->convertTimestamp( $option )
2020-11-22 20:00:48 +00:00
]
);
}
/**
* Set SQL for 'allrevisionssince' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _allrevisionssince( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'rev' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-10-06 18:44:11 +00:00
'rev.revactor_rev',
'rev.revactor_timestamp'
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2021-10-06 18:44:11 +00:00
$this->addOrderBy( 'rev.revactor_rev' );
2021-02-22 23:48:01 +00:00
$this->setOrderDir( 'DESC' );
2021-10-06 18:44:11 +00:00
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
2021-10-06 18:44:11 +00:00
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp >= ' . $this->convertTimestamp( $option )
2020-11-22 20:00:48 +00:00
]
);
}
/**
* Set SQL for 'articlecategory' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _articlecategory( $option ) {
$this->addWhere( "{$this->tableNames['page']}.page_title IN (SELECT p2.page_title FROM {$this->tableNames['page']} p2 INNER JOIN {$this->tableNames['categorylinks']} clstc ON (clstc.cl_from = p2.page_id AND clstc.cl_to = " . $this->dbr->addQuotes( $option ) . ") WHERE p2.page_namespace = 0)" );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'categoriesminmax' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _categoriesminmax( $option ) {
if ( is_numeric( $option[0] ) ) {
$this->addWhere( intval( $option[0] ) . ' <= (SELECT count(*) FROM ' . $this->tableNames['categorylinks'] . ' WHERE ' . $this->tableNames['categorylinks'] . '.cl_from=page_id)' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
if ( isset( $option[1] ) && is_numeric( $option[1] ) ) {
2021-02-22 23:48:01 +00:00
$this->addWhere( intval( $option[1] ) . ' >= (SELECT count(*) FROM ' . $this->tableNames['categorylinks'] . ' WHERE ' . $this->tableNames['categorylinks'] . '.cl_from=page_id)' );
2020-11-22 20:00:48 +00:00
}
}
/**
2021-10-01 22:52:30 +00:00
* Set SQL for 'category' parameter. This includes 'category', 'categorymatch', and 'categoryregexp'.
2020-11-22 20:00:48 +00:00
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _category( $option ) {
2020-11-22 20:00:48 +00:00
$i = 0;
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $comparisonType => $operatorTypes ) {
foreach ( $operatorTypes as $operatorType => $categoryGroups ) {
foreach ( $categoryGroups as $categories ) {
if ( !is_array( $categories ) ) {
2020-11-22 20:00:48 +00:00
continue;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$tableName = ( in_array( '', $categories ) ? 'dpl_clview' : 'categorylinks' );
if ( $operatorType == 'AND' ) {
foreach ( $categories as $category ) {
2020-11-22 20:00:48 +00:00
$i++;
$tableAlias = "cl{$i}";
2021-02-22 23:48:01 +00:00
$this->addTable( $tableName, $tableAlias );
2020-11-22 20:00:48 +00:00
$this->addJoin(
$tableAlias,
[
'INNER JOIN',
"{$this->tableNames['page']}.page_id = {$tableAlias}.cl_from AND $tableAlias.cl_to {$comparisonType} " . $this->dbr->addQuotes( str_replace( ' ', '_', $category ) )
2020-11-22 20:00:48 +00:00
]
);
}
2021-02-22 23:48:01 +00:00
} elseif ( $operatorType == 'OR' ) {
2020-11-22 20:00:48 +00:00
$i++;
$tableAlias = "cl{$i}";
2021-02-22 23:48:01 +00:00
$this->addTable( $tableName, $tableAlias );
2020-11-22 20:00:48 +00:00
$joinOn = "{$this->tableNames['page']}.page_id = {$tableAlias}.cl_from AND (";
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $categories as $category ) {
$ors[] = "{$tableAlias}.cl_to {$comparisonType} " . $this->dbr->addQuotes( str_replace( ' ', '_', $category ) );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$joinOn .= implode( " {$operatorType} ", $ors );
2020-11-22 20:00:48 +00:00
$joinOn .= ')';
$this->addJoin(
$tableAlias,
[
'INNER JOIN',
$joinOn
]
);
}
}
}
}
}
/**
* Set SQL for 'notcategory' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notcategory( $option ) {
2020-11-22 20:00:48 +00:00
$i = 0;
2021-02-22 23:48:01 +00:00
foreach ( $option as $operatorType => $categories ) {
foreach ( $categories as $category ) {
2020-11-22 20:00:48 +00:00
$i++;
$tableAlias = "ecl{$i}";
2021-02-22 23:48:01 +00:00
$this->addTable( 'categorylinks', $tableAlias );
2020-11-22 20:00:48 +00:00
$this->addJoin(
$tableAlias,
[
'LEFT OUTER JOIN',
"{$this->tableNames['page']}.page_id = {$tableAlias}.cl_from AND {$tableAlias}.cl_to {$operatorType}" . $this->dbr->addQuotes( str_replace( ' ', '_', $category ) )
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
2021-12-15 20:22:38 +00:00
"{$tableAlias}.cl_to" => null
2020-11-22 20:00:48 +00:00
]
);
}
}
}
/**
* Set SQL for 'createdby' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _createdby( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision', 'creation_rev' );
$this->addTable( 'revision_actor_temp', 'creation_rev_actor' );
$this->_adduser( null, 'creation_rev_actor' );
$this->addWhere(
[
$this->dbr->addQuotes( $this->userFactory->newFromName( $option )->getActorId() ) . ' = creation_rev_actor.revactor_actor',
2021-10-06 18:44:11 +00:00
'creation_rev_actor.revactor_page = page_id',
'creation_rev.rev_parent_id = 0'
]
);
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'distinct' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _distinct( $option ) {
if ( $option == 'strict' || $option === true ) {
2020-11-22 20:00:48 +00:00
$this->distinct = true;
} else {
$this->distinct = false;
}
}
/**
* Set SQL for 'firstrevisionsince' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _firstrevisionsince( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'rev' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-10-06 18:44:11 +00:00
'rev.revactor_rev',
'rev.revactor_timestamp'
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
// tell the query optimizer not to look at rows that the following subquery will filter out anyway
$this->addWhere(
[
2021-10-06 18:44:11 +00:00
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp >= ' . $this->dbr->addQuotes( $option )
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
2021-10-06 18:44:11 +00:00
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp = (SELECT MIN(rev_aux_snc.rev_timestamp) FROM ' . $this->tableNames['revision'] . ' AS rev_aux_snc WHERE rev_aux_snc.rev_page=rev.rev_page AND rev_aux_snc.rev_timestamp >= ' . $this->convertTimestamp( $option ) . ')'
2020-11-22 20:00:48 +00:00
]
);
}
/**
* Set SQL for 'goal' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _goal( $option ) {
if ( $option == 'categories' ) {
$this->setLimit( false );
$this->setOffset( false );
2020-11-22 20:00:48 +00:00
}
}
/**
* Set SQL for 'hiddencategories' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _hiddencategories( $option ) {
2021-10-01 22:52:30 +00:00
// @TODO: Unfinished functionality! Never implemented by original author.
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'imagecontainer' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _imagecontainer( $option ) {
2021-10-01 22:52:30 +00:00
$where = [];
2021-02-22 23:48:01 +00:00
$this->addTable( 'imagelinks', 'ic' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-12-15 20:22:38 +00:00
'sortkey' => 'ic.il_to'
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( !$this->parameters->getParameter( 'openreferences' ) ) {
2020-11-22 20:00:48 +00:00
$where = [
2021-02-22 23:48:01 +00:00
"{$this->tableNames['page']}.page_namespace = " . intval( NS_FILE ),
2020-11-22 20:00:48 +00:00
"{$this->tableNames['page']}.page_title = ic.il_to"
];
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$ors = [];
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$ors[] = 'LOWER(CAST(ic.il_from AS char) = LOWER(' . $this->dbr->addQuotes( $link->getArticleID() ) . ')';
2020-11-22 20:00:48 +00:00
} else {
$ors[] = 'ic.il_from = ' . $this->dbr->addQuotes( $link->getArticleID() );
2020-11-22 20:00:48 +00:00
}
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where[] = '(' . implode( ' OR ', $ors ) . ')';
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'imageused' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _imageused( $option ) {
2021-10-01 22:52:30 +00:00
$where = [];
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'distinct' ) == 'strict' ) {
$this->addGroupBy( 'page_title' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addTable( 'imagelinks', 'il' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-12-15 20:22:38 +00:00
'image_sel_title' => 'il.il_to'
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$where[] = $this->tableNames['page'] . '.page_id = il.il_from';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$ors[] = 'LOWER(CAST(il.il_to AS char)) = LOWER(' . $this->dbr->addQuotes( $link->getDBkey() ) . ')';
2020-11-22 20:00:48 +00:00
} else {
$ors[] = 'il.il_to = ' . $this->dbr->addQuotes( $link->getDBkey() );
2020-11-22 20:00:48 +00:00
}
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where[] = '(' . implode( ' OR ', $ors ) . ')';
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'lastmodifiedby' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _lastmodifiedby( $option ) {
$this->addWhere( $this->dbr->addQuotes( $this->userFactory->newFromName( $option )->getActorId() ) . ' = (SELECT revactor_actor FROM ' . $this->tableNames['revision_actor_temp'] . ' WHERE ' . $this->tableNames['revision_actor_temp'] . '.revactor_page=page_id ORDER BY ' . $this->tableNames['revision_actor_temp'] . '.revactor_timestamp DESC LIMIT 1)' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'lastrevisionbefore' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _lastrevisionbefore( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'rev' );
$this->addSelect( [ 'rev.revactor_rev', 'rev.revactor_timestamp' ] );
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
// tell the query optimizer not to look at rows that the following subquery will filter out anyway
$this->addWhere(
[
2021-10-06 18:44:11 +00:00
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp < ' . $this->convertTimestamp( $option )
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
2021-10-06 18:44:11 +00:00
$this->tableNames['page'] . '.page_id = rev.revactor_page',
'rev.revactor_timestamp = (SELECT MAX(rev_aux_bef.rev_timestamp) FROM ' . $this->tableNames['revision'] . ' AS rev_aux_bef WHERE rev_aux_bef.rev_page=rev.rev_page AND rev_aux_bef.rev_timestamp < ' . $this->convertTimestamp( $option ) . ')'
2020-11-22 20:00:48 +00:00
]
);
}
/**
* Set SQL for 'linksfrom' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _linksfrom( $option ) {
2021-10-01 22:52:30 +00:00
$where = [];
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'distinct' ) == 'strict' ) {
$this->addGroupBy( 'page_title' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'openreferences' ) ) {
2020-11-22 20:00:48 +00:00
$ors = [];
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
2020-11-22 20:00:48 +00:00
$ors[] = '(pl_from = ' . $link->getArticleID() . ')';
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where[] = '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
$this->addTable( 'pagelinks', 'plf' );
$this->addTable( 'page', 'pagesrc' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'sel_title' => 'pagesrc.page_title',
'sel_ns' => 'pagesrc.page_namespace'
]
);
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$where = [
$this->tableNames['page'] . '.page_namespace = plf.pl_namespace',
$this->tableNames['page'] . '.page_title = plf.pl_title',
'pagesrc.page_id = plf.pl_from'
];
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$ors = [];
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
2020-11-22 20:00:48 +00:00
$ors[] = 'plf.pl_from = ' . $link->getArticleID();
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where[] = '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'linksto' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _linksto( $option ) {
if ( $this->parameters->getParameter( 'distinct' ) == 'strict' ) {
$this->addGroupBy( 'page_title' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( count( $option ) > 0 ) {
$this->addTable( 'pagelinks', 'pl' );
$this->addSelect( [ 'sel_title' => 'pl.pl_title', 'sel_ns' => 'pl.pl_namespace' ] );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $index => $linkGroup ) {
if ( $index == 0 ) {
2020-11-22 20:00:48 +00:00
$where = $this->tableNames['page'] . '.page_id=pl.pl_from AND ';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $linkGroup as $link ) {
$_or = '(pl.pl_namespace=' . intval( $link->getNamespace() );
if ( strpos( $link->getDBkey(), '%' ) >= 0 ) {
2020-11-22 20:00:48 +00:00
$operator = 'LIKE';
} else {
$operator = '=';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or .= ' AND LOWER(CAST(pl.pl_title AS char)) ' . $operator . ' LOWER(' . $this->dbr->addQuotes( $link->getDBkey() ) . ')';
2020-11-22 20:00:48 +00:00
} else {
$_or .= ' AND pl.pl_title ' . $operator . ' ' . $this->dbr->addQuotes( $link->getDBkey() );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$_or .= ')';
$ors[] = $_or;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
} else {
$where = 'EXISTS(select pl_from FROM ' . $this->tableNames['pagelinks'] . ' WHERE (' . $this->tableNames['pagelinks'] . '.pl_from=page_id AND ';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $linkGroup as $link ) {
$_or = '(' . $this->tableNames['pagelinks'] . '.pl_namespace=' . intval( $link->getNamespace() );
if ( strpos( $link->getDBkey(), '%' ) >= 0 ) {
2020-11-22 20:00:48 +00:00
$operator = 'LIKE';
} else {
$operator = '=';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or .= ' AND LOWER(CAST(' . $this->tableNames['pagelinks'] . '.pl_title AS char)) ' . $operator . ' LOWER(' . $this->dbr->addQuotes( $link->getDBkey() ) . ')';
2020-11-22 20:00:48 +00:00
} else {
$_or .= ' AND ' . $this->tableNames['pagelinks'] . '.pl_title ' . $operator . ' ' . $this->dbr->addQuotes( $link->getDBkey() );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$_or .= ')';
$ors[] = $_or;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
$where .= '))';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
}
}
/**
* Set SQL for 'notlinksfrom' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notlinksfrom( $option ) {
if ( $this->parameters->getParameter( 'distinct' ) == 'strict' ) {
$this->addGroupBy( 'page_title' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'openreferences' ) ) {
2020-11-22 20:00:48 +00:00
$ands = [];
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
$ands[] = 'pl_from <> ' . intval( $link->getArticleID() ) . ' ';
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where = '(' . implode( ' AND ', $ands ) . ')';
2020-11-22 20:00:48 +00:00
} else {
$where = 'CONCAT(page_namespace,page_title) NOT IN (SELECT CONCAT(' . $this->tableNames['pagelinks'] . '.pl_namespace,' . $this->tableNames['pagelinks'] . '.pl_title) FROM ' . $this->tableNames['pagelinks'] . ' WHERE ';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
$ors[] = $this->tableNames['pagelinks'] . '.pl_from = ' . intval( $link->getArticleID() );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'notlinksto' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notlinksto( $option ) {
if ( $this->parameters->getParameter( 'distinct' ) == 'strict' ) {
$this->addGroupBy( 'page_title' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( count( $option ) ) {
2020-11-22 20:00:48 +00:00
$where = $this->tableNames['page'] . '.page_id NOT IN (SELECT ' . $this->tableNames['pagelinks'] . '.pl_from FROM ' . $this->tableNames['pagelinks'] . ' WHERE ';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
$_or = '(' . $this->tableNames['pagelinks'] . '.pl_namespace=' . intval( $link->getNamespace() );
if ( strpos( $link->getDBkey(), '%' ) >= 0 ) {
2020-11-22 20:00:48 +00:00
$operator = 'LIKE';
} else {
$operator = '=';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or .= ' AND LOWER(CAST(' . $this->tableNames['pagelinks'] . '.pl_title AS char)) ' . $operator . ' LOWER(' . $this->dbr->addQuotes( $link->getDBkey() ) . '))';
2020-11-22 20:00:48 +00:00
} else {
$_or .= ' AND ' . $this->tableNames['pagelinks'] . '.pl_title ' . $operator . ' ' . $this->dbr->addQuotes( $link->getDBkey() ) . ')';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$ors[] = $_or;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= '(' . implode( ' OR ', $ors ) . '))';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$this->addWhere( $where ?? '' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'linkstoexternal' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _linkstoexternal( $option ) {
if ( $this->parameters->getParameter( 'distinct' ) == 'strict' ) {
$this->addGroupBy( 'page_title' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( count( $option ) > 0 ) {
$this->addTable( 'externallinks', 'el' );
$this->addSelect( [ 'el_to' => 'el.el_to' ] );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $index => $linkGroup ) {
if ( $index == 0 ) {
2020-11-22 20:00:48 +00:00
$where = $this->tableNames['page'] . '.page_id=el.el_from AND ';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $linkGroup as $link ) {
$ors[] = 'el.el_to LIKE ' . $this->dbr->addQuotes( $link );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
} else {
$where = 'EXISTS(SELECT el_from FROM ' . $this->tableNames['externallinks'] . ' WHERE (' . $this->tableNames['externallinks'] . '.el_from=page_id AND ';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $linkGroup as $link ) {
$ors[] = $this->tableNames['externallinks'] . '.el_to LIKE ' . $this->dbr->addQuotes( $link );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
$where .= '))';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
}
}
/**
* Set SQL for 'maxrevisions' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _maxrevisions( $option ) {
2021-10-06 18:44:11 +00:00
$this->addWhere( "((SELECT count(rev_aux3.revactor_page) FROM {$this->tableNames['revision_actor_temp']} AS rev_aux3 WHERE rev_aux3.revactor_page = {$this->tableNames['page']}.page_id) <= {$option})" );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'minoredits' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _minoredits( $option ) {
if ( isset( $option ) && $option == 'exclude' ) {
2022-03-17 15:38:47 +00:00
$this->addTable( 'revision', 'revision' );
$this->addWhere( 'revision.rev_minor_edit = 0' );
2020-11-22 20:00:48 +00:00
}
}
/**
* Set SQL for 'minrevisions' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _minrevisions( $option ) {
2021-10-06 18:44:11 +00:00
$this->addWhere( "((SELECT count(rev_aux2.revactor_page) FROM {$this->tableNames['revision_actor_temp']} AS rev_aux2 WHERE rev_aux2.revactor_page = {$this->tableNames['page']}.page_id) >= {$option})" );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'modifiedby' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _modifiedby( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision_actor_temp', 'change_rev' );
$this->addWhere( $this->dbr->addQuotes( $this->userFactory->newFromName( $option )->getActorId() ) . ' = change_rev.revactor_actor AND change_rev.revactor_page = page_id' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'namespace' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _namespace( $option ) {
if ( is_array( $option ) && count( $option ) ) {
if ( $this->parameters->getParameter( 'openreferences' ) ) {
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
2021-12-15 20:22:38 +00:00
"{$this->tableNames['pagelinks']}.pl_namespace" => $option
2020-11-22 20:00:48 +00:00
]
);
} else {
$this->addWhere(
[
2021-12-15 20:22:38 +00:00
"{$this->tableNames['page']}.page_namespace" => $option
2020-11-22 20:00:48 +00:00
]
);
}
}
}
/**
* Set SQL for 'notcreatedby' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notcreatedby( $option ) {
2021-10-06 18:44:11 +00:00
$this->addTable( 'revision', 'no_creation_rev' );
$this->addTable( 'revision_actor_temp', 'no_creation_rev_actor' );
$this->addWhere( $this->dbr->addQuotes( $this->userFactory->newFromName( $option )->getActorId() ) . ' != no_creation_rev_actor.revactor_actor AND no_creation_rev_actor.revactor_page = page_id AND no_creation_rev.rev_parent_id = 0' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'notlastmodifiedby' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notlastmodifiedby( $option ) {
$this->addWhere( $this->dbr->addQuotes( $this->userFactory->newFromName( $option )->getActorId() ) . ' != (SELECT revactor_actor FROM ' . $this->tableNames['revision_actor_temp'] . ' WHERE ' . $this->tableNames['revision_actor_temp'] . '.revactor_page=page_id ORDER BY ' . $this->tableNames['revision_actor_temp'] . '.revactor_timestamp DESC LIMIT 1)' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'notmodifiedby' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notmodifiedby( $option ) {
$this->addWhere( 'NOT EXISTS (SELECT 1 FROM ' . $this->tableNames['revision_actor_temp'] . ' WHERE ' . $this->tableNames['revision_actor_temp'] . '.revactor_page=page_id AND ' . $this->tableNames['revision_actor_temp'] . '.revactor_actor = ' . $this->dbr->addQuotes( $this->userFactory->newFromName( $option )->getActorId() ) . ' LIMIT 1)' );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'notnamespace' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notnamespace( $option ) {
if ( is_array( $option ) && count( $option ) ) {
if ( $this->parameters->getParameter( 'openreferences' ) ) {
2020-11-22 20:00:48 +00:00
$this->addNotWhere(
[
"{$this->tableNames['pagelinks']}.pl_namespace" => $option
]
);
} else {
$this->addNotWhere(
[
"{$this->tableNames['page']}.page_namespace" => $option
]
);
}
}
}
/**
* Set SQL for 'count' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _count( $option ) {
$this->setLimit( $option );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'offset' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _offset( $option ) {
$this->setOffset( $option );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'order' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _order( $option ) {
$orderMethod = $this->parameters->getParameter( 'ordermethod' );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( !empty( $orderMethod ) && is_array( $orderMethod ) && $orderMethod[0] !== 'none' ) {
if ( $option === 'descending' || $option === 'desc' ) {
$this->setOrderDir( 'DESC' );
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
$this->setOrderDir( 'ASC' );
2020-11-22 20:00:48 +00:00
}
}
}
/**
* Set SQL for 'ordercollation' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
* @return bool
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _ordercollation( $option ) {
$option = mb_strtolower( $option );
2020-11-22 20:00:48 +00:00
$res = $this->dbr->query( 'SHOW CHARACTER SET' );
if ( !$res ) {
2020-11-22 20:00:48 +00:00
return false;
}
foreach ( $res as $row ) {
if ( $option == $row->{'Default collation'} ) {
2021-02-22 23:48:01 +00:00
$this->setCollation( $option );
2020-11-22 20:00:48 +00:00
break;
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return true;
}
/**
* Set SQL for 'ordermethod' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _ordermethod( $option ) {
if ( $this->parameters->getParameter( 'goal' ) == 'categories' ) {
2021-10-01 22:52:30 +00:00
// No order methods for returning categories.
2020-11-22 20:00:48 +00:00
return true;
}
2021-10-01 22:52:30 +00:00
$services = MediaWikiServices::getInstance();
$namespaces = $services->getContentLanguage()->getNamespaces();
// $aStrictNs = array_slice( (array)Config::getSetting( 'allowedNamespaces' ), 1, count( Config::getSetting( 'allowedNamespaces' ) ), true );
2021-02-22 23:48:01 +00:00
$namespaces = array_slice( $namespaces, 3, count( $namespaces ), true );
2020-11-22 20:00:48 +00:00
$_namespaceIdToText = "CASE {$this->tableNames['page']}.page_namespace";
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $namespaces as $id => $name ) {
$_namespaceIdToText .= ' WHEN ' . intval( $id ) . ' THEN ' . $this->dbr->addQuotes( $name . ':' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$_namespaceIdToText .= ' END';
$option = (array)$option;
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $orderMethod ) {
switch ( $orderMethod ) {
2020-11-22 20:00:48 +00:00
case 'category':
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'cl_head.cl_to' );
2021-10-01 22:52:30 +00:00
$this->addSelect( [ 'cl_head.cl_to' ] );
2021-02-22 23:48:01 +00:00
if ( ( is_array( $this->parameters->getParameter( 'catheadings' ) ) && in_array( '', $this->parameters->getParameter( 'catheadings' ) ) ) || ( is_array( $this->parameters->getParameter( 'catnotheadings' ) ) && in_array( '', $this->parameters->getParameter( 'catnotheadings' ) ) ) ) {
2020-11-22 20:00:48 +00:00
$_clTableName = 'dpl_clview';
$_clTableAlias = $_clTableName;
} else {
$_clTableName = 'categorylinks';
$_clTableAlias = 'cl_head';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addTable( $_clTableName, $_clTableAlias );
2020-11-22 20:00:48 +00:00
$this->addJoin(
$_clTableAlias,
[
'LEFT OUTER JOIN',
'page_id = cl_head.cl_from'
2020-11-22 20:00:48 +00:00
]
);
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( is_array( $this->parameters->getParameter( 'catheadings' ) ) && count( $this->parameters->getParameter( 'catheadings' ) ) ) {
2020-11-22 20:00:48 +00:00
$this->addWhere(
[
'cl_head.cl_to' => $this->parameters->getParameter( 'catheadings' )
2020-11-22 20:00:48 +00:00
]
);
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( is_array( $this->parameters->getParameter( 'catnotheadings' ) ) && count( $this->parameters->getParameter( 'catnotheadings' ) ) ) {
2020-11-22 20:00:48 +00:00
$this->addNotWhere(
[
2021-02-22 23:48:01 +00:00
'cl_head.cl_to' => $this->parameters->getParameter( 'catnotheadings' )
2020-11-22 20:00:48 +00:00
]
);
}
break;
case 'categoryadd':
2021-10-01 22:52:30 +00:00
// @TODO: See TODO in __addfirstcategorydate().
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'cl1.cl_timestamp' );
2020-11-22 20:00:48 +00:00
break;
case 'counter':
if ( ExtensionRegistry::getInstance()->isLoaded( 'HitCounters' ) ) {
2021-10-01 22:52:30 +00:00
// If the "addpagecounter" parameter was not used the table and join need to be added now.
2021-02-22 23:48:01 +00:00
if ( !array_key_exists( 'hit_counter', $this->tables ) ) {
$this->addTable( 'hit_counter', 'hit_counter' );
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
if ( !isset( $this->join['hit_counter'] ) ) {
2020-11-22 20:00:48 +00:00
$this->addJoin(
'hit_counter',
[
'LEFT JOIN',
'hit_counter.page_id = ' . $this->tableNames['page'] . '.page_id'
2020-11-22 20:00:48 +00:00
]
);
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'hit_counter.page_counter' );
2020-11-22 20:00:48 +00:00
}
break;
case 'firstedit':
2021-10-06 18:44:11 +00:00
$this->addOrderBy( 'rev.revactor_timestamp' );
$this->addTable( 'revision_actor_temp', 'rev' );
$this->addSelect(
[
'rev.revactor_timestamp'
]
);
if ( !$this->revisionAuxWhereAdded ) {
$this->addWhere(
[
"{$this->tableNames['page']}.page_id = rev.revactor_page",
"rev.revactor_timestamp = (SELECT MIN(rev_aux.revactor_timestamp) FROM {$this->tableNames['revision_actor_temp']} AS rev_aux WHERE rev_aux.revactor_page=rev.revactor_page)"
]
);
}
2021-10-01 22:52:30 +00:00
2021-10-06 18:44:11 +00:00
$this->revisionAuxWhereAdded = true;
2020-11-22 20:00:48 +00:00
break;
case 'lastedit':
if ( Hooks::isLikeIntersection() ) {
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'page_touched' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'page_touched' => "{$this->tableNames['page']}.page_touched"
2020-11-22 20:00:48 +00:00
]
);
} else {
2021-10-06 18:44:11 +00:00
$this->addOrderBy( 'rev.revactor_timestamp' );
$this->addTable( 'revision_actor_temp', 'rev' );
$this->addSelect( [ 'rev.revactor_timestamp' ] );
2021-10-01 22:52:30 +00:00
2021-10-06 18:44:11 +00:00
if ( !$this->revisionAuxWhereAdded ) {
$this->addWhere(
[
"{$this->tableNames['page']}.page_id = rev.revactor_page",
"rev.revactor_timestamp = (SELECT MAX(rev_aux.revactor_timestamp) FROM {$this->tableNames['revision_actor_temp']} AS rev_aux WHERE rev_aux.revactor_page = rev.revactor_page)"
]
);
}
$this->revisionAuxWhereAdded = true;
2020-11-22 20:00:48 +00:00
}
break;
case 'pagesel':
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'sortkey' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'sortkey' => 'CONCAT(pl.pl_namespace, pl.pl_title) ' . $this->getCollateSQL()
]
);
break;
case 'pagetouched':
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'page_touched' );
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'page_touched' => "{$this->tableNames['page']}.page_touched"
2020-11-22 20:00:48 +00:00
]
);
break;
case 'size':
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'page_len' );
2020-11-22 20:00:48 +00:00
break;
case 'sortkey':
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'sortkey' );
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
// If cl_sortkey is null (uncategorized page), generate a sortkey in the usual way (full page name, underscores replaced with spaces).
// UTF-8 created problems with non-utf-8 MySQL databases
$replaceConcat = "REPLACE(CONCAT({$_namespaceIdToText}, " . $this->tableNames['page'] . ".page_title), '_', ' ')";
2021-02-22 23:48:01 +00:00
$category = (array)$this->parameters->getParameter( 'category' );
$notCategory = (array)$this->parameters->getParameter( 'notcategory' );
if ( count( $category ) + count( $notCategory ) > 0 ) {
if ( in_array( 'category', $this->parameters->getParameter( 'ordermethod' ) ) ) {
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'sortkey' => "IFNULL(cl_head.cl_sortkey, {$replaceConcat}) " . $this->getCollateSQL()
]
);
} else {
2021-10-01 22:52:30 +00:00
// This runs on the assumption that at least one category parameter was used and that numbering starts at 1.
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'sortkey' => "IFNULL(cl1.cl_sortkey, {$replaceConcat}) " . $this->getCollateSQL()
]
);
}
} else {
$this->addSelect(
[
'sortkey' => $replaceConcat . $this->getCollateSQL()
]
);
}
break;
case 'titlewithoutnamespace':
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'openreferences' ) ) {
$this->addOrderBy( 'pl_title' );
2020-11-22 20:00:48 +00:00
} else {
$this->addOrderBy( 'page_title' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'sortkey' => "{$this->tableNames['page']}.page_title " . $this->getCollateSQL()
]
);
break;
case 'title':
2021-02-22 23:48:01 +00:00
$this->addOrderBy( 'sortkey' );
if ( $this->parameters->getParameter( 'openreferences' ) ) {
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
2021-10-01 22:52:30 +00:00
'sortkey' => "REPLACE(CONCAT(IF(pl_namespace =0, '', CONCAT(" . $_namespaceIdToText . ", ':')), pl_title), '_', ' ') " . $this->getCollateSQL()
2020-11-22 20:00:48 +00:00
]
);
} else {
2021-10-01 22:52:30 +00:00
// Generate sortkey like for category links. UTF-8 created problems with non-utf-8 MySQL databases.
2020-11-22 20:00:48 +00:00
$this->addSelect(
[
'sortkey' => "REPLACE(CONCAT(IF(" . $this->tableNames['page'] . ".page_namespace = 0, '', CONCAT(" . $_namespaceIdToText . ", ':')), " . $this->tableNames['page'] . ".page_title), '_', ' ') " . $this->getCollateSQL()
]
);
}
break;
case 'user':
2021-10-06 18:44:11 +00:00
$this->addOrderBy( 'rev.revactor_actor' );
$this->addTable( 'revision_actor_temp', 'rev' );
2021-10-01 22:52:30 +00:00
2021-10-06 18:44:11 +00:00
$this->_adduser( null, 'rev' );
2020-11-22 20:00:48 +00:00
break;
case 'none':
break;
}
}
}
/**
* Set SQL for 'redirects' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _redirects( $option ) {
if ( !$this->parameters->getParameter( 'openreferences' ) ) {
switch ( $option ) {
2020-11-22 20:00:48 +00:00
case 'only':
$this->addWhere(
[
$this->tableNames['page'] . '.page_is_redirect' => 1
2020-11-22 20:00:48 +00:00
]
);
break;
case 'exclude':
$this->addWhere(
[
$this->tableNames['page'] . '.page_is_redirect' => 0
2020-11-22 20:00:48 +00:00
]
);
break;
}
}
}
/**
* Set SQL for 'stablepages' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _stablepages( $option ) {
if ( function_exists( 'efLoadFlaggedRevs' ) ) {
2021-10-01 22:52:30 +00:00
// Do not add this again if 'qualitypages' has already added it.
2021-02-22 23:48:01 +00:00
if ( !$this->parametersProcessed['qualitypages'] ) {
2020-11-22 20:00:48 +00:00
$this->addJoin(
'flaggedpages',
[
'LEFT JOIN',
'page_id = fp_page_id'
2020-11-22 20:00:48 +00:00
]
);
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
switch ( $option ) {
2020-11-22 20:00:48 +00:00
case 'only':
$this->addWhere(
[
'fp_stable IS NOT NULL'
]
);
break;
case 'exclude':
$this->addWhere(
[
2021-12-15 20:22:38 +00:00
'fp_stable' => null
2020-11-22 20:00:48 +00:00
]
);
break;
}
}
}
/**
* Set SQL for 'qualitypages' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _qualitypages( $option ) {
if ( function_exists( 'efLoadFlaggedRevs' ) ) {
2021-10-01 22:52:30 +00:00
// Do not add this again if 'stablepages' has already added it.
2021-02-22 23:48:01 +00:00
if ( !$this->parametersProcessed['stablepages'] ) {
2020-11-22 20:00:48 +00:00
$this->addJoin(
'flaggedpages',
[
'LEFT JOIN',
'page_id = fp_page_id'
2020-11-22 20:00:48 +00:00
]
);
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
switch ( $option ) {
2020-11-22 20:00:48 +00:00
case 'only':
2021-02-22 23:48:01 +00:00
$this->addWhere( 'fp_quality >= 1' );
2020-11-22 20:00:48 +00:00
break;
case 'exclude':
2021-02-22 23:48:01 +00:00
$this->addWhere( 'fp_quality = 0' );
2020-11-22 20:00:48 +00:00
break;
}
}
}
/**
* Set SQL for 'title' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _title( $option ) {
2020-11-22 20:00:48 +00:00
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $comparisonType => $titles ) {
foreach ( $titles as $title ) {
if ( $this->parameters->getParameter( 'openreferences' ) ) {
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or = "LOWER(CAST(pl_title AS char)) {$comparisonType}" . strtolower( $this->dbr->addQuotes( $title ) );
2020-11-22 20:00:48 +00:00
} else {
$_or = "pl_title {$comparisonType} " . $this->dbr->addQuotes( $title );
2020-11-22 20:00:48 +00:00
}
} else {
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or = "LOWER(CAST({$this->tableNames['page']}.page_title AS char)) {$comparisonType}" . strtolower( $this->dbr->addQuotes( $title ) );
2020-11-22 20:00:48 +00:00
} else {
$_or = "{$this->tableNames['page']}.page_title {$comparisonType}" . $this->dbr->addQuotes( $title );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$ors[] = $_or;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where = '(' . implode( ' OR ', $ors ) . ')';
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'nottitle' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _nottitle( $option ) {
2020-11-22 20:00:48 +00:00
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $comparisonType => $titles ) {
foreach ( $titles as $title ) {
if ( $this->parameters->getParameter( 'openreferences' ) ) {
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or = "LOWER(CAST(pl_title AS char)) {$comparisonType}" . strtolower( $this->dbr->addQuotes( $title ) );
2020-11-22 20:00:48 +00:00
} else {
$_or = "pl_title {$comparisonType} " . $this->dbr->addQuotes( $title );
2020-11-22 20:00:48 +00:00
}
} else {
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or = "LOWER(CAST({$this->tableNames['page']}.page_title AS char)) {$comparisonType}" . strtolower( $this->dbr->addQuotes( $title ) );
2020-11-22 20:00:48 +00:00
} else {
$_or = "{$this->tableNames['page']}.page_title {$comparisonType}" . $this->dbr->addQuotes( $title );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$ors[] = $_or;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where = 'NOT (' . implode( ' OR ', $ors ) . ')';
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'titlegt' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _titlegt( $option ) {
$operator = '>';
if ( substr( $option, 0, 2 ) === '=_' ) {
$option = substr( $option, 2 );
$operator = '>=';
}
2021-10-01 22:52:30 +00:00
if ( $option === '' ) {
$operator = 'LIKE';
$option = '%';
}
$option = $this->dbr->addQuotes( $option );
if ( $this->parameters->getParameter( 'openreferences' ) ) {
$where = "(pl_title {$operator} {$option})";
} else {
$where = "({$this->tableNames['page']}.page_title {$operator} {$option})";
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'titlelt' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _titlelt( $option ) {
$operator = '<';
if ( substr( $option, 0, 2 ) === '=_' ) {
$option = substr( $option, 2 );
$operator = '<=';
}
2021-10-01 22:52:30 +00:00
if ( $option === '' ) {
$operator = 'LIKE';
$option = '%';
}
$option = $this->dbr->addQuotes( $option );
if ( $this->parameters->getParameter( 'openreferences' ) ) {
$where = "(pl_title {$operator} {$option})";
} else {
$where = "({$this->tableNames['page']}.page_title {$operator} {$option})";
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'usedby' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _usedby( $option ) {
if ( $this->parameters->getParameter( 'openreferences' ) ) {
2020-11-22 20:00:48 +00:00
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
$ors[] = 'tpl_from = ' . intval( $link->getArticleID() );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where = '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
$this->addTable( 'templatelinks', 'tpl' );
$this->addTable( 'page', 'tplsrc' );
$this->addSelect( [ 'tpl_sel_title' => 'tplsrc.page_title', 'tpl_sel_ns' => 'tplsrc.page_namespace' ] );
2020-11-22 20:00:48 +00:00
$where = $this->tableNames['page'] . '.page_namespace = tpl.tl_namespace AND ' .
$this->tableNames['page'] . '.page_title = tpl.tl_title AND tplsrc.page_id = tpl.tl_from AND ';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
$ors[] = 'tpl.tl_from = ' . intval( $link->getArticleID() );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= '(' . implode( ' OR ', $ors ) . ')';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'uses' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _uses( $option ) {
$this->addTable( 'templatelinks', 'tl' );
2020-11-22 20:00:48 +00:00
$where = $this->tableNames['page'] . '.page_id=tl.tl_from AND (';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
$_or = '(tl.tl_namespace=' . intval( $link->getNamespace() );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or .= ' AND LOWER(CAST(tl.tl_title AS char)) = LOWER(' . $this->dbr->addQuotes( $link->getDBkey() ) . '))';
2020-11-22 20:00:48 +00:00
} else {
$_or .= ' AND tl.tl_title = ' . $this->dbr->addQuotes( $link->getDBkey() ) . ')';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$ors[] = $_or;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= implode( ' OR ', $ors ) . ')';
$this->addWhere( $where );
2020-11-22 20:00:48 +00:00
}
/**
* Set SQL for 'notuses' parameter.
*
2021-10-01 22:52:30 +00:00
* @param mixed $option
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private function _notuses( $option ) {
if ( count( $option ) > 0 ) {
2020-11-22 20:00:48 +00:00
$where = $this->tableNames['page'] . '.page_id NOT IN (SELECT ' . $this->tableNames['templatelinks'] . '.tl_from FROM ' . $this->tableNames['templatelinks'] . ' WHERE (';
$ors = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $option as $linkGroup ) {
foreach ( $linkGroup as $link ) {
$_or = '(' . $this->tableNames['templatelinks'] . '.tl_namespace=' . intval( $link->getNamespace() );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $this->parameters->getParameter( 'ignorecase' ) ) {
$_or .= ' AND LOWER(CAST(' . $this->tableNames['templatelinks'] . '.tl_title AS char)) = LOWER(' . $this->dbr->addQuotes( $link->getDBkey() ) . '))';
2020-11-22 20:00:48 +00:00
} else {
$_or .= ' AND ' . $this->tableNames['templatelinks'] . '.tl_title = ' . $this->dbr->addQuotes( $link->getDBkey() ) . ')';
2020-11-22 20:00:48 +00:00
}
$ors[] = $_or;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$where .= implode( ' OR ', $ors ) . '))';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$this->addWhere( $where ?? '' );
2020-11-22 20:00:48 +00:00
}
}