MediaWiki master
PageArchive.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Page;
8
17
22
23 protected Title $title;
24
25 public function __construct( Title $title ) {
26 $this->title = $title;
27 }
28
37 public static function listPagesBySearch( $term ) {
38 $title = Title::newFromText( $term );
39 if ( $title ) {
40 $ns = $title->getNamespace();
41 $termMain = $title->getText();
42 $termDb = $title->getDBkey();
43 } else {
44 // Prolly won't work too good
45 // @todo handle bare namespace names cleanly?
46 $ns = 0;
47 $termMain = $termDb = $term;
48 }
49
50 // Try search engine first
51 $engine = MediaWikiServices::getInstance()->newSearchEngine();
52 $engine->setLimitOffset( 100 );
53 $engine->setNamespaces( [ $ns ] );
54 $results = $engine->searchArchiveTitle( $termMain );
55 if ( !$results->isOK() ) {
56 $results = [];
57 } else {
58 $results = $results->getValue();
59 }
60
61 if ( !$results ) {
62 // Fall back to regular prefix search
63 return self::listPagesByPrefix( $term );
64 }
65
66 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
67 $condTitles = array_values( array_unique( array_map( static function ( Title $t ) {
68 return $t->getDBkey();
69 }, $results ) ) );
70 $conds = [
71 'ar_namespace' => $ns,
72 $dbr->expr( 'ar_title', '=', $condTitles )
73 ->or( 'ar_title', IExpression::LIKE, new LikeValue( $termDb, $dbr->anyString() ) ),
74 ];
75
76 return self::listPages( $dbr, $conds );
77 }
78
87 public static function listPagesByPrefix( $prefix ) {
88 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
89
90 $title = Title::newFromText( $prefix );
91 if ( $title ) {
92 $ns = $title->getNamespace();
93 $prefix = $title->getDBkey();
94 } else {
95 // Prolly won't work too good
96 // @todo handle bare namespace names cleanly?
97 $ns = 0;
98 }
99
100 $conds = [
101 'ar_namespace' => $ns,
102 $dbr->expr( 'ar_title', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ),
103 ];
104
105 return self::listPages( $dbr, $conds );
106 }
107
113 protected static function listPages( IReadableDatabase $dbr, $condition ) {
114 return $dbr->newSelectQueryBuilder()
115 ->select( [ 'ar_namespace', 'ar_title', 'count' => 'COUNT(*)' ] )
116 ->from( 'archive' )
117 ->where( $condition )
118 ->groupBy( [ 'ar_namespace', 'ar_title' ] )
119 ->orderBy( [ 'ar_namespace', 'ar_title' ] )
120 ->limit( 100 )
121 ->caller( __METHOD__ )->fetchResultSet();
122 }
123
132 public function listFiles() {
133 if ( $this->title->getNamespace() !== NS_FILE ) {
134 return null;
135 }
136
137 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
138 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
139 $queryBuilder->where( [ 'fa_name' => $this->title->getDBkey() ] )
140 ->orderBy( 'fa_timestamp', SelectQueryBuilder::SORT_DESC );
141 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
142 }
143
144}
145
147class_alias( PageArchive::class, 'PageArchive' );
const NS_FILE
Definition Defines.php:57
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:69
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1037
getDBkey()
Get the main part with underscores.
Definition Title.php:1028
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1010
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.