Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / page / PageSelectQueryBuilder.php
blob64436648327454bceae4589072737a57f86d100d
1 <?php
3 namespace MediaWiki\Page;
5 use Iterator;
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;
13 /**
14 * @since 1.36
16 class PageSelectQueryBuilder extends SelectQueryBuilder {
18 /** @var PageStore */
19 private $pageStore;
21 /** @var LinkCache|null */
22 private $linkCache;
24 /**
25 * @param IReadableDatabase $db
26 * @param PageStore $pageStore
27 * @param LinkCache|null $linkCache A link cache to store any retrieved rows into
29 * @internal
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;
38 /**
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' );
48 if ( $pageIds ) {
49 $this->conds( [ 'page_id' => $pageIds ] );
50 } else {
51 $this->conds( '0 = 1' ); // force empty result set
54 return $this;
57 /**
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 ] );
66 return $this;
69 /**
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 );
79 $this->conds(
80 $this->db->expr( 'page_title', IExpression::LIKE, new LikeValue( $prefix, $this->db->anyString() ) )
82 return $this;
85 /**
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 ] );
97 return $this;
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 );
109 return $this;
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 );
121 return $this;
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();
133 if ( !$row ) {
134 return null;
137 $rec = $this->pageStore->newPageRecordFromRow( $row );
138 if ( $this->linkCache ) {
139 $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
141 return $rec;
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 );
158 yield $rec;
160 $result->free();
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 {
170 $recs = [];
172 foreach ( $this->fetchPageRecords() as $rec ) {
173 $recs[ $rec->getId() ] = $rec;
176 return $recs;
180 * Returns an array of page ids matching the query.
182 * @return int[]
184 public function fetchPageIds(): array {
185 $this->field( 'page_id' );
186 return array_map( 'intval', $this->fetchFieldValues() );