MediaWiki master
PageArchive.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Page;
22
31
36
37 protected Title $title;
38
39 public function __construct( Title $title ) {
40 $this->title = $title;
41 }
42
51 public static function listPagesBySearch( $term ) {
52 $title = Title::newFromText( $term );
53 if ( $title ) {
54 $ns = $title->getNamespace();
55 $termMain = $title->getText();
56 $termDb = $title->getDBkey();
57 } else {
58 / Prolly won't work too good
59 / @todo handle bare namespace names cleanly?
60 $ns = 0;
61 $termMain = $termDb = $term;
62 }
63
64 / Try search engine first
65 $engine = MediaWikiServices::getInstance()->newSearchEngine();
66 $engine->setLimitOffset( 100 );
67 $engine->setNamespaces( [ $ns ] );
68 $results = $engine->searchArchiveTitle( $termMain );
69 if ( !$results->isOK() ) {
70 $results = [];
71 } else {
72 $results = $results->getValue();
73 }
74
75 if ( !$results ) {
76 / Fall back to regular prefix search
77 return self::listPagesByPrefix( $term );
78 }
79
80 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
81 $condTitles = array_values( array_unique( array_map( static function ( Title $t ) {
82 return $t->getDBkey();
83 }, $results ) ) );
84 $conds = [
85 'ar_namespace' => $ns,
86 $dbr->expr( 'ar_title', '=', $condTitles )
87 ->or( 'ar_title', IExpression::LIKE, new LikeValue( $termDb, $dbr->anyString() ) ),
88 ];
89
90 return self::listPages( $dbr, $conds );
91 }
92
101 public static function listPagesByPrefix( $prefix ) {
102 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
103
104 $title = Title::newFromText( $prefix );
105 if ( $title ) {
106 $ns = $title->getNamespace();
107 $prefix = $title->getDBkey();
108 } else {
109 / Prolly won't work too good
110 / @todo handle bare namespace names cleanly?
111 $ns = 0;
112 }
113
114 $conds = [
115 'ar_namespace' => $ns,
116 $dbr->expr( 'ar_title', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ),
117 ];
118
119 return self::listPages( $dbr, $conds );
120 }
121
127 protected static function listPages( IReadableDatabase $dbr, $condition ) {
128 return $dbr->newSelectQueryBuilder()
129 ->select( [ 'ar_namespace', 'ar_title', 'count' => 'COUNT(*)' ] )
130 ->from( 'archive' )
131 ->where( $condition )
132 ->groupBy( [ 'ar_namespace', 'ar_title' ] )
133 ->orderBy( [ 'ar_namespace', 'ar_title' ] )
134 ->limit( 100 )
135 ->caller( __METHOD__ )->fetchResultSet();
136 }
137
146 public function listFiles() {
147 if ( $this->title->getNamespace() !== NS_FILE ) {
148 return null;
149 }
150
151 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
152 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
153 $queryBuilder->where( [ 'fa_name' => $this->title->getDBkey() ] )
154 ->orderBy( 'fa_timestamp', SelectQueryBuilder::SORT_DESC );
155 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
156 }
157
158}
159
161class_alias( PageArchive::class, 'PageArchive' );
const NS_FILE
Definition Defines.php:71
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Used to show archived pages and eventually restore them.
static listPagesBySearch( $term)
List deleted pages recorded in the archive matching the given term, using search engine archive.
static listPages(IReadableDatabase $dbr, $condition)
static listPagesByPrefix( $prefix)
List deleted pages recorded in the archive table matching the given title prefix.
listFiles()
List the deleted file revisions for this page, if it's a file page.
Represents a title within MediaWiki.
Definition Title.php:78
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1040
getDBkey()
Get the main part with underscores.
Definition Title.php:1031
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1013
Content of like value.
Definition LikeValue.php:14
Build SELECT queries with a fluent interface.
A database connection without write operations.
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.
Result wrapper for grabbing data queried from an IDatabase object.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant