MediaWiki master
grep.php
Go to the documentation of this file.
1<?php
2// phpcs:disable MediaWiki.Files.ClassMatchesFilename.NotMatch
10
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
20class GrepPages extends Maintenance {
22 private $contLang;
23
25 private $wikiPageFactory;
26
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription( 'Search the source text of pages for lines matching ' .
30 'a given regex, and print the lines.' );
31 $this->addOption( 'prefix',
32 'Title prefix. Can be specified more than once. ' .
33 'Use e.g. --prefix=Talk: to search an entire namespace.',
34 false, true, false, true );
35 $this->addOption( 'show-wiki', 'Add the wiki ID to the output' );
36 $this->addOption( 'pages-with-matches',
37 'Suppress normal output; instead print the title of each page ' .
38 'from which output would normally have been printed.',
39 false, false, 'l' );
40 $this->addArg( 'regex', 'The regex to search for' );
41 }
42
43 private function init() {
44 $services = $this->getServiceContainer();
45 $this->contLang = $services->getContentLanguage();
46 $this->wikiPageFactory = $services->getWikiPageFactory();
47 }
48
49 public function execute() {
50 $this->init();
51
52 $showWiki = $this->getOption( 'show-wiki' );
53 $wikiId = WikiMap::getCurrentWikiId();
54 $prefix = $this->getOption( 'prefix' );
55 $regex = $this->getArg( 0 );
56 $titleOnly = $this->hasOption( 'pages-with-matches' );
57
58 if ( ( $regex[0] ?? '' ) === '/' ) {
59 $delimRegex = $regex;
60 } else {
61 $delimRegex = '{' . $regex . '}';
62 }
63
64 foreach ( $this->findPages( $prefix ) as $page ) {
65 $content = $page->getContent( RevisionRecord::RAW );
66 $titleText = $page->getTitle()->getPrefixedDBkey();
67 if ( !$content ) {
68 $this->error( "Page has no content: $titleText" );
69 continue;
70 }
71 if ( !$content instanceof TextContent ) {
72 $this->error( "Page has a non-text content model: $titleText" );
73 continue;
74 }
75
76 $text = $content->getText();
77
78 if ( $titleOnly ) {
79 if ( preg_match( $delimRegex, $text ) ) {
80 if ( $showWiki ) {
81 echo "$wikiId\t$titleText\n";
82 } else {
83 echo "$titleText\n";
84 }
85 }
86 } else {
87 foreach ( StringUtils::explode( "\n", $text ) as $lineNum => $line ) {
88 $lineNum++;
89 if ( preg_match( $delimRegex, $line ) ) {
90 if ( $showWiki ) {
91 echo "$wikiId\t$titleText:$lineNum:$line\n";
92 } else {
93 echo "$titleText:$lineNum:$line\n";
94 }
95 }
96 }
97 }
98 }
99 }
100
101 public function findPages( $prefixes = null ) {
102 $dbr = $this->getReplicaDB();
103 $orConds = [];
104 if ( $prefixes !== null ) {
105 foreach ( $prefixes as $prefix ) {
106 $colonPos = strpos( $prefix, ':' );
107 if ( $colonPos !== false ) {
108 $ns = $this->contLang->getNsIndex( substr( $prefix, 0, $colonPos ) );
109 $prefixDBkey = substr( $prefix, $colonPos + 1 );
110 } else {
111 $ns = NS_MAIN;
112 $prefixDBkey = $prefix;
113 }
114 $prefixExpr = $dbr->expr( 'page_namespace', '=', $ns );
115 if ( $prefixDBkey !== '' ) {
116 $prefixExpr = $prefixExpr->and(
117 'page_title',
118 IExpression::LIKE,
119 new LikeValue( $prefixDBkey, $dbr->anyString() )
120 );
121 }
122 $orConds[] = $prefixExpr;
123 }
124 }
125 $lastId = 0;
126 do {
127 $res = $dbr->newSelectQueryBuilder()
128 ->queryInfo( WikiPage::getQueryInfo() )
129 ->where( $orConds ? $dbr->orExpr( $orConds ) : [] )
130 ->andWhere( $dbr->expr( 'page_id', '>', $lastId ) )
131 ->limit( 200 )
132 ->caller( __METHOD__ )
133 ->fetchResultSet();
134 foreach ( $res as $row ) {
135 $title = Title::newFromRow( $row );
136 yield $this->wikiPageFactory->newFromTitle( $title );
137 $lastId = $row->page_id;
138 }
139 } while ( $res->numRows() );
140 }
141}
142
143// @codeCoverageIgnoreStart
144$maintClass = GrepPages::class;
145require_once RUN_MAINTENANCE_IF_MAIN;
146// @codeCoverageIgnoreEnd
const NS_MAIN
Definition Defines.php:65
Search pages for a given regex.
Definition grep.php:20
execute()
Do the actual work.
Definition grep.php:49
findPages( $prefixes=null)
Definition grep.php:101
__construct()
Default constructor.
Definition grep.php:27
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Content object implementation for representing flat text.
Service for creating WikiPage objects.
Page revision base class.
Represents a title within MediaWiki.
Definition Title.php:78
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Content of like value.
Definition LikeValue.php:14
$maintClass
Definition grep.php:144