8 require_once __DIR__ .
'/Maintenance.php';
20 private $wikiPageFactory;
23 parent::__construct();
24 $this->
addDescription(
'Search the source text of pages for lines matching ' .
25 'a given regex, and print the lines.' );
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' );
32 'Suppress normal output; instead print the title of each page ' .
33 'from which output would normally have been printed.',
35 $this->
addArg(
'regex',
'The regex to search for' );
38 private function init() {
40 $this->contLang = $services->getContentLanguage();
41 $this->wikiPageFactory = $services->getWikiPageFactory();
47 $showWiki = $this->
getOption(
'show-wiki' );
48 $wikiId = WikiMap::getCurrentWikiId();
50 $regex = $this->
getArg( 0 );
51 $titleOnly = $this->
hasOption(
'pages-with-matches' );
53 if ( ( $regex[0] ??
'' ) ===
'/' ) {
54 $delimRegex = $regex[0];
56 $delimRegex =
'{' . $regex .
'}';
59 foreach ( $this->
findPages( $prefix ) as $page ) {
60 $content = $page->getContent( RevisionRecord::RAW );
61 $titleText = $page->getTitle()->getPrefixedDBkey();
63 $this->
error(
"Page has no content: $titleText" );
67 $this->
error(
"Page has a non-text content model: $titleText" );
74 if ( preg_match( $delimRegex, $text ) ) {
76 echo
"$wikiId\t$titleText\n";
84 if ( preg_match( $delimRegex, $line ) ) {
86 echo
"$wikiId\t$titleText:$lineNum:$line\n";
88 echo
"$titleText:$lineNum:$line\n";
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 );
107 $prefixDBkey = $prefix;
109 $prefixCond = [
'page_namespace' => $ns ];
110 if ( $prefixDBkey !==
'' ) {
111 $prefixCond[] =
'page_title ' . $dbr->buildLike( $prefixDBkey, $dbr->anyString() );
113 $orConds[] = $dbr->makeList( $prefixCond,
LIST_AND );
117 $conds = $orConds ? $dbr->makeList( $orConds,
LIST_OR ) : [];
120 $res = $dbr->newSelectQueryBuilder()
121 ->queryInfo( $pageQuery )
123 ->caller( __METHOD__ )
125 foreach ( $res as $row ) {
126 $title = Title::newFromRow( $row );
127 yield $this->wikiPageFactory->newFromTitle( $title );
133 require_once RUN_MAINTENANCE_IF_MAIN;
Search pages for a given regex.
execute()
Do the actual work.
findPages( $prefixes=null)
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
Content object implementation for representing flat text.
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new page object.