16require_once __DIR__ .
'/Maintenance.php';
29 private $wikiPageFactory;
32 parent::__construct();
33 $this->
addDescription(
'Search the source text of pages for lines matching ' .
34 'a given regex, and print the lines.' );
36 'Title prefix. Can be specified more than once. ' .
37 'Use e.g. --prefix=Talk: to search an entire namespace.',
38 false,
true,
false,
true );
39 $this->
addOption(
'show-wiki',
'Add the wiki ID to the output' );
41 'Suppress normal output; instead print the title of each page ' .
42 'from which output would normally have been printed.',
44 $this->
addArg(
'regex',
'The regex to search for' );
47 private function init() {
49 $this->contLang = $services->getContentLanguage();
50 $this->wikiPageFactory = $services->getWikiPageFactory();
56 $showWiki = $this->
getOption(
'show-wiki' );
57 $wikiId = WikiMap::getCurrentWikiId();
59 $regex = $this->
getArg( 0 );
60 $titleOnly = $this->
hasOption(
'pages-with-matches' );
62 if ( ( $regex[0] ??
'' ) ===
'/' ) {
65 $delimRegex =
'{' . $regex .
'}';
68 foreach ( $this->
findPages( $prefix ) as $page ) {
69 $content = $page->getContent( RevisionRecord::RAW );
70 $titleText = $page->getTitle()->getPrefixedDBkey();
72 $this->
error(
"Page has no content: $titleText" );
76 $this->
error(
"Page has a non-text content model: $titleText" );
80 $text = $content->getText();
83 if ( preg_match( $delimRegex, $text ) ) {
85 echo
"$wikiId\t$titleText\n";
91 foreach ( StringUtils::explode(
"\n", $text ) as $lineNum => $line ) {
93 if ( preg_match( $delimRegex, $line ) ) {
95 echo
"$wikiId\t$titleText:$lineNum:$line\n";
97 echo
"$titleText:$lineNum:$line\n";
105 public function findPages( ?array $prefixes =
null ): iterable {
108 if ( $prefixes !==
null ) {
109 foreach ( $prefixes as $prefix ) {
110 $colonPos = strpos( $prefix,
':' );
111 if ( $colonPos !==
false ) {
112 $ns = $this->contLang->getNsIndex( substr( $prefix, 0, $colonPos ) );
113 $prefixDBkey = substr( $prefix, $colonPos + 1 );
116 $prefixDBkey = $prefix;
118 $prefixExpr = $dbr->expr(
'page_namespace',
'=', $ns );
119 if ( $prefixDBkey !==
'' ) {
120 $prefixExpr = $prefixExpr->and(
123 new LikeValue( $prefixDBkey, $dbr->anyString() )
126 $orConds[] = $prefixExpr;
131 $res = $dbr->newSelectQueryBuilder()
132 ->queryInfo( WikiPage::getQueryInfo() )
133 ->where( $orConds ? $dbr->orExpr( $orConds ) : [] )
134 ->andWhere( $dbr->expr(
'page_id',
'>', $lastId ) )
136 ->caller( __METHOD__ )
138 foreach ( $res as $row ) {
139 $title = Title::newFromRow( $row );
140 yield $this->wikiPageFactory->newFromTitle( $title );
141 $lastId = $row->page_id;
143 }
while ( $res->numRows() );
149require_once RUN_MAINTENANCE_IF_MAIN;
Search pages for a given regex.
execute()
Do the actual work.
findPages(?array $prefixes=null)
__construct()
Default constructor.
Content object implementation for representing flat text.
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.
Base representation for an editable wiki page.