LinkTitles extension for MediaWiki
Automatically add links to existing pages.
All Classes Namespaces Functions Variables Modules Pages
linktitles-cli.php
1 <?php
22 namespace LinkTitles;
23 
24 // Attempt to include the maintenance base class from:
25 // $wgScriptPath/maintenance/Maintenance.php
26 // Our script is normally located at:
27 // $wgScriptPath/extensions/LinkTitles/LinkTitles_Maintenance.php
28 $maintenanceScript = __DIR__ . "/../../maintenance/Maintenance.php";
29 if ( file_exists( $maintenanceScript ) ) {
30  require_once $maintenanceScript;
31 }
32 else
33 {
34  // Did not find the script where we expected it (maybe because we are a
35  // symlinked file -- __DIR resolves symbolic links).
36  $maintenanceScript = __DIR__ . "/Maintenance.php";
37  if ( file_exists( $maintenanceScript ) ) {
38  require_once $maintenanceScript;
39  }
40  else
41  {
42  die("FATAL: Could not locate Maintenance.php.\n" .
43  "You may want to create a symbolic link named Maintenance.php in this directory\n" .
44  "which points to <YOUR_MEDIAWIKI_ROOT_IN_FILESYSTEM>/extensions/Maintenance.php\n" .
45  "Ex.: ln -s /var/www/wiki/maintenance/Maintenance.php\n\n");
46  }
47 };
48 
49 require_once( __DIR__ . "/includes/Extension.php" );
50 
60 class Cli extends \Maintenance {
64  public function __construct() {
65  parent::__construct();
66  $this->addDescription("Iterates over wiki pages and automatically adds links to other pages.");
67  $this->addOption(
68  "start",
69  "Set start index.",
70  false, // not required
71  true, // requires argument
72  "s"
73  );
74  $this->addOption(
75  "page",
76  "page name to process",
77  false, // not required
78  true, // requires argument
79  "p"
80  );
81  // TODO: Add back logging options.
82  // TODO: Add configuration options.
83  // $this->addOption(
84  // "log",
85  // "enables logging to console",
86  // false, // not required
87  // false, // requires no argument
88  // "l"
89  // );
90  // $this->addOption(
91  // "debug",
92  // "enables debug logging to console",
93  // false, // not required
94  // false // requires no argument
95  // );
96  }
97 
98  /*
99  * Main function of the maintenance script.
100  * Will iterate over all pages in the wiki (starting at a certain index,
101  * if the `--start` option is given) and call LinkTitles::processPage() for
102  * each page.
103  */
104  public function execute() {
105  // if ($this->hasOption('log'))
106  // {
107  // Extension::$ltConsoleOutput = true;
108  // }
109  // if ($this->hasOption('debug'))
110  // {
111  // Extension::$ltConsoleOutputDebug = true;
112  // }
113  if ( $this->hasOption('page') ) {
114  if ( !$this->hasOption( 'start' ) ) {
115  $this->singlePage();
116  }
117  else {
118  $this->error( 'FATAL: Must not use --start option with --page option.', 2 );
119  }
120  }
121  else {
122  $startIndex = intval( $this->getOption( 'start', 0 ) );
123  if ( $startIndex < 0 ) {
124  $this->error( 'FATAL: Start index must be 0 or greater.', 1 );
125  };
126  $this->allPages( $startIndex );
127  }
128  }
129 
134  private function singlePage() {
135  $pageName = strval( $this->getOption( 'page' ) );
136  $this->output( "Processing single page: '$pageName'\n" );
137  $title = \Title::newFromText( $pageName );
138  $success = Extension::processPage( $title, \RequestContext::getMain() );
139  if ( $success ) {
140  $this->output( "Finished.\n" );
141  }
142  else {
143  $this->error( 'FATAL: There is no such page.', 3 );
144  }
145  return $success;
146  }
147 
153  private function allPages( $index = 0 ) {
154  $config = new Config();
155 
156  // Retrieve page names from the database.
157  $dbr = $this->getDB( DB_SLAVE );
158  $namespacesClause = str_replace( '_', ' ','(' . implode( ', ', $config->sourceNamespaces ) . ')' );
159  $res = $dbr->select(
160  'page',
161  array( 'page_title', 'page_namespace' ),
162  array(
163  'page_namespace IN ' . $namespacesClause,
164  ),
165  __METHOD__,
166  array(
167  'LIMIT' => 999999999,
168  'OFFSET' => $index
169  )
170  );
171  $numPages = $res->numRows();
172  $context = \RequestContext::getMain();
173  $this->output( "Processing ${numPages} pages, starting at index ${index}...\n" );
174 
175  foreach ( $res as $row ) {
176  $index += 1; // at this point, $index is only needed for reporting to user
177  $title = \Title::makeTitleSafe( $row->page_namespace, $row->page_title );
178  $this->output( sprintf( "\rPage #%d (%02.0f%%) ", $index, $index / $numPages * 100 ) );
179  Extension::processPage( $title, $context );
180  }
181 
182  $this->output( "\rFinished. \n" );
183  }
184 }
185 
186 $maintClass = 'LinkTitles\Cli';
187 if( defined('RUN_MAINTENANCE_IF_MAIN') ) {
188  require_once( RUN_MAINTENANCE_IF_MAIN );
189 } else {
190  require_once( DO_MAINTENANCE );
191 }
192 
193 // vim: ts=2:sw=2:noet:comments^=\:///
allPages($index=0)
Process all pages in the Wiki.
singlePage()
Processes a single page.
Core class of the maintanance script.
Holds LinkTitles configuration.
Definition: Config.php:37
The LinkTitles class holds configuration for the LinkTitles extension.
Definition: Config.php:23
__construct()
Constructor.