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