MediaWiki 1.40.4
grep.php
Go to the documentation of this file.
1<?php
2// phpcs:disable MediaWiki.Files.ClassMatchesFilename.NotMatch
8
9require_once __DIR__ . '/Maintenance.php';
10
16class GrepPages extends Maintenance {
18 private $contLang;
19
21 private $wikiPageFactory;
22
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription( 'Search the source text of pages for lines matching ' .
26 'a given regex, and print the lines.' );
27 $this->addOption( 'prefix',
28 'Title prefix. Can be specified more than once. ' .
29 'Use e.g. --prefix=Talk: to search an entire namespace.',
30 false, true, false, true );
31 $this->addOption( 'show-wiki', 'Add the wiki ID to the output' );
32 $this->addOption( 'pages-with-matches',
33 'Suppress normal output; instead print the title of each page ' .
34 'from which output would normally have been printed.',
35 false, false, 'l' );
36 $this->addArg( 'regex', 'The regex to search for' );
37 }
38
39 private function init() {
40 $services = MediaWikiServices::getInstance();
41 $this->contLang = $services->getContentLanguage();
42 $this->wikiPageFactory = $services->getWikiPageFactory();
43 }
44
45 public function execute() {
46 $this->init();
47
48 $showWiki = $this->getOption( 'show-wiki' );
49 $wikiId = WikiMap::getCurrentWikiId();
50 $prefix = $this->getOption( 'prefix' );
51 $regex = $this->getArg( 0 );
52 $titleOnly = $this->hasOption( 'pages-with-matches' );
53
54 if ( ( $regex[0] ?? '' ) === '/' ) {
55 $delimRegex = $regex;
56 } else {
57 $delimRegex = '{' . $regex . '}';
58 }
59
60 foreach ( $this->findPages( $prefix ) as $page ) {
61 $content = $page->getContent( RevisionRecord::RAW );
62 $titleText = $page->getTitle()->getPrefixedDBkey();
63 if ( !$content ) {
64 $this->error( "Page has no content: $titleText" );
65 continue;
66 }
67 if ( !$content instanceof TextContent ) {
68 $this->error( "Page has a non-text content model: $titleText" );
69 continue;
70 }
71
72 $text = $content->getText();
73
74 if ( $titleOnly ) {
75 if ( preg_match( $delimRegex, $text ) ) {
76 if ( $showWiki ) {
77 echo "$wikiId\t$titleText\n";
78 } else {
79 echo "$titleText\n";
80 }
81 }
82 } else {
83 foreach ( StringUtils::explode( "\n", $text ) as $lineNum => $line ) {
84 $lineNum++;
85 if ( preg_match( $delimRegex, $line ) ) {
86 if ( $showWiki ) {
87 echo "$wikiId\t$titleText:$lineNum:$line\n";
88 } else {
89 echo "$titleText:$lineNum:$line\n";
90 }
91 }
92 }
93 }
94 }
95 }
96
97 public function findPages( $prefixes = null ) {
98 $dbr = $this->getDB( DB_REPLICA );
99 $orConds = [];
100 if ( $prefixes !== null ) {
101 foreach ( $prefixes as $prefix ) {
102 $colonPos = strpos( $prefix, ':' );
103 if ( $colonPos !== false ) {
104 $ns = $this->contLang->getNsIndex( substr( $prefix, 0, $colonPos ) );
105 $prefixDBkey = substr( $prefix, $colonPos + 1 );
106 } else {
107 $ns = NS_MAIN;
108 $prefixDBkey = $prefix;
109 }
110 $prefixCond = [ 'page_namespace' => $ns ];
111 if ( $prefixDBkey !== '' ) {
112 $prefixCond[] = 'page_title ' . $dbr->buildLike( $prefixDBkey, $dbr->anyString() );
113 }
114 $orConds[] = $dbr->makeList( $prefixCond, LIST_AND );
115 }
116 }
117
118 $conds = $orConds ? $dbr->makeList( $orConds, LIST_OR ) : [];
119 $pageQuery = WikiPage::getQueryInfo();
120
121 $res = $dbr->newSelectQueryBuilder()
122 ->queryInfo( $pageQuery )
123 ->where( $conds )
124 ->caller( __METHOD__ )
125 ->fetchResultSet();
126 foreach ( $res as $row ) {
127 $title = Title::newFromRow( $row );
128 yield $this->wikiPageFactory->newFromTitle( $title );
129 }
130 }
131}
132
133$maintClass = GrepPages::class;
134require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const NS_MAIN
Definition Defines.php:64
const LIST_OR
Definition Defines.php:46
const LIST_AND
Definition Defines.php:43
Search pages for a given regex.
Definition grep.php:16
execute()
Do the actual work.
Definition grep.php:45
findPages( $prefixes=null)
Definition grep.php:97
__construct()
Default constructor.
Definition grep.php:23
Base class for language-specific code.
Definition Language.php:56
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.
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.
Service locator for MediaWiki core services.
Service for creating WikiPage objects.
Page revision base class.
Represents a title within MediaWiki.
Definition Title.php:82
Helper tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:33
Content object implementation for representing flat text.
$maintClass
Definition grep.php:133
const DB_REPLICA
Definition defines.php:26
$content
Definition router.php:76