14require_once __DIR__ .
'/Maintenance.php';
27 private $wikiPageFactory;
30 parent::__construct();
31 $this->
addDescription(
'Search the source text of pages for lines matching ' .
32 'a given regex, and print the lines.' );
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' );
39 'Suppress normal output; instead print the title of each page ' .
40 'from which output would normally have been printed.',
42 $this->
addArg(
'regex',
'The regex to search for' );
45 private function init() {
47 $this->contLang = $services->getContentLanguage();
48 $this->wikiPageFactory = $services->getWikiPageFactory();
54 $showWiki = $this->
getOption(
'show-wiki' );
55 $wikiId = WikiMap::getCurrentWikiId();
57 $regex = $this->
getArg( 0 );
58 $titleOnly = $this->
hasOption(
'pages-with-matches' );
60 if ( ( $regex[0] ??
'' ) ===
'/' ) {
63 $delimRegex =
'{' . $regex .
'}';
66 foreach ( $this->
findPages( $prefix ) as $page ) {
67 $content = $page->getContent( RevisionRecord::RAW );
68 $titleText = $page->getTitle()->getPrefixedDBkey();
70 $this->
error(
"Page has no content: $titleText" );
74 $this->
error(
"Page has a non-text content model: $titleText" );
78 $text = $content->getText();
81 if ( preg_match( $delimRegex, $text ) ) {
83 echo
"$wikiId\t$titleText\n";
89 foreach ( StringUtils::explode(
"\n", $text ) as $lineNum => $line ) {
91 if ( preg_match( $delimRegex, $line ) ) {
93 echo
"$wikiId\t$titleText:$lineNum:$line\n";
95 echo
"$titleText:$lineNum:$line\n";
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 );
114 $prefixDBkey = $prefix;
116 $prefixExpr = $dbr->expr(
'page_namespace',
'=', $ns );
117 if ( $prefixDBkey !==
'' ) {
118 $prefixExpr = $prefixExpr->and(
121 new LikeValue( $prefixDBkey, $dbr->anyString() )
124 $orConds[] = $prefixExpr;
129 $res = $dbr->newSelectQueryBuilder()
130 ->queryInfo( WikiPage::getQueryInfo() )
131 ->where( $orConds ? $dbr->orExpr( $orConds ) : [] )
132 ->andWhere( $dbr->expr(
'page_id',
'>', $lastId ) )
134 ->caller( __METHOD__ )
136 foreach ( $res as $row ) {
137 $title = Title::newFromRow( $row );
138 yield $this->wikiPageFactory->newFromTitle( $title );
139 $lastId = $row->page_id;
141 }
while ( $res->numRows() );
147require_once RUN_MAINTENANCE_IF_MAIN;
Search pages for a given regex.
execute()
Do the actual work.
findPages( $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.