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