3 namespace MediaWiki\Page
;
6 use MediaWiki\Cache\LinkCache
;
7 use Wikimedia\Assert\Assert
;
8 use Wikimedia\Rdbms\IExpression
;
9 use Wikimedia\Rdbms\IReadableDatabase
;
10 use Wikimedia\Rdbms\LikeValue
;
11 use Wikimedia\Rdbms\SelectQueryBuilder
;
16 class PageSelectQueryBuilder
extends SelectQueryBuilder
{
21 /** @var LinkCache|null */
25 * @param IReadableDatabase $db
26 * @param PageStore $pageStore
27 * @param LinkCache|null $linkCache A link cache to store any retrieved rows into
31 public function __construct( IReadableDatabase
$db, PageStore
$pageStore, ?LinkCache
$linkCache = null ) {
32 parent
::__construct( $db );
33 $this->pageStore
= $pageStore;
34 $this->table( 'page' );
35 $this->linkCache
= $linkCache;
39 * Find by provided page ids.
41 * @param int|int[] $pageIds
43 * @return PageSelectQueryBuilder
45 public function wherePageIds( $pageIds ): self
{
46 Assert
::parameterType( [ 'integer', 'array' ], $pageIds, '$pageIds' );
49 $this->conds( [ 'page_id' => $pageIds ] );
51 $this->conds( '0 = 1' ); // force empty result set
58 * Find by provided namespace.
60 * @param int $namespace
62 * @return PageSelectQueryBuilder
64 public function whereNamespace( int $namespace ): self
{
65 $this->conds( [ 'page_namespace' => $namespace ] );
70 * Find by provided prefix.
72 * @param int $namespace
73 * @param string $prefix
75 * @return PageSelectQueryBuilder
77 public function whereTitlePrefix( int $namespace, string $prefix ): self
{
78 $this->whereNamespace( $namespace );
80 $this->db
->expr( 'page_title', IExpression
::LIKE
, new LikeValue( $prefix, $this->db
->anyString() ) )
86 * Find by provided page titles.
88 * @param int $namespace
89 * @param string|string[] $pageTitles
91 * @return PageSelectQueryBuilder
93 public function whereTitles( int $namespace, $pageTitles ): self
{
94 Assert
::parameterType( [ 'string', 'array' ], $pageTitles, '$pageTitles' );
95 $this->conds( [ 'page_namespace' => $namespace ] );
96 $this->conds( [ 'page_title' => $pageTitles ] );
101 * Order results by namespace and title in $direction
103 * @param string $dir one of self::SORT_ASC or self::SORT_DESC
105 * @return PageSelectQueryBuilder
107 public function orderByTitle( string $dir = self
::SORT_ASC
): self
{
108 $this->orderBy( [ 'page_namespace', 'page_title' ], $dir );
113 * Order results by page id.
115 * @param string $dir one of self::SORT_ASC or self::SORT_DESC
117 * @return PageSelectQueryBuilder
119 public function orderByPageId( string $dir = self
::SORT_ASC
): self
{
120 $this->orderBy( 'page_id', $dir );
125 * Fetch a single PageRecord that matches specified criteria.
127 * @return PageRecord|null
129 public function fetchPageRecord(): ?PageRecord
{
130 $this->fields( $this->pageStore
->getSelectFields() );
132 $row = $this->fetchRow();
137 $rec = $this->pageStore
->newPageRecordFromRow( $row );
138 if ( $this->linkCache
) {
139 $this->linkCache
->addGoodLinkObjFromRow( $rec, $row );
145 * Fetch PageRecords for the specified query.
147 * @return Iterator<ExistingPageRecord>
149 public function fetchPageRecords(): Iterator
{
150 $this->fields( $this->pageStore
->getSelectFields() );
152 $result = $this->fetchResultSet();
153 foreach ( $result as $row ) {
154 $rec = $this->pageStore
->newPageRecordFromRow( $row );
155 if ( $this->linkCache
) {
156 $this->linkCache
->addGoodLinkObjFromRow( $rec, $row );
164 * Fetch PageRecords for the specified query as an associative
165 * array, using page IDs as array keys.
167 * @return ExistingPageRecord[]
169 public function fetchPageRecordArray(): array {
172 foreach ( $this->fetchPageRecords() as $rec ) {
173 $recs[ $rec->getId() ] = $rec;
180 * Returns an array of page ids matching the query.
184 public function fetchPageIds(): array {
185 $this->field( 'page_id' );
186 return array_map( 'intval', $this->fetchFieldValues() );